]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programparser.cpp
Refresh lighting and culling uniforms if the camera changes in pop_state
[libs/gl.git] / source / programparser.cpp
index f003a835b56b0352e7abe2387bcaff24792d87ce..da3ee0b99f5c383b5f9475676dd2d7b3eeb8c3ff 100644 (file)
@@ -95,7 +95,7 @@ void ProgramParser::parse_source()
        iter = source.begin();
        while(1)
        {
-               while(Node *statement = parse_global_declaration())
+               while(RefPtr<Node> statement = parse_global_declaration())
                        cur_stage->content.body.push_back(statement);
 
                parse_token();
@@ -329,11 +329,30 @@ bool ProgramParser::is_identifier(const string &token)
        return re.match(token);
 }
 
-Node *ProgramParser::parse_global_declaration()
+RefPtr<Node> ProgramParser::parse_global_declaration()
 {
        string token = peek_token();
-       if(token=="layout")
-               return parse_layout();
+       if(token=="import")
+               return parse_import();
+       else if(token=="layout")
+       {
+               RefPtr<Layout> layout = parse_layout();
+               token = peek_token();
+               if(is_interface_qualifier(token) && peek_token(1)==";")
+               {
+                       RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
+                       iface_lo->layout.qualifiers = layout->qualifiers;
+                       iface_lo->interface = parse_token();
+                       expect(";");
+                       return iface_lo;
+               }
+               else
+               {
+                       RefPtr<VariableDeclaration> var = parse_variable_declaration();
+                       var->layout = layout;
+                       return var;
+               }
+       }
        else if(token=="struct")
                return parse_struct_declaration();
        else if(is_sampling_qualifier(token) || token=="const")
@@ -358,7 +377,7 @@ Node *ProgramParser::parse_global_declaration()
                throw runtime_error(format("Syntax error at '%s': expected a global declaration", token));
 }
 
-Node *ProgramParser::parse_statement()
+RefPtr<Node> ProgramParser::parse_statement()
 {
        string token = peek_token();
        if(token=="if")
@@ -377,13 +396,25 @@ Node *ProgramParser::parse_statement()
                expr->expression = parse_expression();
                expect(";");
 
-               return expr.release();
+               return expr;
        }
        else
                throw runtime_error(format("Syntax error at '%s': expected a statement", token));
 }
 
-Layout *ProgramParser::parse_layout()
+RefPtr<Import> ProgramParser::parse_import()
+{
+       if(cur_stage->type!=SHARED)
+               throw runtime_error("Imports are only allowed in the shared section");
+
+       expect("import");
+       RefPtr<Import> import = new Import;
+       import->module = parse_token();
+       expect(";");
+       return import;
+}
+
+RefPtr<Layout> ProgramParser::parse_layout()
 {
        expect("layout");
        expect("(");
@@ -407,10 +438,8 @@ Layout *ProgramParser::parse_layout()
                expect(",");
        }
        expect(")");
-       layout->interface = parse_token();
-       expect(";");
 
-       return layout.release();
+       return layout;
 }
 
 void ProgramParser::parse_block(Block &block, bool require_braces)
@@ -433,7 +462,7 @@ void ProgramParser::parse_block(Block &block, bool require_braces)
                expect("}");
 }
 
-Expression *ProgramParser::parse_expression(unsigned precedence)
+RefPtr<Expression> ProgramParser::parse_expression(unsigned precedence)
 {
        RefPtr<Expression> left;
        VariableReference *left_var = 0;
@@ -449,7 +478,7 @@ Expression *ProgramParser::parse_expression(unsigned precedence)
                if(token==";" || token==")" || token=="]" || token=="," || (oper && precedence && oper->precedence>=precedence))
                {
                        if(left)
-                               return left.release();
+                               return left;
                        else
                                throw runtime_error(format("Parse error at '%s': expected an expression", token));
                }
@@ -459,12 +488,12 @@ Expression *ProgramParser::parse_expression(unsigned precedence)
                        {
                                if(!left_var)
                                        throw runtime_error(format("Parse error at '%s': function name must be an identifier", token));
-                               left = parse_function_call(left_var);
+                               left = parse_function_call(*left_var);
                        }
                        else if(token==".")
                        {
                                RefPtr<MemberAccess> memacc = new MemberAccess;
-                               memacc->left = left.release();
+                               memacc->left = left;
                                parse_token();
                                memacc->member = expect_identifier();
                                left = memacc;
@@ -474,11 +503,11 @@ Expression *ProgramParser::parse_expression(unsigned precedence)
                                RefPtr<UnaryExpression> unary = new UnaryExpression;
                                unary->oper = parse_token();
                                unary->prefix = false;
-                               unary->expression = left.release();
+                               unary->expression = left;
                                left = unary;
                        }
                        else if(oper && oper->type==BINARY)
-                               left = parse_binary(left.release(), oper);
+                               left = parse_binary(left, oper);
                        else
                                throw runtime_error(format("Parse error at '%s': expected an operator", token));
                        left_var = 0;
@@ -493,6 +522,12 @@ Expression *ProgramParser::parse_expression(unsigned precedence)
                                expect(")");
                                left = parexpr;
                        }
+                       else if(isdigit(token[0]) || token=="true" || token=="false")
+                       {
+                               RefPtr<Literal> literal = new Literal;
+                               literal->token = parse_token();
+                               left = literal;
+                       }
                        else if(is_identifier(token))
                        {
                                RefPtr<VariableReference> var = new VariableReference;
@@ -508,19 +543,13 @@ Expression *ProgramParser::parse_expression(unsigned precedence)
                                unary->expression = parse_expression(oper->precedence);
                                left = unary;
                        }
