-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtinybasic.y
More file actions
1446 lines (1231 loc) · 43.6 KB
/
Copy pathtinybasic.y
File metadata and controls
1446 lines (1231 loc) · 43.6 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
%{
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Dilshan R Jayakody. All rights reserved.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "value.h"
#include "platform.h"
#include "error.h"
#include "platform_def.h"
#include "math.h"
#include "formatter.h"
#include "version.h"
#include "parser_helper.h"
#ifndef YY_TYPEDEF_YY_BUFFER_STATE
#define YY_TYPEDEF_YY_BUFFER_STATE
typedef struct yy_buffer_state* YY_BUFFER_STATE;
#endif
// Parser runtime limits.
#define STACK_DEPTH 32
#define LOOP_STACK_DEPTH 16
#define CJUMP_MAP_DEPTH 128
extern YY_BUFFER_STATE yy_scan_string(const char*);
extern void yy_delete_buffer(YY_BUFFER_STATE);
extern int yylex(void);
extern int yylineno;
// Variables A..Z stored as uppercase indices.
static VarVal variables[26];
static Line program[MAX_LINES];
static LoopFrame loop_stack[LOOP_STACK_DEPTH];
static ConditionalJumpMap cjump_map[CJUMP_MAP_DEPTH];
static JumpType jump_pending = JUMP_NONE;
static short jump_target = 0;
static unsigned char if_skip = 0;
static unsigned short prog_size = 0;
static short pc = 0;
static size_t loop_top = 0;
static unsigned char cjump_map_size;
static OnGotoFrame on_goto_frame;
static short call_stack[STACK_DEPTH];
static short stack_top = 0;
static PrintMode current_print_mode = PRINT_DEC;
unsigned char is_continue = 1;
unsigned char running = 0;
void yyerror(const char* s);
/**
* @brief Create a new integer variable value.
* @param i Integer value to store.
* @return Variable value containing the integer.
*/
static inline VarVal make_int(int i) {
VarVal v;
v.type = VAL_INT;
v.as.i = i;
return v;
}
/**
* @brief Create a new floating point variable value.
* @param f Floating point value to store.
* @return Variable value containing the floating point number.
*/
static inline VarVal make_float(double f) {
VarVal v;
v.type = VAL_FLOAT;
v.as.f = f;
return v;
}
/**
* @brief Convert a variable value to integer.
* @param v The variable value.
* @return The integer representation of the value.
*/
static inline int to_int(VarVal v) {
if (v.type == VAL_INT) return v.as.i;
if (v.type == VAL_FLOAT) return (int)v.as.f;
return 0;
}
/**
* @brief Convert a variable value to floating point.
* @param v The variable value.
* @return The floating point representation of the value.
*/
static inline double to_float(VarVal v) {
if (v.type == VAL_FLOAT) return v.as.f;
if (v.type == VAL_INT) return (double)v.as.i;
return 0.0;
}
/**
* @brief Compute the absolute value of a floating point number.
* @param d The number.
* @return The absolute value.
*/
static inline double native_fabs(double d) {
return (d < 0.0) ? -d : d;
}
/**
* @brief Retrieve a variable value by its single letter identifier.
* @param c The variable name (A-Z).
* @return The current value of the variable.
*/
static inline VarVal var_get(char c) {
return variables[toupper((unsigned char)c) - 'A'];
}
/**
* @brief Set the value of a variable.
* @param c The variable name (A-Z).
* @param v The value to set.
*/
static inline void var_set(char c, VarVal v) {
variables[toupper((unsigned char)c) - 'A'] = v;
}
/**
* @brief Check if a character signifies the end of a keyword.
* @param c The character to check.
* @return Non-zero if the character is whitespace or null terminator.
*/
static int is_keyword_end(char c) {
return c == '\0' || isspace((unsigned char)c);
}
/**
* @brief Determine the type of a program line from its text.
*
* Detect the kind of a program line by examining its leading keyword.
* This classification is used to build jump targets for FOR/NEXT, WHILE/WEND,
* IF/ELSE/ENDIF, etc.
*
* @param line The program line text.
* @return The detected line type.
*/
static LineType get_prog_line_type(const char* line) {
while (isspace((unsigned char)*line))
line++;
if (strncasecmp(line, "FOR", 3) == 0 && is_keyword_end(line[3]))
return LINE_FOR;
if (strncasecmp(line, "NEXT", 4) == 0 && is_keyword_end(line[4]))
return LINE_NEXT;
if (strncasecmp(line, "WHILE", 5) == 0 && is_keyword_end(line[5]))
return LINE_WHILE;
if (strncasecmp(line, "WEND", 4) == 0 && is_keyword_end(line[4]))
return LINE_WEND;
if (strncasecmp(line, "ELSE", 4) == 0 && is_keyword_end(line[4]))
return LINE_ELSE;
if (strncasecmp(line, "ENDIF", 5) == 0 && is_keyword_end(line[5]))
return LINE_ENDIF;
if (strncasecmp(line, "REPEAT", 6) == 0 && is_keyword_end(line[6]))
return LINE_REPEAT;
if (strncasecmp(line, "UNTIL", 5) == 0 && is_keyword_end(line[5]))
return LINE_UNTIL;
// Identify single line IF and multi-line IF statements.
if (strncasecmp(line, "IF", 2) == 0 && is_keyword_end(line[2])){
const char *then_pos = strcasestr(line, "THEN");
if (then_pos != NULL && is_keyword_end(then_pos[4])) {
then_pos += 4;
while(isspace((unsigned char)*then_pos))
then_pos++;
return (*then_pos == '\0') ? LINE_IF : LINE_SINGLE_IF;
} else {
return LINE_OTHER;
}
}
return LINE_OTHER;
}
/**
* @brief Store a numbered program line in memory.
*
* Store a numbered program line in the program buffer, keeping lines sorted.
* If the text is empty, the line with the matching number is deleted.
*
* @param num The line number.
* @param text The text of the program line.
*/
static void prog_store(int num, const char* text) {
int i;
if (text[0] == '\0') {
for (i = 0; i < prog_size; i++) {
if (program[i].num == num) {
if (i < prog_size - 1) {
memmove(&program[i], &program[i + 1], (prog_size - 1 - i) * sizeof(Line));
}
prog_size--;
return;
}
}
return;
}
for (i = 0; i < prog_size; i++) {
if (program[i].num == num) {
strncpy(program[i].text, text, MAX_LINE_LEN - 1);
program[i].text[MAX_LINE_LEN - 1] = '\0';
program[i].type = get_prog_line_type(text);
return;
}
if (program[i].num > num) {
if (prog_size < MAX_LINES) {
memmove(&program[i + 1], &program[i], (prog_size - i) * sizeof(Line));
prog_size++;
program[i].num = num;
strncpy(program[i].text, text, MAX_LINE_LEN - 1);
program[i].text[MAX_LINE_LEN - 1] = '\0';
program[i].type = get_prog_line_type(text);
}
return;
}
}
if (prog_size < MAX_LINES) {
program[prog_size].num = num;
strncpy(program[prog_size].text, text, MAX_LINE_LEN - 1);
program[prog_size].text[MAX_LINE_LEN - 1] = '\0';
program[prog_size].type = get_prog_line_type(text);
prog_size++;
}
}
/**
* @brief Find the index of a program line by its line number.
* @param num The line number to find.
* @return The index in the program buffer, or -1 if not found.
*/
static int prog_find(int num) {
for (int i = 0; i < prog_size; i++)
if (program[i].num == num) return i;
return -1;
}
/**
* @brief Reset the parser's runtime memory state.
*
* Reset runtime state for a fresh program or after a CLEAR command.
*/
static void flush_memory(void) {
pc = 0;
is_continue = 1;
if_skip = 0;
jump_pending = JUMP_NONE;
jump_target = 0;
cjump_map_size = 0;
current_print_mode = PRINT_DEC;
platform_spi_read_buffer = 0xFF;
loop_top = 0;
stack_top = 0;
memset(variables, 0, sizeof(variables));
}
// Helper to avoid matching nested IF/ELSE/ENDIF entries when scanning for the
// matching line in the conditional jump map.
static int is_line_already_exists(short num) {
for(int i = 0; i < cjump_map_size; i++) {
if((cjump_map[i].root_node_num == num) || (cjump_map[i].end_node_num == num) || (cjump_map[i].else_node_num == num)) {
return 1;
}
}
return 0;
}
/**
* @brief Build the jump map for control flow structures.
*
* Scans the program buffer to pair up structural boundaries like FOR/NEXT,
* WHILE/WEND, REPEAT/UNTIL, and IF/ELSE/ENDIF, populating the conditional
* jump map to quickly resolve jump targets during execution.
*/
static void build_conditional_jump_map(void) {
LineType expected_root_node = LINE_OTHER;
cjump_map_size = 0;
for (int i = 0; i < prog_size; i++) {
if(program[i].type != LINE_OTHER) {
if((program[i].type == LINE_FOR) || (program[i].type == LINE_WHILE) || (program[i].type == LINE_REPEAT) || (program[i].type == LINE_IF)) {
if (cjump_map_size >= CJUMP_MAP_DEPTH) {
err_print(ERR_TOO_MANY_LOOP_BLOCKS);
return;
}
cjump_map[cjump_map_size].type = program[i].type;
cjump_map[cjump_map_size].root_node_num = program[i].num;
cjump_map[cjump_map_size].end_node_num = -1;
cjump_map[cjump_map_size].else_node_num = -1;
cjump_map_size++;
}
else
{
if(program[i].type == LINE_NEXT) expected_root_node = LINE_FOR;
else if(program[i].type == LINE_WEND) expected_root_node = LINE_WHILE;
else if(program[i].type == LINE_UNTIL) expected_root_node = LINE_REPEAT;
else continue;
for(int j = (cjump_map_size - 1); j >= 0; j--) {
if((cjump_map[j].type == expected_root_node) && (cjump_map[j].end_node_num == -1)) {
cjump_map[j].end_node_num = program[i].num;
break;
}
}
}
}
}
// Map components of the IF conditions.
for(int i = (cjump_map_size - 1); i >= 0; i--) {
if((cjump_map[i].type == LINE_IF) && (cjump_map[i].end_node_num == -1)) {
for(int j = (prog_find(cjump_map[i].root_node_num) + 1); j < prog_size; j++) {
if(is_line_already_exists(program[j].num)) {
continue;
}
if(program[j].type == LINE_ELSE) {
cjump_map[i].else_node_num = program[j].num;
continue;
}
if(program[j].type == LINE_ENDIF) {
cjump_map[i].end_node_num = program[j].num;
break;
}
}
}
}
// Looking for any missing nodes.
for (int i = 0; i < cjump_map_size; i++) {
if (cjump_map[i].end_node_num == -1) {
err_print(ERR_MISSING_TERMINATION, cjump_map[i].root_node_num);
}
}
}
/**
* @brief Find the lowest loop node encompassing a given node.
* @param current_node_num The current line number.
* @param node Pointer to store the found loop node.
* @return Non-zero if a loop node was found, zero otherwise.
*/
static unsigned char find_lowest_loop_node(short current_node_num, ConditionalJumpMap **node) {
for(int i = cjump_map_size - 1; i >= 0; i--) {
if((cjump_map[i].type == LINE_FOR) || (cjump_map[i].type == LINE_WHILE) || (cjump_map[i].type == LINE_REPEAT)) {
if((cjump_map[i].root_node_num < current_node_num) && (cjump_map[i].end_node_num > current_node_num)) {
*node = &cjump_map[i];
return 1;
}
}
}
return 0;
}
/**
* @brief Find the end node for a given parent node type and line number.
* @param parent_node_num The line number of the root node (e.g., FOR, WHILE).
* @param node_type The type of the root node.
* @return The line number of the end node, or -1 if not found.
*/
static short find_end_node(short parent_node_num, LineType node_type) {
for(int i = 0; i < cjump_map_size; i++) {
if((cjump_map[i].type == node_type) && (cjump_map[i].root_node_num == parent_node_num)) {
return cjump_map[i].end_node_num;
}
}
return -1;
}
/**
* @brief Find the root node corresponding to an end node.
* @param end_node_num The line number of the end node (e.g., NEXT, WEND).
* @param node_type The type of the root node.
* @return The line number of the root node, or -1 if not found.
*/
static short find_root_node(short end_node_num, LineType node_type) {
for (int i = 0; i < cjump_map_size; i++) {
if (cjump_map[i].type == node_type && cjump_map[i].end_node_num == end_node_num) {
return cjump_map[i].root_node_num;
}
}
return -1;
}
/**
* @brief Find the ELSE node for an IF statement.
* @param parent_node_num The line number of the IF statement.
* @return The line number of the ELSE node, or -1 if not found.
*/
static short find_else_node(short parent_node_num) {
for(int i = 0; i < cjump_map_size; i++) {
if((cjump_map[i].type == LINE_IF) && (cjump_map[i].root_node_num == parent_node_num)) {
return cjump_map[i].else_node_num;
}
}
return -1;
}
/**
* @brief Find the ENDIF node corresponding to an ELSE node.
* @param else_node The line number of the ELSE node.
* @return The line number of the ENDIF node, or -1 if not found.
*/
static short find_end_node_from_else(short else_node) {
for(int i = 0; i < cjump_map_size; i++) {
if((cjump_map[i].type == LINE_IF) && (cjump_map[i].else_node_num == else_node)) {
return cjump_map[i].end_node_num;
}
}
return -1;
}
/**
* @brief Push a return address onto the call stack for GOSUB.
* @param ret_pc The program counter index to return to.
*/
static void stack_push(short ret_pc) {
if (stack_top >= STACK_DEPTH) {
err_print(ERR_GOSUB_STACK_OVERFLOW);
return;
}
call_stack[stack_top++] = ret_pc;
}
/**
* @brief Pop a return address from the call stack for RETURN.
* @return The program counter index to return to.
*/
static short stack_pop(void) {
if (stack_top <= 0) {
err_print(ERR_MISSING_GOSUB);
return 0;
}
return call_stack[--stack_top];
}
/**
* @brief Read a line of text from the terminal.
* @param buffer The buffer to store the read text.
* @param max_len The maximum length of the buffer.
* @return The number of characters read, or -1 on escape.
*/
static int read_terminal_line(char *buffer, unsigned char max_len) {
unsigned char index = 0;
char key;
while(1) {
key = platform_is_key_pressed();
if (key == 27) {
return -1;
}
if (key == '\r') {
chr_print('\r'); chr_print('\n');
break;
}
if (key >= 32 && key <= 126 && index < (max_len - 1)) {
chr_print(key);
buffer[index++] = key;
}
}
buffer[index] = '\0';
return (int)index;
}
/**
* @brief Interactively load a program from the terminal.
*/
static void load_program_from_terminal(void)
{
char line_buffer[MAX_LINE_LEN];
char is_first_line = 1;
str_print("Paste or type your program. To finish press Enter twice.\r\n");
str_print("Press Esc to cancel.\r\n");
while(1) {
int len = read_terminal_line(line_buffer, MAX_LINE_LEN);
if (len <= 0) {
return;
}
char *p = line_buffer;
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == '\0') {
return;
}
if (!isdigit((unsigned char)*p)) {
err_print(ERR_INVALID_LINE_NUM);
return;
}
unsigned short line_num = 0;
char *digit_start = p;
while (isdigit((unsigned char)*p)) {
line_num = (unsigned short)(line_num * 10 + (unsigned char)(*p - '0'));
p++;
}
if (p == digit_start || line_num == 0) {
err_print(ERR_INVALID_LINE_NUM);
return;
}
while (*p == ' ' || *p == '\t') {
p++;
}
if (is_first_line) {
flush_memory();
prog_size = 0;
is_first_line = 0;
}
prog_store(line_num, p);
}
}
%}
%union {
int ival;
double fval;
char cval;
char* sval;
VarVal val;
}
// Terminals for BASIC keywords and operators.
%token <ival> NUMBER
%token <fval> FLOAT_NUMBER
%token <cval> VAR
%token <sval> STRING
%token PRINT IF THEN ELSE ENDIF GOTO INPUT LET GOSUB RETURN CLEAR LIST RUN END CR
%token NEW RAND FOR TO STEP NEXT DELAY ANALOG HIGH LOW PIN IN OUT GET SET ABS
%token REL_LT REL_LE REL_NE REL_GT REL_GE WHILE WEND EXIT REPEAT UNTIL MIN MAX
%token BYTE HBYTE LBYTE LSHIFT RSHIFT MOD WAIT SUM SUMSQ POW AND OR BTRUE BFALSE
%token BAND BOR NOR NAND NOT XNOR XOR HEX BIN BIN8 OCT IFF EQV IMP ASC ON DEG RAD
%token I2C SPI START RESTART STOP INIT READ WRITE INT SGN COS SIN TAN ACOS ASIN ATAN
%token SINH COSH TANH ASINH ACOSH ATANH ATAN2 LOG LOG10 EXP SQRT FLOOR CEIL PWM
%token COT SEC CSC ACOT ASEC ACSC ABSMAX ABSMIN BITSET BITCLR BITGET SOUND LOAD
%type <val> expression term factor sum_args sumsq_args
%type <ival> boolean_expr mode
// Operator precedence and associativity for arithmetic expressions.
%left AND OR
%left '=' REL_NE REL_LT REL_LE REL_GT REL_GE
%left '+' '-'
%left '*' '/' MOD BAND BOR NOR NAND XNOR XOR EQV IMP
%right UMINUS UPLUS INVERT NOT
%%
// Top-level grammar entry point.
program
: line
;
// A line may be a numbered program line or an immediate command.
line
: NUMBER statement CR
| statement CR
| CR
;
statement
: PRINT '(' expr_list ')' { if (!if_skip) str_print("\r\n"); }
| PRINT '(' HEX ',' { if (!if_skip) current_print_mode = PRINT_HEX; }
expr_list ')' { if (!if_skip) { current_print_mode = PRINT_DEC; str_print("\r\n"); } }
| PRINT '(' BIN ',' { if (!if_skip) current_print_mode = PRINT_BIN; }
expr_list ')' { if (!if_skip) { current_print_mode = PRINT_DEC; str_print("\r\n"); } }
| PRINT '(' BIN8 ',' { if (!if_skip) current_print_mode = PRINT_BIN_GRP; }
expr_list ')' { if (!if_skip) { current_print_mode = PRINT_DEC; str_print("\r\n"); } }
| PRINT '(' OCT ',' { if (!if_skip) current_print_mode = PRINT_OCT; }
expr_list ')' { if (!if_skip) { current_print_mode = PRINT_DEC; str_print("\r\n"); } }
| ON expression ','
{
if ((running) && (!if_skip)) {
if($2.type != VAL_INT)
err_print(ERR_REQUIRED_INT);
else {
on_goto_frame.target = $2.as.i;
on_goto_frame.index = 0;
on_goto_frame.target_num = -1;
}
}
}
line_args
{
if ((running) && (!if_skip)) {
if(on_goto_frame.target_num >= 0) {
jump_pending = JUMP_GOSUB;
jump_target = on_goto_frame.target_num;
}
}
}
| DELAY '(' expression ')'
{
if (!if_skip) {
if($3.type != VAL_INT)
err_print(ERR_REQUIRED_INT);
else {
int ms = $3.as.i;
if (ms < 0) ms = 0;
platform_delay_ms(ms);
}
}
}
| PIN '(' expression ',' mode ')'
{
if (!if_skip) {
if($3.type != VAL_INT)
err_print(ERR_REQUIRED_INT);
else
platform_pin_mode($3.as.i, $5);
}
}
| SET '(' expression ',' expression ')'
{
if (!if_skip) {
if(($3.type != VAL_INT) || ($5.type != VAL_INT))
err_print(ERR_REQUIRED_INT);
else
platform_digital_write($3.as.i, $5.as.i);
}
}
| WAIT '(' expression ',' expression ')'
{
if (!if_skip) {
while(is_continue) {
if(($3.type != VAL_INT) || ($5.type != VAL_INT))
err_print(ERR_REQUIRED_INT);
else {
if(platform_digital_read($3.as.i) == $5.as.i) {
break;
}
platform_delay_ms(10);
}
}
}
}
| I2C '(' INIT ')' { if (!if_skip) platform_i2c_init(); }
| I2C '(' START ')' { if (!if_skip) platform_i2c_start(); }
| I2C '(' RESTART ')' { if (!if_skip) platform_i2c_restart(); }
| I2C '(' STOP ')' { if (!if_skip) platform_i2c_stop(); }
| I2C '(' WRITE ',' expression ')'
{
if (!if_skip) {
if($5.type != VAL_INT)
err_print(ERR_REQUIRED_INT);
else
if(!platform_i2c_write($5.as.i)) warn_print(WARN_I2C_RECEIVED_NACK);
}
}
| SPI '(' INIT ')' { if (!if_skip) platform_spi_init(); }
| SPI '(' START ')' { if (!if_skip) platform_spi_select(); }
| SPI '(' STOP ')' { if (!if_skip) platform_spi_deselect(); }
| SPI '(' WRITE ',' expression ')'
{
if (!if_skip) {
if($5.type != VAL_INT)
err_print(ERR_REQUIRED_INT);
else
platform_spi_read_buffer = platform_spi_transfer($5.as.i);
}
}
| SOUND '(' expression ',' expression ')'
{
if(!if_skip) {
platform_play_tone(to_int($3), to_int($5));
}
}
| PWM '(' expression ',' expression ')'
{
if(!if_skip) {
if($3.type != VAL_INT)
err_print(ERR_REQUIRED_INT);
else {
int pwm_value = to_int($5);
if(pwm_value > 255)
err_print(ERR_PWM_OUTOF_RANGE, pwm_value);
else
platform_pwm($3.as.i, pwm_value);
}
}
}
| FOR VAR '=' expression TO expression
{
if ((running) && (!if_skip)) {
if (loop_top >= LOOP_STACK_DEPTH) {
err_print(ERR_LOOP_STACK_OVERFLOW);
} else {
double start = to_float($4);
double limit = to_float($6);
var_set($2, make_float(start));
if (start > limit) {
jump_target = find_end_node(program[pc].num, program[pc].type);
if (jump_target < 0) {
err_print(ERR_MISSING_NEXT);
}
else {
jump_pending = JUMP_CONDITION_SKIP;
}
}
else {
loop_stack[loop_top].type = LOOP_FOR;
loop_stack[loop_top].var = toupper($2);
loop_stack[loop_top].limit = limit;
loop_stack[loop_top].step = 1;
loop_stack[loop_top].ret_pc = pc + 1;
loop_top++;
}
}
}
}
| FOR VAR '=' expression TO expression STEP expression
{
if ((running) && (!if_skip)) {
if (loop_top >= LOOP_STACK_DEPTH) {
err_print(ERR_LOOP_STACK_OVERFLOW);
} else {
double start = to_float($4);
double limit = to_float($6);
double step = to_float($8);
if (step == 0) {
err_print(ERR_STEP_CANNOT_ZERO);
}
else {
var_set($2, make_float(start));
if ((step > 0) ? (start > limit) : (start < limit)) {
jump_target = find_end_node(program[pc].num, program[pc].type);
if (jump_target < 0) {
err_print(ERR_MISSING_NEXT);
}
else {
jump_pending = JUMP_CONDITION_SKIP;
}
}
else {
loop_stack[loop_top].type = LOOP_FOR;
loop_stack[loop_top].var = toupper($2);
loop_stack[loop_top].limit = limit;
loop_stack[loop_top].step = step;
loop_stack[loop_top].ret_pc = pc + 1;
loop_top++;
}
}
}
}
}
| NEXT VAR
{
if ((running) && (!if_skip)) {
if (loop_top == 0) {
err_print(ERR_MISSING_FOR);
}
else if (loop_stack[loop_top - 1].type != LOOP_FOR) {
err_print(ERR_MISSING_FOR);
}
else if (loop_stack[loop_top - 1].var != toupper($2)) {
err_print(ERR_MISMATCH_NEXT_VAR);
} else {
LoopFrame* f = &loop_stack[loop_top - 1];
double newval = to_float(var_get(f->var)) + f->step;
var_set(f->var, make_float(newval));
int done = (f->step > 0) ? (newval > f->limit) : (newval < f->limit);
if (!done) {
jump_pending = JUMP_GOTO;
jump_target = program[f->ret_pc].num;
} else {
loop_top--;
}
}
}
}
| WHILE boolean_expr
{
if ((running) && (!if_skip)) {
if (!$2) {
jump_target = find_end_node(program[pc].num, LINE_WHILE);
if (jump_target < 0)
err_print(ERR_MISSING_WEND);
else
jump_pending = JUMP_CONDITION_SKIP;
}
}
}
| WEND
{
if ((running) && (!if_skip)) {
jump_target = find_root_node(program[pc].num, LINE_WHILE);
if (jump_target < 0)
err_print(ERR_MISSING_WHILE);
else
jump_pending = JUMP_GOTO;
}
}
| REPEAT
{
if ((running) && (!if_skip)) {
if(find_end_node(program[pc].num, LINE_REPEAT) < 0) {
err_print(ERR_MISSING_UNTIL);
}
}
}
| UNTIL boolean_expr
{
if ((running) && (!if_skip)) {
if (!$2) {
jump_target = find_root_node(program[pc].num, LINE_REPEAT);
if (jump_target < 0)
err_print(ERR_MISSING_REPEAT);
else
jump_pending = JUMP_CONDITION_SKIP;
}
}
}
| EXIT
{
if ((running) && (!if_skip)) {
ConditionalJumpMap* lowest_loop_node = NULL;
if(find_lowest_loop_node(program[pc].num, &lowest_loop_node)) {
jump_target = lowest_loop_node->end_node_num;
if((lowest_loop_node->type == LINE_FOR) && (loop_top > 0)) {
loop_top--;
}
if (jump_target < 0) {
err_print(ERR_EXIT_WITHOUT_LOOP);
}
else {
jump_pending = JUMP_CONDITION_SKIP;
}
}
else {
err_print(ERR_EXIT_WITHOUT_LOOP);
}
}
}
| IF boolean_expr THEN
{
// Only evaluate the condition if we're not already skipping due to an outer IF.
if (!if_skip) {
if_skip = !$2;
}
}
statement
| IF boolean_expr THEN
{
if ((running) && (!if_skip)) {
if(!$2)
{
short else_node = find_else_node(program[pc].num);
if(else_node >= 0) {
jump_target = else_node;
}
else {
jump_target = find_end_node(program[pc].num, LINE_IF);
}
if(jump_target < 0) {
err_print(ERR_MISSING_ENDIF);
}
else {
jump_pending = JUMP_CONDITION_SKIP;
}
}
}
}
| ELSE
{
if((running) && (!if_skip))
{
jump_target = find_end_node_from_else(program[pc].num);
if(jump_target < 0) {
err_print(ERR_MISSING_ENDIF);
}
else {
jump_pending = JUMP_CONDITION_SKIP;
}
}
}
| ENDIF
{
if((running) && (!if_skip)) {
if(find_root_node(program[pc].num, LINE_IF) == -1) {
if(program[pc].type != LINE_SINGLE_IF) {
err_print(ERR_ENDIF_WITHOUT_IF);
}
}
}
}
| GOTO expression
{
if (!if_skip) { jump_pending = JUMP_GOTO; jump_target = to_int($2); }
}
| INPUT '(' var_list ')'
| LET VAR '=' expression