Compare commits
No commits in common. "ec268f6047845566acaf4f8c8c890d919baed071" and "64ef79772783a44ae25bdfa4364e20152e854e2e" have entirely different histories.
ec268f6047
...
64ef797727
45
src/ast.c
45
src/ast.c
@ -1,7 +1,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "include/ast.h"
|
||||
#include "include/dstr.h"
|
||||
#include "include/util.h"
|
||||
|
||||
extern AST* root;
|
||||
@ -30,30 +29,15 @@ void ast_destroy(AST* ast) {
|
||||
}
|
||||
}
|
||||
|
||||
void ast_print(AST* ast) { ast_print_i(ast, 0); }
|
||||
|
||||
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:
|
||||
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
|
||||
break;
|
||||
case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break;
|
||||
default: exit(1);
|
||||
}
|
||||
INDENT_FIELD_NONL_END;
|
||||
INDENT_END;
|
||||
void ast_print(AST* ast) {
|
||||
log_dbgf("Tree type: %s", asttype_names[ast->type]);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
|
||||
ASTNumData* ast_num_data_init(double val) {
|
||||
talloc(ASTNumData, num);
|
||||
|
||||
log_dbgf("val: %lf", val);
|
||||
|
||||
*num = val;
|
||||
|
||||
return num;
|
||||
@ -63,19 +47,9 @@ void ast_num_data_destroy(ASTNumData* 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) {
|
||||
talloc(ASTCallData, call);
|
||||
|
||||
log_dbgf("to: %s", to);
|
||||
|
||||
call->to = to;
|
||||
call->argc = argc;
|
||||
call->argv = argv;
|
||||
@ -88,14 +62,3 @@ void ast_call_data_destroy(ASTCallData* call) {
|
||||
for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);
|
||||
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,35 +7,26 @@
|
||||
|
||||
extern AST* root;
|
||||
|
||||
ASTNumData exec_expr(AST* ast) {
|
||||
ast_print(ast);
|
||||
void exec_expr() {
|
||||
ast_print(root);
|
||||
log_dbg("Started execution.");
|
||||
switch (ast->type) {
|
||||
case AST_TYPE_CALL: return exec_call(ast);
|
||||
case AST_TYPE_NUM:
|
||||
exec_print(*(ASTNumData*)ast->data);
|
||||
return *(ASTNumData*)ast->data;
|
||||
switch (root->type) {
|
||||
case AST_TYPE_CALL: exec_call(); break;
|
||||
default: printf("what\n");
|
||||
}
|
||||
}
|
||||
|
||||
ASTNumData exec_call(AST* ast) {
|
||||
void exec_call() {
|
||||
log_dbg("Started call execution.");
|
||||
fflush(stdout);
|
||||
ASTCallData* calldata = (ASTCallData*)ast->data;
|
||||
ASTCallData* calldata = (ASTCallData*)root->data;
|
||||
if (!strcmp(calldata->to, "+") && calldata->argc == 2) {
|
||||
|
||||
/*
|
||||
ASTNumData* n1 = (ASTNumData*)calldata->argv[0]->data;
|
||||
ASTNumData* n2 = (ASTNumData*)calldata->argv[1]->data;
|
||||
*/
|
||||
|
||||
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
||||
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
||||
|
||||
return n1 + n2;
|
||||
exec_return(*n1 + *n2);
|
||||
}
|
||||
return -1000;
|
||||
}
|
||||
|
||||
void exec_print(double n) { printf("= %lf\n", n); }
|
||||
void exec_return(double n) { printf("= %lf\n", n); }
|
||||
|
@ -42,17 +42,6 @@ exp:
|
||||
ast_init(AST_TYPE_NUM, ast_num_data_init($3))
|
||||
};
|
||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("+", 2, argv));
|
||||
}
|
||||
/*| NUM PLUS exp {
|
||||
AST* argv[2] = {
|
||||
ast_init(AST_TYPE_NUM, ast_num_data_init($1)),
|
||||
$3
|
||||
};
|
||||
$$ = 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,13 +17,11 @@ typedef struct {
|
||||
AST* ast_init(ASTType type, void* data);
|
||||
void ast_destroy(AST* ast);
|
||||
void ast_print(AST* ast);
|
||||
void ast_print_i(AST* ast, int i);
|
||||
|
||||
typedef double ASTNumData;
|
||||
|
||||
ASTNumData* ast_num_data_init(double val);
|
||||
void ast_num_data_destroy(ASTNumData* num);
|
||||
void ast_num_print(ASTNumData*, int i);
|
||||
|
||||
typedef struct {
|
||||
char* to; // What the call's to.
|
||||
@ -33,6 +31,5 @@ typedef struct {
|
||||
|
||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
|
||||
void ast_call_data_destroy(ASTCallData* call);
|
||||
void ast_call_print(ASTCallData*, int i);
|
||||
|
||||
#endif
|
||||
|
@ -1,10 +1,8 @@
|
||||
#ifndef EXEC_H
|
||||
#define EXEC_H
|
||||
|
||||
#include "ast.h"
|
||||
|
||||
ASTNumData exec_expr(AST* ast);
|
||||
ASTNumData exec_call(AST* ast);
|
||||
void exec_print(double n);
|
||||
void exec_expr();
|
||||
void exec_call();
|
||||
void exec_return(double n);
|
||||
|
||||
#endif
|
||||
|
39
src/include/token.h
Normal file
39
src/include/token.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef TOKEN_H
|
||||
#define TOKEN_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef enum {
|
||||
TOKEN_TYPE_CALL,
|
||||
TOKEN_TYPE_NUMBER,
|
||||
TOKEN_TYPE_MAX = TOKEN_TYPE_NUMBER,
|
||||
} TokenType;
|
||||
|
||||
// Token.
|
||||
typedef struct {
|
||||
TokenType type; // The type of the Token.
|
||||
size_t valn; // The length of val.
|
||||
char* val; // The text of the Token.
|
||||
size_t len; // Length of the text of the Token.
|
||||
} Token;
|
||||
|
||||
Token* token_init(TokenType type, char* val, size_t valn);
|
||||
void token_destroy(Token* token);
|
||||
|
||||
// Prints out a representation of the Token.
|
||||
void token_print(Token* token);
|
||||
|
||||
// Prints out a representation of the Token, with the specified indent level.
|
||||
void token_print_i(Token* token, int ilevel);
|
||||
|
||||
// Prints out a representation of the TokenType.
|
||||
void tokentype_print(TokenType t);
|
||||
|
||||
// Prints out a representation of the TokenType, with the specified indent
|
||||
// level.
|
||||
void tokentype_print_i(TokenType t, int ilevel);
|
||||
|
||||
// Prints a token's type. That's it.
|
||||
void tokentype_print_raw(TokenType t);
|
||||
|
||||
#endif
|
@ -50,7 +50,7 @@
|
||||
|
||||
// Print & indent the title of a section.
|
||||
#define INDENT_TITLE(THING, WHERE) \
|
||||
printf("%s" COL_BCYA THING COL_RESET " @" COL_MAG " %p\n" COL_RESET, INDENT_spacing->buf, WHERE);
|
||||
printf("%s" COL_BCYA THING " @ %p\n" COL_RESET, INDENT_spacing->buf, WHERE);
|
||||
|
||||
// Print & indent a thing.
|
||||
#define INDENT_FIELD(FIELD, VAL, ...) \
|
||||
@ -64,8 +64,8 @@
|
||||
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__);
|
||||
|
||||
// Print & indent a thing without any newline.
|
||||
#define INDENT_FIELD_EXT_NONL_START(FIELD) \
|
||||
printf("%s " COL_BWHI FIELD ":\n" COL_RESET COL_WHI, INDENT_spacing->buf);
|
||||
#define INDENT_FIELD_NONL_START(FIELD) \
|
||||
printf("%s " COL_BWHI FIELD ": " COL_RESET COL_WHI, INDENT_spacing->buf);
|
||||
#define INDENT_FIELD_NONL_END printf( "\n" COL_RESET);
|
||||
|
||||
// Print an array A of N things, by calling the function F.
|
||||
|
11
src/lexer.c
11
src/lexer.c
@ -15,7 +15,6 @@ int acc_int(int c) {
|
||||
}
|
||||
|
||||
double acc_float(int c) {
|
||||
int dplaces = 0;
|
||||
double value = (double)(c - '0');
|
||||
|
||||
// Grab everything prior to '.'.
|
||||
@ -25,17 +24,13 @@ double acc_float(int c) {
|
||||
}
|
||||
|
||||
if (*inp == '.') {
|
||||
inp++;
|
||||
|
||||
char* oinp = inp++;
|
||||
while (isdigit(*inp)) {
|
||||
// TODO:
|
||||
// Accumulate as int, divide once at end.
|
||||
// value = value + (((double)(*inp - '0'))/pow(10.0l, (double)(inp-oinp))); // Accumulate value.
|
||||
value = value * 10 + (*inp - '0'); // Accumulate value.
|
||||
dplaces++;
|
||||
value = value + (((double)(*inp - '0'))/pow(10.0l, (double)(inp-oinp))); // Accumulate value.
|
||||
inp++;
|
||||
}
|
||||
value = value / pow(10, dplaces);
|
||||
}
|
||||
|
||||
// > 1.20000
|
||||
@ -71,4 +66,4 @@ int yylex() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
void yyerror(char const* s) { fprintf(stderr, "Parse error: %s\n", s); }
|
||||
void yyerror(char const* s) { fprintf(stderr, "Syntax error:\n%s\n", s); }
|
||||
|
@ -48,8 +48,7 @@ int main(int argc, char** argv) {
|
||||
else
|
||||
printf("Parse error.\n");
|
||||
|
||||
//exec_expr(root);
|
||||
ast_print(root);
|
||||
exec_expr();
|
||||
}
|
||||
|
||||
dstr_destroy(ln);
|
||||
|
62
src/token.c
Normal file
62
src/token.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "include/token.h"
|
||||
#include "include/dstr.h"
|
||||
#include "include/util.h"
|
||||
|
||||
static char* tokentype_names[] = {
|
||||
[TOKEN_TYPE_CALL] = "CALL",
|
||||
[TOKEN_TYPE_NUMBER] = "NUMBER",
|
||||
};
|
||||
|
||||
Token* token_init(TokenType type, char* val, size_t valn) {
|
||||
Token* t = malloc(sizeof(Token));
|
||||
|
||||
t->type = type;
|
||||
t->valn = valn;
|
||||
t->val = val;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void token_destroy(Token* t) {
|
||||
free(t->val);
|
||||
free(t);
|
||||
}
|
||||
|
||||
void token_print(Token* token) { token_print_i(token, 0); }
|
||||
|
||||
void token_print_i(Token *token, int ilvl) {
|
||||
INDENT_BEGIN(ilvl);
|
||||
|
||||
INDENT_TITLE("Token", token);
|
||||
INDENT_FIELD_NONL_START("type")
|
||||
tokentype_print_raw(token->type);
|
||||
INDENT_FIELD_NONL_END
|
||||
INDENT_FIELD("valn", "%ld", token->valn);
|
||||
INDENT_FIELD_NL("val", "\"%s\"", token->val);
|
||||
}
|
||||
|
||||
void tokentype_print_raw(TokenType t) {
|
||||
if (t > TOKEN_TYPE_MAX) {
|
||||
printf("Unknown (%d)", t);
|
||||
log_dbgf("%d is not a valid TokenType (max: %d)", t, TOKEN_TYPE_MAX);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s", tokentype_names[t]);
|
||||
}
|
||||
|
||||
void tokentype_print(TokenType t) { tokentype_print_i(t, 0); }
|
||||
|
||||
void tokentype_print_i(TokenType t, int i) {
|
||||
INDENT_BEGIN(i);
|
||||
|
||||
if (t > TOKEN_TYPE_MAX) {
|
||||
INDENT_FIELD("val", "Unknown (%d)", t);
|
||||
log_dbgf("%d is not a valid TokenType (max: %d)", t, TOKEN_TYPE_MAX);
|
||||
return;
|
||||
}
|
||||
|
||||
INDENT_FIELD("val", "%s", tokentype_names[t]);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user