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

@ -2,9 +2,11 @@
#include <stdio.h>
#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] = {

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/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");
}