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 | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 53 | bool *stack_slots; |
| 54 | int num_stack_slots, num_stack_entries; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 57 | void compile_expression(struct environment *env, struct local *local, |
| 58 | value_t val, dasm_State **Dst); |
| 59 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 60 | dasm_State **Dst); |
| 61 | int nextpc(struct local *local, dasm_State **Dst); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 62 | |
| 63 | // Local utilities |
| 64 | unsigned int local_alloc(struct local *local); |
| 65 | void local_free(struct local *local, unsigned int slot); |
| 66 | |
| 67 | void walk_and_alloc(struct local *local, value_t body); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 68 | // Compile top-level declaration |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 69 | void compile_tl(value_t val, struct environment *env); |
| 70 | struct environment compile_all(struct istream *is); |
| 71 | struct function *find_function(struct environment *env, char *name); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 72 | struct variable *add_variable(struct local *local, enum var_type type, |
| 73 | char *name, int number); |
| 74 | // Might return null |
| 75 | struct variable *find_variable(struct local *local, char *name); |
| 76 | void destroy_local(struct local *local); |