From a36ae22d522e0bb416015f2cec4aa32caf807eb1 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Sat, 23 Nov 2024 09:30:35 -0500 Subject: [PATCH] Added exec, grammar files. --- src/exec.c | 31 +++++++++++++++++++++++++++++++ src/grammar.y | 8 +++++--- src/include/exec.h | 10 ++++++++++ src/main.c | 4 +++- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/exec.c create mode 100644 src/include/exec.h diff --git a/src/exec.c b/src/exec.c new file mode 100644 index 0000000..1d1d21f --- /dev/null +++ b/src/exec.c @@ -0,0 +1,31 @@ +#include +#include + +#include "include/ast.h" +#include "include/exec.h" +#include "include/util.h" + +void exec(AST* ast) { + log_dbg("Started execution."); + switch (ast->type) { + case AST_TYPE_CALL: exec_call(ast); break; + default: printf("what\n"); + } +} + +void exec_call(AST* ast) { + log_dbg("Started call execution."); + ASTTypeCall* calldata = (ASTTypeCall*)ast->data; + if (!strcmp(calldata->to, "+") && calldata->argc == 2) { + + exec_return(1); + /* + ASTTypeNum* n1 = (ASTTypeNum*)calldata->argv[0]->data; + ASTTypeNum* n2 = (ASTTypeNum*)calldata->argv[1]->data; + + exec_return(n1->val + n2->val); + */ + } +} + +void exec_return(int n) { printf("= %d\n", n); } diff --git a/src/grammar.y b/src/grammar.y index 5611fa6..af95a11 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -2,9 +2,11 @@ #include #include "../../src/include/ast.h" #include "../../src/include/lexer.h" - + int yylex(void); void yyerror(char const*); + + AST* root = NULL; %} %code requires { @@ -34,10 +36,10 @@ input: line: '\n' - | exp '\n' { printf("it worked. 👍\n"); } + | exp '\n' { root = $1; } ; -exp: +exp: NUM { $$ = ast_init(AST_TYPE_NUM, ast_type_num_init($1)); } | NUM PLUS NUM { AST* argv[2] = { diff --git a/src/include/exec.h b/src/include/exec.h new file mode 100644 index 0000000..5a84546 --- /dev/null +++ b/src/include/exec.h @@ -0,0 +1,10 @@ +#ifndef EXEC_H +#define EXEC_H + +#include "ast.h" + +void exec(AST* ast); +void exec_call(AST* ast); +void exec_return(int n); + +#endif diff --git a/src/main.c b/src/main.c index 449436d..5de1aef 100644 --- a/src/main.c +++ b/src/main.c @@ -2,13 +2,14 @@ #include "include/ast.h" #include "include/dstr.h" +#include "include/exec.h" #include "include/lexer.h" #include "include/util.h" #include "../build/grammars/grammar.tab.h" // Global Abstract Syntax Tree. -AST* root = NULL; +extern AST* root; extern int yyparse(); @@ -30,6 +31,7 @@ int main(int argc, char** argv) { lexer_print(); if (yyparse() == 0) { printf("Parsed successfully!\n"); + exec(root); } else { printf("Parse error.\n"); }