/******************************************************* * * DESCRIPTION: Modelica's syntax tree nodes classes * * AUTHOR: Mariana C. D'Abreu * * EMAIL: mdabreu@dc.uba.ar * ********************************************************/ #ifndef MODSYNNODE_H #define MODSYNNODE_H #include #include #include #include #include #include using namespace std; typedef int OperType; typedef map > ParamsDict; typedef enum { Variable, Type, Param, Package, Reference, ConstantNode, ConnectNode, DeclarationNode, BinaryOperNode, ImportNode, ClassDefinitionNode, ModSyntaxNodeListNode } ModSyntaxNodeType; //--- Forward declarations --- class ModSyntaxNode; class TypeSymbol; class SymbolTable; //****************************************************** // // Class ModSyntaxNode // // Description: // //****************************************************** class ModSyntaxNode { public: virtual ~ModSyntaxNode() {}; virtual double evaluate() = 0; virtual bool semanticalValidation() { return true; } virtual ModSyntaxNodeType getType() const = 0; protected: ModSyntaxNode() {}; }; //****************************************************** // // Class ModSyntaxNodeList // // Description: // //****************************************************** class ModSyntaxNodeList : public ModSyntaxNode { public: typedef list NodesList; public: typedef NodesList::iterator Iterator; ModSyntaxNodeList() {}; virtual ~ModSyntaxNodeList(); virtual double evaluate(); virtual bool semanticalValidation(); virtual ModSyntaxNodeType getType() const { return ModSyntaxNodeListNode; } virtual ModSyntaxNodeList &add( ModSyntaxNode *node ); Iterator begin() { return nodes.begin(); } Iterator end() { return nodes.end(); } ModSyntaxNodeList &addAll( ModSyntaxNodeList *lista ); protected: NodesList nodes; }; inline ModSyntaxNodeList &ModSyntaxNodeList::add( ModSyntaxNode *anode ) { if( anode != NULL ) nodes.push_back( anode ); return *this; } inline ModSyntaxNodeList &ModSyntaxNodeList::addAll( ModSyntaxNodeList *lista ) { ModSyntaxNodeList::Iterator it; for( it = lista->begin(); it != lista->end(); it++ ) this->add( *it ); return *this; } //****************************************************** // // Class // // Description: // //****************************************************** class Symbol : public ModSyntaxNode { public: virtual ~Symbol() {}; string getId() const { return id; } protected: Symbol( const string &aid ) { id = aid; } string id; }; //****************************************************** // // Class // // Description: // //****************************************************** class VarSymbol : public Symbol { public: VarSymbol( const string &id ) :Symbol( id ) {}; VarSymbol( const string &id, TypeSymbol *type, ParamsDict *pdict ) :Symbol( id ) { varType = type; paramsDict = pdict; } ~VarSymbol(); double evaluate() { return 0; } ModSyntaxNodeType getType() const { return Variable; } TypeSymbol *getVarType() { return varType; } ParamsDict *getParamsDict() { return paramsDict; } VarSymbol &setVarType( TypeSymbol *type ) { varType = type; return *this; } VarSymbol &setParamsDict( ParamsDict *pdict ) { paramsDict = pdict; return *this; } protected: TypeSymbol *varType; ParamsDict *paramsDict; }; //****************************************************** // // Class // // Description: // //****************************************************** class TypeSymbol : public Symbol { public: TypeSymbol( const string &aid ) :Symbol( aid ) {}; ~TypeSymbol() {}; double evaluate() { return 0; } ModSyntaxNodeType getType() const { return Type; } TypeSymbol &concat( string &aid ) { id = id + "." + aid; return *this; } string getIdWithNoPackage() const; string getPackageId() const; protected: }; //****************************************************** // // Class // // Description: // //****************************************************** class PackageSymbol : public Symbol { public: PackageSymbol( const string &aid ) :Symbol( aid ) {}; ~PackageSymbol() {}; double evaluate() { return 0; } ModSyntaxNodeType getType() const { return Package; } PackageSymbol &concat( string &aid ) { id = id + "." + aid; return *this; } string getIdWithNoWildcards() const; protected: }; //****************************************************** // // Class // // Description: // //****************************************************** class ReferenceSymbol : public Symbol { public: ReferenceSymbol( const string &acontext, const string &aid ) :Symbol( "" ) { context = acontext; mid = aid; } ~ReferenceSymbol() {}; double evaluate() { return 0; } ModSyntaxNodeType getType() const { return Reference; } string getContextId() const { return context; } string getReferencedId() const { return mid; } protected: string context; string mid; }; //****************************************************** // // Class // // Description: // //****************************************************** class ParamSymbol : public Symbol { public: ParamSymbol( const string &aid, ModSyntaxNode *value ); ~ParamSymbol() {}; double evaluate(); ModSyntaxNodeType getType() const { return Param; } string getId() { return id; } protected: string id; ModSyntaxNode *value; }; inline ParamSymbol::ParamSymbol( const string &aid, ModSyntaxNode *avalue ) :Symbol(aid) { id = aid; value = avalue; } inline double ParamSymbol::evaluate() { return value->evaluate(); } //****************************************************** // // Class // // Description: // //****************************************************** class SymbolTable { protected: typedef map > SymTable; SymTable symTable; static SymbolTable *instance; void initialize(); public: SymbolTable() { initialize(); } virtual ~SymbolTable(); SymbolTable &put( Symbol *symbol ); Symbol *get( string &id ); static SymbolTable *Instance(); ostream &print( ostream & ); }; inline Symbol *SymbolTable::get( string &id ) { SymTable::iterator it = symTable.find( id ); if( it != symTable.end() ) { return it->second; } return NULL; } //****************************************************** // // Class // // Description: // //****************************************************** class Constant : public ModSyntaxNode { public: Constant( double avalue ) { value = avalue; } virtual ~Constant() {}; double evaluate() { return value; } ModSyntaxNodeType getType() const { return ConstantNode; } protected: double value; }; //****************************************************** // // Class // // Description: // //****************************************************** class BinaryOper : public ModSyntaxNode { public: BinaryOper( OperType &otype, ModSyntaxNode *child1, ModSyntaxNode *child2 ); virtual ~BinaryOper(); ModSyntaxNodeType getType() const { return BinaryOperNode; } OperType getOperType() const { return type; } ModSyntaxNode *getChild1() const { return child1; } ModSyntaxNode *getChild2() const { return child2; } protected: OperType type; ModSyntaxNode *child1; ModSyntaxNode *child2; }; inline BinaryOper::~BinaryOper() { if( child1 != NULL ) delete child1; if( child2 != NULL ) delete child2; } inline BinaryOper::BinaryOper( OperType &atype, ModSyntaxNode *achild1, ModSyntaxNode *achild2 ) :ModSyntaxNode() { type = atype; child1 = achild1; child2 = achild2; } //****************************************************** // // Class // // Description: // //****************************************************** class Declaration : public ModSyntaxNode { public: Declaration( TypeSymbol *type, VarSymbol *var, ModSyntaxNodeList *initParams ); virtual ~Declaration(); ModSyntaxNodeType getType() const { return DeclarationNode; } virtual double evaluate(); virtual bool semanticalValidation(); VarSymbol *getVar() { return mvar; } protected: ParamsDict *evaluateInitAndCreateParams(); TypeSymbol *type; VarSymbol *mvar; ModSyntaxNodeList *initParams; }; inline Declaration::Declaration( TypeSymbol *atype, VarSymbol *avar, ModSyntaxNodeList *sInitParams ) { type = atype; mvar = avar; initParams = sInitParams; } inline Declaration::~Declaration() { if( type != NULL ) delete type; if( initParams != NULL ) delete initParams; } //****************************************************** // // Class // // Description: // //****************************************************** class Import : public ModSyntaxNode { public: Import( PackageSymbol *apackage ) { package = apackage; } virtual ~Import(); double evaluate() { return 0; } virtual bool semanticalValidation(); ModSyntaxNodeType getType() const { return ImportNode; } protected: PackageSymbol *package; }; inline Import::~Import() { if( package != NULL ) delete package; } //****************************************************** // // Class // // Description: // //****************************************************** class Connect : public ModSyntaxNode { public: Connect( ReferenceSymbol *aref1, ReferenceSymbol *aref2 ) { ref1 = aref1; ref2 = aref2; } virtual ~Connect(); double evaluate(); virtual bool semanticalValidation(); ModSyntaxNodeType getType() const { return ConnectNode; } ReferenceSymbol *getReference1() const { return ref1; } ReferenceSymbol *getReference2() const { return ref2; } protected: bool isValidReference( const string &refId ) const; ReferenceSymbol *ref1; ReferenceSymbol *ref2; }; inline Connect::~Connect() { if( ref1 != NULL ) delete ref1; if( ref2 != NULL ) delete ref2; } //****************************************************** // // Class // // Description: // //****************************************************** class ClassDefinition : public ModSyntaxNode { public: ClassDefinition( const string &aid, ModSyntaxNode *ccomposition ) { cid = aid; composition = ccomposition; } virtual ~ClassDefinition(); double evaluate() { return 0; } ModSyntaxNodeType getType() const { return ClassDefinitionNode; } string getClassId() const { return cid; } ModSyntaxNode *getComposition() { return composition; } protected: string cid; ModSyntaxNode *composition; }; inline ClassDefinition::~ClassDefinition() { if( composition != NULL ) delete composition; } #endif