-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.cpp
2009 lines (1840 loc) · 67.6 KB
/
translator.cpp
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
// vim:ts=4:sw=4:
// Interactive BASIC Compiler Project
// File: translator.cpp - contains code for the translator class
// Copyright (C) 2010-2012 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2010-03-01 initial release
//
// 2010-03-20 continued development - functions now implemented for
// handling simple expressions (basic operands and operators)
//
// 2010-03-21 needed to pop final result off of done stack, added checks
// added check for EOL at begin of line
// changed unary operator check, corrected in add_operator()
//
// 2010-03-25 added parentheses support
// added switch for special operator token processing
//
// 2010-03-26 corrected issue with setting last_precedence by moving it from
// being set with '(' to being set with ')' added
// do_pending_paren() to replace code in three locations, and call
// from two new locations
//
// 2010-04-02 added array and function support
// added count stack to keep track of (count) commas within
// arrays (subscripts) and functions (arguments), and to make
// sure commas are not found within regular parentheses
// modified close parentheses processing to handle the operands
// (subscripts and arguments) within arrays and functions
//
// 2010-04-04 added internal function number of arguments checking
// changed argument of add_token() to reference so that it can
// modified to point to the token with the error
//
// 2010-04-11 added assignment operator handling including multiple
// assignments (additional comma handling)
// 2010-04-12 added reference support
// 2010-04-13 changed add_operator() argument to reference so that different
// token can be returned
// added code to add_operator() to check for references for
// assignment operator
// 2010-04-14 added code to add_operator() to check for references for
// list assignment operator
// 2010-04-16 added mode checking for the open parentheses token processing
// clear reference flag to top item in done stack for close paren
// added count stack is empty check before checking mode for open
// parentheses, equal operator and no special operator
//
// 2010-04-25 set default data type for operands
// corrected memory leak of comma and close parentheses tokens
// added data type handling for internal functions and operators
// implemented new find_code() and match_code() functions for data
// type handling
// 2010-04-26 changed bug error names
//
// 2010-05-08 implemented data type handling for assignment operators
// modified find_code() to handle references on first operand
// modified find_code() to get number of associated codes
// modified match_code() to handle references on first operand
// added last_operand argument to find_code()
//
// 2010-05-15 added temporary string support:
// changed output list and done stack from Token* to RpnItem*
// for arrays and functions, save pointers to operands
// for internal functions with string operands, saved operands
// for operators with string operands, saved operands
// moved find_code() operand[] array to class for all to access
// removed operandp[] argument from match_code(), now uses member
// added entries to match_code() conversion code table for TmpStr
//
// 2010-05-16 corrected problem where saved operand was not pointing to a
// conversion code that was inserted after the operand - the
// operand was set to the return value of the append call when the
// conversion code is inserted
//
// 2010-05-19 added sub-string support:
// in find_code() rearranged first operand code and added check for
// a sub-string function to get the string operands reference
// flag
// added SubStr entries to the convert code table in match_code()
// 2010-05-21 when an error occurs with a sub-string function where its string
// operand cannot be assigned, the change the token with the error
// to the string operand that can't be assigned instead of pointing
// to the sub-string function
//
// 2010-05-22 corrected issue where string list assignments were not saving
// all of the operands instead of just the last two
// updated add_operator() to handle mix string list assignments
//
// 2010-05-28 changed hold_stack and done_stack from List to SimpleStack,
// which required some small changes of push and pop calls
// replaced switch and case code to token handler functions
// moved special token processing in switch statement to individual
// token handler functions, switch statement was removed
// 2010-05-29 moved no special operator code was also moved to a token handler
// function
// updated for moving/renaming Translator::Status and
// Translator::Mode to TokenStatus and TokenMode
// changed do_pending_paren() from appending dummy parentheses
// token to output list to setting parentheses sub-code of the
// last token appended to list
// changed argument of do_pending_paren() from table entry index
// to token pointer (which was only used to get the precedence);
// this corrects a problem where the token on top of the hold
// stack was an identifier with parentheses (array or function)
// because these tokens don't have an index value, therefore the
// precedence access function needs to be used, which takes these
// token types into account
// added setting of comma sub-code flag of assignment list token
// when receiving an equal in comma assignment mode
// added section of code at beginning of add_token for doing
// initial processing for Command tokens when received,
// if command has token_mode table entry value then command is
// pushed on new command stack, otherwise unexpected command
// occurs
// added check to add_operator() that if operator is an assignment
// (Reference_Flag is set) then check if there is a LET command
// on the command stack, pop it, and set LET sub-code flag on
// assignment operator token
//
// 2010-06-01 corrected a bug when an operand is processed, if the mode is
// currently Command mode, the mode needs to be changed to
// Assignment mode to prevent commands from being accepted
// additional support for commands and command stack
// added support for print-only functions
// 2010-06-02 added token handler for semicolon (for PRINT command)
// 2010-06-03 added PRINT support for comma
// created expression_end() from parts of EndOfLine_Handler for
// end of expression checks (also used by comma and semicolon)
// 2010-06-04 implemented new add_print_code() function to be used by
// Comma_Handler(), SemiColon_Handler() and Print_CmdHandler()
// 2010-06-05 implemented Assign_CmdHandler() and Print_CmdHandler()
// push assignment token to command stack instead of done stack so
// the end of statement is handled properly and simply
// 2010-06-06 correctly check for expression only mode at end-of-line
// end-of-line no longer pops a result off from done stack, which
// should now be empty (commands deal with done stack) except for
// special expression only mode
// added support for end expression flag - codes that can end an
// expression (currently comma, semicolon and EOL)
// switch back to operand state after comma and semicolon
// 2010-06-08 set a print function flag in print command stack item to
// indicate a print function flag was just added to the output,
// which semicolon checks for and sets the semicolon sub-code
// flag on the print function token in case the print function is
// at the end of the print statement
// moved end expression flag check to operand section before unary
// operator check, if in the middle of a parentheses expression
// or array/function, then return expected closing parentheses
// error
// 2010-06-09 changed count stack from holding just a counter to holding the
// counter and the expected number of arguments for internal
// functions, so that the number of arguments for internal
// functions can be checked as each comma token is received
// 2010-06-10 added new state FirstOperand set at the beginning of an
// expression
// renamed errors for clarity
// 2010-06-11 added check to detect invalid print-only functions sooner
// 2010-06-13 changed to push AssignList token to hold stack when first comma
// token is received instead of when equal token is received
// (necessary for proper error condition tests)
// Let_CmdHandler implemented, necessary to catch errors when a
// LET command statement was not completed correctly
// 2010-06-14 added check for immediate command token (append to output list
// and return Done)
// added paren_status() from duplicated code in add_token() and
// expression_end()
// 2010-06-10/14 various changes to return appropriate easy to understand
// error messages
//
// 2010-06-24 corrected errors for internal functions not in expressions
// except for sub-string functions that can be used for
// assignments
// at end of expression, added check if in assignment due to an
// an array or sub-string function, to report correct errors
// 2010-06-26 added end of statement checking in the binary operator section
// made more error reporting corrections related to checking if the
// done stack is empty to determine the correct error to report
// 2010-06-29 added support for expression type
// added data type to expression type conversion array
// 2010-06-30 continues to add support for expression type
// added expected variable messages for assign list errors
// 2010-07-01 moved cvtcode_have_need[], equivalent_datatype[] to begin of
// source file so all can access
// implemented new assign and assign list code to Comma_Handler(),
// Semicolon_Handler() and Assign_CmdHandler() with support
// functions set_assign_command() and check_assignlist_token()
// removed last_operand argument from find_code(), which was for
// assign list support
// 2010-07-02 added more expr_type checking code
// 2010-07-03 added errstatus_datatype[]
// 2010-07-04 temporarily removed all expr_type code (may be removed)
// 2010-07-05 many changes for error reporting
// 2010-07-06 many changes for error reporting
//
// 2010-07-29 set reference of sub-string tokens within assignments so that
// first operand (string being assigned) can be checked
// 2010-08-01 removed EqualAssignment, replaced CommaAssignment with
// AssignmentList (multiple equal assignment statements no longer
// being supported)
// 2010-08-07 begin modification of find_code() to only process one operand
// at a time
// removed match_code() since it's not necessary to match all
// operands of operators or internal functions
// 2010-08-14 changed the way multiple argument codes are handled (due to
// processing one operand at a time)
// 2010-08-30 begin implementation of process_final_operand() to handle
// processing of last operand of operand or internal function
// 2010-09-11 begin implementation of leaving only string operands on stack
// to be attached to an operand or internal function token
// 2010-10-01 corrected code to return expected type errors
// 2010-10-10 check first operand of sub-string assignment is not a string
// variable
// 2010-12-24 corrected process_final_operand() to pop the correct number of
// string operands to attach
// 2010-12-25 modified to leave string operand on stack for sub-string
// functions and not push the sub-string token to the done stack
//
// 2010-12-28 corrected call to process_final_operand() in add_print_code()
// 2010-12-29 modified EndOfLine_Handler() to wait to pop the command on top
// of the command stack until after the command handler is called
// 2011-01-01 made None_DataType return expected error instead of bug error
// 2011-01-03 implemented new function get_expr_datatype() to get the current
// datatype of the expression by looking back at what was
// processed so far; called when an error occurs so that the
// appropriate error can be reported)
// 2011-01-04 assign datatype to open parentheses when being pushed to the
// hold stack (to be used later when getting the datatype for an
// expression)
//
// 2011-01-08 get datatype of expression to get appropriate error when a
// binary operator occurs when an operand is expected
// corrected paren_status() when at last argument of first form
// of a function with multiple forms to report the appropriate
// error (that a comma could also be expected)
// in Comma_Handler() when an error occurs and the done stack is
// empty, get data type of expression to get appropriate error
// in Comma_Handler() when moving to the next multiple form of a
// function, corrected the setting of the index on count stack
//
// 2011-01-13 started implementation of storing first and last operands
// converted hold stack from holding token pointers to holding
// hold stack items (with a token and first pointer)
// added argument to process_final_operand() for passing in an
// operand (either first operand or CloseParen token)
// 2011-01-14 implemented new process_first_operand() with code from
// Operator/Equal handlers
// 2011-01-15 converted done stack from holding output list element pointers
// to holding done stack items (with element, first, and last
// pointers)
// added arguments to find_code() for return first/last operands
// implemented new delete_close_paren() to check for and delete a
// CloseParen token that may be in done stack last token
// 2011-01-16 in delete_close_paren(), also check if token has table entry
// added reporting range of tokens for expression type errors
// in clean_up(), fixed to not delete tokens until after stacks
// in EndOfLine_Handler(), corrected cleanup for expression mode
// 2011-01-20 in process_final_operand(), current unary operators to only set
// last operand (first operand should be the unary operator)
// 2011-01-22 in do_pending_paren(), corrected to delete pending paren token
// when setting parentheses sub-code
// implement new function delete_open_paren()
// in delete_close_paren(), check if close paren token still being
// used as pending paren (don't delete, just clear last sub-code)
// modified find_code() to use work first/last token pointers
// 2011-01-23 in CloseParen_Handler(), save both open and close paren tokens
// as first/last operands (don't delete open paren token),
// and set last and used sub-code flags of close paren token so
// it doesn't get deleted until its not used anymore
// 2011-01-30 corrected token memory leaks
// 2011-02-01 corrected token memory leaks
// 2011-02-02 in find_code() upon a no match error, removed checks if last
// token added was sub-string; not necessary with first/last code
// 2011-02-05 in process_final_operand() added check if token is assignment
// operator to not push to done stack (strings still attached)
// replaced most code in Assign_CmdHandler() with call to
// process_final_operand()
//
// 2011-02-10 implemented new call_command_handler() for comma and semicolon
// token handlers to call for command on top of command stack
// moved code from comma and semicolon token handlers to assign
// and print command handlers (into new switch statements)
// 2011-02-11 in Assign_CmdHandler(), when an error occurs, need to set set
// the command item's token to the passed in token
// in Print_CmdHandler(), make sure good status is returned when
// add_print_code() returns null status for nothing done
// 2011-02-13 moved code from add_token() to add_operator(), which was renamed
// to process_operator()
// moved code from add_token() to process_binary_operator()
//
// 2011-02-22 changed all pop() calls not using return value to null_pop()
// 2011-02-26 updated for change of table index to code
// 2011-03-01 initialized new element member for command stack item
// removed code member from command stack item (now token->code)
// started implementation of Input_CmdHandler()
// 2011-03-03 changed process_final_operand() from checking for print
// functions to functions with no return value, fixed problem
// with deleting second token when not set (for print functions)
// 2011-03-05 added non-empty hold stack to print function token check
// added "_State" to enum Translator::State values
// added new EndExpr_State, check to process_binary_operator()
// replaced PrintOnlyIntFunc with expected xxx expression error
// 2011-03-07 implemented reference mode
// corrected problem with print functions within parentheses
// 2011-03-09 moved sub-string function operand check to begin of
// process_binary_operator() to prevent acceptance of operators
// 2011-03-10 removed unexpected command error, just pass command token on
// 2011-03-11 changed to assignment mode after an operand is received while
// in command mode, changed code for with mode change
// return appropriate error for mode in process_operand() and
// Comma_Handler()
// 2011-03-12 added new operator_error() to determine error when token is not
// an expected operator from parts of process_binary_operator()
// and now also called from process_operator() for command tokens
// that have no token handler
// check for command token with no handler in process_operator()
// implemented new operator_error() from parts of
// process_binary_operator() so that code in process_operator()
// can also call it
// removed extra empty count stack mode check from middle of
// processed_unary_operator() - now at begin of function
// 2011-03-13 added check to make sure first operand of a sub-string function
// assignment is not a unary operator in process_unary_operator()
// changed non-reference return error in find_code()
// 2011-03-14 return error at parentheses for DefFuncP tokens in
// process_operand() and check_assignlist_item()
// don't set DefFuncP reference flag in CloseParen_Handler()
// 2011-03-19 implemented end statement state
// completed implementation of Input_CmdHandler()
// renamed EndStatment_Flag to EndStmt_Flag
// 2011-03-20 don't delete EOL token if not EOL code in EndOfLine_Handler()
// changed to insert InputBegin at beginning of statement,
// required initializing CmdStackItem element to output->last(),
// and new InputBegin_CmdFlag support
// set reference flag of input assign to handle variable correctly
// added reference mode support to Comma_Handler()
// 2011-03-21 set reference mode after string prompt in Input_CmdHandler()
// 2011-03-22 set Operand_State after string prompt in Input_CmdHandler()
// renamed FirstOperand_State to OperandOrEnd_State
// 2011-03-24 set token with error in cmd_item to return in Input_CmdHandler()
// added reference mode to end_expression_error()
// 2011-03-25 removed token delete with prompt error in Input_CmdHandler()
//
// 2011-03-26 modified the token error pointer for DefFuncP tokens to
// to compensate for the lack of the open parentheses that is no
// longer stored in the token string
// 2011-03-27 modified process_operator() to not allow unary operators to
// force other tokens from hold stack
//
// 2012-10-25 corrected if statements that were checking token code without
// making sure code was valid (some token types don't used code)
// 2012-10-27 converted output rpn list from List class to QList
// also required changes to done_stack (element list pointer to
// rpn item pointer), cmd_stack (element list pointer to index
// into output rpn list), and RpnItem.operands[] (element list
// pointer array to RpnItem pointer array)
// 2012-10-29 converted translator stacks from Stack class to QStack
// 2012-11-01 removed immediate command support
#include "translator.h"
#include "token.h"
#include "table.h"
#include "parser.h"
#include "tokenhandlers.h"
// highest precedence value
enum {
HighestPrecedence = 127
// this value was selected as the highest value because it is the highest
// one-byte signed value (in case the precedence member is changed to an
// char); all precedences in the table must be below this value
};
// array of conversion codes [have data type] [need data type]
static Code cvtCodeHaveNeed[numberof_DataType][numberof_DataType] = {
{ // have Double, need:
Null_Code, // Double
CvtInt_Code, // Integer
Invalid_Code, // String
Invalid_Code, // TmpStr
Invalid_Code // SubStr
},
{ // have Integer, need:
CvtDbl_Code, // Double
Null_Code, // Integer
Invalid_Code, // String
Invalid_Code, // TmpStr
Invalid_Code // SubStr
},
{ // have String, need:
Invalid_Code, // Double
Invalid_Code, // Integer
Null_Code, // String
Null_Code, // TmpStr
Null_Code // SubStr
},
{ // have TmpStr, need:
Invalid_Code, // Double
Invalid_Code, // Integer
Null_Code, // String
Null_Code, // TmpStr
Null_Code // SubStr
},
{ // have SubStr, need:
Invalid_Code, // Double
Invalid_Code, // Integer
Null_Code, // String
Null_Code, // TmpStr
Null_Code // SubStr
}
};
// function to return equivalent data type for data type
// (really to convert the various string data types to String_DataType)
DataType Translator::equivalentDataType(DataType dataType)
{
static DataType equivalent[numberof_DataType] = {
Double_DataType, // Double
Integer_DataType, // Integer
String_DataType, // String
String_DataType, // TmpStr
String_DataType // SubStr
};
return equivalent[dataType];
}
// function to return the token error status for an expected data type
TokenStatus Translator::expectedErrStatus(DataType dataType)
{
static TokenStatus tokenStatus[sizeof_DataType] = {
ExpNumExpr_TokenStatus, // Double
ExpNumExpr_TokenStatus, // Integer
ExpStrExpr_TokenStatus, // String
ExpStrExpr_TokenStatus, // TmpStr
ExpStrExpr_TokenStatus, // SubStr
BUG_InvalidDataType, // numberof
ExpExpr_TokenStatus // None
};
return tokenStatus[dataType];
}
// function to return the token error status for an actual data type
TokenStatus Translator::actualErrStatus(DataType dataType)
{
static TokenStatus tokenStatus[sizeof_DataType] = {
ExpStrExpr_TokenStatus, // Double
ExpStrExpr_TokenStatus, // Integer
ExpNumExpr_TokenStatus, // String
ExpNumExpr_TokenStatus, // TmpStr
ExpNumExpr_TokenStatus, // SubStr
BUG_InvalidDataType, // numberof
BUG_InvalidDataType // None
};
return tokenStatus[dataType];
}
// function to return the token error status for a variable data type
TokenStatus Translator::variableErrStatus(DataType dataType)
{
static TokenStatus tokenStatus[sizeof_DataType] = {
ExpDblVar_TokenStatus, // Double
ExpIntVar_TokenStatus, // Integer
ExpStrItem_TokenStatus, // String
BUG_InvalidDataType, // TmpStr
BUG_InvalidDataType, // SubStr
BUG_InvalidDataType, // numberof
ExpAssignItem_TokenStatus // None
};
return tokenStatus[dataType];
}
// function to parse and translate an input line to an RPN output list
//
// - returns true if successful, use output() to get RPN output list
// - returns false if failed, use errorToken() and errorMessage()
// to get token error occurred at and the error message
bool Translator::setInput(const QString &input, bool exprMode)
{
Token *token;
Token *parsedToken;
TokenStatus status;
Parser parser(m_table);
parser.setInput(input);
m_exprMode = exprMode; // save flag
// (expression mode for testing)
m_mode = m_exprMode ? Expression_TokenMode : Command_TokenMode;
m_output = new QList<RpnItem *>;
m_state = Initial_State;
do {
// set parser operand state from translator
parser.setOperandState(m_state == Operand_State
|| m_state == OperandOrEnd_State);
token = parsedToken = parser.getToken();
if (token->isType(Error_TokenType))
{
setErrorToken(token);
// XXX determine error code from what's expected next
m_errorMessage = token->string();
cleanUp();
return false;
}
status = addToken(token);
}
while (status == Good_TokenStatus);
if (status != Done_TokenStatus)
{
// token pointer is set to cause of error
// check if token is the parsed token or is an open paren
if (token == parsedToken || token->hasTableEntry()
&& token->isCode(OpenParen_Code))
{
// token is not already in the output list
setErrorToken(token);
}
else // token is in the rpn output list
{
// make a copy of the token to save
setErrorToken(new Token(*token));
}
m_errorMessage = token->message(status);
cleanUp();
return false;
}
// TODO check if stacks are empty
return true;
}
// function to add a token to the output list, but token may be placed
// on hold stack pending adding it to the output list so that higher
// precedence tokens may be added to the list first
//
// - Done status returned when last token is processed
// - Good status returned when token successfully processed
// - error status returned when an error is detected
// - token argument may be changed when an error is detected
TokenStatus Translator::addToken(Token *&token)
{
TokenStatus status;
if (m_state == Initial_State)
{
// check for end of line at begin of input
if (token->isType(Operator_TokenType) && token->isCode(EOL_Code))
{
delete token; // delete EOL token
return Done_TokenStatus;
}
// push null token to be last operator on stack
// to prevent from popping past bottom of stack
m_holdStack.resize(m_holdStack.size() + 1);
m_holdStack.top().token = m_table.newToken(Null_Code);
m_holdStack.top().first = NULL;
m_state = OperandOrEnd_State;
}
// added check for command token
if (token->isType(Command_TokenType))
{
if (m_mode == Command_TokenMode)
{
if (m_table.tokenMode(token->code()) != Null_TokenMode)
{
m_mode = m_table.tokenMode(token->code());
m_cmdStack.resize(m_cmdStack.size() + 1);
m_cmdStack.top().token = token;
m_cmdStack.top().flag = None_CmdFlag;
// initialize index to current last index of output
m_cmdStack.top().index = m_output->size();
return Good_TokenStatus; // nothing more to do
}
else
{
return BUG_NotYetImplemented;
}
}
// fall through so proper error is reported
}
// check for end statement
if (m_state == EndStmt_State && !(m_table.flags(token) & EndStmt_Flag))
{
return ExpEndStmt_TokenStatus;
}
// check for both operand states
if (m_state == Operand_State || m_state == OperandOrEnd_State)
{
if (!token->isOperator())
{
return processOperand(token);
}
// end-of-statement code acceptable instead of operand
else if (m_table.flags(token) & EndExpr_Flag)
{
if (m_state != OperandOrEnd_State)
{
// an operand is expected, get the appropriate error
return endExpressionError();
}
// fall thru to process end of expression operator token
}
else // operator when expecting operand, must be a unary operator
{
if (processUnaryOperator(token, status) == false)
{
return status;
}
// fall thru to process unary operator token
}
}
else // a binary operator is expected
{
status = processBinaryOperator(token);
if (status != Good_TokenStatus)
{
return status;
}
}
// process all operators
return processOperator(token);
}
// function to process and operand token
//
// - the default data type of the operand is set
// - if the token has a parentheses and is an internal function, then
// - an error occurs if a print function is not in a print command
// - only sub-string functions are permitted in command mode
// - only sub-string functions are permitted in assignment modes
// - function or array is pushed onto count stack
// - number of expected operands put on count stack for internal functions
// - state is set to operand for function or array
//
// - reference flag is set for non-parentheses tokens
// - non-parentheses token is pushed onto done stack, appended to output
// - state is set to binary operator
TokenStatus Translator::processOperand(Token *&token)
{
// check for and add dummy token if necessary
doPendingParen(m_holdStack.top().token);
// set default data type for token if it has none
token->setDataType();
if (token->hasParen())
{
// token is an array or a function
if (token->isType(IntFuncP_TokenType))
{
// detect invalid print-only function
if ((m_table.flags(token->code()) & Print_Flag)
&& (!m_cmdStack.empty()
&& !m_cmdStack.top().token->isCode(Print_Code)
|| !m_holdStack.top().token->isNull()))
{
TokenStatus status;
DataType dataType;
if ((status = getExprDataType(dataType)) == Good_TokenStatus)
{
status = expectedErrStatus(dataType);
}
return status;
}
// check if not in expression mode
switch (m_mode)
{
case Command_TokenMode:
case Assignment_TokenMode:
if (m_countStack.empty())
{
if (m_table.dataType(token->code()) != SubStr_DataType)
{
// return appropriate error for mode
return m_mode == Command_TokenMode
? ExpCmd_TokenStatus : ExpAssignItem_TokenStatus;
}
// set reference of sub-string function
token->setReference();
}
// check if first operand of sub-string
else if (m_holdStack.top().token->reference()
&& m_countStack.top().nOperands == 1)
{
return ExpStrVar_TokenStatus;
}
break;
case AssignmentList_TokenMode:
if (m_table.dataType(token->code()) != SubStr_DataType)
{
// in a comma separated list
return variableErrStatus(m_cmdStack.top().token
->dataType());
}
// set reference flag of sub-string function
token->setReference();
break;
case Reference_TokenMode:
if (m_countStack.empty())
{
return ExpVar_TokenStatus;
}
break;
}
}
// for reference mode, only no parentheses tokens allowed
else if (m_countStack.empty() && !token->isType(Paren_TokenType))
{
// return appropriate error for mode
if (m_mode == Command_TokenMode || m_mode == Assignment_TokenMode)
{
if (token->isType(DefFuncP_TokenType))
{
// TODO these are allowed in the DEF command
// just point to open parentheses on token
token->addLengthToColumn();
token->setLength(1);
}
return ExpEqualOrComma_TokenStatus;
}
else if (m_mode == Reference_TokenMode)
{
return ExpVar_TokenStatus;
}
}
// 2010-06-08: changed count stack to hold count items
m_countStack.resize(m_countStack.size() + 1);
m_countStack.top().nOperands = 1; // assume at least one
if (token->isType(IntFuncP_TokenType))
{
m_countStack.top().nExpected = m_table.nOperands(token->code());
m_countStack.top().code = token->code();
}
else // !token->isType(IntFuncP_TokenType)
{
m_countStack.top().nExpected = 0;
}
m_holdStack.resize(m_holdStack.size() + 1);
m_holdStack.top().token = token;
m_holdStack.top().first = NULL;
// leave state == Operand
m_state = Operand_State; // make sure not OperandOrEnd
}
else // !token->hasParen()
{
// for reference mode, only no parentheses tokens allowed
if (m_mode == Reference_TokenMode && m_countStack.empty()
&& !token->isType(NoParen_TokenType))
{
return ExpVar_TokenStatus;
}
// token is a variable or a function with no arguments
// set reference flag for variable or function
if (token->isType(NoParen_TokenType)
|| token->isType(DefFuncN_TokenType))
{
token->setReference();
}
// add token directly output list
// and push element pointer on done stack
RpnItem *rpnItem = new RpnItem(token);
m_output->append(rpnItem);
m_doneStack.resize(m_doneStack.size() + 1);
m_doneStack.top().rpnItem = rpnItem;
m_doneStack.top().first = m_doneStack.top().last = NULL;
// in reference mode, if have variable, set end expression state
// otherwise next token must be a binary operator
m_state = m_mode == Reference_TokenMode && m_countStack.empty()
? EndExpr_State : BinOp_State;
}
// if command mode then change to assignment mode
if (m_mode == Command_TokenMode)
{
m_mode = Assignment_TokenMode;
}
return Good_TokenStatus;
}
// function to get the error for a premature end to an expression when
// another operand is expected
//
// - for command and assignments modes, count stack determines error
// - for expression mode, current expression type determines error
TokenStatus Translator::endExpressionError(void)
{
TokenStatus status = Good_TokenStatus;
DataType dataType;
// unexpected of end expression - determine error to return
switch (m_mode)
{
case Command_TokenMode:
case Assignment_TokenMode:
case AssignmentList_TokenMode:
// make sure done stack is not empty
if (m_countStack.empty())
{
status = variableErrStatus(m_cmdStack.top().token->dataType());
}
else if (m_countStack.top().nExpected == 0)
{
// in array
status = ExpNumExpr_TokenStatus;
}
else if (m_countStack.top().nOperands == 1)
{
// in function at first argument (sub-string function)
status = ExpStrVar_TokenStatus;
}
else // in function not at first argument
{
status = expectedErrStatus(m_table
.operandDataType(m_countStack.top().code,
m_countStack.top().nOperands - 1));
}
break;
case Expression_TokenMode:
if ((status = getExprDataType(dataType)) == Good_TokenStatus)
{
status = expectedErrStatus(dataType);
}
break;
case Reference_TokenMode:
status = ExpVar_TokenStatus;
break;
default:
status = BUG_InvalidMode;
break;
}
return status;
}
bool Translator::processUnaryOperator(Token *&token, TokenStatus &status)
{
// check if count stack is empty before checking mode
if (m_countStack.empty())
{
switch (m_mode)
{
case Command_TokenMode:
// if command mode, then this is not the way it starts
status = ExpCmd_TokenStatus;
return false;
case Assignment_TokenMode:
status = ExpAssignItem_TokenStatus;
return false;
case AssignmentList_TokenMode:
status = variableErrStatus(m_cmdStack.top().token->dataType());
return false;
case Reference_TokenMode:
status = ExpVar_TokenStatus;
return false;
}
}
else if (m_holdStack.top().token->isDataType(SubStr_DataType)
&& m_holdStack.top().token->reference()
&& m_countStack.top().nOperands == 1)
{
status = ExpStrVar_TokenStatus;
return false;
}
Code unary_code = m_table.unaryCode(token->code());
if (unary_code == Null_Code)
{
DataType dataType;
// oops, not a valid unary operator
if ((status = getExprDataType(dataType)) == Good_TokenStatus)
{
status = expectedErrStatus(dataType);
}
return false;
}
// change token to unary operator
token->setCode(unary_code);
if (unary_code == OpenParen_Code)
{
// check for and add dummy token if necessary
doPendingParen(m_holdStack.top().token);
// assign current expression data type to paren token
DataType dataType = token->dataType();
if ((status = getExprDataType(dataType)) != Good_TokenStatus)
{
return false;
}
token->setDataType(dataType);
// push open parentheses right on stack and return
m_holdStack.resize(m_holdStack.size() + 1);
m_holdStack.top().token = token;
m_holdStack.top().first = NULL;
m_state = Operand_State;
// add a null counter to prevent commas
m_countStack.resize(m_countStack.size() + 1);
m_countStack.top().nOperands = 0;
m_countStack.top().nExpected = 0;
status = Good_TokenStatus;
return false;
}
status = Good_TokenStatus;
return true;
}
// function to process a binary operator token
//
// - error occurs if token is not an operator
// - error occurs if token is a unary operator
// - error occurs comma expected after variable in sub-string assignment
// - if closing parentheses then process any pending parentheses
// and set last precedence to higher value
// - error occurs if end statement operator in uncompleted multiple
// assignment statement
TokenStatus Translator::processBinaryOperator(Token *&token)
{
TokenStatus status = Good_TokenStatus;
// check if after first operand of sub-string assignment
// make sure token has table entry before checking code
if (m_holdStack.top().token->reference()
&& m_countStack.top().nOperands == 1
&& (!token->hasTableEntry() || !token->isCode(Comma_Code)))
{
// only a comma is allowed here
return ExpComma_TokenStatus;
}
// end of expression token expected check
if (m_state == EndExpr_State && !(m_table.flags(token) & EndExpr_Flag))
{
// TODO currently only occurs after print function
// TODO add correct error based on current command
// TODO call command handler to get appropriate error here
return ExpSemiCommaOrEnd_TokenStatus;
}
if (!token->isOperator())
{
// state == BinOp_State, but token is not an operator
return operatorError();
}
else if (m_table.isUnaryOperator(token->code()))
{
status = ExpBinOpOrEnd_TokenStatus;
}