X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramsyntax.h;h=11a67430926849adee88a32cb636bcde544d1736;hb=a36992487d018d8801ead6980b362b00a2f5f5c5;hp=0013d82a97a7cd8c890e3f0a4456ae5dd4875400;hpb=6e6ee01b68056b23c6709d7f60396710dd7623b9;p=libs%2Fgl.git diff --git a/source/programsyntax.h b/source/programsyntax.h index 0013d82a..11a67430 100644 --- a/source/programsyntax.h +++ b/source/programsyntax.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_PROGRAMSYNTAX_H_ #define MSP_GL_PROGRAMSYNTAX_H_ +#include #include #include #include @@ -13,33 +14,139 @@ struct NodeVisitor; struct Node { +private: + Node &operator=(const Node &); +public: virtual ~Node() { } + virtual Node *clone() const = 0; virtual void visit(NodeVisitor &) = 0; }; +template +class NodePtr +{ +private: + T *node; + +public: + NodePtr(T *n = 0): node(n) { } + NodePtr(const NodePtr &p): node(clone(p.node)) { } + NodePtr &operator=(const NodePtr &p) { delete node; node = clone(p.node); return *this; } + ~NodePtr() { delete node; } + +private: + static T *clone(T *n) { return n ? n->clone() : 0; } + +public: + T *operator->() { return node; } + const T *operator->() const { return node; } + T &operator*() { return *node; } + const T &operator*() const { return *node; } + operator void *() const { return node; } +}; + +struct StructDeclaration; +struct VariableDeclaration; + struct Block: Node { - std::vector body; + std::list > body; bool use_braces; + std::map types; + std::map variables; Block(); - virtual ~Block(); + virtual Block *clone() const { return new Block(*this); } + virtual void visit(NodeVisitor &); +}; + +struct Expression: Node +{ + virtual Expression *clone() const = 0; +}; + +struct Literal: Expression +{ + std::string token; + + virtual Literal *clone() const { return new Literal(*this); } + virtual void visit(NodeVisitor &); +}; + +struct ParenthesizedExpression: Expression +{ + NodePtr expression; + + virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); } + virtual void visit(NodeVisitor &); +}; + +struct VariableReference: Expression +{ + std::string name; + VariableDeclaration *declaration; + + VariableReference(); + + virtual VariableReference *clone() const { return new VariableReference(*this); } virtual void visit(NodeVisitor &); }; -struct Expression +struct MemberAccess: Expression { - std::vector tokens; + NodePtr left; + std::string member; + VariableDeclaration *declaration; - bool empty() const { return tokens.empty(); } + virtual MemberAccess *clone() const { return new MemberAccess(*this); } + virtual void visit(NodeVisitor &); +}; + +struct UnaryExpression: Expression +{ + std::string oper; + NodePtr expression; + bool prefix; + + UnaryExpression(); + + virtual UnaryExpression *clone() const { return new UnaryExpression(*this); } + virtual void visit(NodeVisitor &); +}; + +struct BinaryExpression: Expression +{ + NodePtr left; + std::string oper; + NodePtr right; + std::string after; + bool assignment; + + BinaryExpression(); + + virtual BinaryExpression *clone() const { return new BinaryExpression(*this); } + virtual void visit(NodeVisitor &); +}; + +struct FunctionCall: Expression +{ + std::string name; + bool constructor; + std::vector > arguments; + + FunctionCall(); + + virtual FunctionCall *clone() const { return new FunctionCall(*this); } + virtual void visit(NodeVisitor &); }; struct ExpressionStatement: Node { - Expression expression; + NodePtr expression; + virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); } virtual void visit(NodeVisitor &); }; @@ -54,6 +161,7 @@ struct Layout: Node std::vector qualifiers; std::string interface; + virtual Layout *clone() const { return new Layout(*this); } virtual void visit(NodeVisitor &); }; @@ -64,6 +172,7 @@ struct StructDeclaration: Node StructDeclaration(); + virtual StructDeclaration *clone() const { return new StructDeclaration(*this); } virtual void visit(NodeVisitor &); }; @@ -73,13 +182,15 @@ struct VariableDeclaration: Node std::string sampling; std::string interface; std::string type; + StructDeclaration *type_declaration; std::string name; bool array; - Expression array_size; - Expression init_expression; + NodePtr array_size; + NodePtr init_expression; VariableDeclaration(); + virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); } virtual void visit(NodeVisitor &); }; @@ -91,6 +202,7 @@ struct InterfaceBlock: Node InterfaceBlock(); + virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); } virtual void visit(NodeVisitor &); }; @@ -98,42 +210,42 @@ struct FunctionDeclaration: Node { std::string return_type; std::string name; - std::vector parameters; + std::vector > parameters; bool definition; Block body; FunctionDeclaration(); - ~FunctionDeclaration(); + virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); } virtual void visit(NodeVisitor &); }; struct Conditional: Node { - Expression condition; + Expression *condition; Block body; Block else_body; + virtual Conditional *clone() const { return new Conditional(*this); } virtual void visit(NodeVisitor &); }; struct Iteration: Node { - Node *init_statement; - Expression condition; - Expression loop_expression; + NodePtr init_statement; + NodePtr condition; + NodePtr loop_expression; Block body; - Iteration(); - virtual ~Iteration(); - + virtual Iteration *clone() const { return new Iteration(*this); } virtual void visit(NodeVisitor &); }; struct Return: Node { - Expression expression; + NodePtr expression; + virtual Return *clone() const { return new Return(*this); } virtual void visit(NodeVisitor &); }; @@ -142,6 +254,13 @@ struct NodeVisitor virtual ~NodeVisitor() { } virtual void visit(Block &) { } + virtual void visit(Literal &) { } + virtual void visit(ParenthesizedExpression &) { } + virtual void visit(VariableReference &) { } + virtual void visit(MemberAccess &) { } + virtual void visit(UnaryExpression &) { } + virtual void visit(BinaryExpression &) { } + virtual void visit(FunctionCall &) { } virtual void visit(ExpressionStatement &) { } virtual void visit(Layout &) { } virtual void visit(StructDeclaration &) { } @@ -153,6 +272,24 @@ struct NodeVisitor virtual void visit(Return &) { } }; +struct TraversingVisitor: NodeVisitor +{ + virtual void visit(Block &); + virtual void visit(ParenthesizedExpression &); + virtual void visit(MemberAccess &); + virtual void visit(UnaryExpression &); + virtual void visit(BinaryExpression &); + virtual void visit(FunctionCall &); + virtual void visit(ExpressionStatement &); + virtual void visit(StructDeclaration &); + virtual void visit(VariableDeclaration &); + virtual void visit(InterfaceBlock &); + virtual void visit(FunctionDeclaration &); + virtual void visit(Conditional &); + virtual void visit(Iteration &); + virtual void visit(Return &); +}; + enum ContextType { GLOBAL, @@ -176,7 +313,6 @@ struct Module Context vertex_context; Context geometry_context; Context fragment_context; - std::map structs; Module(); };