]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor Parser::parse_expression to take a pointer to the outer operator
authorMikko Rasa <tdb@tdb.fi>
Fri, 12 Mar 2021 18:25:06 +0000 (20:25 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 12 Mar 2021 18:27:38 +0000 (20:27 +0200)
source/glsl/parser.cpp
source/glsl/parser.h

index fd782648083bdb05a1aa700ca65d4ab8f15aa926..b4631daf6d1c4e5b3cafa638400bd94e9d157074 100644 (file)
@@ -434,8 +434,9 @@ void Parser::parse_block(Block &block, bool require_braces, RefPtr<T> (Parser::*
                tokenizer.expect("}");
 }
 
-RefPtr<Expression> Parser::parse_expression(unsigned precedence)
+RefPtr<Expression> Parser::parse_expression(const Operator *outer_oper)
 {
+       unsigned outer_precedence = (outer_oper ? outer_oper->precedence+(outer_oper->assoc==Operator::RIGHT_TO_LEFT) : 20);
        RefPtr<Expression> left;
        VariableReference *left_var = 0;
        while(1)
@@ -447,7 +448,7 @@ RefPtr<Expression> Parser::parse_expression(unsigned precedence)
                        if(token==i->token && (!left || i->type!=Operator::PREFIX) && (left || i->type!=Operator::POSTFIX))
                                oper = i;
 
-               bool lower_precedence = (oper && oper->type!=Operator::PREFIX && precedence && oper->precedence>=precedence);
+               bool lower_precedence = (oper && oper->type!=Operator::PREFIX && oper->precedence>=outer_precedence);
                if(token==";" || token==")" || token=="]" || token=="," || lower_precedence)
                {
                        if(left)
@@ -510,7 +511,7 @@ RefPtr<Expression> Parser::parse_expression(unsigned precedence)
                                RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
                                unary->oper = oper;
                                tokenizer.parse_token();
-                               unary->expression = parse_expression(oper->precedence);
+                               unary->expression = parse_expression(oper);
                                left = unary;
                        }
                        else
@@ -552,7 +553,7 @@ RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, co
                tokenizer.expect("]");
        }
        else
-               binary->right = parse_expression(oper.precedence+(oper.assoc==Operator::RIGHT_TO_LEFT));
+               binary->right = parse_expression(&oper);
        return binary;
 }
 
index 13ec0ab9a1f8de2f0acdd279e3a4bef1ed35403e..bd5641399557cc4655d818d88fd1e0d85be37a43 100644 (file)
@@ -70,7 +70,7 @@ private:
        RefPtr<Layout> parse_layout();
        template<typename T>
        void parse_block(Block &, bool, RefPtr<T> (Parser::*)());
-       RefPtr<Expression> parse_expression(unsigned = 0);
+       RefPtr<Expression> parse_expression(const Operator * = 0);
        RefPtr<Literal> parse_literal();
        RefPtr<BinaryExpression> parse_binary(const RefPtr<Expression> &, const Operator &);
        RefPtr<FunctionCall> parse_function_call(const VariableReference &);