1
mod arithmetic;
2
mod comparison;
3
mod entity;
4
mod io;
5
mod list;
6
mod string;
7
mod structure;
8

            
9
use super::context::CompileContext;
10
use super::emit::FunctionEmitter;
11
use crate::ast::{Expr, WasmType};
12
use crate::error::{Error, Result};
13
use crate::runtime::SymbolTable;
14

            
15
pub(super) use io::compile_debug_effect;
16

            
17
67004
pub(super) fn call(symbols: &mut SymbolTable, name: &str, args: &[Expr]) -> Result<Expr> {
18
67004
    match name {
19
67004
        "+" => arithmetic::add(symbols, args),
20
41616
        "-" => arithmetic::sub(symbols, args),
21
39900
        "*" => arithmetic::mul(symbols, args),
22
39108
        "/" => arithmetic::div(symbols, args),
23
38888
        "MOD" => arithmetic::modulo(symbols, args),
24
38888
        "=" => comparison::num_cmp(symbols, args, "=", |a, b| a == b),
25
22944
        "/=" => comparison::num_neq(symbols, args),
26
22944
        "<" => comparison::num_cmp(symbols, args, "<", |a, b| a < b),
27
22900
        ">" => comparison::num_cmp(symbols, args, ">", |a, b| a > b),
28
22636
        "<=" => comparison::num_cmp(symbols, args, "<=", |a, b| a <= b),
29
22416
        ">=" => comparison::num_cmp(symbols, args, ">=", |a, b| a >= b),
30
20054
        "EQL" => comparison::eql(symbols, args),
31
19922
        "EQUAL" => comparison::equal(symbols, args),
32
19922
        "DEBUG" => io::debug_call(symbols, args),
33
19922
        "LIST" => list::list(symbols, args),
34
18910
        "CONS" => list::cons(symbols, args),
35
18602
        "CAR" => list::car(symbols, args),
36
17006
        "CDR" => list::cdr(symbols, args),
37
16296
        "MAP" => list::map_fn(symbols, args),
38
15768
        "MAKE-STRUCT-INSTANCE" => structure::make_struct_instance(symbols, args),
39
15636
        "STRUCT-FIELD" => structure::struct_field(symbols, args),
40
15636
        "STRUCT-P" => structure::struct_p(symbols, args),
41
15636
        "STRUCT-SET-FIELD" => structure::struct_set_field(symbols, args),
42
15592
        "UPCASE-STRING" => string::upcase_string(symbols, args),
43
15592
        "NOT" => comparison::not_fn(symbols, args),
44
14520
        "NULL?" => comparison::null_p(symbols, args),
45
13180
        "GET-INPUT-ENTITIES" => entity::get_input_entities(symbols, args),
46
13092
        "ENTITY-COUNT" => entity::entity_count(symbols, args),
47
9750
        "CONTEXT-TYPE" => entity::context_type(symbols, args),
48
9750
        "PRIMARY-ENTITY-TYPE" => entity::primary_entity_type(symbols, args),
49
9306
        "PRIMARY-ENTITY-IDX" => entity::primary_entity_idx(symbols, args),
50
8818
        "ENTITY-TYPE" => entity::entity_type(symbols, args),
51
5878
        "ENTITY-PARENT-IDX" => entity::entity_parent_idx(symbols, args),
52
4138
        "TRANSACTION-SPLIT-COUNT" => entity::transaction_split_count(symbols, args),
53
4006
        "TRANSACTION-TAG-COUNT" => entity::transaction_tag_count(symbols, args),
54
3874
        "TRANSACTION-IS-MULTI-CURRENCY" => entity::transaction_is_multi_currency(symbols, args),
55
3742
        "TRANSACTION-POST-DATE" => entity::transaction_post_date(symbols, args),
56
3654
        "TRANSACTION-ENTER-DATE" => entity::transaction_enter_date(symbols, args),
57
3566
        "SPLIT-VALUE-NUM" => entity::split_value_num(symbols, args),
58
3478
        "SPLIT-VALUE-DENOM" => entity::split_value_denom(symbols, args),
59
3390
        "SPLIT-VALUE" => entity::split_value(symbols, args),
60
3302
        "SPLIT-RECONCILE-STATE" => entity::split_reconcile_state(symbols, args),
61
3170
        "SPLIT-RECONCILE-DATE" => entity::split_reconcile_date(symbols, args),
62
3082
        "CREATE-TAG" => entity::create_tag(symbols, args),
63
3082
        "TAG-NAME" => entity::tag_name(symbols, args),
64
1340
        "TAG-VALUE" => entity::tag_value(symbols, args),
65
804
        "STRING=" => string::string_eq(symbols, args),
66
        "DELETE-ENTITY" => entity::delete_entity(symbols, args),
67
        _ => Err(Error::Compile(format!(
68
            "native function '{name}' not yet implemented"
69
        ))),
70
    }
71
67004
}
72

            
73
15524
pub(super) fn compile_for_stack(
74
15524
    ctx: &mut CompileContext,
75
15524
    emit: &mut FunctionEmitter,
76
15524
    symbols: &mut SymbolTable,
77
15524
    name: &str,
78
15524
    args: &[Expr],
79
15524
) -> Result<WasmType> {
80
15524
    match name {
81
15524
        "+" => arithmetic::compile_add_to_stack(ctx, emit, symbols, args),
82
13398
        "-" => arithmetic::compile_sub_to_stack(ctx, emit, symbols, args),
83
13354
        "*" => arithmetic::compile_mul_to_stack(ctx, emit, symbols, args),
84
13266
        "/" => arithmetic::compile_div_to_stack(ctx, emit, symbols, args),
85
13178
        "MOD" => arithmetic::compile_mod_to_stack(ctx, emit, symbols, args),
86
13178
        "=" => comparison::compile_eq_to_stack(ctx, emit, symbols, args),
87
10422
        "/=" => comparison::compile_neq_to_stack(ctx, emit, symbols, args),
88
10422
        "<" => comparison::compile_lt_to_stack(ctx, emit, symbols, args),
89
10422
        ">" => comparison::compile_gt_to_stack(ctx, emit, symbols, args),
90
10378
        "<=" => comparison::compile_le_to_stack(ctx, emit, symbols, args),
91
10378
        ">=" => comparison::compile_ge_to_stack(ctx, emit, symbols, args),
92
9178
        "NOT" => comparison::compile_not_to_stack(ctx, emit, symbols, args),
93
8374
        "NULL?" => comparison::compile_null_p_to_stack(ctx, emit, symbols, args),
94
7436
        "ENTITY-COUNT" => entity::compile_entity_count_to_stack(ctx, emit, symbols, args),
95
6368
        "CONTEXT-TYPE" => entity::compile_context_type_to_stack(ctx, emit, symbols, args),
96
6368
        "PRIMARY-ENTITY-TYPE" => {
97
222
            entity::compile_primary_entity_type_to_stack(ctx, emit, symbols, args)
98
        }
99
6146
        "PRIMARY-ENTITY-IDX" => {
100
354
            entity::compile_primary_entity_idx_to_stack(ctx, emit, symbols, args)
101
        }
102
5792
        "ENTITY-TYPE" => entity::compile_entity_type_to_stack(ctx, emit, symbols, args),
103
4812
        "ENTITY-PARENT-IDX" => entity::compile_entity_parent_idx_to_stack(ctx, emit, symbols, args),
104
3964
        "TRANSACTION-SPLIT-COUNT" => {
105
44
            entity::compile_transaction_split_count_to_stack(ctx, emit, symbols, args)
106
        }
107
3920
        "TRANSACTION-TAG-COUNT" => {
108
44
            entity::compile_transaction_tag_count_to_stack(ctx, emit, symbols, args)
109
        }
110
3876
        "TRANSACTION-IS-MULTI-CURRENCY" => {
111
44
            entity::compile_transaction_is_multi_currency_to_stack(ctx, emit, symbols, args)
112
        }
113
3832
        "TRANSACTION-POST-DATE" => {
114
44
            entity::compile_transaction_post_date_to_stack(ctx, emit, symbols, args)
115
        }
116
3788
        "TRANSACTION-ENTER-DATE" => {
117
44
            entity::compile_transaction_enter_date_to_stack(ctx, emit, symbols, args)
118
        }
119
3744
        "SPLIT-VALUE-NUM" => entity::compile_split_value_num_to_stack(ctx, emit, symbols, args),
120
3700
        "SPLIT-VALUE-DENOM" => entity::compile_split_value_denom_to_stack(ctx, emit, symbols, args),
121
3656
        "SPLIT-VALUE" => entity::compile_split_value_to_stack(ctx, emit, symbols, args),
122
3612
        "SPLIT-RECONCILE-STATE" => {
123
44
            entity::compile_split_reconcile_state_to_stack(ctx, emit, symbols, args)
124
        }
125
3568
        "SPLIT-RECONCILE-DATE" => {
126
44
            entity::compile_split_reconcile_date_to_stack(ctx, emit, symbols, args)
127
        }
128
3524
        "TAG-NAME" => entity::compile_tag_name_to_stack(ctx, emit, symbols, args),
129
2586
        "TAG-VALUE" => entity::compile_tag_value_to_stack(ctx, emit, symbols, args),
130
2050
        "STRING=" => string::compile_string_eq_to_stack(ctx, emit, symbols, args),
131
1648
        "GET-INPUT-ENTITIES" => entity::compile_get_input_entities_to_stack(ctx, emit),
132
1604
        "CONS" => list::compile_cons_to_stack(ctx, emit, symbols, args),
133
490
        "CAR" => list::compile_car_to_stack(ctx, emit, symbols, args),
134
178
        "CDR" => list::compile_cdr_to_stack(ctx, emit, symbols, args),
135
        _ => Err(Error::Compile(format!(
136
            "native function '{name}' cannot produce stack value"
137
        ))),
138
    }
139
15524
}
140

            
141
7180
pub(super) fn compile(
142
7180
    ctx: &mut CompileContext,
143
7180
    emit: &mut FunctionEmitter,
144
7180
    symbols: &mut SymbolTable,
145
7180
    name: &str,
146
7180
    args: &[Expr],
147
7180
) -> Result<()> {
148
7180
    match name {
149
7180
        "+" => arithmetic::compile_add(ctx, emit, symbols, args),
150
5288
        "-" => arithmetic::compile_sub(ctx, emit, symbols, args),
151
4760
        "*" => arithmetic::compile_mul(ctx, emit, symbols, args),
152
4320
        "/" => arithmetic::compile_div(ctx, emit, symbols, args),
153
4012
        "MOD" => arithmetic::compile_mod(ctx, emit, symbols, args),
154
3836
        "=" => comparison::compile_num_cmp(ctx, emit, symbols, args, "=", |a, b| a == b),
155
2956
        "/=" => comparison::compile_num_neq(ctx, emit, symbols, args),
156
2868
        "<" => comparison::compile_num_cmp(ctx, emit, symbols, args, "<", |a, b| a < b),
157
2692
        ">" => comparison::compile_num_cmp(ctx, emit, symbols, args, ">", |a, b| a > b),
158
2560
        "<=" => comparison::compile_num_cmp(ctx, emit, symbols, args, "<=", |a, b| a <= b),
159
2472
        ">=" => comparison::compile_num_cmp(ctx, emit, symbols, args, ">=", |a, b| a >= b),
160
2384
        "EQL" => comparison::compile_eql(ctx, emit, symbols, args),
161
2032
        "EQUAL" => comparison::compile_equal(ctx, emit, symbols, args),
162
1812
        "DEBUG" => io::compile_debug(ctx, emit, symbols, args),
163
1680
        "LIST" => list::compile_list(ctx, emit, symbols, args),
164
1504
        "CONS" => list::compile_cons(ctx, emit, symbols, args),
165
1328
        "CAR" => list::compile_car(ctx, emit, symbols, args),
166
1284
        "CDR" => list::compile_cdr(ctx, emit, symbols, args),
167
1284
        "MAP" => list::compile_map(ctx, emit, symbols, args),
168
1284
        "MAKE-STRUCT-INSTANCE" => structure::compile_make_struct_instance(ctx, emit, symbols, args),
169
1284
        "STRUCT-FIELD" => structure::compile_struct_field(ctx, emit, symbols, args),
170
1284
        "STRUCT-P" => structure::compile_struct_p(ctx, emit, symbols, args),
171
1284
        "STRUCT-SET-FIELD" => structure::compile_struct_set_field(ctx, emit, symbols, args),
172
1284
        "UPCASE-STRING" => string::compile_upcase_string(ctx, emit, symbols, args),
173
1284
        "GET-INPUT-ENTITIES" => entity::compile_get_input_entities(ctx, emit, symbols, args),
174
1284
        "NOT" => comparison::compile_not(ctx, emit, symbols, args),
175
1240
        "NULL?" => comparison::compile_null_p(ctx, emit, symbols, args),
176
1196
        "MAKE-STRUCT-RUNTIME" => structure::compile_make_struct_runtime(ctx, emit, symbols, args),
177
1196
        "ENTITY-COUNT" => entity::compile_entity_count(ctx, emit, symbols, args),
178
1108
        "CONTEXT-TYPE" => entity::compile_context_type(ctx, emit, symbols, args),
179
1064
        "PRIMARY-ENTITY-TYPE" => entity::compile_primary_entity_type(ctx, emit, symbols, args),
180
1020
        "PRIMARY-ENTITY-IDX" => entity::compile_primary_entity_idx(ctx, emit, symbols, args),
181
932
        "ENTITY-TYPE" => entity::compile_entity_type(ctx, emit, symbols, args),
182
888
        "ENTITY-PARENT-IDX" => entity::compile_entity_parent_idx(ctx, emit, symbols, args),
183
844
        "TRANSACTION-SPLIT-COUNT" => {
184
            entity::compile_transaction_split_count(ctx, emit, symbols, args)
185
        }
186
844
        "TRANSACTION-TAG-COUNT" => entity::compile_transaction_tag_count(ctx, emit, symbols, args),
187
844
        "TRANSACTION-IS-MULTI-CURRENCY" => {
188
            entity::compile_transaction_is_multi_currency(ctx, emit, symbols, args)
189
        }
190
844
        "TRANSACTION-POST-DATE" => entity::compile_transaction_post_date(ctx, emit, symbols, args),
191
844
        "TRANSACTION-ENTER-DATE" => {
192
            entity::compile_transaction_enter_date(ctx, emit, symbols, args)
193
        }
194
844
        "SPLIT-VALUE-NUM" => entity::compile_split_value_num(ctx, emit, symbols, args),
195
844
        "SPLIT-VALUE-DENOM" => entity::compile_split_value_denom(ctx, emit, symbols, args),
196
844
        "SPLIT-VALUE" => entity::compile_split_value(ctx, emit, symbols, args),
197
844
        "SPLIT-RECONCILE-STATE" => entity::compile_split_reconcile_state(ctx, emit, symbols, args),
198
844
        "SPLIT-RECONCILE-DATE" => entity::compile_split_reconcile_date(ctx, emit, symbols, args),
199
844
        "CREATE-TAG" => entity::compile_create_tag(ctx, emit, symbols, args),
200
132
        "TAG-NAME" => entity::compile_tag_name(ctx, emit, symbols, args),
201
132
        "TAG-VALUE" => entity::compile_tag_value(ctx, emit, symbols, args),
202
132
        "STRING=" => string::compile_string_eq(ctx, emit, symbols, args),
203
132
        "DELETE-ENTITY" => entity::compile_delete_entity(ctx, emit, symbols, args),
204
        _ => Err(Error::Compile(format!(
205
            "native function '{name}' not yet implemented"
206
        ))),
207
    }
208
7180
}