From 911c6c4acc8218bc40d93917207f9dc32e9f2596 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 13 Mar 2021 21:08:28 +0200 Subject: [PATCH] Rearrange operator metadata A secondary token can now be stored for the operators that need one. The member access operator is categorized as postfix because it only has one actual operand (the right-hand side is just an identifier). --- source/glsl/debug.cpp | 4 +- source/glsl/output.cpp | 8 ++-- source/glsl/parser.cpp | 4 +- source/glsl/syntax.cpp | 89 +++++++++++++++++++++--------------------- source/glsl/syntax.h | 3 +- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/source/glsl/debug.cpp b/source/glsl/debug.cpp index 700acbf9..ddf1013d 100644 --- a/source/glsl/debug.cpp +++ b/source/glsl/debug.cpp @@ -196,7 +196,7 @@ void DumpTree::visit(UnaryExpression &unary) void DumpTree::visit(BinaryExpression &binary) { - append(format("Binary: %s -> %s", (binary.oper->token[0]=='[' ? "[]" : binary.oper->token), format_type(binary.type))); + append(format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type))); begin_sub(); binary.left->visit(*this); last_branch(); @@ -242,7 +242,7 @@ void DumpTree::visit(Assignment &assign) void DumpTree::visit(TernaryExpression &ternary) { - append(format("Ternary: %s -> %s", (ternary.oper->token[0]=='?' ? "?:" : ternary.oper->token), format_type(ternary.type))); + append(format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type))); begin_sub(); ternary.condition->visit(*this); ternary.true_expr->visit(*this); diff --git a/source/glsl/output.cpp b/source/glsl/output.cpp index 364e5e4b..35c52fec 100644 --- a/source/glsl/output.cpp +++ b/source/glsl/output.cpp @@ -156,8 +156,8 @@ void Formatter::visit(BinaryExpression &binary) binary.left->visit(*this); append(binary.oper->token); binary.right->visit(*this); - if(binary.oper->token[0]=='[') - append(']'); + if(binary.oper->token2[0]) + append(binary.oper->token2); } void Formatter::visit(Assignment &assign) @@ -172,8 +172,8 @@ void Formatter::visit(TernaryExpression &ternary) ternary.condition->visit(*this); append(ternary.oper->token); ternary.true_expr->visit(*this); - if(ternary.oper->token[0]=='?') - append(':'); + if(ternary.oper->token2) + append(ternary.oper->token2); ternary.false_expr->visit(*this); } diff --git a/source/glsl/parser.cpp b/source/glsl/parser.cpp index 31f9fcb6..7b034a4c 100644 --- a/source/glsl/parser.cpp +++ b/source/glsl/parser.cpp @@ -551,10 +551,10 @@ RefPtr Parser::parse_binary(const RefPtr &left, co binary->left = left; binary->oper = &oper; tokenizer.expect(oper.token); - if(oper.token[0]=='[') + if(oper.token2[0]) { binary->right = parse_expression(); - tokenizer.expect("]"); + tokenizer.expect(oper.token2); } else binary->right = parse_expression(&oper); diff --git a/source/glsl/syntax.cpp b/source/glsl/syntax.cpp index 025af271..bbd364c0 100644 --- a/source/glsl/syntax.cpp +++ b/source/glsl/syntax.cpp @@ -10,51 +10,50 @@ namespace SL { const Operator Operator::operators[] = { - { "[", 2, BINARY, LEFT_TO_RIGHT }, - { "(", 2, POSTFIX, LEFT_TO_RIGHT }, - { ".", 2, BINARY, LEFT_TO_RIGHT }, - { "++", 2, POSTFIX, LEFT_TO_RIGHT }, - { "--", 2, POSTFIX, LEFT_TO_RIGHT }, - { "++", 3, PREFIX, RIGHT_TO_LEFT }, - { "--", 3, PREFIX, RIGHT_TO_LEFT }, - { "+", 3, PREFIX, RIGHT_TO_LEFT }, - { "-", 3, PREFIX, RIGHT_TO_LEFT }, - { "~", 3, PREFIX, RIGHT_TO_LEFT }, - { "!", 3, PREFIX, RIGHT_TO_LEFT }, - { "*", 4, BINARY, ASSOCIATIVE }, - { "/", 4, BINARY, LEFT_TO_RIGHT }, - { "%", 4, BINARY, LEFT_TO_RIGHT }, - { "+", 5, BINARY, ASSOCIATIVE }, - { "-", 5, BINARY, LEFT_TO_RIGHT }, - { "<<", 6, BINARY, LEFT_TO_RIGHT }, - { ">>", 6, BINARY, LEFT_TO_RIGHT }, - { "<", 7, BINARY, LEFT_TO_RIGHT }, - { ">", 7, BINARY, LEFT_TO_RIGHT }, - { "<=", 7, BINARY, LEFT_TO_RIGHT }, - { ">=", 7, BINARY, LEFT_TO_RIGHT }, - { "==", 8, BINARY, LEFT_TO_RIGHT }, - { "!=", 8, BINARY, LEFT_TO_RIGHT }, - { "&", 9, BINARY, ASSOCIATIVE }, - { "^", 10, BINARY, ASSOCIATIVE }, - { "|", 11, BINARY, ASSOCIATIVE }, - { "&&", 12, BINARY, ASSOCIATIVE }, - { "^^", 13, BINARY, ASSOCIATIVE }, - { "||", 14, BINARY, ASSOCIATIVE }, - { "?", 15, TERNARY, RIGHT_TO_LEFT }, - { ":", 15, TERNARY, RIGHT_TO_LEFT }, - { "=", 16, BINARY, RIGHT_TO_LEFT }, - { "+=", 16, BINARY, RIGHT_TO_LEFT }, - { "-=", 16, BINARY, RIGHT_TO_LEFT }, - { "*=", 16, BINARY, RIGHT_TO_LEFT }, - { "/=", 16, BINARY, RIGHT_TO_LEFT }, - { "%=", 16, BINARY, RIGHT_TO_LEFT }, - { "<<=", 16, BINARY, RIGHT_TO_LEFT }, - { ">>=", 16, BINARY, RIGHT_TO_LEFT }, - { "&=", 16, BINARY, RIGHT_TO_LEFT }, - { "^=", 16, BINARY, RIGHT_TO_LEFT }, - { "|=", 16, BINARY, RIGHT_TO_LEFT }, - { ",", 17, BINARY, LEFT_TO_RIGHT }, - { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT } + { "[", "]", 2, BINARY, LEFT_TO_RIGHT }, + { "(", ")", 2, POSTFIX, LEFT_TO_RIGHT }, + { ".", { }, 2, POSTFIX, LEFT_TO_RIGHT }, + { "++", { }, 2, POSTFIX, LEFT_TO_RIGHT }, + { "--", { }, 2, POSTFIX, LEFT_TO_RIGHT }, + { "++", { }, 3, PREFIX, RIGHT_TO_LEFT }, + { "--", { }, 3, PREFIX, RIGHT_TO_LEFT }, + { "+", { }, 3, PREFIX, RIGHT_TO_LEFT }, + { "-", { }, 3, PREFIX, RIGHT_TO_LEFT }, + { "~", { }, 3, PREFIX, RIGHT_TO_LEFT }, + { "!", { }, 3, PREFIX, RIGHT_TO_LEFT }, + { "*", { }, 4, BINARY, ASSOCIATIVE }, + { "/", { }, 4, BINARY, LEFT_TO_RIGHT }, + { "%", { }, 4, BINARY, LEFT_TO_RIGHT }, + { "+", { }, 5, BINARY, ASSOCIATIVE }, + { "-", { }, 5, BINARY, LEFT_TO_RIGHT }, + { "<<", { }, 6, BINARY, LEFT_TO_RIGHT }, + { ">>", { }, 6, BINARY, LEFT_TO_RIGHT }, + { "<", { }, 7, BINARY, LEFT_TO_RIGHT }, + { ">", { }, 7, BINARY, LEFT_TO_RIGHT }, + { "<=", { }, 7, BINARY, LEFT_TO_RIGHT }, + { ">=", { }, 7, BINARY, LEFT_TO_RIGHT }, + { "==", { }, 8, BINARY, LEFT_TO_RIGHT }, + { "!=", { }, 8, BINARY, LEFT_TO_RIGHT }, + { "&", { }, 9, BINARY, ASSOCIATIVE }, + { "^", { }, 10, BINARY, ASSOCIATIVE }, + { "|", { }, 11, BINARY, ASSOCIATIVE }, + { "&&", { }, 12, BINARY, ASSOCIATIVE }, + { "^^", { }, 13, BINARY, ASSOCIATIVE }, + { "||", { }, 14, BINARY, ASSOCIATIVE }, + { "?", ":", 15, TERNARY, RIGHT_TO_LEFT }, + { "=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "+=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "-=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "*=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "/=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "%=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "<<=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { ">>=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "&=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "^=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { "|=", { }, 16, BINARY, RIGHT_TO_LEFT }, + { ",", { }, 17, BINARY, LEFT_TO_RIGHT }, + { { 0 }, { }, 18, NO_OPERATOR, LEFT_TO_RIGHT } }; const Operator &Operator::get_operator(const string &token, Type type) diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index 81fd6b4f..b8d9c1a6 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -39,7 +39,8 @@ struct Operator }; char token[4]; - unsigned precedence; + char token2[2]; + UInt8 precedence; Type type; Associativity assoc; -- 2.45.2