blob: 1cbd92d39eed00098aa807bf9bb28f00dbc7c9f5 [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;
swissChili67bdf282021-06-06 18:46:08 -070053 bool *stack_slots;
54 int num_stack_slots, num_stack_entries;
swissChilica107a02021-04-14 12:07:30 -070055};
56
swissChili53472e82021-05-08 16:06:32 -070057void compile_expression(struct environment *env, struct local *local,
58 value_t val, dasm_State **Dst);
swissChili6b47b6d2021-06-30 22:08:55 -070059
60/**
61 * Compile a backquoted expression
62 */
63void compile_backquote(struct environment *env, struct local *local,
64 value_t val, dasm_State **Dst);
65
swissChili53472e82021-05-08 16:06:32 -070066void compile_expr_to_func(struct environment *env, char *name, value_t val,
67 dasm_State **Dst);
swissChili6b47b6d2021-06-30 22:08:55 -070068
swissChili53472e82021-05-08 16:06:32 -070069int nextpc(struct local *local, dasm_State **Dst);
swissChili67bdf282021-06-06 18:46:08 -070070
71// Local utilities
72unsigned int local_alloc(struct local *local);
73void local_free(struct local *local, unsigned int slot);
74
75void walk_and_alloc(struct local *local, value_t body);
swissChili8fc5e2f2021-04-22 13:45:10 -070076// Compile top-level declaration
swissChili53472e82021-05-08 16:06:32 -070077void compile_tl(value_t val, struct environment *env);
78struct environment compile_all(struct istream *is);
79struct function *find_function(struct environment *env, char *name);
swissChili923b5362021-05-09 20:31:43 -070080struct variable *add_variable(struct local *local, enum var_type type,
81 char *name, int number);
82// Might return null
83struct variable *find_variable(struct local *local, char *name);
84void destroy_local(struct local *local);