Added exec, grammar files.
This commit is contained in:
parent
ad8ac61b98
commit
a36ae22d52
31
src/exec.c
Normal file
31
src/exec.c
Normal 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); }
|
@ -5,6 +5,8 @@
|
||||
|
||||
int yylex(void);
|
||||
void yyerror(char const*);
|
||||
|
||||
AST* root = NULL;
|
||||
%}
|
||||
|
||||
%code requires {
|
||||
@ -34,7 +36,7 @@ input:
|
||||
|
||||
line:
|
||||
'\n'
|
||||
| exp '\n' { printf("it worked. 👍\n"); }
|
||||
| exp '\n' { root = $1; }
|
||||
;
|
||||
|
||||
exp:
|
||||
|
10
src/include/exec.h
Normal file
10
src/include/exec.h
Normal 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
|
@ -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");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user