Compare commits
3 Commits
5b0950cabb
...
743d16f696
Author | SHA1 | Date | |
---|---|---|---|
743d16f696 | |||
7b99292547 | |||
4a516d8edb |
@ -123,7 +123,7 @@ void ast_exc_print(ASTExcData* data, int i) {
|
|||||||
INDENT_END;
|
INDENT_END;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**)) {
|
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)) {
|
||||||
return (ASTBIFData*)fn;
|
return (ASTBIFData*)fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
AST* builtin_sum(size_t argc, AST** argv) {
|
AST* builtin_sum(size_t argc, AST** argv, Scope* parent) {
|
||||||
ASTNumData total = 0;
|
ASTNumData total = 0;
|
||||||
|
|
||||||
for (int i = 0; i < argc; i++) {
|
for (int i = 0; i < argc; i++) {
|
||||||
AST* arg = exec_exp(argv[i]);
|
AST* arg = exec_exp(argv[i], parent);
|
||||||
if (arg->type == AST_TYPE_EXC)
|
if (arg->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -27,9 +27,9 @@ AST* builtin_sum(size_t argc, AST** argv) {
|
|||||||
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_sub(size_t argc, AST** argv) {
|
AST* builtin_sub(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
log_dbg("Got here");
|
||||||
AST* first = exec_exp(*argv);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -44,7 +44,7 @@ AST* builtin_sub(size_t argc, AST** argv) {
|
|||||||
ASTNumData total = *(ASTNumData*)first->data;
|
ASTNumData total = *(ASTNumData*)first->data;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
AST* arg = exec_exp(argv[i]);
|
AST* arg = exec_exp(argv[i], parent);
|
||||||
if (arg->type == AST_TYPE_EXC)
|
if (arg->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -62,9 +62,9 @@ AST* builtin_sub(size_t argc, AST** argv) {
|
|||||||
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_mul(size_t argc, AST** argv) {
|
AST* builtin_mul(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
log_dbg("Got here");
|
||||||
AST* first = exec_exp(*argv);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -79,7 +79,7 @@ AST* builtin_mul(size_t argc, AST** argv) {
|
|||||||
ASTNumData total = *(ASTNumData*)first->data;
|
ASTNumData total = *(ASTNumData*)first->data;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
AST* arg = exec_exp(argv[i]);
|
AST* arg = exec_exp(argv[i], parent);
|
||||||
if (arg->type == AST_TYPE_EXC)
|
if (arg->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -97,9 +97,9 @@ AST* builtin_mul(size_t argc, AST** argv) {
|
|||||||
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
return ast_init(AST_TYPE_NUM, ast_num_data_init(total));
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_div(size_t argc, AST** argv) {
|
AST* builtin_div(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
log_dbg("Got here");
|
||||||
AST* first = exec_exp(*argv);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
@ -114,7 +114,7 @@ AST* builtin_div(size_t argc, AST** argv) {
|
|||||||
ASTNumData total = *(ASTNumData*)first->data;
|
ASTNumData total = *(ASTNumData*)first->data;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
AST* arg = exec_exp(argv[i]);
|
AST* arg = exec_exp(argv[i], parent);
|
||||||
if (arg->type == AST_TYPE_EXC)
|
if (arg->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
AST_TYPE_EXC,
|
AST_TYPE_EXC,
|
||||||
|
19
src/exec.c
19
src/exec.c
@ -12,15 +12,17 @@
|
|||||||
AST* exec_start(AST* ast) {
|
AST* exec_start(AST* ast) {
|
||||||
log_dbg("Started execution.");
|
log_dbg("Started execution.");
|
||||||
|
|
||||||
Scope* builtin = scope_init(NULL);
|
Scope* global = scope_init(NULL);
|
||||||
|
|
||||||
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
||||||
htab_ins(
|
htab_ins(
|
||||||
builtin->here, BUILTIN_FNS[i].name,
|
global->here, BUILTIN_FNS[i].name,
|
||||||
ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn))
|
ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn))
|
||||||
);
|
);
|
||||||
|
|
||||||
return exec_exp(ast, builtin);
|
log_dbg("Completed startup sequence.");
|
||||||
|
|
||||||
|
return exec_exp(ast, global);
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_exp(AST* ast, Scope* parent) {
|
AST* exec_exp(AST* ast, Scope* parent) {
|
||||||
@ -38,6 +40,7 @@ AST* exec_exp(AST* ast, Scope* parent) {
|
|||||||
AST* exec_block(AST* ast, Scope* parent) {
|
AST* exec_block(AST* ast, Scope* parent) {
|
||||||
ASTBlockData* block = (ASTBlockData*)ast->data;
|
ASTBlockData* block = (ASTBlockData*)ast->data;
|
||||||
|
|
||||||
|
// Blocks create their own scope, shared among their expressions.
|
||||||
ast->scope = scope_init(parent);
|
ast->scope = scope_init(parent);
|
||||||
|
|
||||||
// Loop through all but last ast.
|
// Loop through all but last ast.
|
||||||
@ -67,7 +70,7 @@ AST* exec_call(AST* ast, Scope* parent) {
|
|||||||
switch (fdef->type) {
|
switch (fdef->type) {
|
||||||
case AST_TYPE_BIF:
|
case AST_TYPE_BIF:
|
||||||
ASTBIFData bifdata = fdef->data;
|
ASTBIFData bifdata = fdef->data;
|
||||||
return bifdata(argc, argv);
|
return bifdata(argc, argv, parent);
|
||||||
case AST_TYPE_FDEF: return exec_cf(fdef, argc, argv);
|
case AST_TYPE_FDEF: return exec_cf(fdef, argc, argv);
|
||||||
default:
|
default:
|
||||||
return ast_init(AST_TYPE_EXC, ast_exc_data_init("Good job!", NULL));
|
return ast_init(AST_TYPE_EXC, ast_exc_data_init("Good job!", NULL));
|
||||||
@ -87,15 +90,17 @@ AST* exec_cf(AST* ast, size_t argc, AST** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_vdef(AST* ast, Scope* parent) {
|
AST* exec_vdef(AST* ast, Scope* parent) {
|
||||||
ast->scope = scope_init(parent);
|
// Use parent's scope.
|
||||||
|
ast->scope = parent;
|
||||||
ASTVDefData* data = (ASTVDefData*)ast->data;
|
ASTVDefData* data = (ASTVDefData*)ast->data;
|
||||||
AST* val = data->val;
|
AST* val = data->val;
|
||||||
char* key = data->name;
|
char* key = data->name;
|
||||||
scope_add(parent, key, val);
|
scope_add(parent, key, val); // Add variable definition to parent scope.
|
||||||
return exec_exp(val, ast->scope);
|
return exec_exp(val, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_vref(AST* ast, Scope* parent) {
|
AST* exec_vref(AST* ast, Scope* parent) {
|
||||||
|
// Use parent's scope.
|
||||||
ast->scope = parent;
|
ast->scope = parent;
|
||||||
log_dbg("attempting to reference var");
|
log_dbg("attempting to reference var");
|
||||||
ASTVrefData* vref = (ASTVrefData*)ast->data;
|
ASTVrefData* vref = (ASTVrefData*)ast->data;
|
||||||
|
@ -7,12 +7,13 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
HTab* htab_init() {
|
HTab* htab_init() {
|
||||||
HTab* htab = calloc(1, sizeof(HTab));
|
HTab* htab = malloc(sizeof(HTab));
|
||||||
|
|
||||||
return htab;
|
return htab;
|
||||||
}
|
}
|
||||||
|
|
||||||
void htab_destroy(HTab* htab) { free(htab); }
|
void htab_destroy(HTab* htab) { // free(htab);
|
||||||
|
}
|
||||||
|
|
||||||
// Get the index of a key.
|
// Get the index of a key.
|
||||||
size_t geti(char* key) {
|
size_t geti(char* key) {
|
||||||
|
@ -69,10 +69,10 @@ void ast_exc_data_destroy(ASTExcData* exc);
|
|||||||
void ast_exc_print(ASTExcData*, int i);
|
void ast_exc_print(ASTExcData*, int i);
|
||||||
|
|
||||||
// A built-in function.
|
// A built-in function.
|
||||||
typedef AST* (*ASTBIFData)(size_t argc, AST** argv);
|
typedef AST* (*ASTBIFData)(size_t argc, AST** argv, Scope* scope);
|
||||||
|
|
||||||
// Create a built-in function.
|
// Create a built-in function.
|
||||||
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**));
|
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*));
|
||||||
|
|
||||||
// There is no `ASTBIFData` destroy function, as function pointers are immortal.
|
// There is no `ASTBIFData` destroy function, as function pointers are immortal.
|
||||||
|
|
||||||
|
@ -4,20 +4,20 @@
|
|||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
|
|
||||||
// Sum some nums.
|
// Sum some nums.
|
||||||
AST* builtin_sum(size_t argc, AST** argv);
|
AST* builtin_sum(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
// Subtract nums.
|
// Subtract nums.
|
||||||
AST* builtin_sub(size_t argc, AST** argv);
|
AST* builtin_sub(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
// Multiply nums.
|
// Multiply nums.
|
||||||
AST* builtin_mul(size_t argc, AST** argv);
|
AST* builtin_mul(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
// Divide nums.
|
// Divide nums.
|
||||||
AST* builtin_div(size_t argc, AST** argv);
|
AST* builtin_div(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
struct builtin_data {
|
struct builtin_data {
|
||||||
char* name;
|
char* name;
|
||||||
AST* (*fn)(size_t argc, AST** argv);
|
AST* (*fn)(size_t argc, AST** argv, Scope* parent);
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct builtin_data BUILTIN_FNS[] = {
|
static struct builtin_data BUILTIN_FNS[] = {
|
||||||
|
@ -16,6 +16,6 @@ void scope_destroy(Scope* scope);
|
|||||||
// Destroy the current `Scope` only.
|
// Destroy the current `Scope` only.
|
||||||
void scope_destroy_psv(Scope *scope);
|
void scope_destroy_psv(Scope *scope);
|
||||||
// Insert a key/val pair into the `HTab` of a `Scope`.
|
// Insert a key/val pair into the `HTab` of a `Scope`.
|
||||||
inline void scope_add(Scope* scope, char* key, void* val);
|
void scope_add(Scope* scope, char* key, void* val);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
12
src/main.c
12
src/main.c
@ -4,10 +4,8 @@
|
|||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
#include "include/exec.h"
|
#include "include/exec.h"
|
||||||
#include "include/global.h"
|
|
||||||
#include "include/htab.h"
|
|
||||||
#include "include/lexer.h"
|
#include "include/lexer.h"
|
||||||
#include "include/stack.h"
|
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
#include "../build/grammars/grammar.tab.h"
|
#include "../build/grammars/grammar.tab.h"
|
||||||
@ -17,16 +15,11 @@ extern AST* root;
|
|||||||
extern char* inp;
|
extern char* inp;
|
||||||
extern int yyparse();
|
extern int yyparse();
|
||||||
|
|
||||||
Stack* scope;
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) {
|
if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) {
|
||||||
log_dbg("Parsed successfully!\n");
|
log_dbg("Parsed successfully!\n");
|
||||||
ast_print(exec_start(root));
|
ast_print(exec_start(root));
|
||||||
HTab* global = stack_pop(scope);
|
|
||||||
htab_destroy(global);
|
|
||||||
stack_destroy(scope);
|
|
||||||
ast_destroy(root);
|
ast_destroy(root);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
@ -63,9 +56,6 @@ int main(int argc, char** argv) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
ast_print(exec_start(root));
|
ast_print(exec_start(root));
|
||||||
HTab* global = stack_pop(scope);
|
|
||||||
htab_destroy(global);
|
|
||||||
stack_destroy(scope);
|
|
||||||
|
|
||||||
ast_destroy(root);
|
ast_destroy(root);
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,14 @@ Scope* scope_init(Scope* inherit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void scope_destroy(Scope* scope) {
|
void scope_destroy(Scope* scope) {
|
||||||
|
if (!scope) return;
|
||||||
htab_destroy(scope->here);
|
htab_destroy(scope->here);
|
||||||
if (scope->inherit != NULL) scope_destroy(scope->inherit);
|
if (scope->inherit != NULL) scope_destroy(scope->inherit);
|
||||||
free(scope);
|
free(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
void scope_destroy_psv(Scope* scope) {
|
void scope_destroy_psv(Scope* scope) {
|
||||||
|
if (!scope) return;
|
||||||
htab_destroy(scope->here);
|
htab_destroy(scope->here);
|
||||||
scope->inherit = NULL;
|
scope->inherit = NULL;
|
||||||
free(scope);
|
free(scope);
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 73237c5d224169c7b4d2ec8321f9ac92e8071708
|
Subproject commit cdf1d0297effc2736ee847e557b4275b4f02310b
|
Loading…
x
Reference in New Issue
Block a user