With token.c.
This commit is contained in:
parent
64ef797727
commit
7b19e553f2
41
src/ast.c
41
src/ast.c
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
#include "include/dstr.h"
|
||||||
|
|
||||||
extern AST* root;
|
extern AST* root;
|
||||||
|
|
||||||
@ -30,14 +31,29 @@ void ast_destroy(AST* ast) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ast_print(AST* ast) {
|
void ast_print(AST* ast) {
|
||||||
log_dbgf("Tree type: %s", asttype_names[ast->type]);
|
ast_print_i(ast, 0);
|
||||||
fflush(stdout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ast_print_i(AST* ast, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("AST", ast);
|
||||||
|
INDENT_FIELD("type", "%s", asttype_names[ast->type]);
|
||||||
|
INDENT_FIELD_EXT_NONL_START("data");
|
||||||
|
switch (ast->type) {
|
||||||
|
case AST_TYPE_NUM: ast_num_print(ast->data, i + 1); break;
|
||||||
|
case AST_TYPE_CALL: ast_call_print(ast->data, i + 1); break;
|
||||||
|
default: exit(1);
|
||||||
|
}
|
||||||
|
INDENT_FIELD_NONL_END;
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
ASTNumData* ast_num_data_init(double val) {
|
ASTNumData* ast_num_data_init(double val) {
|
||||||
talloc(ASTNumData, num);
|
talloc(ASTNumData, num);
|
||||||
|
|
||||||
|
log_dbgf("val: %lf", val);
|
||||||
|
|
||||||
*num = val;
|
*num = val;
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
@ -47,9 +63,19 @@ void ast_num_data_destroy(ASTNumData* num) {
|
|||||||
if (!num) return free(num);
|
if (!num) return free(num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ast_num_print(ASTNumData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_FIELD("data", "%lf", *data);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
||||||
talloc(ASTCallData, call);
|
talloc(ASTCallData, call);
|
||||||
|
|
||||||
|
log_dbgf("to: %s", to);
|
||||||
|
|
||||||
call->to = to;
|
call->to = to;
|
||||||
call->argc = argc;
|
call->argc = argc;
|
||||||
call->argv = argv;
|
call->argv = argv;
|
||||||
@ -62,3 +88,14 @@ void ast_call_data_destroy(ASTCallData* call) {
|
|||||||
for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);
|
for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);
|
||||||
free(call);
|
free(call);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ast_call_print(ASTCallData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTCallData", data);
|
||||||
|
INDENT_FIELD("to", "%s", data->to);
|
||||||
|
INDENT_FIELD("argc", "%ld", data->argc);
|
||||||
|
INDENT_FIELD_LIST("argv", data->argv, data->argc, ast_print_i);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
25
src/exec.c
25
src/exec.c
@ -7,26 +7,35 @@
|
|||||||
|
|
||||||
extern AST* root;
|
extern AST* root;
|
||||||
|
|
||||||
void exec_expr() {
|
ASTNumData exec_expr(AST* ast) {
|
||||||
ast_print(root);
|
ast_print(ast);
|
||||||
log_dbg("Started execution.");
|
log_dbg("Started execution.");
|
||||||
switch (root->type) {
|
switch (ast->type) {
|
||||||
case AST_TYPE_CALL: exec_call(); break;
|
case AST_TYPE_CALL: return exec_call(ast);
|
||||||
|
case AST_TYPE_NUM:
|
||||||
|
exec_print(*(ASTNumData*)ast->data);
|
||||||
|
return *(ASTNumData*)ast->data;
|
||||||
default: printf("what\n");
|
default: printf("what\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void exec_call() {
|
ASTNumData exec_call(AST* ast) {
|
||||||
log_dbg("Started call execution.");
|
log_dbg("Started call execution.");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
ASTCallData* calldata = (ASTCallData*)root->data;
|
ASTCallData* calldata = (ASTCallData*)ast->data;
|
||||||
if (!strcmp(calldata->to, "+") && calldata->argc == 2) {
|
if (!strcmp(calldata->to, "+") && calldata->argc == 2) {
|
||||||
|
|
||||||
|
/*
|
||||||
ASTNumData* n1 = (ASTNumData*)calldata->argv[0]->data;
|
ASTNumData* n1 = (ASTNumData*)calldata->argv[0]->data;
|
||||||
ASTNumData* n2 = (ASTNumData*)calldata->argv[1]->data;
|
ASTNumData* n2 = (ASTNumData*)calldata->argv[1]->data;
|
||||||
|
*/
|
||||||
|
|
||||||
exec_return(*n1 + *n2);
|
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
||||||
|
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
||||||
|
|
||||||
|
return n1 + n2;
|
||||||
}
|
}
|
||||||
|
return -1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
void exec_return(double n) { printf("= %lf\n", n); }
|
void exec_print(double n) { printf("= %lf\n", n); }
|
||||||
|
@ -36,12 +36,16 @@ input:
|
|||||||
|
|
||||||
exp:
|
exp:
|
||||||
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
|
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
|
||||||
| NUM PLUS NUM {
|
| NUM PLUS exp {
|
||||||
AST* argv[2] = {
|
AST* argv[2] = {
|
||||||
ast_init(AST_TYPE_NUM, ast_num_data_init($1)),
|
ast_init(AST_TYPE_NUM, ast_num_data_init($1)),
|
||||||
ast_init(AST_TYPE_NUM, ast_num_data_init($3))
|
$3
|
||||||
};
|
};
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("+", 2, argv));
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("+", 2, argv));
|
||||||
};
|
}/*
|
||||||
|
| exp PLUS exp {
|
||||||
|
AST* argv[2] = { $1, $3 };
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("+", 2, argv));
|
||||||
|
};*/
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
@ -17,11 +17,13 @@ typedef struct {
|
|||||||
AST* ast_init(ASTType type, void* data);
|
AST* ast_init(ASTType type, void* data);
|
||||||
void ast_destroy(AST* ast);
|
void ast_destroy(AST* ast);
|
||||||
void ast_print(AST* ast);
|
void ast_print(AST* ast);
|
||||||
|
void ast_print_i(AST* ast, int i);
|
||||||
|
|
||||||
typedef double ASTNumData;
|
typedef double ASTNumData;
|
||||||
|
|
||||||
ASTNumData* ast_num_data_init(double val);
|
ASTNumData* ast_num_data_init(double val);
|
||||||
void ast_num_data_destroy(ASTNumData* num);
|
void ast_num_data_destroy(ASTNumData* num);
|
||||||
|
void ast_num_print(ASTNumData*, int i);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* to; // What the call's to.
|
char* to; // What the call's to.
|
||||||
@ -31,5 +33,6 @@ typedef struct {
|
|||||||
|
|
||||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
|
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
|
||||||
void ast_call_data_destroy(ASTCallData* call);
|
void ast_call_data_destroy(ASTCallData* call);
|
||||||
|
void ast_call_print(ASTCallData*, int i);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
#ifndef EXEC_H
|
#ifndef EXEC_H
|
||||||
#define EXEC_H
|
#define EXEC_H
|
||||||
|
|
||||||
void exec_expr();
|
#include "ast.h"
|
||||||
void exec_call();
|
|
||||||
void exec_return(double n);
|
ASTNumData exec_expr(AST* ast);
|
||||||
|
ASTNumData exec_call(AST* ast);
|
||||||
|
void exec_print(double n);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -64,8 +64,8 @@
|
|||||||
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__);
|
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__);
|
||||||
|
|
||||||
// Print & indent a thing without any newline.
|
// Print & indent a thing without any newline.
|
||||||
#define INDENT_FIELD_NONL_START(FIELD) \
|
#define INDENT_FIELD_EXT_NONL_START(FIELD) \
|
||||||
printf("%s " COL_BWHI FIELD ": " COL_RESET COL_WHI, INDENT_spacing->buf);
|
printf("%s " COL_BWHI FIELD ":\n" COL_RESET COL_WHI, INDENT_spacing->buf);
|
||||||
#define INDENT_FIELD_NONL_END printf( "\n" COL_RESET);
|
#define INDENT_FIELD_NONL_END printf( "\n" COL_RESET);
|
||||||
|
|
||||||
// Print an array A of N things, by calling the function F.
|
// Print an array A of N things, by calling the function F.
|
||||||
|
11
src/lexer.c
11
src/lexer.c
@ -15,6 +15,7 @@ int acc_int(int c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
double acc_float(int c) {
|
double acc_float(int c) {
|
||||||
|
int dplaces = 0;
|
||||||
double value = (double)(c - '0');
|
double value = (double)(c - '0');
|
||||||
|
|
||||||
// Grab everything prior to '.'.
|
// Grab everything prior to '.'.
|
||||||
@ -24,13 +25,17 @@ double acc_float(int c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (*inp == '.') {
|
if (*inp == '.') {
|
||||||
char* oinp = inp++;
|
inp++;
|
||||||
|
|
||||||
while (isdigit(*inp)) {
|
while (isdigit(*inp)) {
|
||||||
// TODO:
|
// TODO:
|
||||||
// Accumulate as int, divide once at end.
|
// Accumulate as int, divide once at end.
|
||||||
value = value + (((double)(*inp - '0'))/pow(10.0l, (double)(inp-oinp))); // Accumulate value.
|
// value = value + (((double)(*inp - '0'))/pow(10.0l, (double)(inp-oinp))); // Accumulate value.
|
||||||
|
value = value * 10 + (*inp - '0'); // Accumulate value.
|
||||||
|
dplaces++;
|
||||||
inp++;
|
inp++;
|
||||||
}
|
}
|
||||||
|
value = value / pow(10, dplaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
// > 1.20000
|
// > 1.20000
|
||||||
@ -66,4 +71,4 @@ int yylex() {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
void yyerror(char const* s) { fprintf(stderr, "Syntax error:\n%s\n", s); }
|
void yyerror(char const* s) { fprintf(stderr, "Parse error: %s\n", s); }
|
||||||
|
@ -48,7 +48,8 @@ int main(int argc, char** argv) {
|
|||||||
else
|
else
|
||||||
printf("Parse error.\n");
|
printf("Parse error.\n");
|
||||||
|
|
||||||
exec_expr();
|
//exec_expr(root);
|
||||||
|
ast_print(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
dstr_destroy(ln);
|
dstr_destroy(ln);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user