Added exec, grammar files.

This commit is contained in:
Jacob Signorovitch 2024-11-23 09:30:35 -05:00
parent ad8ac61b98
commit a36ae22d52
4 changed files with 49 additions and 4 deletions

31
src/exec.c Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
#include <string.h>
#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); }

View File

@ -5,6 +5,8 @@
int yylex(void); int yylex(void);
void yyerror(char const*); void yyerror(char const*);
AST* root = NULL;
%} %}
%code requires { %code requires {
@ -34,7 +36,7 @@ input:
line: line:
'\n' '\n'
| exp '\n' { printf("it worked. 👍\n"); } | exp '\n' { root = $1; }
; ;
exp: exp:

10
src/include/exec.h Normal file
View File

@ -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

View File

@ -2,13 +2,14 @@
#include "include/ast.h" #include "include/ast.h"
#include "include/dstr.h" #include "include/dstr.h"
#include "include/exec.h"
#include "include/lexer.h" #include "include/lexer.h"
#include "include/util.h" #include "include/util.h"
#include "../build/grammars/grammar.tab.h" #include "../build/grammars/grammar.tab.h"
// Global Abstract Syntax Tree. // Global Abstract Syntax Tree.
AST* root = NULL; extern AST* root;
extern int yyparse(); extern int yyparse();
@ -30,6 +31,7 @@ int main(int argc, char** argv) {
lexer_print(); lexer_print();
if (yyparse() == 0) { if (yyparse() == 0) {
printf("Parsed successfully!\n"); printf("Parsed successfully!\n");
exec(root);
} else { } else {
printf("Parse error.\n"); printf("Parse error.\n");
} }