// Statement.h Dave Reed 10/20/03 // // Expression class -- class for storing an expression in the SILLY language // // public member functions: // void Read(Tokenizer & program); // reads & stores the // // expression from program // void Execute(VarTable & variables) const; // evaluates the expression // // given the variable table // void Display() const; // displays the expression // // Statement class -- base class for storing different types of SILLY statements // OutputStatement class -- derived class for storing output statements // AssignStatement class -- derived class for storing assignment statements // // Each class provides the following public member functions: // void Read(Tokenizer & program); // reads & stores the // // statement from program // void Execute(VarTable & variables) const; // executes the statement // // given the variable table // void Display() const; // displays the statement // // Also, the following static member function is provided for reading a stmt // static Statement * GetNext(Tokenizer & program); /////////////////////////////////////////////////////////////////////////////////// #ifndef _STATEMENT_H #define _STATEMENT_H #include #include #include "Token.h" #include "Tokenizer.h" #include "VarTable.h" using namespace std; class Expression { public: Expression() { } void Read(Tokenizer & program); int Evaluate(VarTable & variables) const; void Display() const; private: vector expr; }; class Test { public: Test() { } void Read(Tokenizer & program); bool Evaluate(VarTable & variables) const; void Display() const; private: Expression lhs, rhs; Token op; }; //////////////////////////////////////////////////////////// enum STATEMENT_TYPE {BEGIN, END, ASSIGN, OUTPUT, IF, WHILE, ERROR}; class Statement { public: Statement() { } virtual void Read(Tokenizer & program) = 0; virtual void Execute(VarTable & variables) const = 0; virtual STATEMENT_TYPE GetType() const = 0; virtual void Display() const = 0; static Statement * GetNext(Tokenizer & program); }; class BlockStatement : public Statement { public: BlockStatement() { } void Read(Tokenizer & program); void Execute(VarTable & variables) const; STATEMENT_TYPE GetType() const; void Display() const; private: Token keyword; }; class OutputStatement : public Statement { public: OutputStatement() { } void Read(Tokenizer & program); void Execute(VarTable & variables) const; STATEMENT_TYPE GetType() const; void Display() const; private: string constant; Expression rhs; }; class AssignStatement : public Statement { public: AssignStatement() { } void Read(Tokenizer & program); void Execute(VarTable & variables) const; STATEMENT_TYPE GetType() const; void Display() const; private: string lhs; Expression rhs; }; class IfStatement : public Statement { public: IfStatement() { } void Read(Tokenizer & program); void Execute(VarTable & variables) const; STATEMENT_TYPE GetType() const; void Display() const; private: Test ifTest; vector< Statement* > stmts; }; class WhileStatement : public Statement { public: WhileStatement() { } void Read(Tokenizer & program); void Execute(VarTable & variables) const; STATEMENT_TYPE GetType() const; void Display() const; private: Test whileTest; vector< Statement* > stmts; }; #endif