blob: ddf7ea0b3df3a5e5f1e7151abf1e9e89a9b98555 [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
swissChili2999dd12021-07-02 14:19:53 -07008enum namespace
9{
10 NS_FUNCTION,
11 NS_MACRO,
12};
13
swissChilica107a02021-04-14 12:07:30 -070014struct function
15{
16 char *name;
17 int nargs; // number of arguments
swissChili2999dd12021-07-02 14:19:53 -070018 enum namespace namespace;
swissChilica107a02021-04-14 12:07:30 -070019
swissChili2999dd12021-07-02 14:19:53 -070020 union
21 {
swissChili53472e82021-05-08 16:06:32 -070022 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);
swissChilica107a02021-04-14 12:07:30 -070026 void *code_ptr;
swissChilif3e7f182021-04-20 13:57:22 -070027 uintptr_t code_addr;
swissChilica107a02021-04-14 12:07:30 -070028 };
29
30 struct function *prev;
31};
32
33struct environment
34{
35 struct function *first;
36};
37
swissChili923b5362021-05-09 20:31:43 -070038enum 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
swissChilica107a02021-04-14 12:07:30 -070046struct variable
47{
48 char *name;
swissChili923b5362021-05-09 20:31:43 -070049 uintptr_t number;
50 enum var_type type;
swissChilica107a02021-04-14 12:07:30 -070051 struct variable *prev;
52};
53
54// local environment
55struct local
56{
swissChilica107a02021-04-14 12:07:30 -070057 int num_vars;
58 struct variable *first;
swissChili53472e82021-05-08 16:06:32 -070059 int npc;
60 int nextpc;
swissChili67bdf282021-06-06 18:46:08 -070061 bool *stack_slots;
62 int num_stack_slots, num_stack_entries;
swissChilica107a02021-04-14 12:07:30 -070063};
64
swissChili53472e82021-05-08 16:06:32 -070065void compile_expression(struct environment *env, struct local *local,
66 value_t val, dasm_State **Dst);
swissChili6b47b6d2021-06-30 22:08:55 -070067
68/**
69 * Compile a backquoted expression
70 */
71void compile_backquote(struct environment *env, struct local *local,
72 value_t val, dasm_State **Dst);
73
swissChili53472e82021-05-08 16:06:32 -070074void compile_expr_to_func(struct environment *env, char *name, value_t val,
75 dasm_State **Dst);
swissChili6b47b6d2021-06-30 22:08:55 -070076
swissChili53472e82021-05-08 16:06:32 -070077int nextpc(struct local *local, dasm_State **Dst);
swissChili67bdf282021-06-06 18:46:08 -070078
79// Local utilities
80unsigned int local_alloc(struct local *local);
81void local_free(struct local *local, unsigned int slot);
82
83void walk_and_alloc(struct local *local, value_t body);
swissChili8fc5e2f2021-04-22 13:45:10 -070084// Compile top-level declaration
swissChili53472e82021-05-08 16:06:32 -070085void compile_tl(value_t val, struct environment *env);
86struct environment compile_all(struct istream *is);
87struct function *find_function(struct environment *env, char *name);
swissChili923b5362021-05-09 20:31:43 -070088struct variable *add_variable(struct local *local, enum var_type type,
89 char *name, int number);
90// Might return null
91struct variable *find_variable(struct local *local, char *name);
92void destroy_local(struct local *local);
swissChili2999dd12021-07-02 14:19:53 -070093
94/**
95 * Like `apply` in lisp, calls func with list args and returns the result.
96 */
97value_t call_list(struct function *func, value_t list);