forked from thewhoo/ifj15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
1240 lines (1007 loc) · 30.1 KB
/
parser.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
/*
* Course IFJ @ FIT VUT Brno, 2015
* IFJ15 Interpreter Project
*
* Authors:
* Lukas Osadsky - xosads00
* Pavol Plaskon - xplask00
* Pavel Pospisil - xpospi88
* Matej Postolka - xposto02
*
* Unless otherwise stated, all code is licensed under a
* GNU General Public License v2.0
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "parser.h"
#include "adt.h"
#include "lex.h"
#include "galloc.h"
#include "enums.h"
#include "error.h"
#include "stack.h"
#include "ial.h"
#include "ilist.h"
#include "shared.h"
#include "interpret.h"
#include "expr.h"
#define RETURN_VAR_NAME "return"
#define MAIN_FUNC_NAME "main"
TToken* token;
// Forward declarations of state functions
bool PROG();
bool FUNCTION_DECL();
bool DATA_TYPE();
bool FUNC_DECL_PARAMS();
bool FUNC_DECL_PARAMS_NEXT();
bool NESTED_BLOCK(bool);
bool NBC();
bool DECL_OR_ASSIGN();
bool DECL_ASSIGN();
bool ASSIGN();
bool HARD_VALUE();
bool IF_STATEMENT();
bool ELSE_STATEMENT();
bool COUT();
bool COUT_NEXT();
bool COUT_OUTPUT();
bool CIN();
bool CIN_NEXT();
bool FOR_STATEMENT();
bool RETURN();
// Forward declarations of control functions
TFunction *getNewFunction();
TVariable *getNewVariable();
void pushParam();
void storeFuncName();
void storeVarName();
void storeFunction();
void storeVariable();
TVariable *findVariable();
TList_item *createInstruction();
TList_item *createPseudoFrame();
void killPseudoFrame();
void checkFunctionDefinitions();
void pushVar(TVariable*);
void logger();
enum
{
T_FUNC,
T_VAR,
T_BLOCK,
T_IF
};
void logger(char *c)
{
#ifdef DEBUG_MODE
fprintf(stdout, "parser: %s\n", c);
#endif // PARSER_DEBUG
// Surpress unused variable warning
(void)c;
}
TList_item *createInstruction(int type, void *addr1, void *addr2, void *addr3)
{
TList_item *ins = gmalloc(sizeof(TList_item));
ins->ins_type = type;
ins->addr1 = addr1;
ins->addr2 = addr2;
ins->addr3 = addr3;
return ins;
}
void checkFunctionDefinitions()
{
TFunction *found;
for(unsigned int i = 0; i < G.g_globalTab->htab_size; i++)
{
for(htab_item *item = G.g_globalTab->list[i]; item != NULL; item = item->next)
{
found = item->data.function;
// All functions must be defined at some point
if(!found->defined)
exit_error(E_SEMANTIC_DEF);
// Main must match the fixed type signature
if(!strcmp(found->name, MAIN_FUNC_NAME))
{
if(!(found->return_type == TYPE_INT && stack_empty(found->params_stack)))
exit_error(E_SEMANTIC_DEF);
}
}
}
}
// ============== Control functions start here ==============
void storeFunction(TFunction *f)
{
// Check if the function has already been declared or defined
htab_item *result = htab_lookup(G.g_globalTab, f->name);
if(result)
{
// Retrieve found function
TFunction *found = result->data.function;
// Check if return type matches
if(!(found->return_type == f->return_type))
exit_error(E_SEMANTIC_DEF);
// Check if data types of parameters match
for(int i = 0; i < found->params_stack->used; i++)
{
TVariable *providedVar = f->params_stack->data[i];
TVariable *foundVar = found->params_stack->data[i];
// Param list length mismatch
if(providedVar == NULL || foundVar == NULL)
exit_error(E_SEMANTIC_DEF);
if((providedVar->var_type != foundVar->var_type) || strcmp(providedVar->name, foundVar->name))
exit_error(E_SEMANTIC_DEF);
}
if(found->defined)
{
// A redefinition is wrong!
if(f->defined)
exit_error(E_SEMANTIC_DEF);
// A redeclaration after definition is OK, but don't do anything
else
return;
}
// Replace forward declaration with definition (and update the return var address)
f->return_var = found->return_var;
result->data.function = f;
}
else
{
// Function has not been declared before
htab_item *newFunc = htab_insert(G.g_globalTab, f->name);
newFunc->data.function = f;
newFunc->data_type = TYPE_FUNCTION;
}
}
// Returns pointer to first instruction in frame (need for if-statements and cycles)
TList_item *createPseudoFrame(int type)
{
// Get current function
TFunction *current = stack_top(G.g_frameStack);
// Create and push a pseudo-function which represents a nested block
TFunction *f = gmalloc(sizeof(TFunction));
f->name = NULL;
f->return_type = TYPE_PSEUDO;
f->defined = 1;
f->ins_list = current->ins_list;
f->local_tab = htab_init(HTAB_SIZE);
f->params_stack = NULL;
f->return_var = NULL;
f->var_count = 0;
// Push this on the frameStack
stack_push(G.g_frameStack, f);
// Create the instruction
if(type == T_BLOCK)
{
TList_item *ins = createInstruction(INS_PUSH_TAB, f, NULL, NULL);
list_insert(f->ins_list, ins);
return ins;
}
else
{
// Conditional jump skeleton
TList_item *ins1 = createInstruction(INS_CJMP, NULL, NULL, NULL);
list_insert(f->ins_list, ins1);
// We still need to push local table
TList_item *ins2 = createInstruction(INS_PUSH_TAB, f, NULL, NULL);
list_insert(f->ins_list, ins2);
return ins1;
}
return NULL;
}
void killPseudoFrame()
{
// Get current function
TFunction *f = stack_top(G.g_frameStack);
// Kill the pseudo frame
TList_item *ins = createInstruction(INS_POP_TAB, NULL, NULL, NULL);
list_insert(f->ins_list, ins);
}
void storeNewVariable(TFunction *f, TVariable *v)
{
// Check for redeclaration or clash with function name
if(htab_lookup(f->local_tab, v->name) || htab_lookup(G.g_globalTab, v->name))
exit_error(E_SEMANTIC_DEF);
htab_item *newVar = htab_insert(f->local_tab, v->name);
newVar->data.variable = v;
newVar->data_type = TYPE_VARIABLE;
}
void storeNewConstant(TVariable *c)
{
if(c->var_type == TYPE_STRING)
{
if(htab_lookup(G.g_constTabStr, c->name))
return;
htab_item *newConst = htab_insert(G.g_constTabStr, c->name);
newConst->data.variable = c;
newConst->data_type = TYPE_VARIABLE;
}
else
{
if(htab_lookup(G.g_constTabNum, c->name))
return;
htab_item *newConst = htab_insert(G.g_constTabNum, c->name);
newConst->data.variable = c;
newConst->data_type = TYPE_VARIABLE;
}
}
void pushVar(TVariable *v)
{
// Grab current ins list
TFunction *f = stack_top(G.g_frameStack);
TList_item *ins = createInstruction(INS_PUSH_VAR, v, NULL, NULL);
list_insert(f->ins_list, ins);
}
// This will search the frame stack for the var and return it (returns NULL if the var is not found)
TVariable *findVariable(char *name)
{
// Search all the frames for the variable
for(int i=G.g_frameStack->used-1; i >= 0; i--)
{
TFunction *f = G.g_frameStack->data[i];
htab_item *found = htab_lookup(f->local_tab, name);
if(found)
return found->data.variable;
}
return NULL;
}
TFunction *getNewFunction()
{
TFunction *f = gmalloc(sizeof(TFunction));
f->name = NULL;
f->return_type = 0;
f->defined = 0;
f->ins_list = list_init();
f->local_tab = htab_init(HTAB_SIZE);
f->params_stack = stack_init();
f->return_var = getNewVariable();
f->return_var->name = RETURN_VAR_NAME;
f->return_var->constant = true;
f->var_count = 0;
return f;
}
TVariable *getNewVariable()
{
TVariable *v = gmalloc(sizeof(TVariable));
v->var_type = 0;
v->name = NULL;
v->initialized = 0;
v->constant = false;
return v;
}
void pushParam(TFunction *f, TVariable *p)
{
// Param or function with same name already declared, error!
if(htab_lookup(f->local_tab, p->name) || htab_lookup(G.g_globalTab, p->name) || !strcmp(p->name, f->name))
exit_error(E_SEMANTIC_DEF);
stack_push(f->params_stack, p);
htab_item *param = htab_insert(f->local_tab, p->name);
p->initialized = true;
param->data.variable = p;
param->data_type = TYPE_VARIABLE;
}
void storeFuncName(TFunction *f)
{
f->name = token->data;
}
void storeVarName(TVariable *v)
{
v->name = token->data;
}
// ============== End of control functions ===============
// ============== Rule functions start here ==============
bool PROG()
{
logger("enter PROG");
// We need a function declaration or nothing
if (token->type == TOKEN_INT || token->type == TOKEN_DOUBLE || token->type == TOKEN_STRING)
return FUNCTION_DECL() && PROG();
else if (token->type == TOKEN_EOF)
return true;
// Syntax error
return false;
}
bool FUNCTION_DECL()
{
logger("enter FUNCTION_DECL");
TFunction *currentFunc = getNewFunction();
// Tell everyone we are currently in a new function (block)
stack_push(G.g_frameStack, currentFunc);
// No valid data type detected --> Syntax error
if(!DATA_TYPE(currentFunc, T_FUNC))
return false;
// Store the name
if (token->type == TOKEN_IDENTIFIER)
{
storeFuncName(currentFunc);
token = get_token();
}
// Builtin function redefinition (this must be handled separately as it is not a syntax error)
else if(token->type == TOKEN_LENGTH || token->type == TOKEN_SUBSTR || token->type == TOKEN_CONCAT || token->type == TOKEN_FIND || token->type == TOKEN_SORT)
exit_error(E_SEMANTIC_DEF);
// Syntax error
else
return false;
if (token->type == TOKEN_LROUND_BRACKET)
token = get_token();
// Syntax error
else
return false;
// If all params were processed, store function and process the function block
if(FUNC_DECL_PARAMS(currentFunc))
{
if (token->type == TOKEN_RROUND_BRACKET)
{
token = get_token();
// Function has been declared but not defined
if(token->type == TOKEN_SEMICOLON)
{
currentFunc->defined = false;
// Store the complete function "object" in the global table
storeFunction(currentFunc);
logger("stored function in G.globalTab");
token = get_token();
}
// Process function block if the function is defined
else
{
currentFunc->defined = true;
// Store the complete function "object" in the global table
storeFunction(currentFunc);
logger("stored function in G.globalTab");
if(!NESTED_BLOCK(true))
return false;
// printf("parser: total variables in function %s: %d\n",currentFunc->name, currentFunc->var_count);
}
}
}
return true;
}
// Detect and store the data type of the new function/variable
bool DATA_TYPE(void *object, int type)
{
logger("enter DATA_TYPE");
TFunction *f = NULL;
TVariable *v = NULL;
if(type == T_VAR)
v = object;
else
f = object;
// Add the data type to the currently processed function/variable
switch(token->type)
{
case TOKEN_INT:
if (type == T_VAR)
v->var_type = TYPE_INT;
else
f->return_type = TYPE_INT;
break;
case TOKEN_DOUBLE:
if (type == T_VAR)
v->var_type = TYPE_DOUBLE;
else
f->return_type = TYPE_DOUBLE;
break;
case TOKEN_STRING:
if (type == T_VAR)
v->var_type = TYPE_STRING;
else
f->return_type = TYPE_STRING;
break;
// Syntax error
default:
return false;
}
token = get_token();
return true;
}
bool FUNC_DECL_PARAMS(TFunction *func)
{
logger("enter FUNC_DECL_PARAMS");
// If an identifier arrives, we must create and store a new param variable
if(token->type == TOKEN_INT || token->type == TOKEN_DOUBLE || token->type == TOKEN_STRING)
{
TVariable *param = getNewVariable();
// Record the data type of the new param, then proceed
if(DATA_TYPE(param, T_VAR) && token->type == TOKEN_IDENTIFIER)
{
// Record the name of the param
storeVarName(param);
token = get_token();
// Push param on stack
pushParam(func, param);
// Process next param
if(!FUNC_DECL_PARAMS_NEXT(func))
return false;
return true;
}
else
return false;
}
// If auto arrives, its a semantic error and must be handled separately
else if(token->type == TOKEN_AUTO)
exit_error(E_SEMANTIC_OTHERS);
// No more params, all good
else if(token->type == TOKEN_RROUND_BRACKET)
return true;
return false;
}
bool FUNC_DECL_PARAMS_NEXT(TFunction *func)
{
logger("enter FUNC_DECL_PARAMS_NEXT");
// Next param must start with comma if it exists
if (token->type == TOKEN_COMMA)
{
token = get_token();
// This is not ideal, but i'm too lazy to rewrite the grammar...
if(token->type == TOKEN_RROUND_BRACKET)
return false;
return FUNC_DECL_PARAMS(func);
}
else if (token->type == TOKEN_RROUND_BRACKET)
return true;
else
return false;
}
bool NESTED_BLOCK(bool removeBlockAtEnd)
{
logger("enter NESTED_BLOCK");
TFunction *func = stack_top(G.g_frameStack);
if (token->type == TOKEN_LCURLY_BRACKET)
{
token = get_token();
if(NBC() && token->type == TOKEN_RCURLY_BRACKET)
{
// Loops don't want us to kill their blocks
if(removeBlockAtEnd)
{
// If this is not a function but just a block, kill the pseudo frame
if(func->return_type == TYPE_PSEUDO)
killPseudoFrame();
// Pop current function from frame stack
stack_pop(G.g_frameStack);
}
token = get_token();
return true;
}
}
return false;
}
bool NBC()
{
logger("enter NBC");
switch(token->type)
{
case TOKEN_AUTO:
case TOKEN_INT:
case TOKEN_DOUBLE:
case TOKEN_STRING:
return DECL_OR_ASSIGN() && NBC();
case TOKEN_IDENTIFIER:
return ASSIGN() && NBC();
case TOKEN_IF:
return IF_STATEMENT() && NBC();
case TOKEN_COUT:
return COUT() && NBC();
case TOKEN_CIN:
return CIN() && NBC();
case TOKEN_FOR:
return FOR_STATEMENT() && NBC();
case TOKEN_LCURLY_BRACKET:
createPseudoFrame(T_BLOCK);
return NESTED_BLOCK(true) && NBC();
case TOKEN_RETURN:
return RETURN() && NBC();
case TOKEN_RCURLY_BRACKET:
return true;
default:
return false;
}
}
bool DECL_OR_ASSIGN()
{
logger("enter DECL_OR_ASSIGN");
// Create new variable "object"
TVariable *var = getNewVariable();
pushVar(var);
// The variable belongs to the function or block on top of the frame stack
TFunction *func = stack_top(G.g_frameStack);
// Increment function var count
(func->var_count)++;
// Received identifier, expecting declaration or declaration with assignment
if (token->type == TOKEN_INT || token->type == TOKEN_DOUBLE || token->type == TOKEN_STRING)
{
// Store data type in variable object
if(DATA_TYPE(var, T_VAR) && token->type == TOKEN_IDENTIFIER)
{
// Store variable name
storeVarName(var);
token = get_token();
// Process assignment
if(!(DECL_ASSIGN(var) && token->type == TOKEN_SEMICOLON))
// Syntax error
return false;
// Store variable in function symbol table
storeNewVariable(func, var);
token = get_token();
}
// Syntax error
else
return false;
return true;
}
// Received auto, declaration must contain an assignment
else if (token->type == TOKEN_AUTO)
{
token = get_token();
var->var_type = TYPE_AUTO;
if (token->type == TOKEN_IDENTIFIER)
{
storeVarName(var);
token = get_token();
}
// Syntax error
else
return false;
// There must be an assignment
if (token->type == TOKEN_ASSIGN)
{
expression(var, func->ins_list);
}
// Auto derivation error
else
exit_error(E_AUTO_TYPE);
token = get_token();
// Syntax error
if(!(token->type == TOKEN_SEMICOLON))
return false;
// TODO: Set init and type
// Store variable in function symbol table
storeNewVariable(func, var);
token = get_token();
return true;
}
// Received unexpected token, syntax error
return false;
}
bool DECL_ASSIGN(TVariable *var)
{
logger("enter DECL_ASSIGN");
// Current function
TFunction *func = stack_top(G.g_frameStack);
// We must initialize the variable
if(token->type == TOKEN_ASSIGN)
{
expression(var, func->ins_list);
token = get_token();
return true;
}
// Variable was only declared, not initialized
else if(token->type == TOKEN_SEMICOLON)
{
return true;
}
// Syntax error
return false;
}
bool ASSIGN()
{
logger("enter ASSIGN");
// Current function
TFunction *func = stack_top(G.g_frameStack);
if(token->type == TOKEN_IDENTIFIER)
{
// Retrieve the variable which we will be assigning to
TVariable *var = findVariable(token->data);
// Cannot assign to an undefined variable
if(var == NULL)
exit_error(E_SEMANTIC_DEF);
token = get_token();
if(token->type == TOKEN_ASSIGN)
{
// Use a custom list if it is specified
expression(var, func->ins_list);
token = get_token();
if(token->type == TOKEN_SEMICOLON)
{
token = get_token();
return true;
}
}
// Syntax error
else
return false;
}
// Syntax error, unexpected token
return false;
}
bool HARD_VALUE(TVariable **v)
{
logger("enter HARD_VALUE");
// If we get a magic value, add to constants table if its not already there
if (token->type == TOKEN_INT_VALUE || token->type == TOKEN_DOUBLE_VALUE || token->type == TOKEN_STRING_VALUE)
{
// New constant
TVariable *var = getNewVariable();
// Set constant properties
switch(token->type)
{
case TOKEN_INT_VALUE:
var->var_type = TYPE_INT;
var->name = token->data;
var->data.i = strtod(token->data, NULL);
break;
case TOKEN_DOUBLE_VALUE:
var->var_type = TYPE_DOUBLE;
var->name = token->data;
var->data.d = strtod(token->data, NULL);
break;
case TOKEN_STRING_VALUE:
var->var_type = TYPE_STRING;
var->name = token->data;
var->data.str = var->name;
break;
}
var->constant = true;
var->initialized = true;
// Pass pointer to constant to caller
*v = var;
token = get_token();
return true;
}
// Syntax error
else
return false;
}
bool IF_STATEMENT()
{
logger("enter IF_STATEMENT");
// Current function
TFunction *func = stack_top(G.g_frameStack);
// We need to store the result of the evaluated expression
TVariable *var = getNewVariable();
var->initialized = true;
var->constant = true;
var->name = "";
if(token->type == TOKEN_IF)
{
token = get_token();
if(token->type == TOKEN_LROUND_BRACKET)
{
// Evaluate condition
expression(var, func->ins_list);
token = get_token();
if(token->type == TOKEN_RROUND_BRACKET)
token = get_token();
else
return false;
// Create the "then" block pseudo frame and get ptr to first instruction
TList_item *condIns = createPseudoFrame(T_IF);
condIns->addr1 = var;
// Create the instruction for else block skip
TList_item *skipElse = createInstruction(INS_JMP, NULL, NULL, NULL);
if(NESTED_BLOCK(true))
{
// Insert else skip instruction
list_insert(func->ins_list, skipElse);
// Create "false" label after if block ends
TList_item *ifEnd = createInstruction(INS_LAB, NULL, NULL, NULL);
list_insert(func->ins_list, ifEnd);
// Ammend initial conditional jump address to ifEnd
condIns->addr2 = ifEnd;
if(ELSE_STATEMENT(skipElse))
return true;
}
}
}
// Syntax error
return false;
}
bool ELSE_STATEMENT(TList_item *skipIns)
{
logger("enter ELSE_STATEMENT");
TFunction *func = stack_top(G.g_frameStack);
if(token->type == TOKEN_ELSE)
{
// Create pseudo frame for else block
createPseudoFrame(T_BLOCK);
token = get_token();
if(NESTED_BLOCK(true))
{
// Create skip label after block ends
TList_item *elseEnd = createInstruction(INS_LAB, NULL, NULL, NULL);
list_insert(func->ins_list, elseEnd);
// Amend if true jump to skip else block
skipIns->addr1 = elseEnd;
return true;
}
}
/*
We might implement this later as SIMPLE extension
switch(token->type)
{
case TOKEN_ELSE:
token = get_token();
ret = NESTED_BLOCK();
break;
case TOKEN_AUTO:
case TOKEN_INT:
case TOKEN_DOUBLE:
case TOKEN_STRING:
case TOKEN_IDENTIFIER:
case TOKEN_LENGTH:
case TOKEN_SUBSTR:
case TOKEN_CONCAT:
case TOKEN_FIND:
case TOKEN_SORT:
case TOKEN_IF:
case TOKEN_COUT:
case TOKEN_CIN:
case TOKEN_FOR:
case TOKEN_LCURLY_BRACKET:
case TOKEN_RETURN:
case TOKEN_RCURLY_BRACKET:
ret = true;
break;
}
*/
return false;
}
bool COUT()
{
logger("enter COUT");
if(token->type == TOKEN_COUT)
{
token = get_token();
if(token->type == TOKEN_COUT_BRACKET)
{
token = get_token();
if(COUT_OUTPUT() && COUT_NEXT() && token->type == TOKEN_SEMICOLON)
{
token = get_token();
return true;
}
}
}
return false;
}
bool COUT_OUTPUT()
{
logger("enter COUT_OUTPUT");
// Get current function
TFunction *func = stack_top(G.g_frameStack);
// Print a variable
if(token->type == TOKEN_IDENTIFIER)
{
// Find the variable
TVariable *var = findVariable(token->data);
// Undefined var, semnatic error
if(var == NULL)
exit_error(E_SEMANTIC_DEF);
// Create instruction
TList_item *ins = createInstruction(INS_COUT, var, NULL, NULL);
// Append instruction to current ins list
list_insert(func->ins_list, ins);
token = get_token();
return true;
}
// Print a literal
else
{
// Store the constant here
TVariable *con = getNewVariable();
if(!HARD_VALUE(&con))
// Syntax error
return false;
TList_item *ins = createInstruction(INS_COUT, con, NULL, NULL);
list_insert(func->ins_list, ins);
return true;
}
// Syntax error
return false;
}
bool COUT_NEXT()
{
logger("enter COUT_NEXT");
if(token->type == TOKEN_COUT_BRACKET)
{
token = get_token();
return COUT_OUTPUT() && COUT_NEXT();
}
else if (token->type == TOKEN_SEMICOLON)
return true;
// Syntax error
return false;
}
bool CIN()
{
logger("enter CIN");
// Current function
TFunction *func = stack_top(G.g_frameStack);