parser: support break and fix local decl

This commit is contained in:
2026-04-17 19:22:02 +02:00
parent 9d2293baa6
commit 3534e7a388
3 changed files with 28 additions and 5 deletions

View File

@@ -33,7 +33,7 @@ void print_node(noomP_Node* node, noom_uint_t depth) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
// uhh uhhh uhhhhh // uhh uhhh uhhhhh
const char* code = "while true do end"; const char* code = "local a, b";
noom_uint_t pos = 0; noom_uint_t pos = 0;
printf("LEX OUTPUT:\n"); printf("LEX OUTPUT:\n");

View File

@@ -349,18 +349,27 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (token.type == NOOML_TOKEN_SYMBOL) { if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, "=", 1)) { if (noom_streql(parser->code + token.offset, token.length, "=", 1)) {
noomP_skip(parser, &token); // noomP_skip(parser, &token);
break; break;
} else if (noom_streql(parser->code + token.offset, token.length, ",", 1)) { } else if (noom_streql(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
return 0; // unexpected token break;
} }
} else { } else {
return 0; // unexpected token break;
} }
} }
noomP_peek(parser, &token);
// local with no equals initializes to nil
if (token.type != NOOML_TOKEN_SYMBOL) return localNode;
if (!noom_streql(parser->code + token.offset, token.length, "=", 1)) {
return localNode;
}
noomP_skip(parser, &token);
// equals has already been eaten by loop (thank you loop) // equals has already been eaten by loop (thank you loop)
while (1) { while (1) {
@@ -490,7 +499,19 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
noomP_skip(parser, &token); // skip `end` noomP_skip(parser, &token); // skip `end`
return node; return node;
} } else if (noom_streql(parser->code + token.offset, token.length, "break", 5)) {
noomP_skip(parser, &token); // skip `break`
// uhh yeah that's it i guess? idk. maybe parsers should figure out what loop?
// sounds like a thing for the compiler to bytecode though
noomP_Node* node = noomP_allocNode(parser);
if (node == 0) return 0;
node->type = NOOMP_NODE_BREAK;
node->source_offset = token.offset;
return node;
} else if (noom_streql(parser->code + token.offset, token.length, "break", 5)) {}
} }
while (1) { while (1) {

View File

@@ -10,6 +10,8 @@ typedef enum noomP_NodeType {
NOOMP_NODE_IFSTATEMENT, NOOMP_NODE_IFSTATEMENT,
NOOMP_NODE_WHILELOOP, NOOMP_NODE_WHILELOOP,
NOOMP_NODE_BLOCK, NOOMP_NODE_BLOCK,
NOOMP_NODE_BREAK,
NOOMP_NODE_VARIABLE, NOOMP_NODE_VARIABLE,
NOOMP_NODE_NUMBERLITERAL, NOOMP_NODE_NUMBERLITERAL,