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:
5
projects/comment.j
Normal file
5
projects/comment.j
Normal file
@@ -0,0 +1,5 @@
|
||||
// Esto es un comentario
|
||||
println("Hello World!")
|
||||
|
||||
// Otro comentario mas
|
||||
println(40)
|
||||
@@ -1,2 +1,3 @@
|
||||
x = "Hello"
|
||||
println(len(x) + 3)
|
||||
y = 2 * (4 - 2)
|
||||
println(y)
|
||||
@@ -12,8 +12,7 @@ typedef enum {
|
||||
NODE_VAR, // referencia a variable
|
||||
NODE_ASSIGN, // asignacion: x = expr
|
||||
NODE_BINOP, // operacion binaria: a + b
|
||||
NODE_PRINT, // print(expr)
|
||||
NODE_PRINTLN,
|
||||
NODE_NOP, // noop
|
||||
NODE_IF, // if cond: bloque
|
||||
NODE_WHILE, // while cond: bloque
|
||||
NODE_BLOCK, // secuencia de statements
|
||||
@@ -79,7 +78,7 @@ ASTNode *parse_term(Token *tokens) {
|
||||
pos++;
|
||||
return node;
|
||||
} else if (tokens[pos].type == TOK_ID) {
|
||||
if (tokens[pos +1].type == TOK_LPAREN) {
|
||||
if (tokens[pos + 1].type == TOK_LPAREN) {
|
||||
// Function call
|
||||
char *name = tokens[pos].value;
|
||||
pos++; // consumir ID
|
||||
@@ -103,13 +102,17 @@ ASTNode *parse_term(Token *tokens) {
|
||||
node->data.call.args = args;
|
||||
node->data.call.arg_count = arg_count;
|
||||
return node;
|
||||
|
||||
}
|
||||
ASTNode *node = make_node(NODE_VAR);
|
||||
node->data.string_val = tokens[pos].value;
|
||||
pos++;
|
||||
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) {
|
||||
pos++; // consumir '-'
|
||||
ASTNode *term = parse_term(tokens);
|
||||
@@ -184,6 +187,22 @@ ASTNode *parse_statement(Token *tokens) {
|
||||
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) {
|
||||
pos++; // consumir while
|
||||
ASTNode *cond = parse_expr(tokens);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
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:
|
||||
// Run GC
|
||||
if (gc) {
|
||||
|
||||
Reference in New Issue
Block a user