Compare commits
9 Commits
3e80f6430d
...
3f30662cde
Author | SHA1 | Date | |
---|---|---|---|
3f30662cde | |||
743d16f696 | |||
7b99292547 | |||
4a516d8edb | |||
5b0950cabb | |||
40051de9ae | |||
2d01b09ee9 | |||
ff68b756ef | |||
c2f2658e9c |
@ -11,7 +11,7 @@ TEST_DIR = test
|
|||||||
TEST_BUILD_DIR = $(BUILD_DIR)/test
|
TEST_BUILD_DIR = $(BUILD_DIR)/test
|
||||||
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
|
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
|
||||||
|
|
||||||
CC = clang -std=c2x
|
CC = clang -std=c23
|
||||||
LINK = clang
|
LINK = clang
|
||||||
CFLAGS = -Wall -DDBG -ggdb -fsanitize=leak
|
CFLAGS = -Wall -DDBG -ggdb -fsanitize=leak
|
||||||
LDFLAGS = -lm
|
LDFLAGS = -lm
|
||||||
|
31
src/ast.c
31
src/ast.c
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
#include "include/htab.h"
|
#include "include/scope.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
extern AST* root;
|
extern AST* root;
|
||||||
@ -28,7 +28,7 @@ AST* ast_init(ASTType type, void* data) {
|
|||||||
return ast;
|
return ast;
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* ast_init_scope(ASTType type, void* data, HTab* scope) {
|
AST* ast_init_scope(ASTType type, void* data, Scope* scope) {
|
||||||
AST* ast = malloc(sizeof(AST));
|
AST* ast = malloc(sizeof(AST));
|
||||||
|
|
||||||
ast->type = type;
|
ast->type = type;
|
||||||
@ -53,8 +53,21 @@ void ast_destroy(AST* ast) {
|
|||||||
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
|
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there're no more `AST`s linked to the scope, free.
|
||||||
|
if (ast->scope) {
|
||||||
|
log_dbgf("%p: there is scope.", ast->scope);
|
||||||
|
ast->scope->uses--;
|
||||||
|
log_dbgf(
|
||||||
|
"%p: were %d uses, now %d", ast->scope, ast->scope->uses + 1,
|
||||||
|
ast->scope->uses
|
||||||
|
);
|
||||||
|
if (!ast->scope->uses) {
|
||||||
|
log_dbgf("%p: no more uses, gonna free", ast->scope);
|
||||||
|
scope_destroy_psv(ast->scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
free(ast);
|
free(ast);
|
||||||
htab_destroy(ast->scope);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_print(AST* ast) { ast_print_i(ast, 0); }
|
void ast_print(AST* ast) { ast_print_i(ast, 0); }
|
||||||
@ -123,7 +136,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,3 +289,13 @@ void ast_arg_print(ASTArgData* arg, int i) {
|
|||||||
INDENT_FIELD("name", "%s", arg->name);
|
INDENT_FIELD("name", "%s", arg->name);
|
||||||
INDENT_END;
|
INDENT_END;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AST* ast_find(Scope* scope, char* name) {
|
||||||
|
while (scope) {
|
||||||
|
AST* gotten = htab_get(scope->here, name);
|
||||||
|
if (gotten) return gotten;
|
||||||
|
else scope = scope->inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
@ -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,
|
||||||
|
116
src/exec.c
116
src/exec.c
@ -6,67 +6,70 @@
|
|||||||
#include "include/builtin.h"
|
#include "include/builtin.h"
|
||||||
#include "include/exec.h"
|
#include "include/exec.h"
|
||||||
#include "include/htab.h"
|
#include "include/htab.h"
|
||||||
#include "include/stack.h"
|
#include "include/scope.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
extern AST* root;
|
|
||||||
|
|
||||||
AST* exec_find(char* name);
|
|
||||||
|
|
||||||
AST* exec_start(AST* ast) {
|
AST* exec_start(AST* ast) {
|
||||||
log_dbg("Started execution.");
|
log_dbg("Started execution.");
|
||||||
scope = stack_init();
|
|
||||||
|
|
||||||
HTab* global = htab_init();
|
Scope* global = scope_init(NULL);
|
||||||
|
global->uses = 1;
|
||||||
|
|
||||||
|
// Maybe root ast here should set global as scope?
|
||||||
|
|
||||||
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
||||||
htab_ins(
|
htab_ins(
|
||||||
global, 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))
|
||||||
);
|
);
|
||||||
|
|
||||||
// Push global namespace to `scope`.
|
log_dbg("Completed startup sequence.");
|
||||||
stack_push(scope, global);
|
|
||||||
|
|
||||||
return exec_exp(ast);
|
AST* res = exec_exp(ast, global);
|
||||||
|
log_dbgf("global addr %p uses %d", global, global->uses);
|
||||||
|
scope_destroy_psv(global);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_exp(AST* ast) {
|
AST* exec_exp(AST* ast, Scope* parent) {
|
||||||
switch (ast->type) {
|
switch (ast->type) {
|
||||||
case AST_TYPE_BLOCK: return exec_block(ast);
|
case AST_TYPE_BLOCK: return exec_block(ast, parent);
|
||||||
case AST_TYPE_CALL: return exec_call(ast);
|
case AST_TYPE_CALL: return exec_call(ast, parent);
|
||||||
case AST_TYPE_NUM: return ast;
|
case AST_TYPE_NUM: return ast;
|
||||||
case AST_TYPE_VREF: return exec_vref(ast);
|
case AST_TYPE_VREF: return exec_vref(ast, parent);
|
||||||
case AST_TYPE_VDEF: return exec_vdef(ast);
|
case AST_TYPE_VDEF: return exec_vdef(ast, parent);
|
||||||
case AST_TYPE_FDEF: return exec_fdef(ast);
|
case AST_TYPE_FDEF: return exec_fdef(ast, parent);
|
||||||
default: printf("what\n"); exit(1);
|
default: printf("what\n"); exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_block(AST* ast) {
|
AST* exec_block(AST* ast, Scope* parent) {
|
||||||
ASTBlockData* block = (ASTBlockData*)ast->data;
|
ASTBlockData* block = (ASTBlockData*)ast->data;
|
||||||
|
|
||||||
HTab* local = htab_init();
|
// Blocks create their own scope, shared among their expressions.
|
||||||
stack_push(scope, local);
|
// ast->scope = scope_init(parent);
|
||||||
|
|
||||||
|
// HERE
|
||||||
|
exec_new_scope(ast, parent);
|
||||||
|
|
||||||
// Loop through all but last ast.
|
// Loop through all but last ast.
|
||||||
for (int i = 0; i < block->ln - 1; i++) exec_exp(block->inside[i]);
|
for (int i = 0; i < block->ln - 1; i++)
|
||||||
AST* last = exec_exp(block->inside[block->ln - 1]);
|
exec_exp(block->inside[i], ast->scope);
|
||||||
|
AST* last = exec_exp(block->inside[block->ln - 1], ast->scope);
|
||||||
stack_pop(scope);
|
|
||||||
htab_destroy(local);
|
|
||||||
|
|
||||||
return last;
|
return last;
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_call(AST* ast) {
|
AST* exec_call(AST* ast, Scope* parent) {
|
||||||
log_dbg("Started call execution.");
|
log_dbg("Started call execution.");
|
||||||
ASTCallData* data = (ASTCallData*)ast->data;
|
ASTCallData* data = (ASTCallData*)ast->data;
|
||||||
size_t argc = data->argc;
|
size_t argc = data->argc;
|
||||||
AST** argv = data->argv;
|
AST** argv = data->argv;
|
||||||
char* fname = data->to;
|
char* fname = data->to;
|
||||||
|
|
||||||
AST* fdef = exec_find(fname);
|
ast->scope = parent;
|
||||||
|
|
||||||
|
AST* fdef = ast_find(ast->scope, fname);
|
||||||
|
|
||||||
if (fdef == NULL)
|
if (fdef == NULL)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
@ -76,7 +79,7 @@ AST* exec_call(AST* ast) {
|
|||||||
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));
|
||||||
@ -84,41 +87,41 @@ AST* exec_call(AST* ast) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_cf(AST* ast, size_t argc, AST** argv) {
|
AST* exec_cf(AST* ast, size_t argc, AST** argv) {
|
||||||
|
Scope* callscope = scope_init(ast->scope);
|
||||||
ASTFDefData* fdef = (ASTFDefData*)ast->data;
|
ASTFDefData* fdef = (ASTFDefData*)ast->data;
|
||||||
for (int i = 0; i < argc; i++) {
|
for (int i = 0; i < argc; i++) {
|
||||||
char* key = ((ASTArgData*)fdef->argv[i]->data)->name;
|
char* key = ((ASTArgData*)fdef->argv[i]->data)->name;
|
||||||
AST* val = argv[i];
|
AST* val = argv[i];
|
||||||
htab_ins(scope->buf[scope->ln - 1], key, val);
|
scope_add(callscope, key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
return exec_exp(fdef->body);
|
return exec_exp(fdef->body, callscope);
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_find(char* name) {
|
AST* exec_vdef(AST* ast, Scope* parent) {
|
||||||
AST* val = NULL;
|
// Use parent's scope.
|
||||||
|
// ast->scope = parent;
|
||||||
|
|
||||||
for (int i = scope->ln - 1; i >= 0; i--) {
|
// HERE
|
||||||
HTab* lvl = scope->buf[i];
|
exec_inherit_scope(ast, parent);
|
||||||
val = htab_get(lvl, name);
|
|
||||||
if (val != NULL) return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
AST* exec_vdef(AST* ast) {
|
|
||||||
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;
|
||||||
htab_ins(scope->buf[scope->ln - 1], key, val);
|
scope_add(parent, key, val); // Add variable definition to parent scope.
|
||||||
return exec_exp(val);
|
return exec_exp(val, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_vref(AST* ast) {
|
AST* exec_vref(AST* ast, Scope* parent) {
|
||||||
|
// Use parent's scope.
|
||||||
|
// ast->scope = parent;
|
||||||
|
|
||||||
|
// HERE
|
||||||
|
exec_inherit_scope(ast, parent);
|
||||||
log_dbg("attempting to reference var");
|
log_dbg("attempting to reference var");
|
||||||
ASTVrefData* vref = (ASTVrefData*)ast->data;
|
ASTVrefData* vref = (ASTVrefData*)ast->data;
|
||||||
|
|
||||||
AST* found = exec_find(vref->to);
|
AST* found = ast_find(parent, vref->to);
|
||||||
|
|
||||||
if (found == NULL) {
|
if (found == NULL) {
|
||||||
// TODO: Better memory management here.
|
// TODO: Better memory management here.
|
||||||
@ -130,15 +133,32 @@ AST* exec_vref(AST* ast) {
|
|||||||
return ast_init(AST_TYPE_EXC, ast_exc_data_init(msg, NULL));
|
return ast_init(AST_TYPE_EXC, ast_exc_data_init(msg, NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
return exec_exp(found);
|
return exec_exp(found, ast->scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_fdef(AST* ast) {
|
AST* exec_fdef(AST* ast, Scope* parent) {
|
||||||
|
ast->scope = scope_init(parent);
|
||||||
ASTFDefData* fdef = (ASTFDefData*)ast->data;
|
ASTFDefData* fdef = (ASTFDefData*)ast->data;
|
||||||
AST* val = fdef->body;
|
AST* val = fdef->body;
|
||||||
char* key = fdef->name;
|
char* key = fdef->name;
|
||||||
htab_ins(scope->buf[scope->ln - 1], key, val);
|
scope_add(parent, key, val);
|
||||||
return val; // Function definitions return function body.
|
return val; // Function definitions return function body.
|
||||||
}
|
}
|
||||||
|
|
||||||
void exec_print(double n) { printf("= %lf\n", n); }
|
void exec_print(double n) { printf("= %lf\n", n); }
|
||||||
|
|
||||||
|
inline void exec_new_scope(AST* ast, Scope* inherit) {
|
||||||
|
Scope* scope = scope_init(inherit);
|
||||||
|
ast->scope = scope;
|
||||||
|
|
||||||
|
// Update linked status.
|
||||||
|
scope->uses++;
|
||||||
|
// if (inherit) inherit->uses++;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void exec_inherit_scope(AST* ast, Scope* inherit) {
|
||||||
|
ast->scope = inherit;
|
||||||
|
|
||||||
|
// Update uses.
|
||||||
|
inherit->uses++;
|
||||||
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
#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;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef AST_H
|
#ifndef AST_H
|
||||||
#define AST_H
|
#define AST_H
|
||||||
|
|
||||||
#include "htab.h"
|
#include "scope.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
// The type of an `AST`.
|
// The type of an `AST`.
|
||||||
@ -32,13 +32,13 @@ typedef enum {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
ASTType type; // The type of the `AST`.
|
ASTType type; // The type of the `AST`.
|
||||||
void* data; // The data of the `AST`.
|
void* data; // The data of the `AST`.
|
||||||
HTab* scope; // The scope of the `AST`.
|
Scope* scope; // The scope of the `AST`.
|
||||||
} AST;
|
} AST;
|
||||||
|
|
||||||
// Create a new `AST`.
|
// Create a new `AST`.
|
||||||
AST* ast_init(ASTType type, void* data);
|
AST* ast_init(ASTType type, void* data);
|
||||||
// Create a new `AST` with a specified scope.
|
// Create a new `AST` with a specified scope.
|
||||||
AST* ast_init_scope(ASTType type, void* data, HTab* scope);
|
AST* ast_init_scope(ASTType type, void* data, Scope* scope);
|
||||||
// Destroy an `AST`, recursively.
|
// Destroy an `AST`, recursively.
|
||||||
void ast_destroy(AST* ast);
|
void ast_destroy(AST* ast);
|
||||||
// Print an `AST`, recursively.
|
// Print an `AST`, recursively.
|
||||||
@ -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.
|
||||||
|
|
||||||
@ -153,4 +153,7 @@ void ast_arg_data_destroy(ASTArgData* arg);
|
|||||||
// Print an `ASTArgData`.
|
// Print an `ASTArgData`.
|
||||||
void ast_arg_print(ASTArgData* arg, int i);
|
void ast_arg_print(ASTArgData* arg, int i);
|
||||||
|
|
||||||
|
// Find a name in the scope.
|
||||||
|
AST* ast_find(Scope* scope, char* name);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -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[] = {
|
||||||
|
@ -2,28 +2,31 @@
|
|||||||
#define EXEC_H
|
#define EXEC_H
|
||||||
|
|
||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
#include "stack.h"
|
#include "scope.h"
|
||||||
|
|
||||||
// The `Stack` of `HTab` that makes up the scope of any given `AST`.
|
|
||||||
extern Stack* scope;
|
|
||||||
|
|
||||||
// Start executing at the root of the AST. Initialize the `scope`.
|
// Start executing at the root of the AST. Initialize the `scope`.
|
||||||
AST* exec_start(AST* ast);
|
AST* exec_start(AST* ast);
|
||||||
// Execute an expression. Delegates to the other executor functions.
|
// Execute an expression. Delegates to the other executor functions.
|
||||||
AST* exec_exp(AST* ast);
|
AST* exec_exp(AST* ast, Scope* parent);
|
||||||
// Execute the expressions of a block.
|
// Execute the expressions of a block.
|
||||||
AST* exec_block(AST* ast);
|
AST* exec_block(AST* ast, Scope* parent);
|
||||||
// Execute a call.
|
// Execute a call.
|
||||||
AST* exec_call(AST* ast);
|
AST* exec_call(AST* ast, Scope* parent);
|
||||||
// Execute a custom function call.
|
// Execute a custom function call.
|
||||||
AST* exec_cf(AST* ast, size_t argc, AST** argv);
|
AST* exec_cf(AST* ast, size_t argc, AST** argv);
|
||||||
// Execute a variable reference.
|
|
||||||
AST* exec_vref(AST* ast);
|
|
||||||
// Execute a variable definition.
|
// Execute a variable definition.
|
||||||
AST* exec_vdef(AST* ast);
|
AST* exec_vdef(AST* ast, Scope* parent);
|
||||||
|
// Execute a variable reference.
|
||||||
|
AST* exec_vref(AST* ast, Scope* parent);
|
||||||
// Execute a function definition.
|
// Execute a function definition.
|
||||||
AST* exec_fdef(AST* ast);
|
AST* exec_fdef(AST* ast, Scope* parent);
|
||||||
// Print the result of an execution.
|
// Print the result of an execution.
|
||||||
void exec_print(double n);
|
void exec_print(double n);
|
||||||
|
|
||||||
|
// Create a new scope and mark it as linked. Also update inherited scope.
|
||||||
|
void exec_new_scope(AST* ast, Scope* inherit);
|
||||||
|
|
||||||
|
// Inherit from another scope and mark it as linked.
|
||||||
|
void exec_inherit_scope(AST* ast, Scope* inherit);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
22
src/include/scope.h
Normal file
22
src/include/scope.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef SCOPE_H
|
||||||
|
#define SCOPE_H
|
||||||
|
|
||||||
|
#include "htab.h"
|
||||||
|
|
||||||
|
// Represents the reverse linked tree of scope.
|
||||||
|
typedef struct SCOPE_T {
|
||||||
|
HTab* here; // This scope's hash table.
|
||||||
|
struct SCOPE_T* inherit; // The scope to inherit from.
|
||||||
|
int uses; // How many `AST`s are linked to this scope.
|
||||||
|
} Scope;
|
||||||
|
|
||||||
|
// Create a new `Scope`. Creates new empty `HTab` for current scope.
|
||||||
|
Scope* scope_init(Scope* inherit);
|
||||||
|
// Destroy all linked `Scope`s this inherits from.
|
||||||
|
void scope_destroy(Scope* scope);
|
||||||
|
// Destroy the current `Scope` only.
|
||||||
|
void scope_destroy_psv(Scope *scope);
|
||||||
|
// Insert a key/val pair into the `HTab` of a `Scope`.
|
||||||
|
void scope_add(Scope* scope, char* key, void* val);
|
||||||
|
|
||||||
|
#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);
|
||||||
}
|
}
|
||||||
|
37
src/scope.c
Normal file
37
src/scope.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include "include/scope.h"
|
||||||
|
#include "include/htab.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
Scope* scope_init(Scope* inherit) {
|
||||||
|
Scope* scope = malloc(sizeof(Scope));
|
||||||
|
|
||||||
|
scope->here = htab_init();
|
||||||
|
scope->inherit = inherit;
|
||||||
|
scope->uses = 0;
|
||||||
|
|
||||||
|
log_dbgf("%p: new scope, inherits from %p", scope, inherit);
|
||||||
|
|
||||||
|
return scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
void scope_destroy(Scope* scope) {
|
||||||
|
if (!scope) return;
|
||||||
|
htab_destroy(scope->here);
|
||||||
|
if (scope->inherit != NULL) scope_destroy(scope->inherit);
|
||||||
|
free(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
void scope_destroy_psv(Scope* scope) {
|
||||||
|
log_dbgf("%p got here", scope);
|
||||||
|
if (!scope) return;
|
||||||
|
htab_destroy(scope->here);
|
||||||
|
scope->inherit = NULL;
|
||||||
|
free(scope);
|
||||||
|
log_dbgf("%p got here 2", scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void scope_add(Scope* scope, char* key, void* val) {
|
||||||
|
htab_ins(scope->here, key, val);
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
Subproject commit cdf1d0297effc2736ee847e557b4275b4f02310b
|
Subproject commit 73237c5d224169c7b4d2ec8321f9ac92e8071708
|
Loading…
x
Reference in New Issue
Block a user