Add // line comments, grouped parentheses, and NODE_NOP

- Parse // comments in parse_statement by consuming tokens until newline
- Add NODE_NOP for no-op statements (comments)
- Support grouped expressions with parentheses in parse_term
This commit is contained in:
Jose Luis Montañes Ojados
2026-02-16 22:41:52 +01:00
parent 21efb0563b
commit 2c91cbb561
4 changed files with 33 additions and 19 deletions

5
projects/comment.j Normal file
View File

@@ -0,0 +1,5 @@
// Esto es un comentario
println("Hello World!")
// Otro comentario mas
println(40)

View File

@@ -1,2 +1,3 @@
x = "Hello" x = "Hello"
println(len(x) + 3) y = 2 * (4 - 2)
println(y)

View File

@@ -12,8 +12,7 @@ typedef enum {
NODE_VAR, // referencia a variable NODE_VAR, // referencia a variable
NODE_ASSIGN, // asignacion: x = expr NODE_ASSIGN, // asignacion: x = expr
NODE_BINOP, // operacion binaria: a + b NODE_BINOP, // operacion binaria: a + b
NODE_PRINT, // print(expr) NODE_NOP, // noop
NODE_PRINTLN,
NODE_IF, // if cond: bloque NODE_IF, // if cond: bloque
NODE_WHILE, // while cond: bloque NODE_WHILE, // while cond: bloque
NODE_BLOCK, // secuencia de statements NODE_BLOCK, // secuencia de statements
@@ -103,13 +102,17 @@ ASTNode *parse_term(Token *tokens) {
node->data.call.args = args; node->data.call.args = args;
node->data.call.arg_count = arg_count; node->data.call.arg_count = arg_count;
return node; return node;
} }
ASTNode *node = make_node(NODE_VAR); ASTNode *node = make_node(NODE_VAR);
node->data.string_val = tokens[pos].value; node->data.string_val = tokens[pos].value;
pos++; pos++;
return node; return node;
} else if (tokens[pos].type == TOK_LPAREN) {
pos++; // consumir (
ASTNode *expr = parse_expr(tokens);
pos++; // consumir )
return expr;
} else if (tokens[pos].type == TOK_MINUS) { } else if (tokens[pos].type == TOK_MINUS) {
pos++; // consumir '-' pos++; // consumir '-'
ASTNode *term = parse_term(tokens); ASTNode *term = parse_term(tokens);
@@ -184,6 +187,22 @@ ASTNode *parse_statement(Token *tokens) {
return node; return node;
} }
// Parse comments
if (tokens[pos].type == TOK_SLASH) {
if (tokens[pos + 1].type == TOK_SLASH) {
pos++; // consumir /
pos++; // consumir /
// Consumir hasta NewLine
while (tokens[pos].type != TOK_NEWLINE)
pos++;
pos++; // consumir newline
return make_node(NODE_NOP);
}
}
if (tokens[pos].type == TOK_WHILE) { if (tokens[pos].type == TOK_WHILE) {
pos++; // consumir while pos++; // consumir while
ASTNode *cond = parse_expr(tokens); ASTNode *cond = parse_expr(tokens);

View File

@@ -105,17 +105,6 @@ size_t eval(ASTNode *node, Environment *env, void *allocator, int debug,
return obj_new_int(allocator, l->data.int_val > r->data.int_val); return obj_new_int(allocator, l->data.int_val > r->data.int_val);
} }
} }
case NODE_PRINT: {
size_t val = eval(node->data.print.expr, env, allocator, debug, gc);
obj_print(allocator, val, "");
return val;
}
case NODE_PRINTLN: {
size_t val = eval(node->data.print.expr, env, allocator, debug, gc);
obj_print(allocator, val, "");
printf("\n");
return val;
}
case NODE_BLOCK: case NODE_BLOCK:
// Run GC // Run GC
if (gc) { if (gc) {