Compare commits
13 Commits
v0.2
...
4e8d7131d6
Author | SHA1 | Date | |
---|---|---|---|
4e8d7131d6 | |||
bfce18ab81 | |||
7b648c4bd7 | |||
4ec5d1c075 | |||
5ba070ced7 | |||
8256643c0b | |||
29a217928e | |||
482f2b4877 | |||
36fd838a8f | |||
289243de38 | |||
67aafb3ead | |||
3b5bee0695 | |||
970fc39198 |
10
STATUS.md
10
STATUS.md
@@ -12,10 +12,10 @@
|
|||||||
- [x] Order of operations
|
- [x] Order of operations
|
||||||
- [x] Parse function application
|
- [x] Parse function application
|
||||||
- [x] Parse order of operations with parenthesis
|
- [x] Parse order of operations with parenthesis
|
||||||
- [ ] Parse variable invocation
|
- [x] Parse variable invocation
|
||||||
- [x] Parse variable definition
|
- [x] Parse variable definition
|
||||||
- [ ] Parse types
|
- [ ] Parse types
|
||||||
- [ ] Parse function definition
|
- [x] Parse function definition
|
||||||
- [ ] Parse lists/arrays/vectors
|
- [ ] Parse lists/arrays/vectors
|
||||||
- [x] Parse blocks
|
- [x] Parse blocks
|
||||||
- [ ] Parse control flow
|
- [ ] Parse control flow
|
||||||
@@ -32,9 +32,9 @@
|
|||||||
|
|
||||||
- [ ] Executer
|
- [ ] Executer
|
||||||
- [x] Exec function calls
|
- [x] Exec function calls
|
||||||
- [ ] Exec variable use
|
- [x] Exec variable use
|
||||||
- [ ] Exec variable definition
|
- [x] Exec variable definition
|
||||||
- [ ] Exec function definition
|
- [x] Exec function definition
|
||||||
- [ ] Exec symbolic variables
|
- [ ] Exec symbolic variables
|
||||||
- [ ] Exec control flow statements
|
- [ ] Exec control flow statements
|
||||||
- [ ] Exec variadic functions
|
- [ ] Exec variadic functions
|
||||||
|
6
TODO.md
6
TODO.md
@@ -1,7 +1,7 @@
|
|||||||
0. Create file to describe properties of terminology used; param, arg, var, &c.
|
1. Differentiate parameters and arguments -- params for function definitions,
|
||||||
1. Differenciate parameters and arguments -- params for function definitions,
|
|
||||||
arguments for function calls
|
arguments for function calls
|
||||||
2. Add scope field to all ASTs, and new scope layer for those that need it.
|
|
||||||
|
|
||||||
Change editor to GNU Readline.
|
Change editor to GNU Readline.
|
||||||
Make variables persist through lines in the editor.
|
Make variables persist through lines in the editor.
|
||||||
|
|
||||||
|
Return syntax errors as exceptions.
|
||||||
|
25
definitions.md
Normal file
25
definitions.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
f = \(x) 2 * x
|
||||||
|
g = \(h) \(x) h(h(x))
|
||||||
|
|
||||||
|
f(2) => 4
|
||||||
|
g(f)(2) => 8
|
||||||
|
|
||||||
|
CALL
|
||||||
|
argc: 1
|
||||||
|
argv: [ 2 ]
|
||||||
|
to:
|
||||||
|
CALL
|
||||||
|
argc: 1
|
||||||
|
argv: [
|
||||||
|
VREF
|
||||||
|
name: f
|
||||||
|
]
|
||||||
|
to:
|
||||||
|
VREF
|
||||||
|
name: g
|
||||||
|
fname: NULL
|
||||||
|
|
||||||
|
|
||||||
|
expression + arguments = call = expression
|
||||||
|
expression + parameters = lambda = expression
|
||||||
|
expression + name = variable = expression
|
@@ -1,3 +1,3 @@
|
|||||||
f(n) = 2 * n
|
f(n) 2 * n
|
||||||
|
|
||||||
f(5)
|
f(5)
|
||||||
|
193
src/ast.c
193
src/ast.c
@@ -2,24 +2,12 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/dstr.h"
|
|
||||||
#include "include/gc.h"
|
#include "include/gc.h"
|
||||||
#include "include/scope.h"
|
#include "include/scope.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
extern AST* root;
|
extern AST* root;
|
||||||
|
|
||||||
static char* asttype_names[] = {
|
|
||||||
[AST_TYPE_CALL] = "FUNC CALL",
|
|
||||||
[AST_TYPE_NUM] = "NUMBER",
|
|
||||||
[AST_TYPE_VREF] = "VAR REFERENCE",
|
|
||||||
[AST_TYPE_VDEF] = "VAR DEFINITION",
|
|
||||||
[AST_TYPE_BLOCK] = "BLOCK",
|
|
||||||
[AST_TYPE_EXC] = "EXCEPTION",
|
|
||||||
[AST_TYPE_FDEF] = "FUNCTION DEFINITION",
|
|
||||||
[AST_TYPE_ARG] = "DEFINITION ARGUMENT"
|
|
||||||
};
|
|
||||||
|
|
||||||
AST* ast_init(ASTType type, void* data) {
|
AST* ast_init(ASTType type, void* data) {
|
||||||
AST* ast = gc_alloc(sizeof(AST), GC_TYPE_AST);
|
AST* ast = gc_alloc(sizeof(AST), GC_TYPE_AST);
|
||||||
|
|
||||||
@@ -55,32 +43,12 @@ void ast_destroy(AST* ast) {
|
|||||||
case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break;
|
case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break;
|
||||||
case AST_TYPE_VREF: ast_vref_data_destroy(ast->data); break;
|
case AST_TYPE_VREF: ast_vref_data_destroy(ast->data); break;
|
||||||
case AST_TYPE_VDEF: ast_vdef_data_destroy(ast->data); break;
|
case AST_TYPE_VDEF: ast_vdef_data_destroy(ast->data); break;
|
||||||
case AST_TYPE_BLOCK: ast_block_data_destroy(ast->data); break;
|
|
||||||
case AST_TYPE_FDEF: ast_fdef_data_destroy(ast->data); break;
|
|
||||||
case AST_TYPE_ARG: ast_arg_data_destroy(ast->data); break;
|
|
||||||
default:
|
|
||||||
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 && !--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_BLOCK: ast_block_data_destroy_psv(ast->data); break;
|
||||||
case AST_TYPE_FDEF: ast_fdef_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_ARG: ast_arg_data_destroy(ast->data); break;
|
||||||
case AST_TYPE_BIF: ast_bif_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;
|
case AST_TYPE_EXC: ast_exc_data_destroy(ast->data); break;
|
||||||
|
case AST_TYPE_LAMBDA: ast_lambda_data_destroy(ast->data); break;
|
||||||
default:
|
default:
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -88,35 +56,6 @@ void ast_destroy_psv(AST* ast) {
|
|||||||
free(ast);
|
free(ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_print(AST* ast) {
|
|
||||||
if (!ast) return;
|
|
||||||
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("scope", "%p", ast->scope);
|
|
||||||
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;
|
|
||||||
case AST_TYPE_EXC: ast_exc_print(ast->data, i + 2); break;
|
|
||||||
case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break;
|
|
||||||
case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break;
|
|
||||||
case AST_TYPE_BLOCK: ast_block_print(ast->data, i + 2); break;
|
|
||||||
case AST_TYPE_FDEF: ast_fdef_print(ast->data, i + 2); break;
|
|
||||||
case AST_TYPE_ARG: ast_arg_print(ast->data, i + 2); break;
|
|
||||||
default: exit(1);
|
|
||||||
}
|
|
||||||
INDENT_FIELD_NONL_END;
|
|
||||||
INDENT_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTNumData* ast_num_data_init(double val) {
|
ASTNumData* ast_num_data_init(double val) {
|
||||||
talloc(ASTNumData, num);
|
talloc(ASTNumData, num);
|
||||||
|
|
||||||
@@ -127,14 +66,6 @@ ASTNumData* ast_num_data_init(double val) {
|
|||||||
|
|
||||||
void ast_num_data_destroy(ASTNumData* num) { free(num); }
|
void ast_num_data_destroy(ASTNumData* num) { free(num); }
|
||||||
|
|
||||||
void ast_num_print(ASTNumData* data, int i) {
|
|
||||||
INDENT_BEGIN(i);
|
|
||||||
|
|
||||||
INDENT_FIELD("data", "%lf", *data);
|
|
||||||
|
|
||||||
INDENT_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTExcData* ast_exc_data_init(const char* msg, AST* trace) {
|
ASTExcData* ast_exc_data_init(const char* msg, AST* trace) {
|
||||||
ASTExcData* data = malloc(sizeof(ASTExcData));
|
ASTExcData* data = malloc(sizeof(ASTExcData));
|
||||||
data->msg = msg;
|
data->msg = msg;
|
||||||
@@ -147,33 +78,35 @@ void ast_exc_data_destroy(ASTExcData* exc) {
|
|||||||
free(exc);
|
free(exc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_exc_print(ASTExcData* data, int i) {
|
|
||||||
INDENT_BEGIN(i);
|
|
||||||
|
|
||||||
INDENT_TITLE("ASTExcData", data);
|
|
||||||
INDENT_FIELD("msg", "\"%s\"", data->msg);
|
|
||||||
if (data->trace == NULL) {
|
|
||||||
INDENT_FIELD("trace", "%p", NULL)
|
|
||||||
} else {
|
|
||||||
INDENT_FIELD_EXT_NONL_START("trace");
|
|
||||||
ast_print_i(data->trace, i + 1);
|
|
||||||
INDENT_FIELD_NONL_END;
|
|
||||||
}
|
|
||||||
INDENT_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)) {
|
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)) {
|
||||||
return (ASTBIFData*)fn;
|
return (ASTBIFData*)fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_bif_data_destroy(ASTBIFData* bif) { return; }
|
void ast_bif_data_destroy(ASTBIFData* bif) { return; }
|
||||||
|
|
||||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
// Lambda.
|
||||||
|
|
||||||
|
ASTLambdaData* ast_lambda_data_init(size_t parc, AST** parv, AST* body) {
|
||||||
|
talloc(ASTLambdaData, lambda);
|
||||||
|
|
||||||
|
lambda->parc = parc;
|
||||||
|
lambda->parv = parv;
|
||||||
|
lambda->body = body;
|
||||||
|
|
||||||
|
return lambda;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_lambda_data_destroy(ASTLambdaData* lambda) {
|
||||||
|
free(lambda->parv);
|
||||||
|
free(lambda);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call.
|
||||||
|
|
||||||
|
ASTCallData* ast_call_data_init(size_t argc, AST** argv, AST* exp) {
|
||||||
talloc(ASTCallData, call);
|
talloc(ASTCallData, call);
|
||||||
|
|
||||||
log_dbgf("to: %s", to);
|
call->exp = exp;
|
||||||
|
|
||||||
call->to = to;
|
|
||||||
call->argc = argc;
|
call->argc = argc;
|
||||||
call->argv = argv;
|
call->argv = argv;
|
||||||
|
|
||||||
@@ -182,63 +115,27 @@ ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
|||||||
|
|
||||||
void ast_call_data_destroy(ASTCallData* call) {
|
void ast_call_data_destroy(ASTCallData* call) {
|
||||||
if (!call) return;
|
if (!call) return;
|
||||||
free(call->to);
|
|
||||||
for (size_t i = 0; i < call->argc; i++) ast_destroy(call->argv[i]);
|
|
||||||
free(call->argv);
|
free(call->argv);
|
||||||
free(call);
|
free(call);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_call_data_destroy_psv(ASTCallData* call) {
|
// VDef.
|
||||||
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) {
|
ASTVDefData* ast_vdef_data_init(char* name, AST* exp) {
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTVDefData* ast_vdef_data_init(char* name, AST* val) {
|
|
||||||
talloc(ASTVDefData, vdef);
|
talloc(ASTVDefData, vdef);
|
||||||
|
|
||||||
vdef->name = name;
|
vdef->name = name;
|
||||||
vdef->val = val;
|
vdef->exp = exp;
|
||||||
|
|
||||||
return vdef;
|
return vdef;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_vdef_data_destroy(ASTVDefData* vdef) {
|
void ast_vdef_data_destroy(ASTVDefData* vdef) {
|
||||||
ast_destroy(vdef->val);
|
|
||||||
free(vdef->name);
|
free(vdef->name);
|
||||||
free(vdef);
|
free(vdef);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_vdef_data_destroy_psv(ASTVDefData* vdef) {
|
// VRef.
|
||||||
free(vdef->name);
|
|
||||||
free(vdef);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_vdef_print(ASTVDefData* vdef, int depth) {
|
|
||||||
INDENT_BEGIN(depth);
|
|
||||||
|
|
||||||
INDENT_TITLE("ASTVDefData", vdef);
|
|
||||||
INDENT_FIELD("name", "%s", vdef->name);
|
|
||||||
INDENT_FIELD_EXT_NONL_START("val");
|
|
||||||
ast_print_i(vdef->val, depth + 2); // 2 because already indented.
|
|
||||||
INDENT_FIELD_NONL_END;
|
|
||||||
|
|
||||||
INDENT_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTVrefData* ast_vref_data_init(char* to) {
|
ASTVrefData* ast_vref_data_init(char* to) {
|
||||||
talloc(ASTVrefData, vref);
|
talloc(ASTVrefData, vref);
|
||||||
@@ -253,15 +150,6 @@ void ast_vref_data_destroy(ASTVrefData* vref) {
|
|||||||
free(vref);
|
free(vref);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_vref_print(ASTVrefData* data, int i) {
|
|
||||||
INDENT_BEGIN(i);
|
|
||||||
|
|
||||||
INDENT_TITLE("ASTVrefData", data);
|
|
||||||
INDENT_FIELD("to", "%s", data->to);
|
|
||||||
|
|
||||||
INDENT_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTBlockData* ast_block_data_init(AST** inside, size_t ln) {
|
ASTBlockData* ast_block_data_init(AST** inside, size_t ln) {
|
||||||
ASTBlockData* block = malloc(sizeof(ASTBlockData));
|
ASTBlockData* block = malloc(sizeof(ASTBlockData));
|
||||||
|
|
||||||
@@ -283,16 +171,6 @@ void ast_block_data_destroy_psv(ASTBlockData* block) {
|
|||||||
free(block);
|
free(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_block_print(ASTBlockData* data, int depth) {
|
|
||||||
INDENT_BEGIN(depth);
|
|
||||||
|
|
||||||
INDENT_TITLE("ASTBlockData", data);
|
|
||||||
INDENT_FIELD("ln", "%ld", data->ln);
|
|
||||||
INDENT_FIELD_LIST("inside", data->inside, data->ln, ast_print_i);
|
|
||||||
|
|
||||||
INDENT_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTFDefData*
|
ASTFDefData*
|
||||||
ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body) {
|
ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body) {
|
||||||
ASTFDefData* fdef = malloc(sizeof(ASTFDefData));
|
ASTFDefData* fdef = malloc(sizeof(ASTFDefData));
|
||||||
@@ -317,18 +195,6 @@ void ast_fdef_data_destroy_psv(ASTFDefData* fdef) {
|
|||||||
free(fdef);
|
free(fdef);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_fdef_print(ASTFDefData* fdef, int i) {
|
|
||||||
INDENT_BEGIN(i)
|
|
||||||
INDENT_TITLE("ASTFDefData", fdef);
|
|
||||||
INDENT_FIELD("name", "%s", fdef->name);
|
|
||||||
INDENT_FIELD("argc", "%ld", fdef->argc);
|
|
||||||
INDENT_FIELD_LIST("argv", fdef->argv, fdef->argc, ast_print_i);
|
|
||||||
INDENT_FIELD_EXT_NONL_START("body");
|
|
||||||
ast_print_i(fdef->body, i + 2);
|
|
||||||
INDENT_FIELD_NONL_END;
|
|
||||||
INDENT_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTArgData* ast_arg_data_init(char* name) {
|
ASTArgData* ast_arg_data_init(char* name) {
|
||||||
ASTArgData* arg = malloc(sizeof(ASTArgData));
|
ASTArgData* arg = malloc(sizeof(ASTArgData));
|
||||||
arg->name = name;
|
arg->name = name;
|
||||||
@@ -337,13 +203,6 @@ ASTArgData* ast_arg_data_init(char* name) {
|
|||||||
|
|
||||||
void ast_arg_data_destroy(ASTArgData* arg) { free(arg->name); }
|
void ast_arg_data_destroy(ASTArgData* arg) { free(arg->name); }
|
||||||
|
|
||||||
void ast_arg_print(ASTArgData* arg, int i) {
|
|
||||||
INDENT_BEGIN(i);
|
|
||||||
INDENT_TITLE("ASTArgData", arg);
|
|
||||||
INDENT_FIELD("name", "%s", arg->name);
|
|
||||||
INDENT_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
AST* ast_find(Scope* scope, char* name) {
|
AST* ast_find(Scope* scope, char* name) {
|
||||||
while (scope) {
|
while (scope) {
|
||||||
AST* gotten = htab_get(scope->here, name);
|
AST* gotten = htab_get(scope->here, name);
|
||||||
|
161
src/ast_print.c
Normal file
161
src/ast_print.c
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
#include "include/ast_print.h"
|
||||||
|
#include "include/ast.h"
|
||||||
|
#include "include/builtin.h"
|
||||||
|
#include "include/dstr.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static char* asttype_names[] = {
|
||||||
|
[AST_TYPE_CALL] = "FUNC CALL",
|
||||||
|
[AST_TYPE_NUM] = "NUMBER",
|
||||||
|
[AST_TYPE_VREF] = "VAR REFERENCE",
|
||||||
|
[AST_TYPE_VDEF] = "VAR DEFINITION",
|
||||||
|
[AST_TYPE_BLOCK] = "BLOCK",
|
||||||
|
[AST_TYPE_EXC] = "EXCEPTION",
|
||||||
|
[AST_TYPE_FDEF] = "FUNCTION DEFINITION",
|
||||||
|
[AST_TYPE_ARG] = "DEFINITION ARGUMENT",
|
||||||
|
[AST_TYPE_LAMBDA] = "LAMBDA EXPRESSION",
|
||||||
|
[AST_TYPE_BIF] = "BUILTIN FUNCTION"
|
||||||
|
};
|
||||||
|
|
||||||
|
void ast_print(AST* ast) {
|
||||||
|
if (!ast) return;
|
||||||
|
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("scope", "%p", ast->scope);
|
||||||
|
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;
|
||||||
|
case AST_TYPE_EXC: ast_exc_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_BLOCK: ast_block_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_FDEF: ast_fdef_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_ARG: ast_arg_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_LAMBDA: ast_lambda_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_BIF: ast_bif_print(ast->data, i + 2); break;
|
||||||
|
default: exit(1);
|
||||||
|
}
|
||||||
|
INDENT_FIELD_NONL_END;
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_num_print(ASTNumData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_FIELD("data", "%lf", *data);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_exc_print(ASTExcData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTExcData", data);
|
||||||
|
INDENT_FIELD("msg", "\"%s\"", data->msg);
|
||||||
|
if (data->trace == NULL) {
|
||||||
|
INDENT_FIELD("trace", "%p", NULL)
|
||||||
|
} else {
|
||||||
|
INDENT_FIELD_EXT_NONL_START("trace");
|
||||||
|
ast_print_i(data->trace, i + 1);
|
||||||
|
INDENT_FIELD_NONL_END;
|
||||||
|
}
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_call_print(ASTCallData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTCallData", data);
|
||||||
|
INDENT_FIELD("argc", "%ld", data->argc);
|
||||||
|
INDENT_FIELD_LIST("argv", data->argv, data->argc, ast_print_i);
|
||||||
|
INDENT_FIELD_EXT_NONL_START("exp");
|
||||||
|
ast_print_i(data->exp, i + 2);
|
||||||
|
INDENT_FIELD_NONL_END;
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
void ast_vdef_print(ASTVDefData* vdef, int depth) {
|
||||||
|
INDENT_BEGIN(depth);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTVDefData", vdef);
|
||||||
|
INDENT_FIELD("name", "%s", vdef->name);
|
||||||
|
INDENT_FIELD_EXT_NONL_START("exp");
|
||||||
|
ast_print_i(vdef->exp, depth + 2); // 2 because already indented.
|
||||||
|
INDENT_FIELD_NONL_END;
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_vref_print(ASTVrefData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTVrefData", data);
|
||||||
|
INDENT_FIELD("to", "%s", data->to);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_block_print(ASTBlockData* data, int depth) {
|
||||||
|
INDENT_BEGIN(depth);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTBlockData", data);
|
||||||
|
INDENT_FIELD("ln", "%ld", data->ln);
|
||||||
|
INDENT_FIELD_LIST("inside", data->inside, data->ln, ast_print_i);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_fdef_print(ASTFDefData* fdef, int i) {
|
||||||
|
INDENT_BEGIN(i)
|
||||||
|
INDENT_TITLE("ASTFDefData", fdef);
|
||||||
|
INDENT_FIELD("name", "%s", fdef->name);
|
||||||
|
INDENT_FIELD("argc", "%ld", fdef->argc);
|
||||||
|
INDENT_FIELD_LIST("argv", fdef->argv, fdef->argc, ast_print_i);
|
||||||
|
INDENT_FIELD_EXT_NONL_START("body");
|
||||||
|
ast_print_i(fdef->body, i + 2);
|
||||||
|
INDENT_FIELD_NONL_END;
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
void ast_arg_print(ASTArgData* arg, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
INDENT_TITLE("ASTArgData", arg);
|
||||||
|
INDENT_FIELD("name", "%s", arg->name);
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_lambda_print(ASTLambdaData* lambda, int i) {
|
||||||
|
INDENT_BEGIN(i)
|
||||||
|
INDENT_TITLE("ASTLambdaData", lambda);
|
||||||
|
INDENT_FIELD("parc", "%ld", lambda->parc);
|
||||||
|
INDENT_FIELD_LIST("parv", lambda->parv, lambda->parc, ast_print_i);
|
||||||
|
INDENT_FIELD_EXT_NONL_START("body");
|
||||||
|
ast_print_i(lambda->body, i + 2);
|
||||||
|
INDENT_FIELD_NONL_END;
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ast_bif_print(ASTBIFData* bif, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
INDENT_TITLE("ASTBIFData", bif);
|
||||||
|
|
||||||
|
char* name = "unknown";
|
||||||
|
|
||||||
|
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
||||||
|
if ((void*)BUILTIN_FNS[i].fn == bif) {
|
||||||
|
name = BUILTIN_FNS[i].name;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
INDENT_FIELD("name", "%s", name);
|
||||||
|
INDENT_END;
|
||||||
|
}
|
@@ -2,7 +2,7 @@
|
|||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h> // IWYU pragma: keep. Req by util macros.
|
||||||
|
|
||||||
DList* dlist_init(void) {
|
DList* dlist_init(void) {
|
||||||
DList* dlist = malloc(sizeof(DList));
|
DList* dlist = malloc(sizeof(DList));
|
||||||
|
59
src/exec.c
59
src/exec.c
@@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/builtin.h"
|
#include "include/builtin.h"
|
||||||
#include "include/dlist.h"
|
|
||||||
#include "include/exec.h"
|
#include "include/exec.h"
|
||||||
#include "include/htab.h"
|
#include "include/htab.h"
|
||||||
#include "include/scope.h"
|
#include "include/scope.h"
|
||||||
@@ -42,6 +41,8 @@ AST* exec_exp(AST* ast, Scope* parent) {
|
|||||||
case AST_TYPE_VREF: return exec_vref(ast, parent);
|
case AST_TYPE_VREF: return exec_vref(ast, parent);
|
||||||
case AST_TYPE_VDEF: return exec_vdef(ast, parent);
|
case AST_TYPE_VDEF: return exec_vdef(ast, parent);
|
||||||
case AST_TYPE_FDEF: return exec_fdef(ast, parent);
|
case AST_TYPE_FDEF: return exec_fdef(ast, parent);
|
||||||
|
case AST_TYPE_BIF:
|
||||||
|
case AST_TYPE_LAMBDA: return ast;
|
||||||
default: printf("what\n"); exit(1);
|
default: printf("what\n"); exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,49 +61,29 @@ AST* exec_block(AST* ast, Scope* parent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_call(AST* ast, Scope* parent) {
|
AST* exec_call(AST* ast, Scope* parent) {
|
||||||
log_dbg("Started call execution.");
|
ASTCallData* calldata = (ASTCallData*)ast->data;
|
||||||
ASTCallData* data = (ASTCallData*)ast->data;
|
|
||||||
size_t argc = data->argc;
|
|
||||||
AST** argv = data->argv;
|
|
||||||
char* fname = data->to;
|
|
||||||
|
|
||||||
ast->scope = parent;
|
AST* exp = exec_exp(calldata->exp, parent);
|
||||||
|
|
||||||
AST* fdef = ast_find(ast->scope, fname);
|
switch (exp->type) {
|
||||||
|
|
||||||
if (fdef == NULL)
|
|
||||||
return ast_init(
|
|
||||||
AST_TYPE_EXC, ast_exc_data_init("No such function found.", NULL)
|
|
||||||
);
|
|
||||||
|
|
||||||
switch (fdef->type) {
|
|
||||||
case AST_TYPE_BIF:
|
case AST_TYPE_BIF:
|
||||||
ASTBIFData bifdata = fdef->data;
|
ASTBIFData bifdata = exp->data;
|
||||||
return bifdata(argc, argv, parent);
|
return bifdata(calldata->argc, calldata->argv, parent);
|
||||||
case AST_TYPE_FDEF: return exec_cf(fdef, argc, argv);
|
case AST_TYPE_LAMBDA:
|
||||||
|
return exec_lambda(calldata->argc, calldata->argv, exp, parent);
|
||||||
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("Uncallable.", NULL)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_cf(AST* ast, size_t argc, AST** argv) {
|
|
||||||
Scope* callscope = scope_init(ast->scope);
|
|
||||||
ASTFDefData* fdef = (ASTFDefData*)ast->data;
|
|
||||||
for (int i = 0; i < argc; i++) {
|
|
||||||
char* key = ((ASTArgData*)fdef->argv[i]->data)->name;
|
|
||||||
AST* val = argv[i];
|
|
||||||
scope_add(callscope, key, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return exec_exp(fdef->body, callscope);
|
|
||||||
}
|
|
||||||
|
|
||||||
AST* exec_vdef(AST* ast, Scope* parent) {
|
AST* exec_vdef(AST* ast, Scope* parent) {
|
||||||
// Use parent's scope.
|
// Use parent's scope.
|
||||||
exec_inherit_scope(ast, parent);
|
exec_inherit_scope(ast, parent);
|
||||||
|
|
||||||
ASTVDefData* data = (ASTVDefData*)ast->data;
|
ASTVDefData* data = (ASTVDefData*)ast->data;
|
||||||
AST* val = data->val;
|
AST* val = data->exp;
|
||||||
char* key = data->name;
|
char* key = data->name;
|
||||||
scope_add(parent, key, val); // Add variable definition to parent scope.
|
scope_add(parent, key, val); // Add variable definition to parent scope.
|
||||||
return exec_exp(val, parent);
|
return exec_exp(val, parent);
|
||||||
@@ -132,14 +113,24 @@ AST* exec_vref(AST* ast, Scope* parent) {
|
|||||||
AST* exec_fdef(AST* ast, Scope* parent) {
|
AST* exec_fdef(AST* ast, Scope* parent) {
|
||||||
ast->scope = scope_init(parent);
|
ast->scope = scope_init(parent);
|
||||||
ASTFDefData* fdef = (ASTFDefData*)ast->data;
|
ASTFDefData* fdef = (ASTFDefData*)ast->data;
|
||||||
log_dbgf("IS THIS SUSPICIOUS??? %i", fdef->body->type);
|
|
||||||
AST* val = ast;
|
AST* val = ast;
|
||||||
char* key = fdef->name;
|
char* key = fdef->name;
|
||||||
scope_add(parent, key, val);
|
scope_add(parent, key, val);
|
||||||
// TODO: Create lambda functions.
|
|
||||||
return fdef->body; // Function definitions return function body.
|
return fdef->body; // Function definitions return function body.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AST* exec_lambda(size_t argc, AST** argv, AST* exp, Scope* parent) {
|
||||||
|
Scope* callscope = scope_init(parent);
|
||||||
|
ASTLambdaData* lambda = (ASTLambdaData*)exp->data;
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
char* key = ((ASTArgData*)lambda->parv[i]->data)->name;
|
||||||
|
AST* val = argv[i];
|
||||||
|
scope_add(callscope, key, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return exec_exp(lambda->body, callscope);
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
inline void exec_new_scope(AST* ast, Scope* inherit) {
|
||||||
|
2
src/gc.c
2
src/gc.c
@@ -48,7 +48,7 @@ void gc_hack_free() {
|
|||||||
((AST*)gc->p)->type, AST_TYPE_MAX, gc, gc->p
|
((AST*)gc->p)->type, AST_TYPE_MAX, gc, gc->p
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
ast_destroy_psv(gc->p);
|
ast_destroy(gc->p);
|
||||||
break;
|
break;
|
||||||
case GC_TYPE_SCOPE: scope_destroy_psv(gc->p); break;
|
case GC_TYPE_SCOPE: scope_destroy_psv(gc->p); break;
|
||||||
}
|
}
|
||||||
|
120
src/grammar.y
120
src/grammar.y
@@ -4,6 +4,7 @@
|
|||||||
#include "../../src/include/ast.h"
|
#include "../../src/include/ast.h"
|
||||||
#include "../../src/include/lexer.h"
|
#include "../../src/include/lexer.h"
|
||||||
#include "../../src/include/dlist.h"
|
#include "../../src/include/dlist.h"
|
||||||
|
#include "../../src/include/builtin.h"
|
||||||
|
|
||||||
int yylex(void);
|
int yylex(void);
|
||||||
void yyerror(char const*);
|
void yyerror(char const*);
|
||||||
@@ -14,6 +15,7 @@
|
|||||||
%code requires {
|
%code requires {
|
||||||
#include "../../src/include/ast.h"
|
#include "../../src/include/ast.h"
|
||||||
#include "../../src/include/dlist.h"
|
#include "../../src/include/dlist.h"
|
||||||
|
#include "../../src/include/builtin.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
%union {
|
%union {
|
||||||
@@ -47,6 +49,8 @@
|
|||||||
|
|
||||||
%token NL // Newline.
|
%token NL // Newline.
|
||||||
|
|
||||||
|
%token BACKSLASH
|
||||||
|
|
||||||
%left ADD SUB
|
%left ADD SUB
|
||||||
%left MUL DIV
|
%left MUL DIV
|
||||||
%precedence NEG
|
%precedence NEG
|
||||||
@@ -122,29 +126,81 @@ block:
|
|||||||
;
|
;
|
||||||
|
|
||||||
exp:
|
exp:
|
||||||
|
// Number.
|
||||||
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
|
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
|
||||||
|
|
||||||
// Function definitions.
|
// Variable reference.
|
||||||
| WORD GROUPS arg GROUPE EQ exp {
|
| WORD {
|
||||||
|
$$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call (general form).
|
||||||
|
| exp GROUPS arg GROUPE {
|
||||||
size_t argc = $3->ln;
|
size_t argc = $3->ln;
|
||||||
AST** argv = $3->buf;
|
AST** argv = $3->buf;
|
||||||
argarr_destroypsv($3);
|
argarr_destroypsv($3);
|
||||||
$$ = ast_init(AST_TYPE_FDEF, ast_fdef_data_init($1, argc, argv, $6));
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(
|
||||||
|
argc,
|
||||||
|
argv,
|
||||||
|
$1
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call (convenient form).
|
||||||
|
| WORD GROUPS arg GROUPE {
|
||||||
|
size_t argc = $3->ln;
|
||||||
|
AST** argv = $3->buf;
|
||||||
|
argarr_destroypsv($3);
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(
|
||||||
|
argc,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_VREF, ast_vref_data_init($1))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function definitions. Convert to VDef of Lambda.
|
||||||
|
| WORD GROUPS arg GROUPE exp {
|
||||||
|
size_t parc = $3->ln;
|
||||||
|
AST** parv = $3->buf;
|
||||||
|
argarr_destroypsv($3);
|
||||||
|
$$ = ast_init(AST_TYPE_VDEF, ast_vdef_data_init(
|
||||||
|
$1,
|
||||||
|
ast_init(AST_TYPE_LAMBDA, ast_lambda_data_init(
|
||||||
|
parc, parv, $5
|
||||||
|
))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lambda definitions.
|
||||||
|
| BACKSLASH GROUPS arg GROUPE exp {
|
||||||
|
size_t parc = $3->ln;
|
||||||
|
AST** parv = $3->buf;
|
||||||
|
argarr_destroypsv($3);
|
||||||
|
$$ = ast_init(AST_TYPE_LAMBDA, ast_lambda_data_init(parc, parv, $5));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block.
|
||||||
| BLOCKS block BLOCKE {
|
| BLOCKS block BLOCKE {
|
||||||
$$ = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $2->buf, $2->ln));
|
$$ = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $2->buf, $2->ln));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Negative.
|
||||||
| SUB exp {
|
| SUB exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
|
||||||
argv[1] = $2;
|
argv[1] = $2;
|
||||||
char* to = malloc(4);
|
$$ = ast_init(AST_TYPE_CALL,
|
||||||
strcpy(to, "mul");
|
ast_call_data_init(
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
2,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_BIF,
|
||||||
|
ast_bif_data_init(builtin_mul)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Group.
|
||||||
| GROUPS exp GROUPE { $$ = $2; }
|
| GROUPS exp GROUPE { $$ = $2; }
|
||||||
|
|
||||||
// Variable definition.
|
// Variable definition.
|
||||||
@@ -152,51 +208,55 @@ exp:
|
|||||||
$$ = ast_init(AST_TYPE_VDEF, ast_vdef_data_init($1, $3));
|
$$ = ast_init(AST_TYPE_VDEF, ast_vdef_data_init($1, $3));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variable reference.
|
|
||||||
| WORD {
|
|
||||||
$$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1));
|
|
||||||
}
|
|
||||||
|
|
||||||
| WORD GROUPS arg GROUPE {
|
|
||||||
size_t argc = $3->ln;
|
|
||||||
AST** argv = $3->buf;
|
|
||||||
argarr_destroypsv($3);
|
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, argc, argv));
|
|
||||||
}
|
|
||||||
|
|
||||||
| exp ADD exp {
|
| exp ADD exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $1;
|
argv[0] = $1;
|
||||||
argv[1] = $3;
|
argv[1] = $3;
|
||||||
char* to = malloc(4);
|
$$ = ast_init(AST_TYPE_CALL,
|
||||||
strcpy(to, "sum");
|
ast_call_data_init(
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
2,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_sum))
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
| exp SUB exp {
|
| exp SUB exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $1;
|
argv[0] = $1;
|
||||||
argv[1] = $3;
|
argv[1] = $3;
|
||||||
char* to = malloc(4);
|
$$ = ast_init(AST_TYPE_CALL,
|
||||||
strcpy(to, "sub");
|
ast_call_data_init(
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
2,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_sub))
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
| exp MUL exp {
|
| exp MUL exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $1;
|
argv[0] = $1;
|
||||||
argv[1] = $3;
|
argv[1] = $3;
|
||||||
char* to = malloc(4);
|
$$ = ast_init(AST_TYPE_CALL,
|
||||||
strcpy(to, "mul");
|
ast_call_data_init(
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
2,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_mul))
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
| exp DIV exp {
|
| exp DIV exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $1;
|
argv[0] = $1;
|
||||||
argv[1] = $3;
|
argv[1] = $3;
|
||||||
char* to = malloc(4);
|
$$ = ast_init(AST_TYPE_CALL,
|
||||||
strcpy(to, "div");
|
ast_call_data_init(
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
2,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_div))
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
%%
|
%%
|
||||||
|
@@ -24,6 +24,7 @@ typedef enum {
|
|||||||
AST_TYPE_VREF, // A variable reference.
|
AST_TYPE_VREF, // A variable reference.
|
||||||
AST_TYPE_BLOCK, // A block of code (scope).
|
AST_TYPE_BLOCK, // A block of code (scope).
|
||||||
AST_TYPE_FDEF, // A function definition.
|
AST_TYPE_FDEF, // A function definition.
|
||||||
|
AST_TYPE_LAMBDA, // An anonymous function definition.
|
||||||
AST_TYPE_ARG, // A definition argument.
|
AST_TYPE_ARG, // A definition argument.
|
||||||
AST_TYPE_MAX = AST_TYPE_ARG,
|
AST_TYPE_MAX = AST_TYPE_ARG,
|
||||||
} ASTType;
|
} ASTType;
|
||||||
@@ -39,14 +40,8 @@ typedef struct {
|
|||||||
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, Scope* scope);
|
AST* ast_init_scope(ASTType type, void* data, Scope* scope);
|
||||||
// Destroy an `AST`, recursively.
|
|
||||||
void ast_destroy(AST* ast);
|
|
||||||
// Destroy an `AST`.
|
// Destroy an `AST`.
|
||||||
void ast_destroy_psv(AST* ast);
|
void ast_destroy(AST* ast);
|
||||||
// Print an `AST`, recursively.
|
|
||||||
void ast_print(AST* ast);
|
|
||||||
// Helper function to `ast_print()`, where `i` is indentation level.
|
|
||||||
void ast_print_i(AST* ast, int i);
|
|
||||||
|
|
||||||
// A number.
|
// A number.
|
||||||
typedef double ASTNumData;
|
typedef double ASTNumData;
|
||||||
@@ -55,8 +50,6 @@ typedef double ASTNumData;
|
|||||||
ASTNumData* ast_num_data_init(double val);
|
ASTNumData* ast_num_data_init(double val);
|
||||||
// Destroy an `ASTNumData`.
|
// Destroy an `ASTNumData`.
|
||||||
void ast_num_data_destroy(ASTNumData* num);
|
void ast_num_data_destroy(ASTNumData* num);
|
||||||
// Print an `ASTNumData`.
|
|
||||||
void ast_num_print(ASTNumData*, int i);
|
|
||||||
|
|
||||||
// An exception.
|
// An exception.
|
||||||
typedef struct ASTEXCDATA {
|
typedef struct ASTEXCDATA {
|
||||||
@@ -67,8 +60,20 @@ typedef struct ASTEXCDATA {
|
|||||||
ASTExcData* ast_exc_data_init(const char* msg, AST* trace);
|
ASTExcData* ast_exc_data_init(const char* msg, AST* trace);
|
||||||
// Destroy an `ASTExecData`.
|
// Destroy an `ASTExecData`.
|
||||||
void ast_exc_data_destroy(ASTExcData* exc);
|
void ast_exc_data_destroy(ASTExcData* exc);
|
||||||
// Print an `ASTExecData`.
|
|
||||||
void ast_exc_print(ASTExcData*, int i);
|
// Argument list as anonymous struct.
|
||||||
|
#define ARGS \
|
||||||
|
struct { \
|
||||||
|
size_t argc; \
|
||||||
|
AST** argv; \
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parameter list as anonymous struct.
|
||||||
|
#define PARS \
|
||||||
|
struct { \
|
||||||
|
size_t parc; \
|
||||||
|
AST** parv; \
|
||||||
|
}
|
||||||
|
|
||||||
// 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, Scope* scope);
|
||||||
@@ -78,38 +83,40 @@ ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*));
|
|||||||
// Destroy an `ASTBIFData`.
|
// Destroy an `ASTBIFData`.
|
||||||
void ast_bif_data_destroy(ASTBIFData* bif);
|
void ast_bif_data_destroy(ASTBIFData* bif);
|
||||||
|
|
||||||
// A call (to a function).
|
// A lambda.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* to; // What the call's to.
|
PARS; // The parameters the lambda can accept.
|
||||||
size_t argc; // Argument count.
|
AST* body; // The body expression to be executed.
|
||||||
AST** argv; // Argument vector.
|
} ASTLambdaData;
|
||||||
|
|
||||||
|
// Creates a new `ASTLambdaData`.
|
||||||
|
ASTLambdaData* ast_lambda_data_init(size_t parc, AST** parv, AST* body);
|
||||||
|
// Destroy an `ASTLambdaData`.
|
||||||
|
void ast_lambda_data_destroy(ASTLambdaData*);
|
||||||
|
|
||||||
|
// A call.
|
||||||
|
typedef struct {
|
||||||
|
ARGS; // The arguments the call is made with.
|
||||||
|
AST* exp; // The expression the call is to.
|
||||||
} ASTCallData;
|
} ASTCallData;
|
||||||
|
|
||||||
// Create a new `ASTCallData`.
|
// Create a new `ASTCallData`.
|
||||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
|
ASTCallData* ast_call_data_init(size_t argc, AST** argv, AST* exp);
|
||||||
// Destroy an `ASTCallData` recursively.
|
|
||||||
void ast_call_data_destroy(ASTCallData* call);
|
|
||||||
// Destroy an `ASTCallData`.
|
// 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);
|
|
||||||
|
|
||||||
// A variable definition's data.
|
// A variable definition. Associates a name with an expression.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* name;
|
char* name;
|
||||||
AST* val;
|
AST* exp;
|
||||||
} ASTVDefData;
|
} ASTVDefData;
|
||||||
|
|
||||||
// Create a new `ASTVDefData`.
|
// Create a new `ASTVDefData`.
|
||||||
ASTVDefData* ast_vdef_data_init(char* name, AST* val);
|
ASTVDefData* ast_vdef_data_init(char* name, AST* exp);
|
||||||
// Destroys the `ASTVDefData`, `ASTVDefData->name`, and `ASTVDefData->val`.
|
|
||||||
void ast_vdef_data_destroy(ASTVDefData* vdef);
|
|
||||||
// Destroy an `ASTVDefData`.
|
// Destroy an `ASTVDefData`.
|
||||||
void ast_vdef_data_destroy_psv(ASTVDefData* vdef);
|
void ast_vdef_data_destroy(ASTVDefData* vdef);
|
||||||
// Print an `ASTVDefData`.
|
|
||||||
void ast_vdef_print(ASTVDefData*, int depth);
|
|
||||||
|
|
||||||
// A variable reference's data.
|
// A variable reference.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* to; // What the reference's to.
|
char* to; // What the reference's to.
|
||||||
} ASTVrefData;
|
} ASTVrefData;
|
||||||
@@ -118,8 +125,6 @@ typedef struct {
|
|||||||
ASTVrefData* ast_vref_data_init(char* to);
|
ASTVrefData* ast_vref_data_init(char* to);
|
||||||
// Destroy an `ASTVRefData`.
|
// Destroy an `ASTVRefData`.
|
||||||
void ast_vref_data_destroy(ASTVrefData* call);
|
void ast_vref_data_destroy(ASTVrefData* call);
|
||||||
// Print an `ASTVRefData`.
|
|
||||||
void ast_vref_print(ASTVrefData*, int i);
|
|
||||||
|
|
||||||
// A code block.
|
// A code block.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -133,13 +138,10 @@ ASTBlockData* ast_block_data_init(AST** inside, size_t ln);
|
|||||||
void ast_block_data_destroy(ASTBlockData* block);
|
void ast_block_data_destroy(ASTBlockData* block);
|
||||||
// Destroy an `ASTBlockData`.
|
// Destroy an `ASTBlockData`.
|
||||||
void ast_block_data_destroy_psv(ASTBlockData* block);
|
void ast_block_data_destroy_psv(ASTBlockData* block);
|
||||||
// Print an `ASTBlockData`.
|
|
||||||
void ast_block_print(ASTBlockData*, int i);
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* name; // Function name.
|
char* name; // Function name.
|
||||||
size_t argc; // Argument count.
|
ARGS; // Function args.
|
||||||
AST** argv; // Argument vector.
|
|
||||||
AST* body; // Function body.
|
AST* body; // Function body.
|
||||||
} ASTFDefData;
|
} ASTFDefData;
|
||||||
|
|
||||||
@@ -149,8 +151,6 @@ ASTFDefData* ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body);
|
|||||||
void ast_fdef_data_destroy(ASTFDefData* fdef);
|
void ast_fdef_data_destroy(ASTFDefData* fdef);
|
||||||
// Destroy an `ASTFDefData`.
|
// Destroy an `ASTFDefData`.
|
||||||
void ast_fdef_data_destroy_psv(ASTFDefData* fdef);
|
void ast_fdef_data_destroy_psv(ASTFDefData* fdef);
|
||||||
// Print an `ASTFDefData`.
|
|
||||||
void ast_fdef_print(ASTFDefData* fdef, int i);
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* name; // Argument name.
|
char* name; // Argument name.
|
||||||
@@ -160,10 +160,8 @@ typedef struct {
|
|||||||
ASTArgData* ast_arg_data_init(char* name);
|
ASTArgData* ast_arg_data_init(char* name);
|
||||||
// Destroy an `ASTArgData`.
|
// Destroy an `ASTArgData`.
|
||||||
void ast_arg_data_destroy(ASTArgData* arg);
|
void ast_arg_data_destroy(ASTArgData* arg);
|
||||||
// Print an `ASTArgData`.
|
|
||||||
void ast_arg_print(ASTArgData* arg, int i);
|
|
||||||
|
|
||||||
// Find a name in the scope.
|
// Find the expression associated with a name in the nearest scope.
|
||||||
AST* ast_find(Scope* scope, char* name);
|
AST* ast_find(Scope* scope, char* name);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
41
src/include/ast_print.h
Normal file
41
src/include/ast_print.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef AST_PRINT_H
|
||||||
|
#define AST_PRINT_H
|
||||||
|
|
||||||
|
#include "ast.h"
|
||||||
|
|
||||||
|
// Print an `AST`, recursively.
|
||||||
|
void ast_print(AST* ast);
|
||||||
|
// Helper function to `ast_print()`, where `i` is indentation level.
|
||||||
|
void ast_print_i(AST* ast, int i);
|
||||||
|
|
||||||
|
// Print an `ASTNumData`.
|
||||||
|
void ast_num_print(ASTNumData*, int i);
|
||||||
|
|
||||||
|
// Print an `ASTExecData`.
|
||||||
|
void ast_exc_print(ASTExcData*, int i);
|
||||||
|
|
||||||
|
// Print an `ASTCallData`.
|
||||||
|
void ast_call_print(ASTCallData*, int i);
|
||||||
|
|
||||||
|
// Print an `ASTVDefData`.
|
||||||
|
void ast_vdef_print(ASTVDefData*, int depth);
|
||||||
|
|
||||||
|
// Print an `ASTVRefData`.
|
||||||
|
void ast_vref_print(ASTVrefData*, int i);
|
||||||
|
|
||||||
|
// Print an `ASTBlockData`.
|
||||||
|
void ast_block_print(ASTBlockData*, int i);
|
||||||
|
|
||||||
|
// Print an `ASTFDefData`.
|
||||||
|
void ast_fdef_print(ASTFDefData* fdef, int i);
|
||||||
|
|
||||||
|
// Print an `ASTArgData`.
|
||||||
|
void ast_arg_print(ASTArgData* arg, int i);
|
||||||
|
|
||||||
|
// Print an `ASTLambdaData`.
|
||||||
|
void ast_lambda_print(ASTLambdaData* arg, int i);
|
||||||
|
|
||||||
|
// Print an `ASTBIFData`.
|
||||||
|
void ast_bif_print(ASTBIFData* arg, int i);
|
||||||
|
|
||||||
|
#endif
|
@@ -12,14 +12,14 @@ AST* exec_exp(AST* ast, Scope* parent);
|
|||||||
AST* exec_block(AST* ast, Scope* parent);
|
AST* exec_block(AST* ast, Scope* parent);
|
||||||
// Execute a call.
|
// Execute a call.
|
||||||
AST* exec_call(AST* ast, Scope* parent);
|
AST* exec_call(AST* ast, Scope* parent);
|
||||||
// Execute a custom function call.
|
|
||||||
AST* exec_cf(AST* ast, size_t argc, AST** argv);
|
|
||||||
// Execute a variable definition.
|
// Execute a variable definition.
|
||||||
AST* exec_vdef(AST* ast, Scope* parent);
|
AST* exec_vdef(AST* ast, Scope* parent);
|
||||||
// Execute a variable reference.
|
// Execute a variable reference.
|
||||||
AST* exec_vref(AST* ast, Scope* parent);
|
AST* exec_vref(AST* ast, Scope* parent);
|
||||||
// Execute a function definition.
|
// Execute a function definition.
|
||||||
AST* exec_fdef(AST* ast, Scope* parent);
|
AST* exec_fdef(AST* ast, Scope* parent);
|
||||||
|
// Execute a lambda expression.
|
||||||
|
AST* exec_lambda(size_t argc, AST** argv, AST* exp, Scope* parent);
|
||||||
// Print the result of an execution.
|
// Print the result of an execution.
|
||||||
void exec_print(double n);
|
void exec_print(double n);
|
||||||
|
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
#ifndef UTIL_H
|
#ifndef UTIL_H
|
||||||
#define UTIL_H
|
#define UTIL_H
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
// Most of this file is cursed printing macros for `ast_print()`. Do not attempt
|
// Most of this file is cursed printing macros for `ast_print()`. Do not attempt
|
||||||
// to comprehend.
|
// to comprehend.
|
||||||
|
|
||||||
@@ -10,6 +12,9 @@
|
|||||||
// Get the length of an array.
|
// Get the length of an array.
|
||||||
#define arrln(A) (sizeof(A)/sizeof(*A))
|
#define arrln(A) (sizeof(A)/sizeof(*A))
|
||||||
|
|
||||||
|
// Trap GDB &c.
|
||||||
|
#define TRAP() RAISE(SIGTRAP)
|
||||||
|
|
||||||
#ifdef DBG // Debug macros
|
#ifdef DBG // Debug macros
|
||||||
|
|
||||||
// Log a message.
|
// Log a message.
|
||||||
|
@@ -116,6 +116,7 @@ int yylex() {
|
|||||||
case '{': return BLOCKS;
|
case '{': return BLOCKS;
|
||||||
case '}': return BLOCKE;
|
case '}': return BLOCKE;
|
||||||
case '=': return EQ;
|
case '=': return EQ;
|
||||||
|
case '\\': return BACKSLASH;
|
||||||
default: fprintf(stderr, "Unexpected character: %c\n", c);
|
default: fprintf(stderr, "Unexpected character: %c\n", c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
|
#include "include/ast_print.h"
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
#include "include/exec.h"
|
#include "include/exec.h"
|
||||||
#include "include/gc.h"
|
#include "include/gc.h"
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h> // IWYU pragma: keep. Req by util macros.
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "include/stack.h"
|
#include "include/stack.h"
|
||||||
|
21
type-notes.md
Normal file
21
type-notes.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
Primitive Types
|
||||||
|
- num
|
||||||
|
- bool
|
||||||
|
- str
|
||||||
|
|
||||||
|
```
|
||||||
|
fact(x) = ? (x <= 1) 1 : f(x-1) * x
|
||||||
|
> lambda
|
||||||
|
fact(5) == 120
|
||||||
|
> True
|
||||||
|
```
|
||||||
|
|
||||||
|
Ordered collections.
|
||||||
|
| Name | Homogeneous | Fixed Size | Unique |
|
||||||
|
|---|---|---|---|
|
||||||
|
| Collection | No | No | No |
|
||||||
|
| List | Yes | No | No |
|
||||||
|
| Array | Yes | Yes | No |
|
||||||
|
| Group | No | Yes | No |
|
||||||
|
| Set | Yes | No | Yes |
|
||||||
|
| Perm | Yes | Yes | Yes |
|
Reference in New Issue
Block a user