From f3651e85c55cf2ed6bb33950383905aa635f7f29 Mon Sep 17 00:00:00 2001 From: Blendi Date: Wed, 29 Apr 2026 13:23:59 +0200 Subject: [PATCH] parser: goto + labels --- src/main.c | 2 +- src/parser.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/parser.h | 3 +++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index d00d4a8..b894a13 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 = "for k,v,a,b,c in 8,5,6,3,2,5 do end"; + const char* code = "::lol:: print('hi') goto lol"; noom_uint_t pos = 0; printf("LEX OUTPUT:\n"); diff --git a/src/parser.c b/src/parser.c index f14a1ae..bfd6b7f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -57,6 +57,10 @@ const char *noomP_formatNodeType(noomP_NodeType node_type) { return "function name"; case NOOMP_NODE_RETURN: return "return"; + case NOOMP_NODE_GOTO: + return "goto"; + case NOOMP_NODE_LABEL: + return "label"; case NOOMP_NODE_FIELDNAME: return "field name"; case NOOMP_NODE_METHODNAME: @@ -1230,6 +1234,58 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) { noomP_skip(parser, &token); return forl; + } else if (noom_streql(parser->code + token.offset, token.length, "goto", 4)) { + noomP_skip(parser, &token); + + noomP_Node* thing = noomP_allocNode(parser); + if (thing == 0) return 0; + + thing->type = NOOMP_NODE_GOTO; + thing->source_offset = token.offset; + + noomP_peek(parser, &token); + if (token.type != NOOML_TOKEN_IDENTIFIER) return 0; + noomP_skip(parser, &token); + + noomP_Node* name = noomP_allocNode(parser); + if (name == 0) return 0; + + name->type = NOOMP_NODE_VARNAME; // eh good enough + name->source_offset = token.offset; + + noomP_addSubnode(thing, name); + + return thing; + } + } else if (token.type == NOOML_TOKEN_SYMBOL) { + if (noom_streql(parser->code + token.offset, token.length, "::", 2)) { + noomP_skip(parser, &token); + + noomP_Node* thing = noomP_allocNode(parser); + if (thing == 0) return 0; + + thing->type = NOOMP_NODE_LABEL; + thing->source_offset = token.offset; + + noomP_peek(parser, &token); + if (token.type != NOOML_TOKEN_IDENTIFIER) return 0; + noomP_skip(parser, &token); + + noomP_Node* name = noomP_allocNode(parser); + if (name == 0) return 0; + + name->type = NOOMP_NODE_VARNAME; // eh good enough + name->source_offset = token.offset; + + noomP_addSubnode(thing, name); + + noomP_peek(parser, &token); + if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "::", 2)) { + return 0; + } + noomP_skip(parser, &token); + + return thing; } } diff --git a/src/parser.h b/src/parser.h index e0a95e7..70670ce 100644 --- a/src/parser.h +++ b/src/parser.h @@ -39,6 +39,9 @@ typedef enum noomP_NodeType { NOOMP_NODE_RETURN, + NOOMP_NODE_GOTO, + NOOMP_NODE_LABEL, + NOOMP_NODE_FIELDNAME, NOOMP_NODE_METHODNAME, // only used in function decl as of right now.