]> git.tdb.fi Git - libs/gl.git/commitdiff
Add an intermediate node type for statements
authorMikko Rasa <tdb@tdb.fi>
Fri, 21 Jun 2019 10:19:47 +0000 (13:19 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 21 Jun 2019 11:54:28 +0000 (14:54 +0300)
source/programcompiler.cpp
source/programcompiler.h
source/programparser.cpp
source/programparser.h
source/programsyntax.cpp
source/programsyntax.h

index 9a3986a12b21e094824175bf0dc7b052dd2af776..ca3913cde1358656d307dc85cbcb170121b246be 100644 (file)
@@ -168,7 +168,7 @@ void ProgramCompiler::append_stage(Stage &stage)
                target = &*i;
        }
 
-       for(NodeList<Node>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
+       for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
                target->content.body.push_back(*i);
        apply<DeclarationCombiner>(*target);
 }
@@ -241,8 +241,8 @@ void ProgramCompiler::finalize(Stage &stage)
 
 void ProgramCompiler::inject_block(Block &target, const Block &source)
 {
-       list<RefPtr<Node> >::iterator insert_point = target.body.begin();
-       for(list<RefPtr<Node> >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
+       list<RefPtr<Statement> >::iterator insert_point = target.body.begin();
+       for(list<RefPtr<Statement> >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
                target.body.insert(insert_point, (*i)->clone());
 }
 
@@ -284,7 +284,7 @@ void ProgramCompiler::BlockModifier::flatten_block(Block &block)
        remove_node = true;
 }
 
-void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Node> >::iterator &i)
+void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Statement> >::iterator &i)
 {
        block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
        insert_nodes.clear();
@@ -298,7 +298,7 @@ void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefP
 
 void ProgramCompiler::BlockModifier::visit(Block &block)
 {
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       for(list<RefPtr<Statement> >::iterator i=block.body.begin(); i!=block.body.end(); )
        {
                (*i)->visit(*this);
                apply_and_increment(block, i);
@@ -410,7 +410,7 @@ void ProgramCompiler::Formatter::visit(Block &block)
 
        SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
        string spaces(indent*2, ' ');
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       for(list<RefPtr<Statement> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
        {
                if(i!=block.body.begin())
                        formatted += '\n';
@@ -865,7 +865,7 @@ void ProgramCompiler::InterfaceGenerator::apply(Stage &s)
 void ProgramCompiler::InterfaceGenerator::visit(Block &block)
 {
        SetForScope<unsigned> set(scope_level, scope_level+1);
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       for(list<RefPtr<Statement> >::iterator i=block.body.begin(); i!=block.body.end(); )
        {
                (*i)->visit(*this);
 
@@ -873,7 +873,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block)
                {
                        for(map<string, RefPtr<VariableDeclaration> >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
                        {
-                               list<RefPtr<Node> >::iterator k = block.body.insert(i, j->second);
+                               list<RefPtr<Statement> >::iterator k = block.body.insert(i, j->second);
                                (*k)->visit(*this);
                        }
                        iface_declarations.clear();
@@ -1069,13 +1069,13 @@ void ProgramCompiler::DeclarationReorderer::visit(Block &block)
        if(scope_level>1)
                return Visitor::visit(block);
 
-       list<RefPtr<Node> >::iterator struct_insert_point = block.body.end();
-       list<RefPtr<Node> >::iterator variable_insert_point = block.body.end();
-       list<RefPtr<Node> >::iterator function_insert_point = block.body.end();
+       list<RefPtr<Statement> >::iterator struct_insert_point = block.body.end();
+       list<RefPtr<Statement> >::iterator variable_insert_point = block.body.end();
+       list<RefPtr<Statement> >::iterator function_insert_point = block.body.end();
        unsigned unordered_func_count = 0;
        bool ordered_any_funcs = false;
 
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       for(list<RefPtr<Statement> >::iterator i=block.body.begin(); i!=block.body.end(); )
        {
                kind = NO_DECLARATION;
                (*i)->visit(*this);
@@ -1210,7 +1210,7 @@ void ProgramCompiler::FunctionInliner::visit(Block &block)
        if(extract_result)
                --extract_result;
 
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       for(list<RefPtr<Statement> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
        {
                (*i)->visit(*this);
                if(extract_result)
@@ -1693,7 +1693,7 @@ ProgramCompiler::NodeRemover::NodeRemover(const set<Node *> &r):
 
 void ProgramCompiler::NodeRemover::visit(Block &block)
 {
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       for(list<RefPtr<Statement> >::iterator i=block.body.begin(); i!=block.body.end(); )
        {
                (*i)->visit(*this);
                if(to_remove.count(i->get()))
index 76e9eef352f83f67a46c6b8a620d0411c0bf0440..5a2f42401620505c5801cf0326a5311e16a58f37 100644 (file)
@@ -29,12 +29,12 @@ private:
        struct BlockModifier: Visitor
        {
                bool remove_node;
-               std::vector<RefPtr<ProgramSyntax::Node> > insert_nodes;
+               std::vector<RefPtr<ProgramSyntax::Statement> > insert_nodes;
 
                BlockModifier();
 
                void flatten_block(ProgramSyntax::Block &);
-               void apply_and_increment(ProgramSyntax::Block &, std::list<RefPtr<ProgramSyntax::Node> >::iterator &);
+               void apply_and_increment(ProgramSyntax::Block &, std::list<RefPtr<ProgramSyntax::Statement> >::iterator &);
                using Visitor::visit;
                virtual void visit(ProgramSyntax::Block &);
        };
index 4f08b97718d26afe4ab1b41b60af3ee5f737d70b..07fa19de655e774aa0765e5bd20f3c0d14f58d48 100644 (file)
@@ -112,7 +112,7 @@ void ProgramParser::parse_source()
        source_end = source.end();
        current_line = 1;
        allow_preprocess = true;
-       while(RefPtr<Node> statement = parse_global_declaration())
+       while(RefPtr<Statement> statement = parse_global_declaration())
                cur_stage->content.body.push_back(statement);
 }
 
@@ -426,7 +426,7 @@ void ProgramParser::preprocess_stage()
        cur_stage = &module->stages.back();
 }
 
-RefPtr<Node> ProgramParser::parse_global_declaration()
+RefPtr<Statement> ProgramParser::parse_global_declaration()
 {
        allow_stage_change = true;
        string token = peek_token();
@@ -480,7 +480,7 @@ RefPtr<Node> ProgramParser::parse_global_declaration()
                throw runtime_error(format_syntax_error("a global declaration"));
 }
 
-RefPtr<Node> ProgramParser::parse_statement()
+RefPtr<Statement> ProgramParser::parse_statement()
 {
        string token = peek_token();
        if(token=="if")
index 707815405684252f1fc06c33341ea19b5ba5199c..72817e7ede06f8fe9607a2c17d8d99a503ea6c8e 100644 (file)
@@ -90,8 +90,8 @@ private:
        void preprocess_pragma_msp();
        void preprocess_stage();
 
-       RefPtr<ProgramSyntax::Node> parse_global_declaration();
-       RefPtr<ProgramSyntax::Node> parse_statement();
+       RefPtr<ProgramSyntax::Statement> parse_global_declaration();
+       RefPtr<ProgramSyntax::Statement> parse_statement();
        RefPtr<ProgramSyntax::Import> parse_import();
        RefPtr<ProgramSyntax::Precision> parse_precision();
        RefPtr<ProgramSyntax::Layout> parse_layout();
index 47b59f6c8900868c21f91c633cf2c4b512da55e4..46f51c2cb699f26c10b57a59c17a152d26a61425 100644 (file)
@@ -213,7 +213,7 @@ void NodeVisitor::visit(Assignment &assign)
 
 void TraversingVisitor::visit(Block &block)
 {
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       for(list<RefPtr<Statement> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
                (*i)->visit(*this);
 }
 
index 1f26bc568800e1f9374cb0f4b9e20647442d6804..3db059c4f4d79c4e6252537c5a1cfc27b90e223e 100644 (file)
@@ -64,9 +64,14 @@ struct StructDeclaration;
 struct VariableDeclaration;
 struct FunctionDeclaration;
 
+struct Statement: Node
+{
+       virtual Statement *clone() const = 0;
+};
+
 struct Block: Node
 {
-       NodeList<Node> body;
+       NodeList<Statement> body;
        bool use_braces;
        std::map<std::string, StructDeclaration *> types;
        std::map<std::string, VariableDeclaration *> variables;
@@ -166,7 +171,7 @@ struct FunctionCall: Expression
        virtual void visit(NodeVisitor &);
 };
 
-struct ExpressionStatement: Node
+struct ExpressionStatement: Statement
 {
        NodePtr<Expression> expression;
 
@@ -174,7 +179,7 @@ struct ExpressionStatement: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Import: Node
+struct Import: Statement
 {
        std::string module;
 
@@ -182,7 +187,7 @@ struct Import: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Precision: Node
+struct Precision: Statement
 {
        std::string precision;
        std::string type;
@@ -206,7 +211,7 @@ struct Layout: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct InterfaceLayout: Node
+struct InterfaceLayout: Statement
 {
        std::string interface;
        Layout layout;
@@ -215,7 +220,7 @@ struct InterfaceLayout: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct StructDeclaration: Node
+struct StructDeclaration: Statement
 {
        std::string name;
        Block members;
@@ -226,7 +231,7 @@ struct StructDeclaration: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct VariableDeclaration: Node
+struct VariableDeclaration: Statement
 {
        bool constant;
        std::string sampling;
@@ -248,7 +253,7 @@ struct VariableDeclaration: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct InterfaceBlock: Node
+struct InterfaceBlock: Statement
 {
        std::string interface;
        std::string name;
@@ -262,7 +267,7 @@ struct InterfaceBlock: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct FunctionDeclaration: Node
+struct FunctionDeclaration: Statement
 {
        std::string return_type;
        std::string name;
@@ -277,7 +282,7 @@ struct FunctionDeclaration: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Conditional: Node
+struct Conditional: Statement
 {
        NodePtr<Expression> condition;
        Block body;
@@ -287,7 +292,7 @@ struct Conditional: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Iteration: Node
+struct Iteration: Statement
 {
        NodePtr<Node> init_statement;
        NodePtr<Expression> condition;
@@ -298,7 +303,7 @@ struct Iteration: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Passthrough: Node
+struct Passthrough: Statement
 {
        NodePtr<Expression> subscript;
 
@@ -306,7 +311,7 @@ struct Passthrough: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Return: Node
+struct Return: Statement
 {
        NodePtr<Expression> expression;
 
@@ -314,7 +319,7 @@ struct Return: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Jump: Node
+struct Jump: Statement
 {
        std::string keyword;