Compare commits
35 Commits
3f30662cde
...
master
Author | SHA1 | Date | |
---|---|---|---|
4e5b12b5b2 | |||
0e6bb7aa16 | |||
14ddf51f3c | |||
ac1d76361f | |||
b789193484 | |||
ab97f78fab | |||
7c0b212ab4 | |||
80c8038374 | |||
e3cd78e1b4 | |||
b7b90f528b | |||
0ef44be808 | |||
8924818ec4 | |||
73efa7e136 | |||
4e8d7131d6 | |||
bfce18ab81 | |||
7b648c4bd7 | |||
4ec5d1c075 | |||
5ba070ced7 | |||
8256643c0b | |||
29a217928e | |||
482f2b4877 | |||
36fd838a8f | |||
289243de38 | |||
67aafb3ead | |||
3b5bee0695 | |||
970fc39198 | |||
8d3e43d7dc | |||
d4293e87f3 | |||
abb8ff6b58 | |||
80122b6572 | |||
a4afd3b58a | |||
90c8c91410 | |||
f5ab0e9cb0 | |||
0fb1f1d55f | |||
1d83aa65a4 |
81
README.md
81
README.md
@@ -1,20 +1,20 @@
|
|||||||
# SCL: Simple CAS Language
|
# SCL: Simple CAS Language
|
||||||
|
|
||||||
Version v1.0-alpha
|
*v0.3*
|
||||||
|
|
||||||
SCL aims to be a human-friendly Computer Algebra System (CAS) inspired by
|
SCL aims to be a human-friendly Computer Algebra System (CAS) inspired by
|
||||||
[maxima](https://maxima.sourceforge.io/) that feels like writing on paper. In
|
[maxima](https://maxima.sourceforge.io/) that feels like writing on paper. In
|
||||||
its current state, SCL can be used as a basic 4-function calculator with order
|
its current state, SCL can be used as a functional programming language capable
|
||||||
of operations and local variables. The codebase is about 1,400 lines of C,
|
of performing simple arithmetic. The codebase is about 2,000 lines of
|
||||||
including a parser, interpreter, and runtime. It uses a linked environment
|
handwritten C, including a parser, interpreter, and runtime. It uses a linked
|
||||||
scoping model.
|
environment scoping model.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
To download and run:
|
To download and run:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://git.signorovitch.org/jacob/scl -b stable && cd scl
|
git clone https://git.signorovitch.org/scl/scl -b stable && cd scl
|
||||||
make release
|
make release
|
||||||
./scl.out
|
./scl.out
|
||||||
```
|
```
|
||||||
@@ -22,75 +22,44 @@ make release
|
|||||||
### For Development
|
### For Development
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone git@signorovitch.org:jacob/scl --recurse-submodules && cd scl
|
git clone git@signorovitch.org:scl/scl --recurse-submodules && cd scl
|
||||||
make all test
|
make all test
|
||||||
./scl.out
|
./scl.out
|
||||||
```
|
```
|
||||||
|
|
||||||
If you wish to run tests, make sure to run `git clone --recurse-submodules` to
|
If you wish to run tests, make sure to run `git clone --recurse-submodules` to
|
||||||
include the [Unity](https://github.com/ThrowTheSwitch/Unity) test framework.
|
include the [Unity](https://github.com/ThrowTheSwitch/Unity) test framework.
|
||||||
|
*Note that tests are currently in poor use. I hope to amend this in the future.*
|
||||||
|
|
||||||
## Syntax
|
## Syntax
|
||||||
|
|
||||||
As one would expect, you can evaluate simple infix expressions:
|
SCL's syntax will feel familiar to other functional programming languages.
|
||||||
|
|
||||||
```scl
|
```scl
|
||||||
> 1 + 1
|
> x = 3 + 3 * 3; x + 1
|
||||||
|
= 13
|
||||||
|
> f(x) x + 1
|
||||||
|
> f(1)
|
||||||
= 2
|
= 2
|
||||||
```
|
> (\(x) 2 * x)(5)
|
||||||
|
= 10
|
||||||
You can also define your own functions and variables:
|
> f(g) g(2)
|
||||||
|
> f(\(x) 2 * x)
|
||||||
```scl
|
|
||||||
> f(x) = 2x
|
|
||||||
> n = 3
|
|
||||||
> f(n)
|
|
||||||
= 6
|
|
||||||
```
|
|
||||||
|
|
||||||
As SCL uses a linked environment model for scope, arguments are passed by
|
|
||||||
reference by default. If you would like to pass by value (i.e., a copy) you may
|
|
||||||
use the syntax:
|
|
||||||
|
|
||||||
```scl
|
|
||||||
> f(x) = x = 1
|
|
||||||
> n = 4
|
|
||||||
> f($n) # Pass a copy of n.
|
|
||||||
= 1
|
|
||||||
> n
|
|
||||||
= 4
|
= 4
|
||||||
> f(n) # Pass a reference to n.
|
|
||||||
= 1
|
|
||||||
> n
|
|
||||||
> 1
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Symbolic algebra is done in the following manner:
|
Here's a simple factorial function, using recursion:
|
||||||
|
|
||||||
```scl
|
```scl
|
||||||
> f(x) = x^4
|
> factorial(n) {
|
||||||
> diff(f, x:sym, 2)
|
> if (n == 0) { 1 }
|
||||||
= 12x^2
|
> else { n * factorial(n - 1) }
|
||||||
|
> }
|
||||||
```
|
```
|
||||||
|
|
||||||
SCL will dynamically decide on types, but you can state them explicitly as
|
SCL's syntax is quite flexible. The above function could be more concisely
|
||||||
well:
|
written as:
|
||||||
|
|
||||||
```scl
|
```scl
|
||||||
> f(x: int): int = 2x
|
> factorial(n) ? n == 0 1 n * factorial(n - 1)
|
||||||
> f(3)
|
|
||||||
= 6
|
|
||||||
> f(3.1)
|
|
||||||
! Traceback:
|
|
||||||
! In call to `f(x: int): int`:
|
|
||||||
! TypeError (58): Argument `x` must be of type `int`.
|
|
||||||
```
|
|
||||||
|
|
||||||
Variables can be defined, with several attributes:
|
|
||||||
|
|
||||||
```scl
|
|
||||||
> a = 1 // Interpret type automatically.
|
|
||||||
> b:int = 1 // Must be int.
|
|
||||||
> c:const:int = 1 // Constant: value can never change.
|
|
||||||
> x:sym // Treated symbolicaly.
|
|
||||||
```
|
```
|
||||||
|
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
|
||||||
|
10
TODO.md
10
TODO.md
@@ -1,7 +1,9 @@
|
|||||||
0. Create file to describe properties of terminology used; param, arg, var, &c.
|
EXCEPTION HANDLING: exception ast type should have as data a giant enum of
|
||||||
1. Differenciate parameters and arguments -- params for function definitions,
|
possible types, rather than a char* message. A description of each type could be
|
||||||
arguments for function calls
|
handled under the exception type and print logic. For now, executor checks
|
||||||
2. Add scope field to all ASTs, and new scope layer for those that need it.
|
message for special exceptions e.g. exit().
|
||||||
|
|
||||||
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,2 @@
|
|||||||
f(n) = 2 * n
|
apply(f, x) f(x)
|
||||||
|
apply(\(x) x + 1, 2)
|
||||||
f(5)
|
|
||||||
|
196
src/ast.c
196
src/ast.c
@@ -1,30 +1,27 @@
|
|||||||
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/dstr.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 = malloc(sizeof(AST));
|
AST* ast = gc_alloc(sizeof(AST), GC_TYPE_AST);
|
||||||
|
|
||||||
ast->type = type;
|
ast->type = type;
|
||||||
ast->data = data;
|
ast->data = data;
|
||||||
ast->scope = NULL;
|
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;
|
return ast;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,59 +40,23 @@ void ast_destroy(AST* ast) {
|
|||||||
|
|
||||||
switch (ast->type) {
|
switch (ast->type) {
|
||||||
case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
|
case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
|
||||||
|
case AST_TYPE_BOOL: ast_bool_data_destroy(ast->data); break;
|
||||||
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_BLOCK: ast_block_data_destroy_psv(ast->data); break;
|
||||||
case AST_TYPE_FDEF: ast_fdef_data_destroy(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_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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_print(AST* ast) { 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);
|
||||||
|
|
||||||
@@ -106,46 +67,57 @@ 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) {
|
ASTBoolData* ast_bool_data_init(int val) {
|
||||||
INDENT_BEGIN(i);
|
talloc(ASTBoolData, bol);
|
||||||
|
|
||||||
INDENT_FIELD("data", "%lf", *data);
|
*bol = val;
|
||||||
|
|
||||||
INDENT_END;
|
return bol;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTExcData* ast_exc_data_init(char* msg, AST* trace) {
|
void ast_bool_data_destroy(ASTBoolData* bol) { free(bol); }
|
||||||
|
|
||||||
|
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;
|
||||||
data->trace = trace;
|
data->trace = trace;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_exc_print(ASTExcData* data, int i) {
|
void ast_exc_data_destroy(ASTExcData* exc) {
|
||||||
INDENT_BEGIN(i);
|
// `msg` is static, and `trace` will get freed in GC.
|
||||||
|
free(exc);
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
void ast_bif_data_destroy(ASTBIFData* bif) { return; }
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
@@ -154,49 +126,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_print(ASTCallData* data, int i) {
|
// VDef.
|
||||||
INDENT_BEGIN(i);
|
|
||||||
|
|
||||||
INDENT_TITLE("ASTCallData", data);
|
ASTVDefData* ast_vdef_data_init(char* name, AST* exp) {
|
||||||
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_print(ASTVDefData* vdef, int depth) {
|
// VRef.
|
||||||
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);
|
||||||
@@ -211,15 +161,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));
|
||||||
|
|
||||||
@@ -236,14 +177,9 @@ void ast_block_data_destroy(ASTBlockData* block) {
|
|||||||
free(block);
|
free(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_block_print(ASTBlockData* data, int depth) {
|
void ast_block_data_destroy_psv(ASTBlockData* block) {
|
||||||
INDENT_BEGIN(depth);
|
free(block->inside);
|
||||||
|
free(block);
|
||||||
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*
|
||||||
@@ -264,15 +200,10 @@ void ast_fdef_data_destroy(ASTFDefData* fdef) {
|
|||||||
ast_destroy(fdef->body);
|
ast_destroy(fdef->body);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_fdef_print(ASTFDefData* fdef, int i) {
|
void ast_fdef_data_destroy_psv(ASTFDefData* fdef) {
|
||||||
INDENT_BEGIN(i)
|
free(fdef->name);
|
||||||
INDENT_TITLE("ASTFDefData", fdef);
|
free(fdef->argv);
|
||||||
INDENT_FIELD("name", "%s", fdef->name);
|
free(fdef);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTArgData* ast_arg_data_init(char* name) {
|
ASTArgData* ast_arg_data_init(char* name) {
|
||||||
@@ -283,13 +214,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);
|
||||||
|
175
src/ast_print.c
Normal file
175
src/ast_print.c
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
#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_BOOL] = "BOOLEAN",
|
||||||
|
[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_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_BOOL:
|
||||||
|
printf(
|
||||||
|
"%s %s\n", INDENT_spacing->buf,
|
||||||
|
*(ASTBoolData*)ast->data ? "true" : "false"
|
||||||
|
);
|
||||||
|
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_bool_print(ASTBoolData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_FIELD("data", "%s", *data ? "true" : "false");
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
@@ -1,10 +1,10 @@
|
|||||||
#include "include/builtin.h"
|
|
||||||
#include "include/ast.h"
|
|
||||||
#include "include/exec.h"
|
|
||||||
#include "include/util.h"
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "include/ast.h"
|
||||||
|
#include "include/builtin.h"
|
||||||
|
#include "include/exec.h"
|
||||||
|
|
||||||
AST* builtin_sum(size_t argc, AST** argv, Scope* parent) {
|
AST* builtin_sum(size_t argc, AST** argv, Scope* parent) {
|
||||||
ASTNumData total = 0;
|
ASTNumData total = 0;
|
||||||
|
|
||||||
@@ -28,7 +28,8 @@ AST* builtin_sum(size_t argc, AST** argv, Scope* parent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_sub(size_t argc, AST** argv, Scope* parent) {
|
AST* builtin_sub(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
if (argc <= 0) return ast_init(AST_TYPE_NUM, ast_num_data_init(0));
|
||||||
|
|
||||||
AST* first = exec_exp(*argv, parent);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
@@ -63,7 +64,8 @@ AST* builtin_sub(size_t argc, AST** argv, Scope* parent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_mul(size_t argc, AST** argv, Scope* parent) {
|
AST* builtin_mul(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
if (argc <= 0) return ast_init(AST_TYPE_NUM, ast_num_data_init(0));
|
||||||
|
|
||||||
AST* first = exec_exp(*argv, parent);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
@@ -98,7 +100,8 @@ AST* builtin_mul(size_t argc, AST** argv, Scope* parent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AST* builtin_div(size_t argc, AST** argv, Scope* parent) {
|
AST* builtin_div(size_t argc, AST** argv, Scope* parent) {
|
||||||
log_dbg("Got here");
|
if (argc <= 0) return ast_init(AST_TYPE_NUM, ast_num_data_init(0));
|
||||||
|
|
||||||
AST* first = exec_exp(*argv, parent);
|
AST* first = exec_exp(*argv, parent);
|
||||||
if (first->type == AST_TYPE_EXC)
|
if (first->type == AST_TYPE_EXC)
|
||||||
return ast_init(
|
return ast_init(
|
||||||
@@ -131,3 +134,77 @@ AST* builtin_div(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_die(size_t argc, AST** argv, Scope* parent) {
|
||||||
|
return ast_init(AST_TYPE_EXC, ast_exc_data_init("8", NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
AST* builtin_if(size_t argc, AST** argv, Scope* parent) {
|
||||||
|
if (argc != 3)
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_EXC,
|
||||||
|
ast_exc_data_init("If invoked with too few args.", NULL)
|
||||||
|
);
|
||||||
|
|
||||||
|
AST* pred = exec_exp(argv[0], parent);
|
||||||
|
AST* body = argv[1];
|
||||||
|
AST* alt = argv[2];
|
||||||
|
|
||||||
|
if (pred->type != AST_TYPE_BOOL) {
|
||||||
|
if (pred->type == AST_TYPE_EXC) {
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_EXC, ast_exc_data_init("if touched an error", pred)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_EXC,
|
||||||
|
ast_exc_data_init("if works on booleans idiot", NULL)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*(ASTBoolData*)pred->data) return exec_exp(body, parent);
|
||||||
|
else return exec_exp(alt, parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
AST* builtin_eq(size_t argc, AST** argv, Scope* parent) {
|
||||||
|
if (argc < 1) return ast_init(AST_TYPE_EXC, ast_exc_data_init("bad", NULL));
|
||||||
|
else if (argc == 1) return ast_init(AST_TYPE_BOOL, ast_bool_data_init(1));
|
||||||
|
|
||||||
|
AST* first = exec_exp(argv[0], parent);
|
||||||
|
ASTType type = first->type;
|
||||||
|
|
||||||
|
AST* second = exec_exp(argv[1], parent);
|
||||||
|
if (first->type == AST_TYPE_EXC)
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_EXC, ast_exc_data_init("first was bad", first)
|
||||||
|
);
|
||||||
|
if (second->type == AST_TYPE_EXC)
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_EXC, ast_exc_data_init("second was bad", second)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (second->type != type)
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_EXC,
|
||||||
|
ast_exc_data_init("apples and oranges or something idk", NULL)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Later when I put together an anctual type system I'll have this
|
||||||
|
// delegated to each type. For now this works.
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case AST_TYPE_NUM:
|
||||||
|
if (*(ASTNumData*)first->data == *(ASTNumData*)second->data)
|
||||||
|
return ast_init(AST_TYPE_BOOL, ast_bool_data_init(1));
|
||||||
|
else return ast_init(AST_TYPE_BOOL, ast_bool_data_init(0));
|
||||||
|
case AST_TYPE_BOOL:
|
||||||
|
if (*(ASTNumData*)first->data == *(ASTNumData*)second->data)
|
||||||
|
return ast_init(AST_TYPE_BOOL, ast_bool_data_init(1));
|
||||||
|
else return ast_init(AST_TYPE_BOOL, ast_bool_data_init(0));
|
||||||
|
default:
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_BOOL, ast_bool_data_init(0)
|
||||||
|
); // Can't equate nonprimatives. I think. Maybe.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h> // IWYU pragma: keep. Req by util macros.
|
||||||
|
|
||||||
#include "include/dlist.h"
|
#include "include/dlist.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
DList* dlist_init(void) {
|
DList* dlist_init(void) {
|
||||||
DList* dlist = malloc(sizeof(DList));
|
DList* dlist = malloc(sizeof(DList));
|
||||||
|
|
||||||
|
90
src/exec.c
90
src/exec.c
@@ -12,11 +12,11 @@
|
|||||||
AST* exec_start(AST* ast) {
|
AST* exec_start(AST* ast) {
|
||||||
log_dbg("Started execution.");
|
log_dbg("Started execution.");
|
||||||
|
|
||||||
|
if (!ast) return ast;
|
||||||
|
|
||||||
Scope* global = scope_init(NULL);
|
Scope* global = scope_init(NULL);
|
||||||
global->uses = 1;
|
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->here, BUILTIN_FNS[i].name,
|
global->here, BUILTIN_FNS[i].name,
|
||||||
@@ -26,8 +26,7 @@ AST* exec_start(AST* ast) {
|
|||||||
log_dbg("Completed startup sequence.");
|
log_dbg("Completed startup sequence.");
|
||||||
|
|
||||||
AST* res = exec_exp(ast, global);
|
AST* res = exec_exp(ast, global);
|
||||||
log_dbgf("global addr %p uses %d", global, global->uses);
|
|
||||||
scope_destroy_psv(global);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,10 +34,19 @@ AST* exec_exp(AST* ast, Scope* parent) {
|
|||||||
switch (ast->type) {
|
switch (ast->type) {
|
||||||
case AST_TYPE_BLOCK: return exec_block(ast, parent);
|
case AST_TYPE_BLOCK: return exec_block(ast, parent);
|
||||||
case AST_TYPE_CALL: return exec_call(ast, parent);
|
case AST_TYPE_CALL: return exec_call(ast, parent);
|
||||||
case AST_TYPE_NUM: return ast;
|
case AST_TYPE_NUM:
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_NUM, ast_num_data_init(*(ASTNumData*)ast->data)
|
||||||
|
);
|
||||||
|
case AST_TYPE_BOOL:
|
||||||
|
return ast_init(
|
||||||
|
AST_TYPE_BOOL, ast_bool_data_init(*(ASTBoolData*)ast->data)
|
||||||
|
);
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,10 +54,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);
|
|
||||||
|
|
||||||
// HERE
|
|
||||||
exec_new_scope(ast, parent);
|
exec_new_scope(ast, parent);
|
||||||
|
|
||||||
// Loop through all but last ast.
|
// Loop through all but last ast.
|
||||||
@@ -61,52 +65,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.
|
||||||
// ast->scope = parent;
|
|
||||||
|
|
||||||
// HERE
|
|
||||||
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);
|
||||||
@@ -114,9 +95,6 @@ AST* exec_vdef(AST* ast, Scope* parent) {
|
|||||||
|
|
||||||
AST* exec_vref(AST* ast, Scope* parent) {
|
AST* exec_vref(AST* ast, Scope* parent) {
|
||||||
// Use parent's scope.
|
// Use parent's scope.
|
||||||
// ast->scope = parent;
|
|
||||||
|
|
||||||
// HERE
|
|
||||||
exec_inherit_scope(ast, parent);
|
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;
|
||||||
@@ -133,16 +111,29 @@ AST* exec_vref(AST* ast, Scope* parent) {
|
|||||||
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, ast->scope);
|
// return exec_exp(found, ast->scope);
|
||||||
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
AST* val = fdef->body;
|
AST* val = ast;
|
||||||
char* key = fdef->name;
|
char* key = fdef->name;
|
||||||
scope_add(parent, key, val);
|
scope_add(parent, key, val);
|
||||||
return val; // 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 = exec_exp(argv[i], parent);
|
||||||
|
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); }
|
||||||
@@ -153,7 +144,6 @@ inline void exec_new_scope(AST* ast, Scope* inherit) {
|
|||||||
|
|
||||||
// Update linked status.
|
// Update linked status.
|
||||||
scope->uses++;
|
scope->uses++;
|
||||||
// if (inherit) inherit->uses++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void exec_inherit_scope(AST* ast, Scope* inherit) {
|
inline void exec_inherit_scope(AST* ast, Scope* inherit) {
|
||||||
|
57
src/gc.c
Normal file
57
src/gc.c
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#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(gc->p);
|
||||||
|
break;
|
||||||
|
case GC_TYPE_SCOPE: scope_destroy_psv(gc->p); break;
|
||||||
|
}
|
||||||
|
gc_destroy(gc);
|
||||||
|
}
|
||||||
|
}
|
188
src/grammar.y
188
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 {
|
||||||
@@ -26,6 +28,12 @@
|
|||||||
|
|
||||||
%define parse.error verbose
|
%define parse.error verbose
|
||||||
|
|
||||||
|
%token BOOLT // Boolean true (TRUE or T).
|
||||||
|
%token BOOLF // Boolean false (FALSE or F).
|
||||||
|
|
||||||
|
%token IF // if or ?.
|
||||||
|
%token ELSE // else or :.
|
||||||
|
|
||||||
%token BLOCKS // Block start {.
|
%token BLOCKS // Block start {.
|
||||||
%token BLOCKE // Block end }.
|
%token BLOCKE // Block end }.
|
||||||
|
|
||||||
@@ -34,6 +42,7 @@
|
|||||||
%token SEP // Seperator ,.
|
%token SEP // Seperator ,.
|
||||||
|
|
||||||
%token EQ // Equals =.
|
%token EQ // Equals =.
|
||||||
|
%token DEQ // Double equals ==.
|
||||||
|
|
||||||
%token EXPSEP // Expression seperator ;.
|
%token EXPSEP // Expression seperator ;.
|
||||||
|
|
||||||
@@ -47,6 +56,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
|
||||||
@@ -85,6 +96,7 @@ inputend:
|
|||||||
%empty
|
%empty
|
||||||
| input {
|
| input {
|
||||||
root = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $1->buf, $1->ln));
|
root = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $1->buf, $1->ln));
|
||||||
|
dlist_destroypsv($1);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -121,28 +133,47 @@ 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)); }
|
||||||
|
|
||||||
//| BLOCKS exp BLOCKE { $$ = $2; }
|
| BOOLT { $$ = ast_init(AST_TYPE_BOOL, ast_bool_data_init(1)); }
|
||||||
|
| BOOLF { $$ = ast_init(AST_TYPE_BOOL, ast_bool_data_init(0)); }
|
||||||
|
|
||||||
| BLOCKS block BLOCKE {
|
| exp DEQ exp {
|
||||||
$$ = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $2->buf, $2->ln));
|
|
||||||
}
|
|
||||||
|
|
||||||
| 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] = $1;
|
||||||
argv[1] = $2;
|
argv[1] = $3;
|
||||||
char* to = malloc(4);
|
|
||||||
strcpy(to, "mul");
|
$$ = ast_init(AST_TYPE_CALL, 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_eq))
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
| GROUPS exp GROUPE { $$ = $2; }
|
| IF exp exp exp {
|
||||||
|
AST** argv = calloc(3, sizeof(AST*));
|
||||||
|
argv[0] = $2;
|
||||||
|
argv[1] = $3;
|
||||||
|
argv[2] = $4;
|
||||||
|
|
||||||
// Variable definition.
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(
|
||||||
| WORD EQ exp {
|
3,
|
||||||
$$ = ast_init(AST_TYPE_VDEF, ast_vdef_data_init($1, $3));
|
argv,
|
||||||
|
ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_if))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
| IF exp exp ELSE exp {
|
||||||
|
AST** argv = calloc(3, sizeof(AST*));
|
||||||
|
argv[0] = $2;
|
||||||
|
argv[1] = $3;
|
||||||
|
argv[2] = $5;
|
||||||
|
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(
|
||||||
|
3,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_if))
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variable reference.
|
// Variable reference.
|
||||||
@@ -150,53 +181,140 @@ exp:
|
|||||||
$$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1));
|
$$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call (general form).
|
||||||
|
| exp 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,
|
||||||
|
$1
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call (convenient form).
|
||||||
| WORD GROUPS arg GROUPE {
|
| WORD 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_CALL, ast_call_data_init($1, argc, argv));
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(
|
||||||
|
argc,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_VREF, ast_vref_data_init($1))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call (hacky convenient form).
|
||||||
|
| WORD GROUPS GROUPE {
|
||||||
|
size_t argc = 0;
|
||||||
|
AST** argv = NULL;
|
||||||
|
$$ = 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 {
|
||||||
|
$$ = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $2->buf, $2->ln));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Negative.
|
||||||
|
| SUB exp {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
|
||||||
|
argv[1] = $2;
|
||||||
|
$$ = ast_init(AST_TYPE_CALL,
|
||||||
|
ast_call_data_init(
|
||||||
|
2,
|
||||||
|
argv,
|
||||||
|
ast_init(AST_TYPE_BIF,
|
||||||
|
ast_bif_data_init(builtin_mul)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Group.
|
||||||
|
| GROUPS exp GROUPE { $$ = $2; }
|
||||||
|
|
||||||
|
// Variable definition.
|
||||||
|
| WORD EQ exp {
|
||||||
|
$$ = ast_init(AST_TYPE_VDEF, ast_vdef_data_init($1, $3));
|
||||||
}
|
}
|
||||||
|
|
||||||
| 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))
|
||||||
| 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));
|
|
||||||
}
|
}
|
||||||
%%
|
%%
|
||||||
|
@@ -7,7 +7,9 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
HTab* htab_init() {
|
HTab* htab_init() {
|
||||||
HTab* htab = malloc(sizeof(HTab));
|
HTab* htab = calloc(1, sizeof(HTab));
|
||||||
|
|
||||||
|
log_dbgf("HTAB %p", htab);
|
||||||
|
|
||||||
return htab;
|
return htab;
|
||||||
}
|
}
|
||||||
@@ -29,7 +31,6 @@ void* htab_get(HTab* htab, char* key) {
|
|||||||
|
|
||||||
void htab_ins(HTab* htab, char* key, void* data) {
|
void htab_ins(HTab* htab, char* key, void* data) {
|
||||||
size_t i = geti(key);
|
size_t i = geti(key);
|
||||||
// assert((*htab)[i] == NULL);
|
|
||||||
(*htab)[i] = data;
|
(*htab)[i] = data;
|
||||||
log_dbgf("Inserted something to hash table @ index %lu", i);
|
log_dbgf("Inserted something to hash table @ index %lu", i);
|
||||||
}
|
}
|
||||||
|
@@ -10,6 +10,7 @@ typedef enum {
|
|||||||
AST_TYPE_NUM, // A number (float).
|
AST_TYPE_NUM, // A number (float).
|
||||||
AST_TYPE_STR, // A string
|
AST_TYPE_STR, // A string
|
||||||
AST_TYPE_INT, // An integer.
|
AST_TYPE_INT, // An integer.
|
||||||
|
AST_TYPE_BOOL, // A boolean.
|
||||||
AST_TYPE_SYM, // A symbol.
|
AST_TYPE_SYM, // A symbol.
|
||||||
AST_TYPE_EXC, // Exception.
|
AST_TYPE_EXC, // Exception.
|
||||||
|
|
||||||
@@ -24,8 +25,9 @@ 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_FDEF,
|
AST_TYPE_MAX = AST_TYPE_ARG,
|
||||||
} ASTType;
|
} ASTType;
|
||||||
|
|
||||||
// An Abstract Syntax Tree.
|
// An Abstract Syntax Tree.
|
||||||
@@ -39,12 +41,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.
|
// Destroy an `AST`.
|
||||||
void ast_destroy(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;
|
||||||
@@ -53,57 +51,81 @@ 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);
|
// A boolean.
|
||||||
|
typedef int ASTBoolData;
|
||||||
|
|
||||||
|
// Create a new `ASTBoolData`.
|
||||||
|
ASTBoolData* ast_bool_data_init(int val);
|
||||||
|
// Destroy an `ASTBoolData`.
|
||||||
|
void ast_bool_data_destroy(ASTBoolData* bol);
|
||||||
|
|
||||||
// An exception.
|
// An exception.
|
||||||
typedef struct ASTEXCDATA {
|
typedef struct ASTEXCDATA {
|
||||||
char* msg; // The exception message.
|
const char* msg; // The exception message.
|
||||||
AST* trace; // The previous exception.
|
AST* trace; // The previous exception.
|
||||||
} ASTExcData;
|
} ASTExcData;
|
||||||
// Create a new `ASTExecData.
|
// Create a new `ASTExecData. `msg` should be static.
|
||||||
ASTExcData* ast_exc_data_init(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);
|
||||||
|
|
||||||
// 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**, Scope*));
|
||||||
|
// Destroy an `ASTBIFData`.
|
||||||
|
void ast_bif_data_destroy(ASTBIFData* bif);
|
||||||
|
|
||||||
// There is no `ASTBIFData` destroy function, as function pointers are immortal.
|
// A lambda.
|
||||||
|
|
||||||
// A call (to a function).
|
|
||||||
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`.
|
// Destroy an `ASTCallData`.
|
||||||
void ast_call_data_destroy(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`.
|
// Destroy an `ASTVDefData`.
|
||||||
void ast_vdef_data_destroy(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;
|
||||||
@@ -112,8 +134,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 {
|
||||||
@@ -125,22 +145,21 @@ typedef struct {
|
|||||||
ASTBlockData* ast_block_data_init(AST** inside, size_t ln);
|
ASTBlockData* ast_block_data_init(AST** inside, size_t ln);
|
||||||
// Destroy an `ASTBlockData`, recursively.
|
// Destroy an `ASTBlockData`, recursively.
|
||||||
void ast_block_data_destroy(ASTBlockData* block);
|
void ast_block_data_destroy(ASTBlockData* block);
|
||||||
// Print an `ASTBlockData`.
|
// Destroy an `ASTBlockData`.
|
||||||
void ast_block_print(ASTBlockData*, int i);
|
void ast_block_data_destroy_psv(ASTBlockData* block);
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
// Create a new `ASTFDefData`.
|
// Create a new `ASTFDefData`.
|
||||||
ASTFDefData* ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body);
|
ASTFDefData* ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body);
|
||||||
// Destroy an `ASTFDefData`.
|
// Destroy an `ASTFDefData`, recursively.
|
||||||
void ast_fdef_data_destroy(ASTFDefData* fdef);
|
void ast_fdef_data_destroy(ASTFDefData* fdef);
|
||||||
// Print an `ASTFDefData`.
|
// Destroy an `ASTFDefData`.
|
||||||
void ast_fdef_print(ASTFDefData* fdef, int i);
|
void ast_fdef_data_destroy_psv(ASTFDefData* fdef);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* name; // Argument name.
|
char* name; // Argument name.
|
||||||
@@ -150,10 +169,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
|
||||||
|
44
src/include/ast_print.h
Normal file
44
src/include/ast_print.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#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 `ASTBoolData`.
|
||||||
|
void ast_bool_print(ASTBoolData*, 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
|
@@ -2,6 +2,7 @@
|
|||||||
#define BUILTIN_H
|
#define BUILTIN_H
|
||||||
|
|
||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
|
#include <stddef.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, Scope* parent);
|
||||||
@@ -15,6 +16,15 @@ AST* builtin_mul(size_t argc, AST** argv, Scope* parent);
|
|||||||
// Divide nums.
|
// Divide nums.
|
||||||
AST* builtin_div(size_t argc, AST** argv, Scope* parent);
|
AST* builtin_div(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
|
// Die.
|
||||||
|
AST* builtin_die(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
|
// If statement.
|
||||||
|
AST* builtin_if(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
|
// Equality.
|
||||||
|
AST* builtin_eq(size_t argc, AST** argv, Scope* parent);
|
||||||
|
|
||||||
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, Scope* parent);
|
||||||
@@ -25,6 +35,9 @@ static struct builtin_data BUILTIN_FNS[] = {
|
|||||||
{ "sub", builtin_sub },
|
{ "sub", builtin_sub },
|
||||||
{ "mul", builtin_mul },
|
{ "mul", builtin_mul },
|
||||||
{ "div", builtin_div },
|
{ "div", builtin_div },
|
||||||
|
{ "die", builtin_die },
|
||||||
|
{"_if", builtin_if},
|
||||||
|
{"eq", builtin_eq},
|
||||||
};
|
};
|
||||||
#define BUILTIN_FNS_LN (arrln(BUILTIN_FNS))
|
#define BUILTIN_FNS_LN (arrln(BUILTIN_FNS))
|
||||||
|
|
||||||
|
@@ -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);
|
||||||
|
|
||||||
|
31
src/include/gc.h
Normal file
31
src/include/gc.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#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
|
@@ -7,7 +7,7 @@
|
|||||||
typedef struct SCOPE_T {
|
typedef struct SCOPE_T {
|
||||||
HTab* here; // This scope's hash table.
|
HTab* here; // This scope's hash table.
|
||||||
struct SCOPE_T* inherit; // The scope to inherit from.
|
struct SCOPE_T* inherit; // The scope to inherit from.
|
||||||
int uses; // How many `AST`s are linked to this scope.
|
int uses; // How many `AST`s are linked to this scope. // TODO: REMOVE.
|
||||||
} Scope;
|
} Scope;
|
||||||
|
|
||||||
// Create a new `Scope`. Creates new empty `HTab` for current scope.
|
// Create a new `Scope`. Creates new empty `HTab` for current scope.
|
||||||
|
@@ -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.
|
||||||
|
39
src/lexer.c
39
src/lexer.c
@@ -3,6 +3,7 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
#include "include/lexer.h"
|
#include "include/lexer.h"
|
||||||
@@ -26,7 +27,7 @@ void argarr_destroy(ArgArr* argarr) {
|
|||||||
void argarr_destroypsv(ArgArr* argarr) { free(argarr); }
|
void argarr_destroypsv(ArgArr* argarr) { free(argarr); }
|
||||||
|
|
||||||
void argarr_add(ArgArr* argarr, AST* arg) {
|
void argarr_add(ArgArr* argarr, AST* arg) {
|
||||||
if ((argarr->ln + 1) * argarr->sz > argarr->sz) {
|
if ((argarr->ln + 1) * sizeof(AST*) > argarr->sz) {
|
||||||
argarr->sz *= 2;
|
argarr->sz *= 2;
|
||||||
argarr->buf = realloc(argarr->buf, argarr->sz);
|
argarr->buf = realloc(argarr->buf, argarr->sz);
|
||||||
log_dbgf(
|
log_dbgf(
|
||||||
@@ -72,7 +73,20 @@ double acc_float(int c) {
|
|||||||
|
|
||||||
char* acc_word(int c) {
|
char* acc_word(int c) {
|
||||||
Dstr* val = dstr_init();
|
Dstr* val = dstr_init();
|
||||||
while (isalpha(*inp)) {
|
while (isalpha(*inp) || *inp == '_') {
|
||||||
|
dstr_appendch(val, *(inp - 1));
|
||||||
|
inp++;
|
||||||
|
}
|
||||||
|
dstr_appendch(val, *(inp - 1));
|
||||||
|
|
||||||
|
char* ret = val->buf;
|
||||||
|
dstr_destroypsv(val);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* acc_deq(int c) {
|
||||||
|
Dstr* val = dstr_init();
|
||||||
|
while (*inp == '=') {
|
||||||
dstr_appendch(val, *(inp - 1));
|
dstr_appendch(val, *(inp - 1));
|
||||||
inp++;
|
inp++;
|
||||||
}
|
}
|
||||||
@@ -98,8 +112,22 @@ int yylex() {
|
|||||||
return NUM;
|
return NUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isalpha(c)) {
|
if (c == '=') {
|
||||||
|
yylval.strval = acc_deq(c);
|
||||||
|
if (!strcmp(yylval.strval, "=")) return EQ;
|
||||||
|
if (!strcmp(yylval.strval, "==")) return DEQ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isalpha(c) || c == '_') {
|
||||||
yylval.strval = acc_word(c);
|
yylval.strval = acc_word(c);
|
||||||
|
|
||||||
|
if (!strcmp(yylval.strval, "TRUE") || !strcmp(yylval.strval, "T"))
|
||||||
|
return BOOLT;
|
||||||
|
if (!strcmp(yylval.strval, "FALSE") || !strcmp(yylval.strval, "F"))
|
||||||
|
return BOOLF;
|
||||||
|
if (!strcmp(yylval.strval, "if")) return IF;
|
||||||
|
if (!strcmp(yylval.strval, "else")) return ELSE;
|
||||||
|
|
||||||
return WORD;
|
return WORD;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +143,10 @@ int yylex() {
|
|||||||
case ';': return EXPSEP;
|
case ';': return EXPSEP;
|
||||||
case '{': return BLOCKS;
|
case '{': return BLOCKS;
|
||||||
case '}': return BLOCKE;
|
case '}': return BLOCKE;
|
||||||
case '=': return EQ;
|
// case '=': return EQ;
|
||||||
|
case '\\': return BACKSLASH;
|
||||||
|
case '?': return IF;
|
||||||
|
case ':': return ELSE;
|
||||||
default: fprintf(stderr, "Unexpected character: %c\n", c);
|
default: fprintf(stderr, "Unexpected character: %c\n", c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
src/main.c
24
src/main.c
@@ -2,9 +2,10 @@
|
|||||||
#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/lexer.h"
|
#include "include/lexer.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
|
|
||||||
@@ -19,8 +20,12 @@ 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(root);
|
||||||
ast_destroy(root);
|
AST* eval = exec_start(root);
|
||||||
|
ast_print(eval);
|
||||||
|
// ast_destroy(eval);
|
||||||
|
// ast_destroy(root);
|
||||||
|
gc_hack_free();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,9 +60,16 @@ int main(int argc, char** argv) {
|
|||||||
ast_print(root);
|
ast_print(root);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ast_print(exec_start(root));
|
AST* eval = exec_start(root);
|
||||||
|
ast_print(eval);
|
||||||
ast_destroy(root);
|
// Awful hack to exit when die() is called, until proper exception
|
||||||
|
// handling is implemented. TODO TODO TODO PLSFIX.
|
||||||
|
if (eval->type == AST_TYPE_EXC &&
|
||||||
|
((ASTExcData*)eval->data)->msg[0] == '8') {
|
||||||
|
gc_hack_free();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
gc_hack_free();
|
||||||
}
|
}
|
||||||
|
|
||||||
dstr_destroy(ln);
|
dstr_destroy(ln);
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
#include "include/scope.h"
|
#include "include/scope.h"
|
||||||
|
#include "include/gc.h"
|
||||||
#include "include/htab.h"
|
#include "include/htab.h"
|
||||||
#include "include/util.h"
|
#include "include/util.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
Scope* scope_init(Scope* inherit) {
|
Scope* scope_init(Scope* inherit) {
|
||||||
Scope* scope = malloc(sizeof(Scope));
|
Scope* scope = gc_alloc(sizeof(Scope), GC_TYPE_SCOPE);
|
||||||
|
|
||||||
scope->here = htab_init();
|
scope->here = htab_init();
|
||||||
scope->inherit = inherit;
|
scope->inherit = inherit;
|
||||||
@@ -24,12 +25,11 @@ void scope_destroy(Scope* scope) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void scope_destroy_psv(Scope* scope) {
|
void scope_destroy_psv(Scope* scope) {
|
||||||
log_dbgf("%p got here", scope);
|
|
||||||
if (!scope) return;
|
if (!scope) return;
|
||||||
|
log_dbgf("%p: destroying", scope);
|
||||||
htab_destroy(scope->here);
|
htab_destroy(scope->here);
|
||||||
scope->inherit = NULL;
|
scope->inherit = NULL;
|
||||||
free(scope);
|
free(scope);
|
||||||
log_dbgf("%p got here 2", scope);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void scope_add(Scope* scope, char* key, void* val) {
|
inline void scope_add(Scope* scope, char* key, void* val) {
|
||||||
|
@@ -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