From 1476e64621ecbd7b17b00ae2c958322fd39918de Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 12 Mar 2021 20:25:06 +0200 Subject: [PATCH] Refactor Parser::parse_expression to take a pointer to the outer operator --- source/glsl/parser.cpp | 9 +++++---- source/glsl/parser.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/source/glsl/parser.cpp b/source/glsl/parser.cpp index fd782648..b4631daf 100644 --- a/source/glsl/parser.cpp +++ b/source/glsl/parser.cpp @@ -434,8 +434,9 @@ void Parser::parse_block(Block &block, bool require_braces, RefPtr (Parser::* tokenizer.expect("}"); } -RefPtr Parser::parse_expression(unsigned precedence) +RefPtr Parser::parse_expression(const Operator *outer_oper) { + unsigned outer_precedence = (outer_oper ? outer_oper->precedence+(outer_oper->assoc==Operator::RIGHT_TO_LEFT) : 20); RefPtr left; VariableReference *left_var = 0; while(1) @@ -447,7 +448,7 @@ RefPtr 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 Parser::parse_expression(unsigned precedence) RefPtr unary = create_node(); 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 Parser::parse_binary(const RefPtr &left, co tokenizer.expect("]"); } else - binary->right = parse_expression(oper.precedence+(oper.assoc==Operator::RIGHT_TO_LEFT)); + binary->right = parse_expression(&oper); return binary; } diff --git a/source/glsl/parser.h b/source/glsl/parser.h index 13ec0ab9..bd564139 100644 --- a/source/glsl/parser.h +++ b/source/glsl/parser.h @@ -70,7 +70,7 @@ private: RefPtr parse_layout(); template void parse_block(Block &, bool, RefPtr (Parser::*)()); - RefPtr parse_expression(unsigned = 0); + RefPtr parse_expression(const Operator * = 0); RefPtr parse_literal(); RefPtr parse_binary(const RefPtr &, const Operator &); RefPtr parse_function_call(const VariableReference &); -- 2.45.2