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 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 8 | enum namespace |
| 9 | { |
| 10 | NS_FUNCTION, |
| 11 | NS_MACRO, |
| 12 | }; |
| 13 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 14 | struct function |
| 15 | { |
| 16 | char *name; |
| 17 | int nargs; // number of arguments |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 18 | enum namespace namespace; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 19 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 20 | union |
| 21 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 22 | value_t (*def0)(); |
| 23 | value_t (*def1)(value_t); |
| 24 | value_t (*def2)(value_t, value_t); |
| 25 | value_t (*def3)(value_t, value_t, value_t); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 26 | void *code_ptr; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 27 | uintptr_t code_addr; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | struct function *prev; |
| 31 | }; |
| 32 | |
| 33 | struct environment |
| 34 | { |
| 35 | struct function *first; |
| 36 | }; |
| 37 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 38 | enum var_type |
| 39 | { |
| 40 | V_BOUND, // Bound local variable |
| 41 | V_ARGUMENT, // Bound function argument |
| 42 | V_GLOBAL, // Global variable |
| 43 | V_FREE // Free (lexical) variable |
| 44 | }; |
| 45 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 46 | struct variable |
| 47 | { |
| 48 | char *name; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 49 | uintptr_t number; |
| 50 | enum var_type type; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 51 | struct variable *prev; |
| 52 | }; |
| 53 | |
| 54 | // local environment |
| 55 | struct local |
| 56 | { |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 57 | int num_vars; |
| 58 | struct variable *first; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 59 | int npc; |
| 60 | int nextpc; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 61 | bool *stack_slots; |
| 62 | int num_stack_slots, num_stack_entries; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 65 | void compile_expression(struct environment *env, struct local *local, |
| 66 | value_t val, dasm_State **Dst); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Compile a backquoted expression |
| 70 | */ |
| 71 | void compile_backquote(struct environment *env, struct local *local, |
| 72 | value_t val, dasm_State **Dst); |
| 73 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 74 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 75 | dasm_State **Dst); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 76 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 77 | int nextpc(struct local *local, dasm_State **Dst); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 78 | |
| 79 | // Local utilities |
| 80 | unsigned int local_alloc(struct local *local); |
| 81 | void local_free(struct local *local, unsigned int slot); |
| 82 | |
| 83 | void walk_and_alloc(struct local *local, value_t body); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 84 | // Compile top-level declaration |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 85 | void compile_tl(value_t val, struct environment *env); |
| 86 | struct environment compile_all(struct istream *is); |
| 87 | struct function *find_function(struct environment *env, char *name); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 88 | struct variable *add_variable(struct local *local, enum var_type type, |
| 89 | char *name, int number); |
| 90 | // Might return null |
| 91 | struct variable *find_variable(struct local *local, char *name); |
| 92 | void destroy_local(struct local *local); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Like `apply` in lisp, calls func with list args and returns the result. |
| 96 | */ |
| 97 | value_t call_list(struct function *func, value_t list); |