Compare commits

..

No commits in common. "abb8ff6b5840f1243449fd46b1e560d86fa761fa" and "1d83aa65a45485fdeb0312ccc122d8fd5e566d8b" have entirely different histories.

11 changed files with 62 additions and 200 deletions

View File

@ -1,9 +1,7 @@
#include <inttypes.h>
#include <stdio.h>
#include "include/ast.h"
#include "include/dstr.h"
#include "include/gc.h"
#include "include/scope.h"
#include "include/util.h"
@ -21,19 +19,12 @@ static char* asttype_names[] = {
};
AST* ast_init(ASTType type, void* data) {
AST* ast = gc_alloc(sizeof(AST), GC_TYPE_AST);
AST* ast = malloc(sizeof(AST));
ast->type = type;
ast->data = data;
ast->scope = NULL;
if (ast->type > AST_TYPE_MAX) {
log_dbgf(
"Attempted to create invalid AST (%i > %i) to GC: ast:%p",
ast->type, AST_TYPE_MAX, ast
);
}
return ast;
}
@ -63,35 +54,15 @@ void ast_destroy(AST* ast) {
}
// If there're no more `AST`s linked to the scope, free.
if (ast->scope && !--ast->scope->uses) scope_destroy_psv(ast->scope);
free(ast);
}
void ast_destroy_psv(AST* ast) {
if (!ast) return;
switch (ast->type) {
case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
case AST_TYPE_CALL: ast_call_data_destroy_psv(ast->data); break;
case AST_TYPE_VREF: ast_vref_data_destroy(ast->data); break;
case AST_TYPE_VDEF: ast_vdef_data_destroy_psv(ast->data); break;
case AST_TYPE_BLOCK: ast_block_data_destroy_psv(ast->data); break;
case AST_TYPE_FDEF: ast_fdef_data_destroy_psv(ast->data); break;
case AST_TYPE_ARG: ast_arg_data_destroy(ast->data); break;
case AST_TYPE_BIF: ast_bif_data_destroy(ast->data); break;
case AST_TYPE_EXC: ast_exc_data_destroy(ast->data); break;
default:
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
if (ast->scope) {
ast->scope->uses--;
if (!ast->scope->uses) { scope_destroy_psv(ast->scope); }
}
free(ast);
}
void ast_print(AST* ast) {
if (!ast) return;
ast_print_i(ast, 0);
}
void ast_print(AST* ast) { ast_print_i(ast, 0); }
void ast_print_i(AST* ast, int i) {
INDENT_BEGIN(i);
@ -135,18 +106,13 @@ void ast_num_print(ASTNumData* data, int i) {
INDENT_END;
}
ASTExcData* ast_exc_data_init(const char* msg, AST* trace) {
ASTExcData* ast_exc_data_init(char* msg, AST* trace) {
ASTExcData* data = malloc(sizeof(ASTExcData));
data->msg = msg;
data->trace = trace;
return data;
}
void ast_exc_data_destroy(ASTExcData* exc) {
// `msg` is static, and `trace` will get freed in GC.
free(exc);
}
void ast_exc_print(ASTExcData* data, int i) {
INDENT_BEGIN(i);
@ -166,8 +132,6 @@ ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)) {
return (ASTBIFData*)fn;
}
void ast_bif_data_destroy(ASTBIFData* bif) { return; }
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
talloc(ASTCallData, call);
@ -188,15 +152,6 @@ void ast_call_data_destroy(ASTCallData* call) {
free(call);
}
void ast_call_data_destroy_psv(ASTCallData* call) {
if (!call) return;
free(call->to);
call->to = NULL;
free(call->argv);
call->argv = NULL;
free(call);
}
void ast_call_print(ASTCallData* data, int i) {
INDENT_BEGIN(i);
@ -223,11 +178,6 @@ void ast_vdef_data_destroy(ASTVDefData* vdef) {
free(vdef);
}
void ast_vdef_data_destroy_psv(ASTVDefData* vdef) {
free(vdef->name);
free(vdef);
}
void ast_vdef_print(ASTVDefData* vdef, int depth) {
INDENT_BEGIN(depth);
@ -278,11 +228,6 @@ void ast_block_data_destroy(ASTBlockData* block) {
free(block);
}
void ast_block_data_destroy_psv(ASTBlockData* block) {
free(block->inside);
free(block);
}
void ast_block_print(ASTBlockData* data, int depth) {
INDENT_BEGIN(depth);
@ -311,13 +256,6 @@ void ast_fdef_data_destroy(ASTFDefData* fdef) {
ast_destroy(fdef->body);
}
void ast_fdef_data_destroy_psv(ASTFDefData* fdef) {
free(fdef->name);
free(fdef->argv);
free(fdef->body);
free(fdef);
}
void ast_fdef_print(ASTFDefData* fdef, int i) {
INDENT_BEGIN(i)
INDENT_TITLE("ASTFDefData", fdef);

View File

@ -13,21 +13,36 @@
AST* exec_start(AST* ast) {
log_dbg("Started execution.");
if (!ast) return ast;
Scope* global = scope_init(NULL);
global->uses = 1;
for (int i = 0; i < BUILTIN_FNS_LN; i++)
// Keep track of built-in function ASTs, as they arent part of the main
// tree.
// DList* builtins = dlist_init();
for (int i = 0; i < BUILTIN_FNS_LN; i++) {
// AST* builtin_ast =
// ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn));
// dlist_append(builtins, builtin_ast);
htab_ins(
global->here, BUILTIN_FNS[i].name,
ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn))
);
}
log_dbg("Completed startup sequence.");
AST* res = exec_exp(ast, global);
// Clean up built-in function ASTs.
/*for (int i = 0; i < builtins->ln; i++) ast_destroy(builtins->buf[i]);
dlist_destroy(builtins);
*/
scope_destroy_psv(global);
return res;
}
@ -49,6 +64,10 @@ AST* exec_exp(AST* ast, Scope* parent) {
AST* exec_block(AST* ast, Scope* parent) {
ASTBlockData* block = (ASTBlockData*)ast->data;
// Blocks create their own scope, shared among their expressions.
// ast->scope = scope_init(parent);
// HERE
exec_new_scope(ast, parent);
// Loop through all but last ast.
@ -99,6 +118,9 @@ AST* exec_cf(AST* ast, size_t argc, AST** argv) {
AST* exec_vdef(AST* ast, Scope* parent) {
// Use parent's scope.
// ast->scope = parent;
// HERE
exec_inherit_scope(ast, parent);
ASTVDefData* data = (ASTVDefData*)ast->data;
@ -110,6 +132,9 @@ AST* exec_vdef(AST* ast, Scope* parent) {
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");
ASTVrefData* vref = (ASTVrefData*)ast->data;
@ -132,12 +157,10 @@ AST* exec_vref(AST* ast, Scope* parent) {
AST* exec_fdef(AST* ast, Scope* parent) {
ast->scope = scope_init(parent);
ASTFDefData* fdef = (ASTFDefData*)ast->data;
log_dbgf("IS THIS SUSPICIOUS??? %i", fdef->body->type);
AST* val = ast;
AST* val = fdef->body;
char* key = fdef->name;
scope_add(parent, key, val);
// TODO: Create lambda functions.
return fdef->body; // Function definitions return function body.
return val; // Function definitions return function body.
}
void exec_print(double n) { printf("= %lf\n", n); }

View File

@ -1,57 +0,0 @@
#include "include/gc.h"
#include "include/ast.h"
#include "include/scope.h"
#include "include/util.h"
#include <assert.h>
#include <stdio.h>
#include <stddef.h>
GC* gclist = NULL;
void gc_destroy(GC* gc) { free(gc); }
void f() {}
void* gc_alloc(size_t sz, GCType type) {
assert(type <= GC_TYPE_MAX);
void* mem = malloc(sz);
GC* gc = malloc(sizeof(GC));
gc->p = mem;
gc->type = type;
gc->marked = false;
gc->nxt = gclist;
gclist = gc;
if (type == GC_TYPE_AST) {
log_dbgf("Alloc'd AST for GC: %p", mem);
} else if (type == GC_TYPE_SCOPE) {
log_dbgf("Alloc'd scope for GC: %p", mem);
}
return mem;
}
void gc_hack_free() {
while (gclist) {
GC* gc = gclist;
gclist = gclist->nxt;
switch (gc->type) {
case GC_TYPE_AST:
f();
if (((AST*)gc->p)->type > AST_TYPE_MAX) {
log_dbgf(
"Attempted to free invalid AST (%i > %i) to GC: gc:%p "
"ast:%p",
((AST*)gc->p)->type, AST_TYPE_MAX, gc, gc->p
);
}
ast_destroy_psv(gc->p);
break;
case GC_TYPE_SCOPE: scope_destroy_psv(gc->p); break;
}
gc_destroy(gc);
}
}

View File

@ -124,13 +124,7 @@ block:
exp:
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
// Function definitions.
| WORD GROUPS arg GROUPE EQ exp {
size_t argc = $3->ln;
AST** argv = $3->buf;
argarr_destroypsv($3);
$$ = ast_init(AST_TYPE_FDEF, ast_fdef_data_init($1, argc, argv, $6));
}
//| BLOCKS exp BLOCKE { $$ = $2; }
| BLOCKS block BLOCKE {
$$ = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $2->buf, $2->ln));
@ -199,4 +193,11 @@ exp:
strcpy(to, "div");
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
}
| WORD GROUPS arg GROUPE EQ exp {
size_t argc = $3->ln;
AST** argv = $3->buf;
argarr_destroypsv($3);
$$ = ast_init(AST_TYPE_FDEF, ast_fdef_data_init($1, argc, argv, $6));
}
%%

View File

@ -7,9 +7,7 @@
#include <string.h>
HTab* htab_init() {
HTab* htab = calloc(1, sizeof(HTab));
log_dbgf("HTAB %p", htab);
HTab* htab = malloc(sizeof(HTab));
return htab;
}
@ -31,6 +29,7 @@ void* htab_get(HTab* htab, char* key) {
void htab_ins(HTab* htab, char* key, void* data) {
size_t i = geti(key);
// assert((*htab)[i] == NULL);
(*htab)[i] = data;
log_dbgf("Inserted something to hash table @ index %lu", i);
}

View File

@ -25,7 +25,7 @@ typedef enum {
AST_TYPE_BLOCK, // A block of code (scope).
AST_TYPE_FDEF, // A function definition.
AST_TYPE_ARG, // A definition argument.
AST_TYPE_MAX = AST_TYPE_ARG,
AST_TYPE_MAX = AST_TYPE_FDEF,
} ASTType;
// An Abstract Syntax Tree.
@ -41,8 +41,6 @@ AST* ast_init(ASTType type, void* data);
AST* ast_init_scope(ASTType type, void* data, Scope* scope);
// Destroy an `AST`, recursively.
void ast_destroy(AST* ast);
// Destroy an `AST`.
void ast_destroy_psv(AST* ast);
// Print an `AST`, recursively.
void ast_print(AST* ast);
// Helper function to `ast_print()`, where `i` is indentation level.
@ -60,11 +58,11 @@ void ast_num_print(ASTNumData*, int i);
// An exception.
typedef struct ASTEXCDATA {
const char* msg; // The exception message.
char* msg; // The exception message.
AST* trace; // The previous exception.
} ASTExcData;
// Create a new `ASTExecData. `msg` should be static.
ASTExcData* ast_exc_data_init(const char* msg, AST* trace);
// Create a new `ASTExecData.
ASTExcData* ast_exc_data_init(char* msg, AST* trace);
// Destroy an `ASTExecData`.
void ast_exc_data_destroy(ASTExcData* exc);
// Print an `ASTExecData`.
@ -75,8 +73,8 @@ typedef AST* (*ASTBIFData)(size_t argc, AST** argv, Scope* scope);
// Create a built-in function.
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*));
// Destroy an `ASTBIFData`.
void ast_bif_data_destroy(ASTBIFData* bif);
// There is no `ASTBIFData` destroy function, as function pointers are immortal.
// A call (to a function).
typedef struct {
@ -87,10 +85,8 @@ typedef struct {
// Create a new `ASTCallData`.
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
// Destroy an `ASTCallData` recursively.
void ast_call_data_destroy(ASTCallData* call);
// Destroy an `ASTCallData`.
void ast_call_data_destroy_psv(ASTCallData *call);
void ast_call_data_destroy(ASTCallData* call);
// Print an `ASTCallData`.
void ast_call_print(ASTCallData*, int i);
@ -104,8 +100,6 @@ typedef struct {
ASTVDefData* ast_vdef_data_init(char* name, AST* val);
// Destroys the `ASTVDefData`, `ASTVDefData->name`, and `ASTVDefData->val`.
void ast_vdef_data_destroy(ASTVDefData* vdef);
// Destroy an `ASTVDefData`.
void ast_vdef_data_destroy_psv(ASTVDefData* vdef);
// Print an `ASTVDefData`.
void ast_vdef_print(ASTVDefData*, int depth);
@ -131,8 +125,6 @@ typedef struct {
ASTBlockData* ast_block_data_init(AST** inside, size_t ln);
// Destroy an `ASTBlockData`, recursively.
void ast_block_data_destroy(ASTBlockData* block);
// Destroy an `ASTBlockData`.
void ast_block_data_destroy_psv(ASTBlockData *block);
// Print an `ASTBlockData`.
void ast_block_print(ASTBlockData*, int i);
@ -145,10 +137,8 @@ typedef struct {
// Create a new `ASTFDefData`.
ASTFDefData* ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body);
// Destroy an `ASTFDefData`, recursively.
void ast_fdef_data_destroy(ASTFDefData* fdef);
// Destroy an `ASTFDefData`.
void ast_fdef_data_destroy_psv(ASTFDefData* fdef);
void ast_fdef_data_destroy(ASTFDefData* fdef);
// Print an `ASTFDefData`.
void ast_fdef_print(ASTFDefData* fdef, int i);

View File

@ -1,31 +0,0 @@
#ifndef GC_H
#define GC_H
#include <stdlib.h>
// The type a GC can refer to.
typedef enum {
GC_TYPE_AST,
GC_TYPE_SCOPE,
GC_TYPE_MAX = GC_TYPE_SCOPE
} GCType;
// Added to each AST and Scope; keep track of what's actually still accessible.
typedef struct GC_STRUCT {
void* p; // Pointer to the data.
struct GC_STRUCT* nxt; // The next GC in the linked list.
GCType type; // What type of data.
bool marked; // Whether the data is still accessible.
} GC;
GC* gc_init(void* p, GCType type, GC*);
// Does not free ->p or ->nxt.
void gc_destroy(GC* gc);
// Allocate for an object in the heap, and keep track of it in the GC.
void* gc_alloc(size_t sz, GCType type);
// Free everything, immediately.
void gc_hack_free();
#endif

View File

@ -7,7 +7,7 @@
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. // TODO: REMOVE.
int uses; // How many `AST`s are linked to this scope.
} Scope;
// Create a new `Scope`. Creates new empty `HTab` for current scope.

View File

@ -26,7 +26,7 @@ void argarr_destroy(ArgArr* argarr) {
void argarr_destroypsv(ArgArr* argarr) { free(argarr); }
void argarr_add(ArgArr* argarr, AST* arg) {
if ((argarr->ln + 1) * sizeof(AST*) > argarr->sz) {
if ((argarr->ln + 1) * argarr->sz > argarr->sz) {
argarr->sz *= 2;
argarr->buf = realloc(argarr->buf, argarr->sz);
log_dbgf(

View File

@ -4,7 +4,7 @@
#include "include/ast.h"
#include "include/dstr.h"
#include "include/exec.h"
#include "include/gc.h"
#include "include/lexer.h"
#include "include/util.h"
@ -22,9 +22,8 @@ int main(int argc, char** argv) {
ast_print(root);
AST* eval = exec_start(root);
ast_print(eval);
// ast_destroy(eval);
// ast_destroy(root);
gc_hack_free();
ast_destroy(eval);
ast_destroy(root);
exit(0);
}
@ -61,7 +60,8 @@ int main(int argc, char** argv) {
AST* eval = exec_start(root);
ast_print(eval);
gc_hack_free();
ast_destroy(eval);
ast_destroy(root);
}
dstr_destroy(ln);

View File

@ -1,12 +1,11 @@
#include "include/scope.h"
#include "include/gc.h"
#include "include/htab.h"
#include "include/util.h"
#include <stdio.h>
#include <stdlib.h>
Scope* scope_init(Scope* inherit) {
Scope* scope = gc_alloc(sizeof(Scope), GC_TYPE_SCOPE);
Scope* scope = malloc(sizeof(Scope));
scope->here = htab_init();
scope->inherit = inherit;