Compare commits

..

No commits in common. "743d16f696d323baebc6d9fe803a8e2232fc8a51" and "5b0950cabbaa791b55daf4e2fac30877ba387da8" have entirely different histories.

10 changed files with 41 additions and 39 deletions

View File

@ -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**, Scope*)) { ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**)) {
return (ASTBIFData*)fn; return (ASTBIFData*)fn;
} }

View File

@ -5,11 +5,11 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
AST* builtin_sum(size_t argc, AST** argv, Scope* parent) { AST* builtin_sum(size_t argc, AST** argv) {
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], parent); AST* arg = exec_exp(argv[i]);
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, Scope* parent) {
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, Scope* parent) { AST* builtin_sub(size_t argc, AST** argv) {
log_dbg("Got here"); log_dbg("Got here");
AST* first = exec_exp(*argv, parent); AST* first = exec_exp(*argv);
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, Scope* parent) {
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], parent); AST* arg = exec_exp(argv[i]);
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, Scope* parent) {
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, Scope* parent) { AST* builtin_mul(size_t argc, AST** argv) {
log_dbg("Got here"); log_dbg("Got here");
AST* first = exec_exp(*argv, parent); AST* first = exec_exp(*argv);
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, Scope* parent) {
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], parent); AST* arg = exec_exp(argv[i]);
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, Scope* parent) {
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, Scope* parent) { AST* builtin_div(size_t argc, AST** argv) {
log_dbg("Got here"); log_dbg("Got here");
AST* first = exec_exp(*argv, parent); AST* first = exec_exp(*argv);
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, Scope* parent) {
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], parent); AST* arg = exec_exp(argv[i]);
if (arg->type == AST_TYPE_EXC) if (arg->type == AST_TYPE_EXC)
return ast_init( return ast_init(
AST_TYPE_EXC, AST_TYPE_EXC,

View File

@ -12,17 +12,15 @@
AST* exec_start(AST* ast) { AST* exec_start(AST* ast) {
log_dbg("Started execution."); log_dbg("Started execution.");
Scope* global = scope_init(NULL); Scope* builtin = 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(
global->here, BUILTIN_FNS[i].name, builtin->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))
); );
log_dbg("Completed startup sequence."); return exec_exp(ast, builtin);
return exec_exp(ast, global);
} }
AST* exec_exp(AST* ast, Scope* parent) { AST* exec_exp(AST* ast, Scope* parent) {
@ -40,7 +38,6 @@ 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.
@ -70,7 +67,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, parent); return bifdata(argc, argv);
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));
@ -90,17 +87,15 @@ AST* exec_cf(AST* ast, size_t argc, AST** argv) {
} }
AST* exec_vdef(AST* ast, Scope* parent) { AST* exec_vdef(AST* ast, Scope* parent) {
// Use parent's scope. ast->scope = scope_init(parent);
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); // Add variable definition to parent scope. scope_add(parent, key, val);
return exec_exp(val, parent); return exec_exp(val, ast->scope);
} }
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;

View File

@ -7,13 +7,12 @@
#include <string.h> #include <string.h>
HTab* htab_init() { HTab* htab_init() {
HTab* htab = malloc(sizeof(HTab)); HTab* htab = calloc(1, 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) {

View File

@ -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, Scope* scope); typedef AST* (*ASTBIFData)(size_t argc, AST** argv);
// Create a built-in function. // Create a built-in function.
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)); ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**));
// There is no `ASTBIFData` destroy function, as function pointers are immortal. // There is no `ASTBIFData` destroy function, as function pointers are immortal.

View File

@ -4,20 +4,20 @@
#include "ast.h" #include "ast.h"
// Sum some nums. // Sum some nums.
AST* builtin_sum(size_t argc, AST** argv, Scope* parent); AST* builtin_sum(size_t argc, AST** argv);
// Subtract nums. // Subtract nums.
AST* builtin_sub(size_t argc, AST** argv, Scope* parent); AST* builtin_sub(size_t argc, AST** argv);
// Multiply nums. // Multiply nums.
AST* builtin_mul(size_t argc, AST** argv, Scope* parent); AST* builtin_mul(size_t argc, AST** argv);
// Divide nums. // Divide nums.
AST* builtin_div(size_t argc, AST** argv, Scope* parent); AST* builtin_div(size_t argc, AST** argv);
struct builtin_data { struct builtin_data {
char* name; char* name;
AST* (*fn)(size_t argc, AST** argv, Scope* parent); AST* (*fn)(size_t argc, AST** argv);
}; };
static struct builtin_data BUILTIN_FNS[] = { static struct builtin_data BUILTIN_FNS[] = {

View File

@ -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`.
void scope_add(Scope* scope, char* key, void* val); inline void scope_add(Scope* scope, char* key, void* val);
#endif #endif

View File

@ -4,8 +4,10 @@
#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"
@ -15,11 +17,16 @@ 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);
} }
@ -56,6 +63,9 @@ 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);
} }

View File

@ -12,14 +12,12 @@ 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 cdf1d0297effc2736ee847e557b4275b4f02310b Subproject commit 73237c5d224169c7b4d2ec8321f9ac92e8071708