diff --git a/projects/comment.j b/projects/comment.j new file mode 100644 index 0000000..a36150d --- /dev/null +++ b/projects/comment.j @@ -0,0 +1,5 @@ +// Esto es un comentario +println("Hello World!") + +// Otro comentario mas +println(40) \ No newline at end of file diff --git a/projects/functions.j b/projects/functions.j index 4013919..f6461f4 100644 --- a/projects/functions.j +++ b/projects/functions.j @@ -1,2 +1,3 @@ x = "Hello" -println(len(x) + 3) \ No newline at end of file +y = 2 * (4 - 2) +println(y) \ No newline at end of file diff --git a/src/frontend/parser.h b/src/frontend/parser.h index 0df9701..7e5a938 100644 --- a/src/frontend/parser.h +++ b/src/frontend/parser.h @@ -12,11 +12,10 @@ 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_IF, // if cond: bloque - NODE_WHILE, // while cond: bloque - NODE_BLOCK, // secuencia de statements + NODE_NOP, // noop + NODE_IF, // if cond: bloque + NODE_WHILE, // while cond: bloque + NODE_BLOCK, // secuencia de statements NODE_CALL, } NodeType; @@ -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); diff --git a/src/vm/eval.h b/src/vm/eval.h index 5d8f113..0c0fb50 100644 --- a/src/vm/eval.h +++ b/src/vm/eval.h @@ -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) {