parser: goto + labels

This commit is contained in:
2026-04-29 13:23:59 +02:00
parent bba1be30d7
commit f3651e85c5
3 changed files with 60 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 = "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");

View File

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

View File

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