#ifndef AST_DOT_H #define AST_DOT_H #include #include #include // types of AST nodes class Program; class Declaration; class Statement; class AssignStatement; class PrintStatement; class Expression; class Identifier; class MathOp; class Int; class Float; typedef enum { T_UNDEFINED, T_FLOAT, T_INT } Type; typedef string IDTYPE; typedef enum { T_PLUS, T_MINUS } OP; ostream& operator<<( ostream&, OP ); // defined in main.cc Expression* make_num( string s ); // defined in main.cc class Statement { public: virtual ~Statement() {} }; class Expression { public: virtual Type type() const =0; virtual ~Expression() {} }; // Program // vector declarations // vector statements class Program { vector _declarations ; vector _statements ; public: Program( vector declarations , vector statements ) : _declarations (declarations ), _statements (statements ) {} vector declarations () const { return _declarations ; } vector statements () const { return _statements ; } }; // Declaration // IDTYPE id // Type type class Declaration { IDTYPE _id; Type _type; public: Declaration( IDTYPE id, Type type ) : _id(id), _type(type) {} IDTYPE id() const { return _id; } Type type() const { return _type; } }; // AssignStatement Statement // Identifier* lhs // Expression* rhs class AssignStatement : public Statement { Identifier* _lhs; Expression* _rhs; public: AssignStatement( Identifier* lhs, Expression* rhs ) : _lhs(lhs), _rhs(rhs) {} Identifier* lhs() const { return _lhs; } Expression* rhs() const { return _rhs; } }; // PrintStatement Statement // Identifier* id class PrintStatement : public Statement { Identifier* _id; public: PrintStatement( Identifier* id ) : _id(id) {} Identifier* id() const { return _id; } }; // Identifier Expression // IDTYPE id // Type type : T_UNDEFINED class Identifier : public Expression { IDTYPE _id ; Type _type; public: Identifier( IDTYPE id ) : _id (id ), _type(T_UNDEFINED) {} IDTYPE id () const { return _id ; } Type type() const { return _type; } void set_type( Type type ) { _type = type; } }; // MathOp Expression // OP op // Expression* lhs, rhs : // Type type : T_UNDEFINED class MathOp : public Expression { OP _op; Expression* _lhs; Expression* _rhs; Type _type; public: MathOp( OP op, Expression* lhs, Expression* rhs ) : _op(op), _lhs(lhs), _rhs(rhs), _type(T_UNDEFINED) {} OP op() const { return _op; } Expression* lhs() const { return _lhs; } void set_lhs( Expression* lhs ) { _lhs = lhs; } Expression* rhs() const { return _rhs; } void set_rhs( Expression* rhs ) { _rhs = rhs; } Type type() const { return _type; } void set_type( Type type ) { _type = type; } }; // Int Expression // int value // Type type : T_INT class Int : public Expression { int _value; Type _type; public: Int( int value ) : _value(value), _type(T_INT) {} int value() const { return _value; } Type type() const { return _type; } }; // Float Expression // float value // Type type : T_FLOAT class Float : public Expression { float _value; Type _type; public: Float( float value ) : _value(value), _type(T_FLOAT) {} float value() const { return _value; } Type type() const { return _type; } }; // IntToFloat Expression // Expression* exp // Type type : T_FLOAT class IntToFloat : public Expression { Expression* _exp; Type _type; public: IntToFloat( Expression* exp ) : _exp(exp), _type(T_FLOAT) {} Expression* exp() const { return _exp; } Type type() const { return _type; } }; #endif