diff --git a/STATUS.md b/STATUS.md index b7bb613..7c12a2b 100644 --- a/STATUS.md +++ b/STATUS.md @@ -11,7 +11,7 @@ - [x] Parse infix operators - [x] Order of operations - [x] Parse function application - - [ ] Parse order of operations with parenthesis + - [x] Parse order of operations with parenthesis - [ ] Parse variable invocation - [ ] Parse variable definition - [ ] Parse types diff --git a/src/ast.c b/src/ast.c index 5cb85c0..9956721 100644 --- a/src/ast.c +++ b/src/ast.c @@ -7,8 +7,9 @@ extern AST* root; static char* asttype_names[] = { - [AST_TYPE_CALL] = "CALL", + [AST_TYPE_CALL] = "FUNC CALL", [AST_TYPE_NUM] = "NUMBER", + [AST_TYPE_VREF] = "VAR REFERENCE" }; AST* ast_init(ASTType type, void* data) { @@ -44,6 +45,7 @@ void ast_print_i(AST* ast, int i) { printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data); break; case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break; + case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break; default: exit(1); } INDENT_FIELD_NONL_END; @@ -100,3 +102,12 @@ void ast_call_print(ASTCallData* data, int i) { INDENT_END; } + +void ast_vref_print(ASTVrefData* data, int i) { + INDENT_BEGIN(i); + + INDENT_TITLE("ASTVrefData", data); + INDENT_FIELD("to", "%s", data->to); + + INDENT_END; +} diff --git a/src/grammar.y b/src/grammar.y index 4e50a75..4b2ae93 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -25,7 +25,7 @@ %token RGROUP %token SEP -%token CALL +%token WORD %token NUM %token SUB @@ -60,8 +60,12 @@ exp: | LGROUP exp RGROUP { $$ = $2; } + // Variable reference. + | WORD + + // Function call. // name(thing, thing) - | CALL LGROUP exp SEP exp RGROUP { + | WORD LGROUP exp SEP exp RGROUP { AST** argv = calloc(2, sizeof(AST*)); argv[0] = $3; argv[1] = $5; diff --git a/src/include/ast.h b/src/include/ast.h index c13d760..3c5dd93 100644 --- a/src/include/ast.h +++ b/src/include/ast.h @@ -4,8 +4,9 @@ #include typedef enum { - AST_TYPE_NUM, - AST_TYPE_CALL, + AST_TYPE_NUM, // A number. + AST_TYPE_CALL, // A function call. + AST_TYPE_VREF, // A variable reference. AST_TYPE_MAX = AST_TYPE_CALL } ASTType; @@ -35,4 +36,12 @@ ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv); void ast_call_data_destroy(ASTCallData* call); void ast_call_print(ASTCallData*, int i); +typedef struct { + char* to; // What the reference's to. +} ASTVrefData; + +ASTCallData* ast_vref_data_init(char* to); +void ast_vref_data_destroy(ASTCallData* call); +void ast_vref_print(ASTVrefData*, int i); + #endif diff --git a/src/lexer.c b/src/lexer.c index 3f3f065..2691e36 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -98,7 +98,7 @@ int yylex() { if (isalpha(c)) { yylval.strval = acc_word(c); - return CALL; + return WORD; } switch (c) {