blob: 4d8ffba6a982e3a4e66d55afc69d2c67dbaad76d [file] [log] [blame]
swissChilica107a02021-04-14 12:07:30 -07001#pragma once
2
3#include "lisp.h"
swissChilif3e7f182021-04-20 13:57:22 -07004#include <dasm_proto.h>
swissChilica107a02021-04-14 12:07:30 -07005#include <stdbool.h>
swissChilib3ca4fb2021-04-20 10:33:00 -07006#include <stdint.h>
swissChilica107a02021-04-14 12:07:30 -07007
8struct function
9{
10 char *name;
11 int nargs; // number of arguments
12
13 union {
swissChili53472e82021-05-08 16:06:32 -070014 value_t (*def0)();
15 value_t (*def1)(value_t);
16 value_t (*def2)(value_t, value_t);
17 value_t (*def3)(value_t, value_t, value_t);
swissChilica107a02021-04-14 12:07:30 -070018 void *code_ptr;
swissChilif3e7f182021-04-20 13:57:22 -070019 uintptr_t code_addr;
swissChilica107a02021-04-14 12:07:30 -070020 };
21
22 struct function *prev;
23};
24
25struct environment
26{
27 struct function *first;
28};
29
swissChili923b5362021-05-09 20:31:43 -070030enum var_type
31{
32 V_BOUND, // Bound local variable
33 V_ARGUMENT, // Bound function argument
34 V_GLOBAL, // Global variable
35 V_FREE // Free (lexical) variable
36};
37
swissChilica107a02021-04-14 12:07:30 -070038struct variable
39{
40 char *name;
swissChili923b5362021-05-09 20:31:43 -070041 uintptr_t number;
42 enum var_type type;
swissChilica107a02021-04-14 12:07:30 -070043 struct variable *prev;
44};
45
46// local environment
47struct local
48{
swissChilica107a02021-04-14 12:07:30 -070049 int num_vars;
50 struct variable *first;
swissChili53472e82021-05-08 16:06:32 -070051 int npc;
52 int nextpc;
swissChilica107a02021-04-14 12:07:30 -070053};
54
swissChili53472e82021-05-08 16:06:32 -070055void compile_expression(struct environment *env, struct local *local,
56 value_t val, dasm_State **Dst);
57void compile_expr_to_func(struct environment *env, char *name, value_t val,
58 dasm_State **Dst);
59int nextpc(struct local *local, dasm_State **Dst);
swissChili8fc5e2f2021-04-22 13:45:10 -070060// Compile top-level declaration
swissChili53472e82021-05-08 16:06:32 -070061void compile_tl(value_t val, struct environment *env);
62struct environment compile_all(struct istream *is);
63struct function *find_function(struct environment *env, char *name);
swissChili923b5362021-05-09 20:31:43 -070064struct variable *add_variable(struct local *local, enum var_type type,
65 char *name, int number);
66// Might return null
67struct variable *find_variable(struct local *local, char *name);
68void destroy_local(struct local *local);