-                       else if(isdigit(token[0]))
-                       {
-                               RefPtr<Literal> literal = new Literal;
-                               literal->token = parse_token();
-                               left = literal;
-                       }
                        else
                                throw runtime_error(format("Parse error at '%s': expected an expression", token));
                }
        }
 }
 
-BinaryExpression *ProgramParser::parse_binary(Expression *left, const Operator *oper)
+RefPtr<BinaryExpression> ProgramParser::parse_binary(const RefPtr<Expression> &left, const Operator *oper)
 {
        RefPtr<BinaryExpression> binary = (oper->precedence==16 ? new Assignment : new BinaryExpression);
        binary->left = left;
@@ -533,13 +562,13 @@ BinaryExpression *ProgramParser::parse_binary(Expression *left, const Operator *
        }
        else
                binary->right = parse_expression(oper->precedence+(oper->assoc==RIGHT_TO_LEFT));
-       return binary.release();
+       return binary;
 }
 
-FunctionCall *ProgramParser::parse_function_call(VariableReference *var)
+RefPtr<FunctionCall> ProgramParser::parse_function_call(const VariableReference &var)
 {
        RefPtr<FunctionCall> call = new FunctionCall;
-       call->name = var->name;
+       call->name = var.name;
        call->constructor = is_type(call->name);
        expect("(");
        while(peek_token()!=")")
@@ -549,10 +578,10 @@ FunctionCall *ProgramParser::parse_function_call(VariableReference *var)
                call->arguments.push_back(parse_expression());
        }
        expect(")");
-       return call.release();
+       return call;
 }
 
-StructDeclaration *ProgramParser::parse_struct_declaration()
+RefPtr<StructDeclaration> ProgramParser::parse_struct_declaration()
 {
        expect("struct");
        RefPtr<StructDeclaration> strct = new StructDeclaration;
@@ -562,10 +591,10 @@ StructDeclaration *ProgramParser::parse_struct_declaration()
        expect(";");
 
        declared_types.insert(strct->name);
-       return strct.release();
+       return strct;
 }
 
-VariableDeclaration *ProgramParser::parse_variable_declaration()
+RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
 {
        RefPtr<VariableDeclaration> var = new VariableDeclaration;
 
@@ -603,10 +632,10 @@ VariableDeclaration *ProgramParser::parse_variable_declaration()
                var->init_expression = parse_expression();
 
        expect(";");
-       return var.release();
+       return var;
 }
 
-FunctionDeclaration *ProgramParser::parse_function_declaration()
+RefPtr<FunctionDeclaration> ProgramParser::parse_function_declaration()
 {
        RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
 
@@ -621,14 +650,14 @@ FunctionDeclaration *ProgramParser::parse_function_declaration()
                RefPtr<VariableDeclaration> var = new VariableDeclaration;
                var->type = expect_type();
                var->name = expect_identifier();
-               func->parameters.push_back(var.release());
+               func->parameters.push_back(var);
        }
        expect(")");
 
        string token = peek_token();
        if(token=="{")
        {
-               func->definition = true;
+               func->definition = func.get();
                parse_block(func->body, true);
        }
        else if(token==";")
@@ -636,10 +665,10 @@ FunctionDeclaration *ProgramParser::parse_function_declaration()
        else
                throw runtime_error(format("Parse error at '%s': expected '{' or ';'", token));
 
-       return func.release();
+       return func;
 }
 
-InterfaceBlock *ProgramParser::parse_interface_block()
+RefPtr<InterfaceBlock> ProgramParser::parse_interface_block()
 {
        RefPtr<InterfaceBlock> iface = new InterfaceBlock;
 
@@ -660,10 +689,10 @@ InterfaceBlock *ProgramParser::parse_interface_block()
                expect(";");
        }
 
-       return iface.release();
+       return iface;
 }
 
-Conditional *ProgramParser::parse_conditional()
+RefPtr<Conditional> ProgramParser::parse_conditional()
 {
        expect("if");
        expect("(");
@@ -680,10 +709,10 @@ Conditional *ProgramParser::parse_conditional()
                parse_block(cond->else_body, false);
        }
 
-       return cond.release();
+       return cond;
 }
 
-Iteration *ProgramParser::parse_iteration()
+RefPtr<Iteration> ProgramParser::parse_iteration()
 {
        expect("for");
        expect("(");
@@ -696,7 +725,7 @@ Iteration *ProgramParser::parse_iteration()
                RefPtr<ExpressionStatement> expr = new ExpressionStatement;
                expr->expression = parse_expression();
                expect(";");
-               loop->init_statement = expr.release();
+               loop->init_statement = expr;
        }
        loop->condition = parse_expression();
        expect(";");
@@ -705,10 +734,10 @@ Iteration *ProgramParser::parse_iteration()
 
        parse_block(loop->body, false);
 
-       return loop.release();
+       return loop;
 }
 
-Passthrough *ProgramParser::parse_passthrough()
+RefPtr<Passthrough> ProgramParser::parse_passthrough()
 {
        expect("passthrough");
        RefPtr<Passthrough> pass = new Passthrough;
@@ -719,17 +748,17 @@ Passthrough *ProgramParser::parse_passthrough()
                expect("]");
        }
        expect(";");
-       return pass.release();
+       return pass;
 }
 
-Return *ProgramParser::parse_return()
+RefPtr<Return> ProgramParser::parse_return()
 {
        expect("return");
        RefPtr<Return> ret = new Return;
        if(peek_token()!=";")
                ret->expression = parse_expression();
        expect(";");
-       return ret.release();
+       return ret;
 }
 
 } // namespace GL