parser: parenthesized expressions

This commit is contained in:
2026-04-27 18:07:09 +02:00
parent 146b524587
commit 1346bf399f
3 changed files with 32 additions and 1 deletions

View File

@@ -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");

View File

@@ -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;

View File

@@ -29,6 +29,8 @@ typedef enum noomP_NodeType {
NOOMP_NODE_FIELDNAME,
NOOMP_NODE_PARENTHESIZED,
NOOMP_NODE_NCOUNT,
} noomP_NodeType;