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); }
|
@ -2,9 +2,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "../../src/include/ast.h"
|
#include "../../src/include/ast.h"
|
||||||
#include "../../src/include/lexer.h"
|
#include "../../src/include/lexer.h"
|
||||||
|
|
||||||
int yylex(void);
|
int yylex(void);
|
||||||
void yyerror(char const*);
|
void yyerror(char const*);
|
||||||
|
|
||||||
|
AST* root = NULL;
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%code requires {
|
%code requires {
|
||||||
@ -34,10 +36,10 @@ input:
|
|||||||
|
|
||||||
line:
|
line:
|
||||||
'\n'
|
'\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 { $$ = ast_init(AST_TYPE_NUM, ast_type_num_init($1)); }
|
||||||
| NUM PLUS NUM {
|
| NUM PLUS NUM {
|
||||||
AST* argv[2] = {
|
AST* argv[2] = {
|
||||||
|
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/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");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user