-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.cpp
2511 lines (2249 loc) · 69.2 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 - translator class source file
// Copyright (C) 2010-2013 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 version
#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 a
// char); all precedences in the table must be below this value
};
Translator::Translator(Table &table): m_table(table),
m_parser(new Parser(table)), m_output(NULL), m_pendingParen(NULL)
{
}
Translator::~Translator(void)
{
delete m_parser;
}
// 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
RpnList *Translator::translate(const QString &input, bool exprMode)
{
Token *token;
Token *parsedToken;
TokenStatus status;
m_parser->setInput(input);
m_exprMode = exprMode; // save flag
// (expression mode for testing)
m_mode = m_exprMode ? Expression_TokenMode : Command_TokenMode;
m_output = new RpnList;
m_state = Initial_State;
do {
// set parser operand state from translator
token = parsedToken = m_parser->token(m_state == Operand_State
|| m_state == OperandOrEnd_State
|| m_state == Initial_State && m_exprMode);
if (token->isType(Error_TokenType))
{
if (m_mode == Command_TokenMode)
{
token->setLength(1); // just point to first character
status = ExpCmd_TokenStatus;
}
else if (m_state == EndExpr_State)
{
token->setLength(1); // just point to first character
status = endExpressionError();
}
else if (m_state != Operand_State && m_state != OperandOrEnd_State
&& m_state != Initial_State)
{
token->setLength(1); // just point to first character
status = operatorError();
}
else // Operand_State
{
DataType dataType;
switch (m_mode)
{
case Assignment_TokenMode:
case AssignmentList_TokenMode:
// in a comma separated list
status = assignmentError();
if (status != ExpNumExpr_TokenStatus)
{
token->setLength(1); // just point to first character
}
else if (token->dataType() == Double_DataType)
{
// token has number error, return parser error
status = Null_TokenStatus;
}
break;
case Expression_TokenMode:
// something is on the stack that's not suppose to be there
// this is a diagnostic error, should not occur
if (m_state == Initial_State)
{
status = ExpExpr_TokenStatus; // (hold stack empty)
}
else if ((status = getExprDataType(dataType))
== Good_TokenStatus)
{
status = expectedErrStatus(dataType);
}
if ((status == ExpNumExpr_TokenStatus
|| status == ExpExpr_TokenStatus)
&& token->dataType() == Double_DataType)
{
// for numeric parser errors in numeric expressions
status = Null_TokenStatus; // return parser error
}
else
{
token->setLength(1); // just point to first character
}
break;
case Reference_TokenMode:
if (m_countStack.isEmpty())
{
token->setLength(1); // just point to first character
status = ExpVar_TokenStatus;
}
else if (token->dataType() != Double_DataType)
{
token->setLength(1); // just point to first character
status = ExpNumExpr_TokenStatus;
}
else
{
// for numeric parser errors in numeric expressions
status = Null_TokenStatus; // return parser error
}
break;
default:
status = BUG_InvalidMode;
break;
}
}
m_output->setErrorMessage(status == Null_TokenStatus
? token->string() : token->message(status));
setErrorToken(token);
cleanUp();
RpnList *output = m_output;
m_output = NULL;
return output;
}
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
{
// token is already in output so don't delete it
m_output->setError(token);
}
m_output->setErrorMessage(token->message(status));
cleanUp();
}
// TODO check if stacks are empty
RpnList *output = m_output;
m_output = NULL;
return output;
}
// 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.push(m_table.newToken(Null_Code));
m_state = OperandOrEnd_State;
}
// 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.hasFlag(token, EndStmt_Flag))
{
return ExpEndStmt_TokenStatus;
}
// check for both operand states
if (m_state == Operand_State || m_state == OperandOrEnd_State)
{
if (!token->isType(Operator_TokenType)
&& !token->isType(Command_TokenType))
{
return processOperand(token);
}
// end-of-statement code acceptable instead of operand
else if (m_table.hasFlag(token, EndExpr_Flag))
{
if (m_state != OperandOrEnd_State)
{
// an operand is expected, get the appropriate error
return unexpectedEndError();
}
// 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.hasFlag(token, Print_Flag)
&& (!m_cmdStack.isEmpty()
&& !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.isEmpty())
{
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.isEmpty())
{
return ExpVar_TokenStatus;
}
break;
}
}
// for reference mode, only no parentheses tokens allowed
else if (m_countStack.isEmpty() && !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.push(token);
// 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.isEmpty()
&& !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))
{
if (m_mode != Expression_TokenMode
|| !m_holdStack.isEmpty()
&& m_holdStack.top().token->isType(Paren_TokenType))
{
token->setReference();
}
}
else if (token->isType(DefFuncN_TokenType))
{
if (m_mode != Expression_TokenMode)
{
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.push(rpnItem);
// 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.isEmpty()
? EndExpr_State : BinOp_State;
}
// if command mode then change to assignment mode
if (m_mode == Command_TokenMode)
{
m_mode = Assignment_TokenMode;
}
return Good_TokenStatus;
}
bool Translator::processUnaryOperator(Token *&token, TokenStatus &status)
{
// check if count stack is empty before checking mode
if (m_countStack.isEmpty())
{
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;
}
if (token->isCode(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.push(token);
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;
}
Code unary_code = m_table.unaryCode(token->code());
if (unary_code == Null_Code)
{
DataType dataType = Any_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);
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.hasFlag(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 endExpressionError();
}
if (!token->isType(Operator_TokenType) && !token->isType(Command_TokenType))
{
// state == BinOp_State, but token is not an operator
return operatorError();
}
else if (m_table.isUnaryOperator(token->code()))
{
status = ExpBinOpOrEnd_TokenStatus;
}
// initialize last precedence before emptying stack for ')'
else if (token->isCode(CloseParen_Code))
{
// check for and add dummy token if necessary before emptying stack
doPendingParen(m_holdStack.top().token);
// now set last precedence to highest in case no operators in ( )
m_lastPrecedence = HighestPrecedence;
}
// checking at end of statement
else if (m_table.hasFlag(token, EndStmt_Flag)
&& m_mode == AssignmentList_TokenMode)
{
// no equal token received yet
status = ExpEqualOrComma_TokenStatus;
}
return status;
}
// function to process an operator token received, all operators with higher
// precedence on the hold stack are added to output list first
//
// for each operator added to the output list, any pending parentheses is
// processed first, the final operand of the operator is processed
//
// - if error occurs on last operand, the operator token passed in is
// deleted and the token with the error is returned
// - sets last_precedence for operator being added to output list
// - after higher precedence operators are processed from the hold stack,
// the token handler for the operator is called
TokenStatus Translator::processOperator(Token *&token)
{
// unary operators don't force other tokens from hold stack
while (m_table.precedence(m_holdStack.top().token)
>= m_table.precedence(token->code())
&& !m_table.isUnaryOperator(token->code()))
{
// pop operator on top of stack and add it to the output
Token *topToken = m_holdStack.top().token;
// (don't pop token here in case error occurs)
doPendingParen(topToken);
// change token operator code or insert conversion codes as needed
TokenStatus status = processFinalOperand(topToken,
m_holdStack.top().first,
m_table.isUnaryOperator(topToken->code()) ? 0 : 1);
if (status != Good_TokenStatus)
{
delete token;
token = topToken; // return token with error
return status;
}
// save precedence of operator being added
// (doesn't matter if not currently within parentheses,
// it will be reset upon next open parentheses)
m_lastPrecedence = m_table.precedence(topToken->code());
m_holdStack.drop();
}
// check for special token processing
TokenHandler tokenHandler = m_table.tokenHandler(token->code());
if (tokenHandler == NULL) // no special handler?
{
// check for command token with no token handler
if (token->isType(Command_TokenType))
{
// valid commands in BinOp state must have a token handler
return operatorError();
}
// use default operator token handler
tokenHandler = Operator_Handler;
}
return (*tokenHandler)(*this, token);
}
// function to process the first operand for an operator
//
// - called from the Operator_Handler or Equal_Handler (for equal operator)
// - attaches first operand token from operand on top of done stack
// - no first operand token for unary operators
TokenStatus Translator::processFirstOperand(Token *&token)
{
Token *first = NULL; // first operand token
// check first operand of binary operators
if (!m_table.isUnaryOperator(token->code()))
{
// changed token operator code or insert conversion codes as needed
Token *orgToken = token;
TokenStatus status = processDoneStackTop(token, 0, &first);
if (status != Good_TokenStatus)
{
// check if different token has error
if (token != orgToken)
{
delete orgToken; // delete operator token
}
return status;
}
}
// push it onto the holding stack and attach first operand
m_holdStack.push(token, first);
// expecting another operand next
// FIXME removed this for new translator
if (m_state != UnSet_State)
{
m_state = Translator::Operand_State;
}
return Good_TokenStatus;
}
// function to process the final operand for an operator, function, array or
// command
//
// - called with token final operand is for
// - operand_index only used when number of operands is zero
// - for arrays and non-internal functions, number of operands is non-zero
// (all operands are attached to RPN output item)
// - for operands/internal functions/commands: find_code() is called find
// correct code and insert any necessary hidden conversion codes;
// string operands are counted (only string operands are attached to RPN
// output item); and print-only functions are checked for
// - if there are operands to save: an operand array is allocated and
// operands are popped from the done stack and put into the array
// - for sub-string functions, string operand is left on stack for next token
// - an RPN output item is created and any operands are attached
// - the RPN output item is appended to the output list
// - item is pushed to the done stack if not a print-only function
// - from add_operator, token2 if first operand attached on hold stack
// - from add_print_code, token2 is NULL
// - from CloseParen_Handler, token2 is CloseParen token
// - from Assign_CmdHandler, token2 will be NULL (not used)
TokenStatus Translator::processFinalOperand(Token *&token, Token *token2,
int operandIndex, int nOperands) // FIXME removed nOperands with old trans
{
bool donePush = true; // for print-only functions
bool doneAppend = true; // for sub-string assignments
Token *first; // first operand token
Token *last; // last operand token
if (nOperands == 0) // internal function or operator
{
TokenStatus status = processDoneStackTop(token, operandIndex, &first,
&last);
if (status != Good_TokenStatus)
{
return status;
}
// check for non-assignment operator
if (m_table.hasFlag(token, Reference_Flag))
{
// for assignment operators, nothing gets pushed to the done stack
donePush = false;
// no longer need the first and last operands
DoneItem::deleteOpenParen(first);
DoneItem::deleteCloseParen(last);
}
// save string operands only
// get number of strings for non-sub-string codes
// include sub-string reference, which are put on done stack
else
{
// set first and last operands
DoneItem::deleteOpenParen(first);
if (token->isType(Operator_TokenType))
{
// if unary operator, then operator, else first from hold stack
// (set first token to unary op)
first = operandIndex == 0 ? token : token2;
// last operand from token that was on done stack
}
else // internal function
// FIXME will be for internal code functions only with new trans
{
first = NULL; // token itself is first operand
DoneItem::deleteCloseParen(last);
last = token2; // last operand is CloseParen token
}
// FIXME remove check when sub-string data type removed
if (token->isDataType(SubStr_DataType) && !token->reference())
{
// change non-reference sub-string function data type to string
token->setDataType(String_DataType);
}
// FIXME remove check for state for new transator
if (token->reference() && m_state == UnSet_State)
{
doneAppend = false; // don't append sub-string assignment
}
}
// process print-only internal functions
// (check for function with no return value)
// FIXME remove this section for new translator
if (token->isDataType(None_DataType) && m_state != UnSet_State)
{
if (m_table.hasFlag(token, Print_Flag))
{
// tell PRINT to stay on same line
m_cmdStack.top().flag = PrintStay_CmdFlag | PrintFunc_CmdFlag;
}
donePush = false; // don't push on done stack
// delete closing parentheses if print function
// (token2 could be null, e.g. print code)
if (token2 != NULL)
{
// if print function, make sure expression ends
if (m_table.hasFlag(token, Print_Flag))
{
m_state = EndExpr_State;
}
delete token2;
}
}
}
// FIXME remove with old translator
else // array or user function
{
first = NULL; // token itself is first operand
last = token2; // token's CloseParen is last
// check for end of array in reference mode
if (m_mode == Reference_TokenMode && m_countStack.isEmpty())
{
// have array element, set end expression state
m_state = EndExpr_State;
}
}
// FIXME remove section with old translator
RpnItem **operand;
if (nOperands == 0) // no operands to save?
{
operand = NULL;
}
else // pop string operands off of stack into new array
{
operand = new RpnItem *[nOperands];
// save operands for storage in output list
for (int i = nOperands; --i >= 0;)
{
if (m_doneStack.isEmpty())
{
return BUG_DoneStackEmptyOperands;
}
// TODO need to keep first/last operands for array/function args
// TODO (in case entire expression needs to be reported as an error)
// TODO (RpnItem should have DoneItem operands, not RpnItem)
operand[i] = m_doneStack.pop();
}
}
// add token to output list and push element pointer on done stack
RpnItem *rpnItem;
// check if item should be appended to output (sub-string assignments don't)
if (doneAppend)
{
rpnItem = outputAppend(token, nOperands, operand);
}
else
{
rpnItem = new RpnItem(token, nOperands, operand);
}
// check if output item is to be pushed on done stack
if (donePush)
{
// push index of rpm item about to be added to output list
m_doneStack.push(rpnItem, first, last);
}
return Good_TokenStatus;
}
// function to do end of expression hold stack check to make sure
// nothing is on the hold stack (the initial null entry should be on
// top)
//
// - checks if there is an open parentheses, parentheses token or
// internal function on hold stack (missing closing parentheses)
// - checks the null token is on top of stack
// - or some other unexpected token (BUG)
// - performs pending parentheses check before returning
// - returns good status or error status
TokenStatus Translator::expressionEnd(void)
{
TokenStatus status;
Token *token;
// do end of statement processing
if (m_holdStack.isEmpty())
{
// oops, stack is empty
return BUG_HoldStackEmpty;
}
if ((status = parenStatus()) != Good_TokenStatus)
{
return status;
}
else
{
token = m_holdStack.top().token;
if (!token->hasTableEntry())
{
return BUG_Debug2;
}
if (!token->isCode(Null_Code))
{
// check if there is some unexpected token on hold stack
// could be assignment on hold stack
switch (m_mode)
{
// note: Command_TokenMode can't happen here
case Assignment_TokenMode:
// expression hasn't started yet
return ExpEqualOrComma_TokenStatus;
case AssignmentList_TokenMode:
// in a comma separated list
return ExpEqualOrComma_TokenStatus;
case Expression_TokenMode:
// something is on the stack that's not suppose to be there
// this is a diagnostic error, should not occur
return ExpOpOrEnd_TokenStatus;
default:
return BUG_InvalidMode;
}
}
}
// check if there is a pending closing parentheses
// pass Null_Code token (will always add dummy)
doPendingParen(token);
return Good_TokenStatus;
}
// function to call command handler of command on top of command stack
//
// - if command stack empty and expression only mode, return null
// status indicating token was not supported by command (caller will
// report appropriate error)
// - otherwise if command stack, then bug
// - if command stack top does not have a command handler, then bug
// - if command handler returns an error, then token passed in is
// deletedunless it is the token reported as the error
// - for errors, the token returned by the command handler is returned
TokenStatus Translator::callCommandHandler(Token *&token)
{
if (m_cmdStack.isEmpty())
{
// token was not expected
// let caller report the appropriate error
return Null_TokenStatus;
}
CmdItem cmdItem = m_cmdStack.top();
CommandHandler cmdHandler = m_table.commandHandler(cmdItem.token->code());
if (cmdHandler == NULL) // missing command handler?
{
return BUG_NotYetImplemented;
}
TokenStatus status = (*cmdHandler)(*this, &cmdItem, token);
if (status != Good_TokenStatus)
{
// delete token if error at another token
if (cmdItem.token != token)
{
delete token;
}
token = cmdItem.token; // command decided where error is
}
return status;
}
// function to find a code where the expected data types of the operands match
// the actual operands present; the code in the token is checked first followed
// by any associated codes that the main code has; the token is updated to the
// code found
//
// - the token is unchanged is there is an exact match of data types
// - conversion codes are inserted into the output after operands that need
// to be converted
// - if there is no match, then an appropriate error is returned and the
// token argument is changed to pointer to the token with the error
//
// - operand index must be valid to token code