From 1346bf399f982760c90eeecdf67336d0f7edd4aa Mon Sep 17 00:00:00 2001 From: Blendi Date: Mon, 27 Apr 2026 18:07:09 +0200 Subject: [PATCH] parser: parenthesized expressions --- src/main.c | 2 +- src/parser.c | 29 +++++++++++++++++++++++++++++ src/parser.h | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 2da24cd..11bab50 100644 --- a/src/main.c +++ b/src/main.c @@ -33,7 +33,7 @@ void print_node(noomP_Node* node, noom_uint_t depth) { int main(int argc, char** argv) { // uhh uhhh uhhhhh - const char* code = "local a = a().a:temp(2,5)"; + const char* code = "local a = (5+5)"; noom_uint_t pos = 0; printf("LEX OUTPUT:\n"); diff --git a/src/parser.c b/src/parser.c index 54f7950..6b63ff7 100644 --- a/src/parser.c +++ b/src/parser.c @@ -41,6 +41,8 @@ const char *noomP_formatNodeType(noomP_NodeType node_type) { return "method call"; case NOOMP_NODE_FIELDNAME: return "fieldname"; + case NOOMP_NODE_PARENTHESIZED: + return "parenthesized"; default: return "unknown"; } @@ -325,6 +327,33 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) { return litNode; } + } else if (token.type == NOOML_TOKEN_SYMBOL) { + if (noom_streql(parser->code + token.offset, token.length, "(", 1)) { // parenthesized + noomP_skip(parser, &token); + noom_uint_t sym_loc = token.offset; + + noomP_Node* expr = noomP_parseExpression(parser); + if (expr == 0) return 0; + + // now to close the parentheses + + noomP_peek(parser, &token); + if (token.type != NOOML_TOKEN_SYMBOL) return 0; // unexpected + if (!noom_streql(parser->code + token.offset, token.length, ")", 1)) return 0; // unexpected + noomP_skip(parser, &token); + + noomP_Node* paren = noomP_allocNode(parser); // it has to be a node cause it limits to one value + if (paren == 0) return 0; + + paren->type = NOOMP_NODE_PARENTHESIZED; + paren->source_offset = sym_loc; + + noomP_addSubnode(paren, expr); + + // buttt we're not done YET! it could still go :dsg().dsdh():dsh() + + return noomP_parseComplexExpression(parser, paren); // thank you for being a function :heart: + } } return 0; diff --git a/src/parser.h b/src/parser.h index 0fcd098..7ccf881 100644 --- a/src/parser.h +++ b/src/parser.h @@ -29,6 +29,8 @@ typedef enum noomP_NodeType { NOOMP_NODE_FIELDNAME, + NOOMP_NODE_PARENTHESIZED, + NOOMP_NODE_NCOUNT, } noomP_NodeType;