swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "lisp.h" |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 4 | #include <dasm_proto.h> |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 5 | #include <stdbool.h> |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 6 | #include <stdint.h> |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 7 | |
| 8 | struct function |
| 9 | { |
| 10 | char *name; |
| 11 | int nargs; // number of arguments |
| 12 | |
| 13 | union { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 14 | 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); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 18 | void *code_ptr; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 19 | uintptr_t code_addr; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | struct function *prev; |
| 23 | }; |
| 24 | |
| 25 | struct environment |
| 26 | { |
| 27 | struct function *first; |
| 28 | }; |
| 29 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 30 | enum 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 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 38 | struct variable |
| 39 | { |
| 40 | char *name; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 41 | uintptr_t number; |
| 42 | enum var_type type; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 43 | struct variable *prev; |
| 44 | }; |
| 45 | |
| 46 | // local environment |
| 47 | struct local |
| 48 | { |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 49 | int num_vars; |
| 50 | struct variable *first; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 51 | int npc; |
| 52 | int nextpc; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 55 | void compile_expression(struct environment *env, struct local *local, |
| 56 | value_t val, dasm_State **Dst); |
| 57 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 58 | dasm_State **Dst); |
| 59 | int nextpc(struct local *local, dasm_State **Dst); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 60 | // Compile top-level declaration |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 61 | void compile_tl(value_t val, struct environment *env); |
| 62 | struct environment compile_all(struct istream *is); |
| 63 | struct function *find_function(struct environment *env, char *name); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 64 | struct variable *add_variable(struct local *local, enum var_type type, |
| 65 | char *name, int number); |
| 66 | // Might return null |
| 67 | struct variable *find_variable(struct local *local, char *name); |
| 68 | void destroy_local(struct local *local); |