-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpr_jit.cpp
More file actions
2349 lines (1960 loc) · 50 KB
/
expr_jit.cpp
File metadata and controls
2349 lines (1960 loc) · 50 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include "expr_jit.h"
#if EXPR_JIT_COMPILER_MSVC
extern "C" unsigned char _BitScanForward(unsigned long * _Index, unsigned long _Mask);
#pragma intrinsic(_BitScanForward)
extern "C" unsigned char _BitScanReverse(unsigned long * _Index, unsigned long _Mask);
#pragma intrinsic(_BitScanReverse)
#endif
namespace expr_jit
{
uint32_t constexpr c_sign_bit = 0x80000000;
uint32_t constexpr c_sign_mask = 0x7fffffff;
static void* malloc_wrapper(void*, size_t _size)
{
return malloc(_size);
}
static void free_wrapper(void*, void* _ptr)
{
free(_ptr);
}
static void null_err_cb(char const*)
{
}
static uint32_t next_pow2(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
static uint32_t fnv1a(char const* _str)
{
uint32_t hash = 0x811c9dc5;
while (*_str)
{
hash ^= *_str++;
hash *= 16777619u;
}
return hash;
}
static uint32_t fnv1a(char const* _str, uint32_t _len)
{
uint32_t hash = 0x811c9dc5;
for(uint32_t i = 0; i < _len; ++i)
{
hash ^= _str[i];
hash *= 16777619u;
}
return hash;
}
static uint32_t find_first_set_lsb(uint32_t _v)
{
#if EXPR_JIT_COMPILER_CLANG || EXPR_JIT_COMPILER_GCC
return __builtin_ctz(_v);
#elif EXPR_JIT_COMPILER_MSVC
unsigned long idx;
return ::_BitScanForward(&idx, _v) ? idx : 32;
#else
#error Not implemented
#endif
}
static uint32_t find_first_set_msb(uint32_t _v)
{
#if EXPR_JIT_COMPILER_CLANG || EXPR_JIT_COMPILER_GCC
return __builtin_clz(_v);
#elif EXPR_JIT_COMPILER_MSVC
unsigned long idx;
return ::_BitScanReverse(&idx, _v) ? idx : 32;
#else
#error Not implemented
#endif
}
static void output_error(error_cb _err, char const* _fmt, ...)
{
char buffer[256];
va_list args;
va_start(args, _fmt);
vsnprintf(buffer, sizeof(buffer), _fmt, args);
_err(buffer);
va_end(args);
}
template <typename T>
void swap(T& _lhs, T& _rhs)
{
T temp = _lhs;
_lhs = _rhs;
_rhs = temp;
}
// Push element at index _arr_size - 1 onto heap.
template<typename T, typename PredT>
void heap_push(T* _ptr, uint32_t _arr_size, PredT _pred)
{
EXPR_JIT_ASSERT(_arr_size);
uint32_t idx = _arr_size - 1;
uint32_t parent = idx / 2;
while (idx != 0 && _pred(_ptr[idx], _ptr[parent]))
{
swap(_ptr[idx], _ptr[parent]);
idx = parent;
parent = idx / 2;
}
}
// Pop element at index 0 off the heap.
template<typename T, typename PredT>
void heap_pop(T* _ptr, uint32_t _arr_size, PredT _pred)
{
EXPR_JIT_ASSERT(_arr_size);
swap(_ptr[0], _ptr[_arr_size - 1]);
uint32_t const new_size = _arr_size - 1;
if (new_size < 2)
{
return;
}
uint32_t idx = 0;
uint32_t left = idx * 2 + 1;
while (left < new_size)
{
uint32_t right = left + 1;
uint32_t child_to_consider = left;
if (right < new_size && _pred(_ptr[right], _ptr[left]))
{
child_to_consider = right;
}
if (_pred(_ptr[child_to_consider], _ptr[idx]))
{
swap(_ptr[child_to_consider], _ptr[idx]);
idx = child_to_consider;
left = idx * 2 + 1;
}
else
{
break;
}
}
}
template <typename T>
struct stack
{
void init(T* _mem, uint32_t _cap)
{
mem = _mem;
size = 0;
cap = _cap;
}
void push(T const& _t)
{
EXPR_JIT_ASSERT(size < cap);
mem[size++] = _t;
}
bool pop(T* o_val)
{
if (!size)
{
return false;
}
*o_val = mem[--size];
return true;
}
T* mem;
uint32_t cap = 0;
uint32_t size = 0;
};
template <typename T>
struct dyn_pod_array
{
dyn_pod_array() = default;
~dyn_pod_array()
{
if (mem)
{
hooks.free(hooks.ctx, mem);
}
}
dyn_pod_array(dyn_pod_array const&) = delete;
dyn_pod_array& operator=(dyn_pod_array const&) = delete;
void init(alloc_hooks _hooks)
{
hooks = _hooks;
}
void ensure_cap(uint32_t _req_cap)
{
if (cap < _req_cap)
{
uint32_t const amortized_grow = cap + cap / 2;
uint32_t const new_cap = amortized_grow < _req_cap ? _req_cap : amortized_grow;
T* new_mem = (T*)hooks.alloc(hooks.ctx, sizeof(T) * new_cap);
if (mem)
{
memcpy(new_mem, mem, size * sizeof(T));
hooks.free(hooks.ctx, mem);
}
cap = new_cap;
mem = new_mem;
}
}
void append(T const& _v)
{
*append() = _v;
}
T* append()
{
ensure_cap(size + 1);
return &mem[size++];
}
T* append_n(uint32_t _n)
{
ensure_cap(size + _n);
T* ptr = mem + size;
size += _n;
return ptr;
}
T* begin()
{
return mem;
}
T* end()
{
return mem + size;
}
T& operator[](uint32_t _idx)
{
EXPR_JIT_ASSERT(_idx < size);
return mem[_idx];
}
T* mem = nullptr;
uint32_t size = 0;
uint32_t cap = 0;
alloc_hooks hooks;
};
enum class builtin_function
{
abs,
min,
max,
sqrt,
clamp,
num_functions
};
char const* c_builtin_function_name[uint32_t(builtin_function::num_functions)]
{
"abs",
"min",
"max",
"sqrt",
"clamp"
};
static uint32_t const c_builtin_function_num_param[uint32_t(builtin_function::num_functions)] =
{
1,
2,
2,
1,
3
};
enum class symbol_type
{
invalid = 0,
constant,
variable,
function
};
struct symbol
{
char const* str;
uint32_t str_len;
symbol_type type;
union
{
float constant_val;
uint32_t variable_idx;
builtin_function function;
};
};
struct symbol_table
{
symbol* insert(char const* _str)
{
uint32_t const hash = fnv1a(_str);
uint32_t idx = hash & count_mask;
for (;;)
{
if (hashes[idx] == 0)
{
hashes[idx] = hash;
symbol* sym = &symbols[idx];
sym->str = _str;
sym->str_len = uint32_t(strlen(_str));
return &symbols[idx];
}
if (hashes[idx] == hash && strcmp(symbols[idx].str, _str) == 0)
{
return &symbols[idx];
}
idx = (idx + 1) & count_mask;
}
}
symbol* find(char const* _str, uint32_t _str_len)
{
uint32_t const hash = fnv1a(_str, _str_len);
uint32_t idx = hash & count_mask;
for (;;)
{
if (hashes[idx] == 0)
{
return nullptr;
}
if (hashes[idx] == hash
&& symbols[idx].str_len == _str_len
&& strncmp(symbols[idx].str, _str, _str_len) == 0)
{
return &symbols[idx];
}
idx = (idx + 1) & count_mask;
}
}
bool init(symbol* _symbol_mem, uint32_t* _hash_mem, uint32_t _max_entries, expression_info const* _expr_info, error_cb _err_cb)
{
EXPR_JIT_ASSERT((_max_entries & (_max_entries - 1)) == 0 && "_max_entries must be a power of two for hash table.");
symbols = _symbol_mem;
hashes = _hash_mem;
count_mask = _max_entries - 1;
memset(_hash_mem, 0, sizeof(uint32_t) * _max_entries);
memset(_symbol_mem, 0, sizeof(symbol) * _max_entries);
for (uint32_t i = 0; i < _expr_info->num_variables; ++i)
{
char const* sym_string = _expr_info->variables[i];
symbol* sym = insert(sym_string);
if (sym->type != symbol_type::invalid)
{
output_error(_err_cb, "Variable symbol name \"%s\" is already defined.", sym_string);
return false;
}
sym->type = symbol_type::variable;
sym->variable_idx = i;
}
for (uint32_t i = 0; i < _expr_info->num_constants; ++i)
{
char const* sym_string = _expr_info->constant_names[i];
symbol* sym = insert( sym_string);
if (sym->type != symbol_type::invalid)
{
output_error(_err_cb, "Constant symbol name \"%s\" is already defined.", sym_string);
return false;
}
sym->type = symbol_type::constant;
sym->constant_val = _expr_info->constant_values[i];
}
for (uint32_t i = 0; i < uint32_t(builtin_function::num_functions); ++i)
{
char const* sym_string = c_builtin_function_name[i];
symbol* sym = insert(sym_string);
if (sym->type != symbol_type::invalid)
{
output_error(_err_cb, "Function symbol name \"%s\" is already defined.", sym_string);
return false;
}
sym->type = symbol_type::function;
sym->function = builtin_function(i);
}
return true;
}
symbol* symbols = nullptr;
uint32_t* hashes = nullptr;
// hash table size - 1
uint32_t count_mask = 0;
};
enum class ast_node_type
{
invalid,
constant,
variable,
function,
bin_add,
bin_sub,
bin_mul,
bin_div,
un_neg
};
static bool is_bin_op_associative(ast_node_type _type)
{
switch (_type)
{
case ast_node_type::bin_add:
case ast_node_type::bin_mul:
{
return true;
}
default:
{
return false;
}
}
}
static bool is_bin_op_commutative(ast_node_type _type)
{
switch (_type)
{
case ast_node_type::bin_add:
case ast_node_type::bin_mul:
{
return true;
}
default:
{
return false;
}
}
}
struct ast_node
{
bool is_constant() const { return type == ast_node_type::constant; }
bool is_constant_or_var() const { return type == ast_node_type::constant || type == ast_node_type::variable; }
bool is_binary_op() const
{
switch (type)
{
case ast_node_type::bin_add:
case ast_node_type::bin_sub:
case ast_node_type::bin_mul:
case ast_node_type::bin_div:
{
return true;
}
default:
{
return false;
}
}
}
uint32_t op_precedence() const
{
switch (type)
{
case ast_node_type::function: return 0;
case ast_node_type::bin_add: return 3;
case ast_node_type::bin_sub: return 3;
case ast_node_type::bin_mul: return 2;
case ast_node_type::bin_div: return 2;
case ast_node_type::un_neg: return 1;
case ast_node_type::invalid:
case ast_node_type::constant:
case ast_node_type::variable:
{
EXPR_JIT_ASSERT(false); return 0;
} break;
}
EXPR_JIT_UNREACHABLE;
}
uint32_t get_fun_param_count() const { EXPR_JIT_ASSERT(type == ast_node_type::function); return c_builtin_function_num_param[uint32_t(function.index)]; }
ast_node* parent;
ast_node_type type;
uint32_t node_id;
union
{
float constant_val;
uint32_t variable_idx;
struct
{
ast_node* left;
ast_node* right;
} binary_op;
struct
{
builtin_function index;
ast_node* params[3];
} function;
ast_node* unary_op_child;
ast_node* free_list_next;
};
};
// Copy contents but leave parent and node_id intact.
void copy_ast_node_contents(ast_node* _dest, ast_node const* _src)
{
switch (_src->type)
{
case ast_node_type::function:
{
memcpy(&_dest->function, &_src->function, sizeof(_src->function));
} break;
case ast_node_type::bin_add:
case ast_node_type::bin_sub:
case ast_node_type::bin_mul:
case ast_node_type::bin_div:
{
memcpy(&_dest->binary_op, &_src->binary_op, sizeof(_src->binary_op)); break;
} break;
case ast_node_type::un_neg:
{
memcpy(&_dest->unary_op_child, &_src->unary_op_child, sizeof(_src->unary_op_child)); break;
} break;
case ast_node_type::constant:
{
memcpy(&_dest->constant_val, &_src->constant_val, sizeof(_src->constant_val)); break;
} break;
case ast_node_type::variable:
{
memcpy(&_dest->variable_idx, &_src->variable_idx, sizeof(_src->variable_idx)); break;
} break;
case ast_node_type::invalid: break;
}
_dest->type = _src->type;
}
template <typename VisitFn>
static void visit_ast_depth_first_post_order(ast_node* _node, VisitFn _fn)
{
switch (_node->type)
{
case ast_node_type::bin_add:
case ast_node_type::bin_sub:
case ast_node_type::bin_mul:
case ast_node_type::bin_div:
{
ast_node* left_node = _node->binary_op.left;
ast_node* right_node = _node->binary_op.right;
visit_ast_depth_first_post_order(left_node, _fn);
visit_ast_depth_first_post_order(right_node, _fn);
} break;
case ast_node_type::un_neg:
{
visit_ast_depth_first_post_order(_node->unary_op_child, _fn);
} break;
case ast_node_type::function:
{
for (ast_node* param : _node->function.params)
{
if (param)
{
visit_ast_depth_first_post_order(param, _fn);
}
}
} break;
default:
{
} break;
}
_fn(_node);
}
struct ast_node_pool
{
static uint32_t const c_nodes_per_chunk = 32;
struct chunk
{
chunk* next_chunk;
ast_node nodes[c_nodes_per_chunk];
uint32_t next_node;
};
void free_all(alloc_hooks _hooks)
{
chunk* c = chunk_list;
while (c)
{
chunk* next = c->next_chunk;
_hooks.free(_hooks.ctx, c);
c = next;
}
}
ast_node* alloc_node(alloc_hooks const& _hooks)
{
if (!free_list || !chunk_list || chunk_list->next_node == c_nodes_per_chunk)
{
chunk* c = (chunk*)_hooks.alloc(_hooks.ctx, sizeof(chunk));
memset(c->nodes, 0, sizeof(c->nodes));
c->next_chunk = chunk_list;
chunk_list = c;
c->next_node = 0;
}
if (free_list)
{
ast_node* n = free_list;
free_list = n->free_list_next;
return n;
}
else
{
ast_node* n = &chunk_list->nodes[chunk_list->next_node++];
n->node_id = total_nodes++;
return n;
}
}
// Note: not necessary for leaks (whole pool is freed), only used to recycle nodes when performing transformations on AST.
void free_node(ast_node* _n)
{
visit_ast_depth_first_post_order(_n, [this](ast_node* _node)
{
_node->free_list_next = free_list;
free_list = _node;
});
}
chunk* chunk_list;
ast_node* free_list;
uint32_t total_nodes;
};
struct expr
{
ast_node* alloc_ast_node()
{
return node_pool.alloc_node(alloc);
}
alloc_hooks alloc;
ast_node_pool node_pool;
uint32_t num_variables;
ast_node* root = nullptr;
};
struct parser_ctx
{
void init(expr* _expr, expression_info const* _info, error_cb _err)
{
expression = _expr;
expr_info = _info;
err_cb = _err;
}
symbol_table symbols;
expr* expression;
expression_info const* expr_info;
error_cb err_cb;
};
enum class token_type
{
left_paren,
right_paren,
comma,
constant,
symbol,
plus,
minus,
divide,
multiply,
eof
};
struct token
{
token_type type;
union
{
float constant_val;
symbol* sym;
};
};
struct lexer_ctx
{
void init(symbol_table* _table, char const* _begin, char const* _end, error_cb _err)
{
begin = cur = _begin;
eof = _end;
error_cb = _err;
sym_table = _table;
}
char const* begin;
char const* cur;
char const* eof;
error_cb error_cb;
symbol_table* sym_table;
token peek;
};
static char to_lower(char c)
{
return c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c;
}
static bool lex_next(lexer_ctx& _ctx)
{
do
{
if (_ctx.cur == _ctx.eof)
{
_ctx.peek.type = token_type::eof;
return true;
}
char const c = *_ctx.cur;
switch (c)
{
case '(': _ctx.peek.type = token_type::left_paren; ++_ctx.cur; return true;
case ')': _ctx.peek.type = token_type::right_paren; ++_ctx.cur; return true;
case '+': _ctx.peek.type = token_type::plus; ++_ctx.cur; return true;
case '*': _ctx.peek.type = token_type::multiply; ++_ctx.cur; return true;
case '-': _ctx.peek.type = token_type::minus; ++_ctx.cur; return true;
case '/': _ctx.peek.type = token_type::divide; ++_ctx.cur; return true;
case ',': _ctx.peek.type = token_type::comma; ++_ctx.cur; return true;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
_ctx.peek.type = token_type::constant;
_ctx.peek.constant_val = strtof(_ctx.cur, (char**)&_ctx.cur);
return true;
} break;
case ' ':
case '\n':
case '\t':
case '\r':
{
++_ctx.cur;
} break;
default:
{
char const c_lower = to_lower(c);
if (c_lower >= 'a' && c_lower <= 'z')
{
char const* str_begin = _ctx.cur++;
char next_c = to_lower(*_ctx.cur);
while (next_c >= 'a' && next_c <= 'z')
{
next_c = *(++_ctx.cur);
}
symbol* sym = _ctx.sym_table->find(str_begin, uint32_t(_ctx.cur - str_begin));
if (!sym)
{
output_error(_ctx.error_cb, "Found undeclared symbol: \"%.*s\".", uint32_t(_ctx.cur - str_begin), str_begin);
return false;
}
_ctx.peek.type = token_type::symbol;
_ctx.peek.sym = sym;
return true;
}
output_error(_ctx.error_cb, "Unexpected token '%c' at index %u", *_ctx.cur, uint32_t(_ctx.cur - _ctx.begin));
return false;
} break;
}
} while (true);
}
static bool lex_expect(lexer_ctx& _lexer, token_type _type)
{
if (_lexer.peek.type != _type)
{
return false;
}
lex_next(_lexer);
return true;
}
static ast_node* parse_expr_ast(parser_ctx& _parser, lexer_ctx& _lexer);
static ast_node* parse_term_ast(parser_ctx& _parser, lexer_ctx& _lexer);
static ast_node* parse_factor_ast(parser_ctx& _parser, lexer_ctx& _lexer);
static float eval_node(ast_node const* _node, float const* _args);
static ast_node* parse_factor_ast(parser_ctx& _parser, lexer_ctx& _lexer)
{
// factor -> [-] constant | variable | function | (expr)
ast_node* neg_node = nullptr;
ast_node* factor_node = nullptr;
if (_lexer.peek.type == token_type::minus)
{
lex_next(_lexer);
neg_node = _parser.expression->alloc_ast_node();
neg_node->type = ast_node_type::un_neg;
}
switch (_lexer.peek.type)
{
case token_type::symbol:
{
token const tok = _lexer.peek;
lex_next(_lexer);
switch (tok.sym->type)
{
case symbol_type::constant:
{
factor_node = _parser.expression->alloc_ast_node();
factor_node->type = ast_node_type::constant;
factor_node->constant_val = tok.sym->constant_val;
} break;
case symbol_type::variable:
{
factor_node = _parser.expression->alloc_ast_node();
factor_node->type = ast_node_type::variable;
factor_node->variable_idx = tok.sym->variable_idx;
} break;
case symbol_type::function:
{
factor_node = _parser.expression->alloc_ast_node();
factor_node->type = ast_node_type::function;
factor_node->function.index = tok.sym->function;
uint32_t const fn_params = c_builtin_function_num_param[uint32_t(tok.sym->function)];
if (!lex_expect(_lexer, token_type::left_paren))
{
output_error(_lexer.error_cb, "Expected '(' after function \"%s\"", c_builtin_function_name[uint32_t(tok.sym->function)]);
return nullptr;
}
for (uint32_t i = 0; i < fn_params; ++i)
{
factor_node->function.params[i] = parse_expr_ast(_parser, _lexer);
if (!factor_node->function.params[i])
{
return nullptr;
}
factor_node->function.params[i]->parent = factor_node;
if (i != fn_params - 1)
{
if (!lex_expect(_lexer, token_type::comma))
{
output_error(_lexer.error_cb, "Expected ',' after param %u for function \"%s\"", i, c_builtin_function_name[uint32_t(tok.sym->function)]);
return nullptr;
}
}
}
if (!lex_expect(_lexer, token_type::right_paren))
{
output_error(_lexer.error_cb, "Expected ')' after function \"%s\"", c_builtin_function_name[uint32_t(tok.sym->function)]);
return nullptr;
}
} break;
}
} break;
case token_type::constant:
{
lex_next(_lexer);
factor_node = _parser.expression->alloc_ast_node();
factor_node->type = ast_node_type::constant;
factor_node->constant_val = _lexer.peek.constant_val;
} break;
case token_type::left_paren:
{
lex_next(_lexer);
factor_node = parse_expr_ast(_parser, _lexer);
if (!factor_node || !lex_expect(_lexer, token_type::right_paren))
{
return nullptr;
}
} break;
}