-
Notifications
You must be signed in to change notification settings - Fork 2
/
expression.c
2950 lines (2815 loc) · 77.9 KB
/
expression.c
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
/* Copyright (c) 2018-2024 Griefer@Work *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement (see the following) in the product *
* documentation is required: *
* Portions Copyright (c) 2018-2024 Griefer@Work *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
*/
#ifndef GUARD_DEEMON_COMPILER_LEXER_EXPRESSION_C
#define GUARD_DEEMON_COMPILER_LEXER_EXPRESSION_C 1
#include <deemon/alloc.h>
#include <deemon/api.h>
#include <deemon/bool.h>
#include <deemon/compiler/ast.h>
#include <deemon/compiler/lexer.h>
#include <deemon/compiler/optimize.h>
#include <deemon/compiler/tpp.h>
#include <deemon/dict.h>
#include <deemon/error.h>
#include <deemon/float.h>
#include <deemon/hashset.h>
#include <deemon/int.h>
#include <deemon/list.h>
#include <deemon/map.h>
#include <deemon/module.h>
#include <deemon/none.h>
#include <deemon/seq.h>
#include <deemon/string.h>
#include <deemon/stringutils.h>
#include <deemon/system-features.h> /* memchr() */
#include <deemon/tuple.h>
#include "../../runtime/strings.h"
DECL_BEGIN
#ifndef CONFIG_HAVE_memrchr
#define CONFIG_HAVE_memrchr
#undef memrchr
#define memrchr dee_memrchr
DeeSystem_DEFINE_memrchr(dee_memrchr)
#endif /* !CONFIG_HAVE_memrchr */
#define GET_CHOP(x) (ASSERT((x) < 128), chops[x])
INTERN uint8_t const chops[128] = {
/*[[[deemon
#include <util>
local table = dict {
"+": "OPERATOR_ADD",
"-": "OPERATOR_SUB",
"*": "OPERATOR_MUL",
"/": "OPERATOR_DIV",
"%": "OPERATOR_MOD",
"<": "OPERATOR_LO",
">": "OPERATOR_GR",
"&": "OPERATOR_AND",
"|": "OPERATOR_OR",
"^": "OPERATOR_XOR",
};
for (local x: util::range(128)) {
local key = util::chr((uint8_t)x);
local value = table.get(key);
if (value is none)
value = "0";
print "\t/" "* " + repr(key) + " *" "/", value + ",";
}
]]]*/
/* "\0" */ 0,
/* "\1" */ 0,
/* "\2" */ 0,
/* "\3" */ 0,
/* "\4" */ 0,
/* "\5" */ 0,
/* "\6" */ 0,
/* "\a" */ 0,
/* "\b" */ 0,
/* "\t" */ 0,
/* "\n" */ 0,
/* "\v" */ 0,
/* "\f" */ 0,
/* "\r" */ 0,
/* "\16" */ 0,
/* "\17" */ 0,
/* "\20" */ 0,
/* "\21" */ 0,
/* "\22" */ 0,
/* "\23" */ 0,
/* "\24" */ 0,
/* "\25" */ 0,
/* "\26" */ 0,
/* "\27" */ 0,
/* "\30" */ 0,
/* "\31" */ 0,
/* "\32" */ 0,
/* "\e" */ 0,
/* "\34" */ 0,
/* "\35" */ 0,
/* "\36" */ 0,
/* "\37" */ 0,
/* " " */ 0,
/* "!" */ 0,
/* "\"" */ 0,
/* "#" */ 0,
/* "$" */ 0,
/* "%" */ OPERATOR_MOD,
/* "&" */ OPERATOR_AND,
/* "\'" */ 0,
/* "(" */ 0,
/* ")" */ 0,
/* "*" */ OPERATOR_MUL,
/* "+" */ OPERATOR_ADD,
/* "," */ 0,
/* "-" */ OPERATOR_SUB,
/* "." */ 0,
/* "/" */ OPERATOR_DIV,
/* "0" */ 0,
/* "1" */ 0,
/* "2" */ 0,
/* "3" */ 0,
/* "4" */ 0,
/* "5" */ 0,
/* "6" */ 0,
/* "7" */ 0,
/* "8" */ 0,
/* "9" */ 0,
/* ":" */ 0,
/* ";" */ 0,
/* "<" */ OPERATOR_LO,
/* "=" */ 0,
/* ">" */ OPERATOR_GR,
/* "?" */ 0,
/* "@" */ 0,
/* "A" */ 0,
/* "B" */ 0,
/* "C" */ 0,
/* "D" */ 0,
/* "E" */ 0,
/* "F" */ 0,
/* "G" */ 0,
/* "H" */ 0,
/* "I" */ 0,
/* "J" */ 0,
/* "K" */ 0,
/* "L" */ 0,
/* "M" */ 0,
/* "N" */ 0,
/* "O" */ 0,
/* "P" */ 0,
/* "Q" */ 0,
/* "R" */ 0,
/* "S" */ 0,
/* "T" */ 0,
/* "U" */ 0,
/* "V" */ 0,
/* "W" */ 0,
/* "X" */ 0,
/* "Y" */ 0,
/* "Z" */ 0,
/* "[" */ 0,
/* "\\" */ 0,
/* "]" */ 0,
/* "^" */ OPERATOR_XOR,
/* "_" */ 0,
/* "`" */ 0,
/* "a" */ 0,
/* "b" */ 0,
/* "c" */ 0,
/* "d" */ 0,
/* "e" */ 0,
/* "f" */ 0,
/* "g" */ 0,
/* "h" */ 0,
/* "i" */ 0,
/* "j" */ 0,
/* "k" */ 0,
/* "l" */ 0,
/* "m" */ 0,
/* "n" */ 0,
/* "o" */ 0,
/* "p" */ 0,
/* "q" */ 0,
/* "r" */ 0,
/* "s" */ 0,
/* "t" */ 0,
/* "u" */ 0,
/* "v" */ 0,
/* "w" */ 0,
/* "x" */ 0,
/* "y" */ 0,
/* "z" */ 0,
/* "{" */ 0,
/* "|" */ OPERATOR_OR,
/* "}" */ 0,
/* "~" */ 0,
/* "\x7F" */ 0,
/*[[[end]]]*/
};
/* Lookup mode used by secondary AST operands */
#define LOOKUP_SYM_SECONDARY LOOKUP_SYM_NORMAL
/* Parser flags (Set of `PARSE_F*') */
INTERN uint16_t parser_flags = PARSE_FNORMAL;
/* Return 1 if the current token may be the begin of an expression.
* @return: 1: Yes
* @return: 0: No
* @return: -1: Error */
INTERN WUNUSED int DCALL maybe_expression_begin(void) {
switch (tok) {
case '+':
case '-':
case '~':
case '(':
case '#':
case '<': /* For cells. (deprecated syntax) */
case '[': /* For lists. */
case '{': /* Brace initializers. */
case TOK_INC:
case TOK_DEC:
case TOK_INT:
case TOK_CHAR:
case TOK_FLOAT:
case TOK_STRING:
case TOK_DOTS:
case TOK_COLON_COLON: /* Deprecated global-accessor syntax. */
goto yes;
/* Keywords that can only appear inside of expressions */
case KWD_as:
case KWD_in:
case KWD_is:
/* Keywords that can only appear inside of expressions/statements */
case KWD_else:
case KWD_catch:
case KWD_finally:
/* Keywords that can only appear inside of statements */
case KWD_from:
case KWD_return:
case KWD_yield:
case KWD_throw:
case KWD_print:
case KWD_break:
case KWD_continue:
case KWD___asm:
case KWD___asm__:
case KWD_goto:
case KWD_switch:
case KWD_case:
case KWD_default:
goto no;
case '!': {
struct TPPFile *tok_file;
struct TPPKeyword *kwd;
char *tok_begin;
/* Check if this ! is eventually followed by `is' or `in'
* If this is the case, then this can't be the start of an
* expression! */
tok_begin = peek_next_token(&tok_file);
for (;;) {
if unlikely(!tok_begin)
goto err;
if (*tok_begin != '!')
break;
tok_begin = peek_next_advance(tok_begin + 1, &tok_file);
}
kwd = peek_keyword(tok_file, tok_begin, 0);
if (!kwd) {
if unlikely(tok == TOK_ERR)
goto err;
} else {
/* This isn't an expression. */
if (kwd->k_id == KWD_is || kwd->k_id == KWD_in)
goto no;
}
goto yes;
} break;
default:
/* Any kind of keyword can appear in expressions, either
* a native expression keyword, or as a variable name. */
if (TPP_ISKEYWORD(tok))
goto yes;
/* XXX: TPP_ISUSERKEYWORD()? */
goto no;
}
no:
return 0;
yes:
return 1;
err:
return -1;
}
/* Same as `maybe_expression_begin()', but for the next (peeked) token. */
INTERN WUNUSED int DCALL maybe_expression_begin_peek(void) {
char *tok_begin, peek;
struct TPPFile *tok_file;
tok_begin = peek_next_token(&tok_file);
if unlikely(!tok_begin)
goto err;
peek = *tok_begin;
switch (peek) {
case '~':
case '(':
case '#':
case '[': /* For lists. */
case '{': /* Brace initializers. */
case '_': /* For identifiers */
case '$': /* For identifiers */
goto yes;
case '+':
case '-':
case '<': /* For cells. (deprecated syntax) */
tok_begin = advance_wraplf(tok_begin);
if unlikely(!tok_begin)
goto err;
{
char next;
next = *tok_begin;
if (next == peek) {
if (peek == '<')
goto no; /* `<<' cannot appear at the start of expression */
goto yes; /* `++' and `--' can appear, though */
}
if (next == '=')
goto no; /* +=, -=, <= can only appear in the middle of expressions! */
}
goto yes;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
/* Integer / float */
case '\'':
case '\"':
/* String constants */
goto yes;
case '.': /* TOK_DOTS */
tok_begin = advance_wraplf(tok_begin);
if (*tok_begin != '.')
goto no;
tok_begin = advance_wraplf(tok_begin);
if (*tok_begin != '.')
goto no;
goto yes; /* ... */
case ':': /* Deprecated global-accessor syntax. */
tok_begin = advance_wraplf(tok_begin);
if (*tok_begin != ':')
goto no;
goto yes; /* :: */
case '!': {
struct TPPKeyword *kwd;
/* Check if this ! is eventually followed by `is' or `in'
* If this is the case, then this can't be the start of an
* expression! */
for (;;) {
tok_begin = peek_next_advance(tok_begin + 1, &tok_file);
if unlikely(!tok_begin)
goto err;
if (*tok_begin != '!')
break;
}
kwd = peek_keyword(tok_file, tok_begin, 0);
if (!kwd) {
if unlikely(tok == TOK_ERR)
goto err;
} else {
/* This isn't an expression. */
if (kwd->k_id == KWD_is || kwd->k_id == KWD_in)
goto no;
}
goto yes;
} break;
default: {
struct TPPKeyword *kwd;
if (!tpp_is_keyword_start(peek))
goto no;
kwd = peek_keyword(tok_file, tok_begin, 0);
if unlikely(!kwd) {
if unlikely(tok == TOK_ERR)
goto err;
goto yes; /* First-time-used user-defined keyword token. */
}
switch (kwd->k_id) {
/* Keywords that can only appear inside of expressions */
case KWD_as:
case KWD_in:
case KWD_is:
/* Keywords that can only appear inside of expressions/statements */
case KWD_else:
case KWD_catch:
case KWD_finally:
/* Keywords that can only appear inside of statements */
case KWD_from:
case KWD_return:
case KWD_yield:
case KWD_throw:
case KWD_print:
case KWD_break:
case KWD_continue:
case KWD___asm:
case KWD___asm__:
case KWD_goto:
case KWD_switch:
case KWD_case:
case KWD_default:
goto no;
default:
break;
}
goto yes;
}
}
no:
return 0;
yes:
return 1;
err:
return -1;
}
PRIVATE WUNUSED NONNULL((1, 2)) DREF struct ast *DFCALL
make_bound_expression(struct ast *__restrict base_expr,
struct ast_loc *__restrict loc) {
DREF struct ast *result;
if (base_expr->a_type == AST_SYM) {
/* Check if a given symbol is bound. */
result = ast_setddi(ast_bound(base_expr->a_sym), &base_expr->a_ddi);
} else if (base_expr->a_type == AST_OPERATOR &&
base_expr->a_flag == OPERATOR_GETATTR &&
base_expr->a_operator.o_op1) {
/* Check if a given attribute is bound. */
result = ast_action2(AST_FACTION_BOUNDATTR,
base_expr->a_operator.o_op0,
base_expr->a_operator.o_op1);
} else if (base_expr->a_type == AST_OPERATOR &&
base_expr->a_flag == OPERATOR_GETITEM &&
base_expr->a_operator.o_op1) {
/* Check if a given item is bound. */
result = ast_action2(AST_FACTION_BOUNDITEM,
base_expr->a_operator.o_op0,
base_expr->a_operator.o_op1);
} else if (WARNAST(base_expr, W_CANNOT_TEST_EXPRESSION_BINDING)) {
result = NULL;
} else {
/* Fallback-after-warning: Return `true' */
result = ast_constexpr(Dee_True);
}
return ast_putddi(result, loc);
}
PRIVATE WUNUSED NONNULL((1, 2)) int DCALL
DeeString_DecodeLFEscaped(struct unicode_printer *__restrict printer,
/*utf-8*/ char const *__restrict start,
size_t length) {
/* Still allow escaped line-feeds! */
char *flush_start = (char *)start;
char *end = (char *)start + length;
for (;;) {
char *candidate;
uint32_t ch;
candidate = (char *)memchr(start, '\\', (size_t)(end - (char *)start));
if (!candidate)
break;
if (unicode_printer_printutf8(printer, flush_start,
(size_t)(candidate - flush_start)) < 0)
goto err;
flush_start = candidate;
++candidate;
start = (char *)candidate;
ASSERT(start <= end);
if (start < end) {
ch = unicode_readutf8_n(&candidate, end);
if (DeeUni_IsLF(ch)) {
if (ch == '\r' && candidate < end && *candidate == '\n')
++candidate; /* CRLF */
start = flush_start = candidate;
}
}
}
if (unicode_printer_printutf8(printer, flush_start,
(size_t)(end - flush_start)) < 0)
goto err;
return 0;
err:
return -1;
}
INTERN WUNUSED NONNULL((1)) int DCALL
ast_decode_unicode_string(struct unicode_printer *__restrict printer) {
ASSERT(tok == TOK_STRING || tok == TOK_CHAR);
char *escape_start = token.t_begin;
char *escape_end = token.t_end;
if (escape_start < escape_end && escape_start[0] == 'r') {
++escape_start;
if (escape_start < escape_end &&
(escape_start[0] == '\"' || escape_start[0] == '\''))
++escape_start;
if (escape_end > escape_start &&
(escape_end[-1] == '\"' || escape_end[-1] == '\''))
--escape_end;
if unlikely(escape_end < escape_start)
escape_end = escape_start;
if unlikely(DeeString_DecodeLFEscaped(printer,
escape_start,
(size_t)(escape_end - escape_start)))
goto err;
} else {
if (escape_start < escape_end &&
(escape_start[0] == '\"' || escape_start[0] == '\''))
++escape_start;
if (escape_end > escape_start &&
(escape_end[-1] == '\"' || escape_end[-1] == '\''))
--escape_end;
if unlikely(escape_end < escape_start)
escape_end = escape_start;
if unlikely(DeeString_DecodeBackslashEscaped(printer,
escape_start,
(size_t)(escape_end - escape_start),
STRING_ERROR_FSTRICT))
goto err;
}
return 0;
err:
return -1;
}
INTERN WUNUSED DREF DeeObject *DFCALL
ast_parse_string(void) {
struct unicode_printer printer = UNICODE_PRINTER_INIT;
ASSERT(tok == TOK_STRING || tok == TOK_CHAR);
do {
if unlikely(ast_decode_unicode_string(&printer))
goto err;
if unlikely(yield() < 0)
goto err;
} while (tok == TOK_STRING ||
(tok == TOK_CHAR && !HAS(EXT_CHARACTER_LITERALS)));
return unicode_printer_pack(&printer);
err:
unicode_printer_fini(&printer);
return NULL;
}
INTERN WUNUSED DREF struct ast *DFCALL
ast_sym_import_from_deemon(void) {
struct symbol *import_symbol;
import_symbol = new_unnamed_symbol();
if unlikely(!import_symbol)
goto err;
/* Setup an external symbol pointing at `import from deemon' */
import_symbol->s_type = SYMBOL_TYPE_EXTERN;
import_symbol->s_extern.e_module = DeeModule_GetDeemon();
Dee_Incref(import_symbol->s_extern.e_module);
import_symbol->s_extern.e_symbol = DeeModule_GetSymbol(import_symbol->s_extern.e_module,
(DeeObject *)&str_import);
ASSERT(import_symbol->s_extern.e_symbol);
return ast_sym(import_symbol);
err:
return NULL;
}
PRIVATE WUNUSED NONNULL((1, 2)) size_t DFCALL
count_chars_without_escaped_linefeeds(char const *start,
char const *end) {
size_t result = 0;
while (start < end) {
uint32_t ch;
ch = unicode_readutf8_n(&start, end);
if (ch == '\\') {
ch = unicode_readutf8_n(&start, end);
if (ch == '\r')
unicode_readutf8_n(&start, end);
continue;
}
++result;
}
return result;
}
PRIVATE WUNUSED int DFCALL verify_consistent_grouping(void) {
char const *tok_start, *tok_end, *iter, *lastsep;
size_t interval, wanted_interval;
tok_start = token.t_begin;
tok_end = token.t_end;
iter = (char const *)memrchr(tok_start, '_', (size_t)(tok_end - tok_start));
if likely(!iter)
return 0; /* No separators present. */
ASSERTF(iter > tok_start, "If the '_' was at the start, it'd be a keyword token");
/* Figure out the */
interval = count_chars_without_escaped_linefeeds(iter + 1, tok_end);
lastsep = iter;
while (iter > tok_start) {
size_t offset;
--iter;
if (*iter != '_')
continue;
offset = count_chars_without_escaped_linefeeds(iter + 1, lastsep);
if (offset != interval) {
return WARN(W_INCONSISTENT_THOUSANDS_SEPERATORS,
interval, offset);
}
lastsep = iter;
}
/* Decimals should use a thousands-interval of `3'.
* For every other radix, the interval should be `4'. */
wanted_interval = *tok_start == '0' ? 4 : 3;
if (wanted_interval != interval) {
return WARN(W_INCORRECT_THOUSANDS_SEPERATORS,
wanted_interval, interval);
}
return 0;
}
INTERN WUNUSED DREF struct ast *DFCALL
ast_parse_unaryhead(unsigned int lookup_mode) {
DREF struct ast *result;
DREF struct ast *merge;
struct ast_loc loc;
uint32_t old_flags;
switch (tok) {
case TOK_INT: {
tint_t value;
DREF DeeObject *resval;
size_t toklen;
/* Verify that thousands-separators (if present) are used consistently. */
if unlikely(verify_consistent_grouping())
goto err;
/* Use our own integer parser, so we can process arbitrary-precision integers. */
toklen = (size_t)(token.t_end - token.t_begin);
resval = DeeInt_FromString(token.t_begin, toklen,
DEEINT_STRING(0, DEEINT_STRING_FESCAPED |
DEEINT_STRING_FTRY));
/* Check if the integer failed to be parsed. */
if unlikely(resval == ITER_DONE) {
if (WARN(W_INVALID_INTEGER))
goto err;
goto create_none;
}
create_constexpr:
if unlikely(!resval)
goto err;
result = ast_sethere(ast_constexpr(resval));
Dee_Decref(resval);
if unlikely(!result)
goto err;
if unlikely(yield() < 0)
goto err_r;
break;
case TOK_CHAR:
if (!HAS(EXT_CHARACTER_LITERALS))
goto decode_string;
if unlikely(TPP_Atoi(&value) == TPP_ATOI_ERR)
goto err;
if (WARN(W_DEPRECATED_CHARACTER_INT))
goto err;
resval = DeeInt_NewInt64(value);
goto create_constexpr;
} break;
case TOK_STRING: {
DREF DeeObject *resval;
decode_string:
loc_here(&loc);
resval = ast_parse_string();
if unlikely(!resval)
goto err;
result = ast_setddi(ast_constexpr(resval), &loc);
Dee_Decref(resval);
} break;
case KWD_f:
case KWD_F:
/* Check if this might be a template string. */
if ((*token.t_end == '\"') ||
(*token.t_end == '\'' && !HAS(EXT_CHARACTER_LITERALS))) {
if unlikely(yield() < 0)
goto err;
ASSERT(tok == TOK_STRING || tok == TOK_CHAR);
result = ast_parse_template_string();
break;
}
goto do_keyword;
case TOK_FLOAT: {
tfloat_t value;
DREF DeeObject *resval;
if (TPP_Atof(&value) == TPP_ATOF_ERR)
goto err;
resval = DeeFloat_New((double)value);
if unlikely(!resval)
goto err;
/* Construct a new branch for the constant float value. */
result = ast_sethere(ast_constexpr(resval));
Dee_Decref(resval);
if unlikely(!result)
goto err;
if unlikely(yield() < 0)
goto err_r;
} break;
case KWD_none: {
DeeObject *constval;
create_none:
constval = Dee_None;
mkconst:
result = ast_sethere(ast_constexpr(constval));
if unlikely(!result)
goto err;
if unlikely(yield() < 0)
goto err_r;
break;
case KWD_true:
constval = Dee_True;
goto mkconst;
case KWD_false:
constval = Dee_False;
goto mkconst;
} break;
case KWD_bound: {
loc_here(&loc);
if unlikely(yield() < 0)
goto err;
if (tok == '(') {
result = ast_parse_unaryhead(LOOKUP_SYM_SECONDARY |
PARSE_UNARY_DISALLOW_CASTS);
} else {
result = ast_parse_unary(LOOKUP_SYM_SECONDARY);
}
if unlikely(!result)
goto err;
merge = make_bound_expression(result, &loc);
ast_decref(result);
if unlikely(!merge)
goto err;
result = merge;
} break;
case KWD_str: {
uint16_t opid; /* Unary expressions. */
opid = OPERATOR_STR;
goto do_unary_operator_kwd;
case KWD_repr:
opid = OPERATOR_REPR;
goto do_unary_operator_kwd;
case KWD_copy:
opid = OPERATOR_COPY;
goto do_unary_operator_kwd;
case KWD_deepcopy:
opid = OPERATOR_DEEPCOPY;
do_unary_operator_kwd:
loc_here(&loc);
if unlikely(yield() < 0)
goto err;
if (tok == '(') {
result = ast_parse_unaryhead(LOOKUP_SYM_SECONDARY |
PARSE_UNARY_DISALLOW_CASTS);
} else {
result = ast_parse_unary(LOOKUP_SYM_SECONDARY);
}
if unlikely(!result)
goto err;
merge = ast_setddi(ast_operator1(opid, 0, result), &loc);
ast_decref(result);
if unlikely(!merge)
goto err;
result = merge;
break;
case '#':
opid = OPERATOR_SIZE;
goto do_unary_operator;
case '~':
opid = OPERATOR_INV;
goto do_unary_operator;
case '+':
opid = OPERATOR_POS;
goto do_unary_operator;
case '-':
opid = OPERATOR_NEG;
goto do_unary_operator;
case TOK_INC:
opid = OPERATOR_INC;
goto do_unary_operator;
case TOK_DEC:
opid = OPERATOR_DEC;
do_unary_operator:
loc_here(&loc);
if (yield() < 0)
goto err;
result = ast_parse_unary(LOOKUP_SYM_SECONDARY);
if unlikely(!result)
goto err;
merge = ast_setddi(ast_operator1(opid, 0, result), &loc);
ast_decref(result);
if unlikely(!merge)
goto err;
result = merge;
break;
case KWD_type:
loc_here(&loc);
if unlikely(yield() < 0)
goto err;
if (tok == '(') {
result = ast_parse_unaryhead(LOOKUP_SYM_SECONDARY |
PARSE_UNARY_DISALLOW_CASTS);
} else {
result = ast_parse_unary(LOOKUP_SYM_SECONDARY);
}
if unlikely(!result)
goto err;
merge = ast_setddi(ast_action1(AST_FACTION_TYPEOF, result), &loc);
ast_decref(result);
if unlikely(!merge)
goto err;
result = merge;
} break;
case '!': /* not */
loc_here(&loc);
if (yield() < 0)
goto err;
result = ast_parse_unary(LOOKUP_SYM_SECONDARY);
if unlikely(!result)
goto err;
merge = ast_setddi(ast_bool(AST_FBOOL_NEGATE, result), &loc);
ast_decref(result);
result = merge;
break;
/* I admit it. I was an idiot for adding this dedicated syntax for constructing
* Cell objects. I wasn't thinking and as a result of that, I have to maintain
* it for backwards compatibility.
* Anyways... The most I can do for now is have it emit a warning, telling that
* you should be using `Cell from deemon' instead (which actually won't even
* break backwards-compatibility with the old deemon, who's `Cell' object
* offered you the same functionality)
* NOTE: To ensure backwards-compatibility, you may place this
* in your code in order to simply always use `Cell(...)':
* >> #if __DEEMON__ >= 200
* >> import Cell from deemon;
* >> #else
* >> #define Cell(...) < __VA_ARGS__ >
* >> #endif */
case TOK_LOGT:
loc_here(&loc);
if (WARN(W_DEPRECATED_CELL_SYNTAX))
goto err;
goto do_empty_cell;
case '<': /* Cell (deprecated syntax) */
loc_here(&loc);
if (WARN(W_DEPRECATED_CELL_SYNTAX))
goto err;
if (yield() < 0)
goto err;
if (tok == '>') {
/* empty Cell. */
do_empty_cell:
result = ast_action0(AST_FACTION_CELL0);
} else {
result = ast_parse_unary(LOOKUP_SYM_SECONDARY);
if unlikely(!result)
goto err;
if (tok != '>' && WARN(W_EXPECTED_RANGLE_AFTER_LANGLE))
goto err_r;
merge = ast_action1(AST_FACTION_CELL1, result);
ast_decref(result);
result = merge;
}
ast_setddi(result, &loc);
if unlikely(yield() < 0)
goto err_r;
break;
case KWD_if: {
/* if-in-expressions. */
DREF struct ast *tt_branch;
DREF struct ast *ff_branch;
uint16_t expect;
bool has_paren;
expect = current_tags.at_expect;
loc_here(&loc);
if unlikely(yield() < 0)
goto err;
old_flags = TPPLexer_Current->l_flags;
TPPLexer_Current->l_flags &= ~TPPLEXER_FLAG_WANTLF;
if (paren_begin(&has_paren, W_EXPECTED_LPAREN_AFTER_IF))
goto err_flags;
result = ast_parse_expr(LOOKUP_SYM_SECONDARY);
TPPLexer_Current->l_flags |= old_flags & TPPLEXER_FLAG_WANTLF;
if unlikely(!result)
goto err;
if (paren_end(has_paren, W_EXPECTED_RPAREN_AFTER_IF))
goto err;
tt_branch = NULL;
if (tok != KWD_else && tok != KWD_elif) {
tt_branch = ast_parse_expr(LOOKUP_SYM_SECONDARY);
if unlikely(!tt_branch)
goto err_r;
}
ff_branch = NULL;
if (tok == KWD_elif) {
token.t_id = KWD_if; /* Cheat a bit... */
goto do_else_branch;
}
if (tok == KWD_else) {
if unlikely(yield() < 0) {
err_tt:
ast_xdecref(tt_branch);
goto err_r;
}
do_else_branch:
ff_branch = ast_parse_expr(LOOKUP_SYM_SECONDARY);
if unlikely(!ff_branch)
goto err_tt;
}
merge = ast_setddi(ast_conditional(AST_FCOND_EVAL | expect, result, tt_branch, ff_branch), &loc);
ast_xdecref(ff_branch);
ast_xdecref(tt_branch);
ast_xdecref(result);
result = merge;
} break;
case KWD_assert:
result = ast_parse_assert(true);
break;
case KWD_function: {
struct TPPKeyword *function_name;
if (WARN(W_DEPRECATED_FUNCTION_IN_EXPRESSION))
goto err;
/* Create a new function */
loc_here(&loc);
function_name = NULL;
if unlikely(yield() < 0)
goto err;
if (TPP_ISKEYWORD(tok)) {
function_name = token.t_kwd;
if unlikely(yield() < 0)
goto err;
}
result = ast_parse_function(function_name, NULL, false, &loc
#ifdef CONFIG_LANGUAGE_DECLARATION_DOCUMENTATION
,
NULL
#endif /* CONFIG_LANGUAGE_DECLARATION_DOCUMENTATION */
);
} break;
case KWD_final: {
struct TPPKeyword *class_name;
uint16_t class_flags;
class_flags = TP_FFINAL;
if unlikely(yield() < 0)
goto err;
if (unlikely(tok != KWD_class) &&
WARN(W_EXPECTED_CLASS_AFTER_FINAL))
goto err;
goto do_create_class;
case KWD_class:
class_flags = TP_FNORMAL;
do_create_class: