diff --git a/src/ast.c b/src/ast.c index fd6019c..795e946 100644 --- a/src/ast.c +++ b/src/ast.c @@ -10,6 +10,7 @@ static char* asttype_names[] = { [AST_TYPE_CALL] = "FUNC CALL", [AST_TYPE_NUM] = "NUMBER", [AST_TYPE_VREF] = "VAR REFERENCE", + [AST_TYPE_VDEF] = "VAR DEFINITION", [AST_TYPE_BLOCK] = "BLOCK", }; @@ -29,6 +30,7 @@ void ast_destroy(AST* ast) { case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break; case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break; case AST_TYPE_VREF: ast_vref_data_destroy(ast->data); break; + case AST_TYPE_VDEF: ast_vdef_data_destroy(ast->data); break; default: log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX); } @@ -50,6 +52,7 @@ void ast_print_i(AST* ast, int i) { 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; + case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break; default: exit(1); } INDENT_FIELD_NONL_END; @@ -105,6 +108,33 @@ void ast_call_print(ASTCallData* data, int i) { INDENT_END; } +ASTVDefData* ast_vdef_data_init(char* name, AST* val) { + talloc(ASTVDefData, vdef); + + vdef->name = name; + vdef->val = val; + + return vdef; +} + +void ast_vdef_data_destroy(ASTVDefData* vdef) { + ast_destroy(vdef->val); + free(vdef->name); + free(vdef); +} + +void ast_vdef_print(ASTVDefData* vdef, int depth) { + INDENT_BEGIN(depth); + + INDENT_TITLE("ASTVDefData", vdef); + INDENT_FIELD("name", "%s", vdef->name); + INDENT_FIELD_EXT_NONL_START("val"); + ast_print_i(vdef->val, depth + 2); // 2 because already indented. + INDENT_FIELD_NONL_END; + + INDENT_END; +} + ASTVrefData* ast_vref_data_init(char* to) { talloc(ASTVrefData, vref); diff --git a/src/exec.c b/src/exec.c index a34f74b..f4f5b97 100644 --- a/src/exec.c +++ b/src/exec.c @@ -14,6 +14,7 @@ ASTNumData exec_exp(AST* ast) { case AST_TYPE_CALL: return exec_call(ast); case AST_TYPE_NUM: return *(ASTNumData*)ast->data; case AST_TYPE_VREF: return exec_vref(ast); + case AST_TYPE_VDEF: return exec_vdef(ast); default: printf("what\n"); exit(1); } @@ -68,6 +69,12 @@ ASTNumData exec_call(AST* ast) { return -1000; } +ASTNumData exec_vdef(AST* ast) { + ASTVDefData* data = (ASTVDefData*) ast->data; + AST* val = data->val; + return exec_exp(val); +} + ASTNumData exec_vref(AST* ast) { return *ast_num_data_init(42.42); } diff --git a/src/grammar.y b/src/grammar.y index 946ec05..839a707 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -33,6 +33,8 @@ %token GROUPE // Group end ). %token SEP // Seperator ,. +%token EQ // Equals =. + %token EXPSEP // Expression seperator ;. %token WORD // Word, i.e. keyword. @@ -116,11 +118,16 @@ exp: | GROUPS exp GROUPE { $$ = $2; } - // Variable reference. - | WORD { - $$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1)); + // Variable definition. + | WORD EQ exp { + $$ = ast_init(AST_TYPE_VDEF, ast_vdef_data_init($1, $3)); } + // Variable reference. + //| WORD { + // $$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1)); + //} + | WORD GROUPS arg GROUPE { size_t argc = $3->ln; AST** argv = $3->buf; diff --git a/src/include/ast.h b/src/include/ast.h index cf38ebc..a5bc766 100644 --- a/src/include/ast.h +++ b/src/include/ast.h @@ -6,6 +6,7 @@ typedef enum { AST_TYPE_NUM, // A number. AST_TYPE_CALL, // A function call. + AST_TYPE_VDEF, // A variable definition. AST_TYPE_VREF, // A variable reference. AST_TYPE_BLOCK, // A block of code (scope). AST_TYPE_MAX = AST_TYPE_BLOCK, @@ -37,6 +38,18 @@ 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); +// A variable definition's data. +typedef struct { + char* name; + AST* val; +} ASTVDefData; + +ASTVDefData* ast_vdef_data_init(char* name, AST* val); +// Destroys the vdef, its name, and its ->val. +void ast_vdef_data_destroy(ASTVDefData* vdef); +void ast_vdef_print(ASTVDefData*, int depth); + +// A variable reference's data. typedef struct { char* to; // What the reference's to. } ASTVrefData; diff --git a/src/include/exec.h b/src/include/exec.h index cbf1353..4caaeca 100644 --- a/src/include/exec.h +++ b/src/include/exec.h @@ -6,6 +6,7 @@ ASTNumData exec_exp(AST* ast); ASTNumData exec_call(AST* ast); ASTNumData exec_vref(AST* ast); +ASTNumData exec_vdef(AST* ast); void exec_print(double n); #endif diff --git a/src/lexer.c b/src/lexer.c index b3bcee9..53dd80f 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -115,6 +115,7 @@ int yylex() { case ';': return EXPSEP; case '{': return BLOCKS; case '}': return BLOCKE; + case '=': return EQ; default: fprintf(stderr, "Unexpected character: %c\n", c); }