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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user