X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramsyntax.h;h=88f333ac1559d2e662dbaeca7903aecfbdbfa1ed;hb=5945ad9b63bbc55c3ed21f0c023d17f73aaac370;hp=0013d82a97a7cd8c890e3f0a4456ae5dd4875400;hpb=6e6ee01b68056b23c6709d7f60396710dd7623b9;p=libs%2Fgl.git diff --git a/source/programsyntax.h b/source/programsyntax.h index 0013d82a..88f333ac 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,143 @@ 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; } +#if __cplusplus>=201103L + NodePtr(NodePtr &&p): node(p.node) { p.node = 0; } + NodePtr &operator=(NodePtr &&p) { delete node; node = p.node; p.node = 0; return *this; } +#endif + ~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 +165,7 @@ struct Layout: Node std::vector qualifiers; std::string interface; + virtual Layout *clone() const { return new Layout(*this); } virtual void visit(NodeVisitor &); }; @@ -64,6 +176,7 @@ struct StructDeclaration: Node StructDeclaration(); + virtual StructDeclaration *clone() const { return new StructDeclaration(*this); } virtual void visit(NodeVisitor &); }; @@ -73,13 +186,16 @@ 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 *linked_declaration; VariableDeclaration(); + virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); } virtual void visit(NodeVisitor &); }; @@ -88,9 +204,12 @@ struct InterfaceBlock: Node std::string interface; std::string name; Block members; + std::string instance_name; + bool array; InterfaceBlock(); + virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); } virtual void visit(NodeVisitor &); }; @@ -98,42 +217,50 @@ 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; + NodePtr 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 Passthrough: Node +{ + NodePtr subscript; + virtual Passthrough *clone() const { return new Passthrough(*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 +269,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 &) { } @@ -150,33 +284,52 @@ struct NodeVisitor virtual void visit(FunctionDeclaration &) { } virtual void visit(Conditional &) { } virtual void visit(Iteration &) { } + virtual void visit(Passthrough &) { } virtual void visit(Return &) { } }; -enum ContextType +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(Passthrough &); + virtual void visit(Return &); +}; + +enum StageType { - GLOBAL, + SHARED, VERTEX, GEOMETRY, FRAGMENT }; -struct Context +struct Stage { - ContextType type; - bool present; + StageType type; + Stage *previous; ProgramSyntax::Block content; + std::map in_variables; + std::map out_variables; - Context(ContextType); + Stage(StageType); }; struct Module { - Context global_context; - Context vertex_context; - Context geometry_context; - Context fragment_context; - std::map structs; + Stage shared; + std::list stages; Module(); };