/
test_ibcp.cpp
769 lines (710 loc) · 16.9 KB
/
test_ibcp.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
// vim:ts=4:sw=4:
// Interactive BASIC Compiler Project
// File: test_ibcp.cpp - tester class source file
// Copyright (C) 2010-2014 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 <fstream>
#include <iomanip>
#include <iostream>
#include "test_ibcp.h"
#include "table.h"
#include "token.h"
#include "parser.h"
#include "statusmessage.h"
#include "utility.h"
// overloaded output stream operator for abbreviated contents of a token
std::ostream &operator<<(std::ostream &os, const TokenPtr &token)
{
bool second {};
switch (token->type())
{
case Type::NoParen:
os << token->stringWithDataType();
if (token->hasFlag(Reference_Flag))
{
os << "<ref>";
}
break;
case Type::DefFunc:
if (token->index() == DefFuncN_Code
|| token->index() == DefFuncNInt_Code
|| token->index() == DefFuncNStr_Code)
{
os << token->stringWithDataType();
break;
}
// else fall thru (has parentheses)
case Type::Paren:
os << token->stringWithDataType() << '(';
break;
case Type::Constant:
switch (token->dataType())
{
case DataType::Integer:
case DataType::Double:
os << token->string();
if (token->dataType() == DataType::Integer)
{
os << "%";
}
break;
case DataType::String:
os << '"' << token->string() << '"';
break;
default:
break;
}
break;
case Type::Operator:
if (token->isCode(RemOp_Code))
{
os << token->name();
second = true;
}
else
{
os << token->debugName();
}
break;
case Type::Command:
if (token->isCode(Rem_Code))
{
os << token->name();
second = true;
}
else
{
os << token->name();
if (!token->name2().empty())
{
os << '-' << token->name2();
}
}
break;
default:
// output debug name by default
os << token->debugName();
break;
}
if (token->reference())
{
os << "<ref>";
}
if (token->hasSubCode())
{
os << '\'';
bool command = token->isType(Type::Command)
|| token->hasFlag(Command_Flag);
if (!command && token->hasSubCode(Paren_SubCode))
{
os << ')';
}
if (token->hasSubCode(Option_SubCode))
{
std::string option {token->optionName()};
if (option.empty())
{
os << "BUG";
}
else
{
os << option;
}
}
if (command && token->hasSubCode(Colon_SubCode))
{
os << ':';
}
if (token->hasSubCode(Double_SubCode))
{
os << "Double";
}
os << '\'';
}
if (second)
{
os << '|' << token->string() << '|';
}
return os;
}
// overloaded output stream operator for abbreviated contents of rpn list
std::ostream &operator<<(std::ostream &os, const RpnList &rpnList)
{
std::unordered_map<RpnItemPtr, int> itemIndex;
int index {};
for (RpnItemPtr rpnItem : rpnList)
{
if (index > 0)
{
os << ' ';
}
itemIndex[rpnItem] = index++;
os << rpnItem->token();
if (rpnItem->attachedCount() > 0)
{
char separator {'['};
for (RpnItemPtr item : rpnItem->attached())
{
if (item)
{
os << separator << itemIndex[item] << ':' << item->token();
separator = ',';
}
}
if (separator != '[')
{
os << ']';
}
}
}
return os;
}
// overloaded output stream operator for abbreviated contents of rpn list
std::ostream &operator<<(std::ostream &os, const Dictionary *const dictionary)
{
for (size_t i {}; i < dictionary->m_iterator.size(); i++)
{
if (dictionary->m_iterator[i] != dictionary->m_keyMap.end())
{
auto iterator = dictionary->m_iterator[i];
os << i << ": " << iterator->second.m_useCount << " |"
<< iterator->first << "|\n";
}
}
os << "Free:";
if (dictionary->m_freeStack.empty())
{
os << " none";
}
else
{
std::stack<uint16_t> tempStack = dictionary->m_freeStack;
while (!tempStack.empty())
{
int index {tempStack.top()};
tempStack.pop();
os << ' ' << index;
if (dictionary->m_iterator[index] != dictionary->m_keyMap.end())
{
os << '|' << dictionary->m_iterator[index]->first << '|';
}
}
}
os << '\n';
return os;
}
// function to process a test input file specified on the command line
// or accept input lines from the user
Tester::Tester(const std::string &programName,
const std::list<std::string> &args, std::ostream &cout) :
m_programName {programName},
m_cout(cout),
m_programUnit {new ProgramModel}
{
std::unordered_map<Option, std::string, EnumClassHash> name {
{Option::Parser, "parser"},
{Option::Expression, "expression"},
{Option::Translator, "translator"},
{Option::Encoder, "encoder"}
};
// scan arguments for test options (ignore others)
m_option = Option{};
m_recreate = false;
switch (args.size())
{
case 1:
if (isOption(args.front(), "-tp", Option::Parser, name[Option::Parser])
|| isOption(args.front(), "-te", Option::Expression,
name[Option::Expression])
|| isOption(args.front(), "-tt", Option::Translator,
name[Option::Translator])
|| isOption(args.front(), "-tc", Option::Encoder,
name[Option::Encoder])
|| isOption(args.front(), "-tr", Option::Recreator, "recreator"))
{
break;
}
// not one of the above options, fall through
case 2:
if (args.front() == "-to")
{
m_recreate = true;
}
else if (args.front() != "-t")
{
break; // no test option found
}
if (args.size() == 1)
{
throw m_programName + ": missing test file name";
}
else
{
// find start of file name less path
m_testFileName = args.back();
std::string baseName {Utility::baseFileName(m_testFileName)};
for (auto iterator : name)
{
// check beginning of file name
if (noCaseStringBeginsWith(baseName, iterator.second))
{
m_option = iterator.first;
m_testName = iterator.second;
break;
}
}
if (m_option == Option{}) // no matching names?
{
std::string errorMessage = "usage: " + m_programName + " -t";
if (m_recreate)
{
errorMessage += 'o';
}
errorMessage += " (";
if (m_recreate)
{
errorMessage += name[Option::Parser] + '|';
}
errorMessage += name[Option::Expression] + '|'
+ name[Option::Translator] + ")[xx]";
throw errorMessage;
}
else if (m_option == Option::Parser && m_recreate)
{
throw m_programName + ": cannot use -to with "
+ name[Option::Parser] + " files";
}
}
}
// ignore non-test or invalid test options
}
// function to see if argument is expected option
bool Tester::isOption(const std::string &arg, const std::string &exp,
Option option, std::string name)
{
if (arg == exp)
{
m_option = option;
m_testName = name;
return true;
}
return false;
}
// function to return list of valid options
std::string Tester::options()
{
return "-t <test_file>|-tp|-te|-tt|-tc|-tr|-to <test_file>";
}
void Tester::operator()(std::string copyrightStatement)
{
std::ifstream ifs;
bool inputMode {m_testFileName.empty()};
if (!inputMode)
{
ifs.open(m_testFileName);
if (!ifs.is_open())
{
throw m_programName + ": error opening '" + m_testFileName + '\'';
}
}
if (inputMode)
{
m_cout << m_programName << copyrightStatement << "\n\n";
m_cout << "This program comes with ABSOLUTELY NO WARRANTY.\n";
m_cout << "This is free software, and you are welcome to\n";
m_cout << "redistribute it under certain conditions.\n";
m_cout << "\nTable initialization successful.\n";
m_cout << "\nTesting " << m_testName;
}
for (int lineno {1};; lineno++)
{
std::string inputLine;
if (inputMode)
{
m_cout << "\nInput: " << std::flush;
std::getline(std::cin, inputLine);
if (inputLine.empty() || inputLine[0] == '\n')
{
break;
}
}
else
{
if (!std::getline(ifs, inputLine))
{
break; // no more lines
}
if (m_option != Option::Encoder)
{
if (inputLine.empty() || inputLine[0] == '#')
{
continue; // skip blank and comment lines
}
printInput(inputLine);
}
else if (!inputLine.empty() && inputLine[0] == '#')
{
continue; // skip comment lines
}
}
switch (m_option)
{
case Option::Parser:
parseInput(inputLine);
break;
case Option::Expression:
translateInput(inputLine, true);
break;
case Option::Translator:
translateInput(inputLine, false);
break;
case Option::Encoder:
encodeInput(std::move(inputLine));
break;
case Option::Recreator:
recreateInput(inputLine);
break;
}
}
if (!inputMode && m_option != Option::Parser)
{
m_cout << '\n'; // not for parser testing
}
if (m_option == Option::Encoder)
{
// for encoder testing, output program lines
m_cout << "Program:\n";
for (int i {}; i < m_programUnit->rowCount(); i++)
{
m_cout << i << ": "
<< m_programUnit->debugText(i, true) << '\n';
}
m_cout << "\nRemarks:\n" << m_programUnit->remDictionary();
m_cout << "\nNumber Constants:\n"
<< m_programUnit->constNumDictionary();
m_cout << "\nString Constants:\n"
<< m_programUnit->constStrDictionary();
m_cout << "\nDouble Variables:\n" << m_programUnit->varDblDictionary();
m_cout << "\nInteger Variables:\n" << m_programUnit->varIntDictionary();
m_cout << "\nString Variables:\n" << m_programUnit->varStrDictionary();
if (m_recreate)
{
m_cout << "\nOutput:\n";
for (int i {}; i < m_programUnit->rowCount(); i++)
{
m_cout << i << ": " << m_programUnit->lineText(i) << '\n';
}
}
}
}
// function to parse an input line and print the resulting tokens
void Tester::parseInput(const std::string &testInput)
try
{
Parser parse {testInput};
for (;;)
{
TokenPtr token {parse(DataType::Any, Reference::None)};
printToken(token);
if (token->index() == DefFuncP_Code
|| token->index() == DefFuncPInt_Code
|| token->index() == DefFuncPStr_Code || token->isType(Type::Paren))
{
parse.getParen();
}
if (token->isCode(EOL_Code))
{
return;
}
}
}
catch (TokenError &error)
{
printError(error);
}
// function to parse an input line, translate to an RPN list
// and output the resulting RPN list
RpnList Tester::translateInput(const std::string &testInput, bool exprMode,
std::string &&header)
try
{
RpnList rpnList {Translator{testInput}(exprMode
? Translator::TestMode::Expression : Translator::TestMode::Yes)};
m_cout << (!header.empty() ? header : "Output") << ": ";
if (m_recreate)
{
// recreate text from rpn list
m_cout << Recreator{}(rpnList, exprMode);
}
else
{
m_cout << rpnList;
}
m_cout << ' ' << std::endl;
return rpnList;
}
catch (TokenError &error)
{
printError(error);
return RpnList{}; // return an empty list
}
// function to parse an input line, translate to an RPN list,
// recreate the line and output the resulting recreated text
void Tester::recreateInput(const std::string &testInput)
{
RpnList rpnList {translateInput(testInput, false, "Tokens")};
if (!rpnList.empty())
{
// recreate text from rpn list
m_cout << "Output: " << Recreator{}(std::move(rpnList)) << " \n";
}
}
// function to parse an input line, translate to an RPN list
// and output the resulting RPN list
void Tester::encodeInput(std::string testInput)
{
// parse beginning of line for line number program operation
Operation operation {Operation::Change};
size_t pos = 0;
if (pos < testInput.length())
{
if (testInput[pos] == '+')
{
operation = Operation::Insert;
pos++;
}
else if (testInput[pos] == '-')
{
operation = Operation::Remove;
pos++;
}
}
int digitCount = 0;
int lineIndex = 0;
while (pos < testInput.length() && isdigit(testInput[pos]))
{
int digit {testInput[pos] - '0'};
lineIndex = lineIndex * 10 + digit;
pos++;
digitCount++;
}
if (digitCount == 0) // no number at beginning?
{
if (operation == Operation::Change)
{
// no number at beginning, insert at end of program
operation = Operation::Insert;
lineIndex = m_programUnit->rowCount();
}
else if (operation == Operation::Insert)
{
// no number at beginning, insert at end of program
lineIndex = m_programUnit->rowCount();
}
else // no line index number after + or -
{
printInput(testInput);
m_cout << " ^-- expected line index number\n";
return;
}
}
else
{
if (lineIndex > m_programUnit->rowCount()
|| (operation != Operation::Insert
&& lineIndex == m_programUnit->rowCount()))
{
printInput(testInput);
m_cout << " " << (operation == Operation::Change ? "" : " ")
<< "^-- line index number out of range\n";
return;
}
if (operation == Operation::Remove && pos < testInput.length())
{
printInput(testInput);
m_cout << std::string(7 + pos, ' ')
<< "^-- no statement expected with remove line\n";
return;
}
}
// skip spaces
while (pos < testInput.length() && isspace(testInput[pos]))
{
pos++;
}
testInput.erase(0, pos); // remove operation, number and any spaces
// call update with arguments dependent on operation
if (operation == Operation::Remove)
{
m_programUnit->update(lineIndex, 1, 0, std::vector<std::string>{});
}
else // Change_Operation or Insert_Operation
{
m_programUnit->update(lineIndex, 0,
operation == Operation::Insert ? 1 : 0,
std::vector<std::string>{testInput});
}
const ErrorItem *errorItem {m_programUnit->lineError(lineIndex)};
// only output line if no operation/line number or has an error
if (pos == 0 || errorItem)
{
printInput(testInput);
if (errorItem)
{
TokenError error {errorItem->status(), errorItem->column(),
errorItem->length()};
printError(error);
}
else // get text of encoded line and output it
{
m_cout << "Output: " << m_programUnit->debugText(lineIndex) << '\n';
}
}
}
// function to convert data type enumerator to string
static const char *dataTypeName(DataType dataType)
{
switch (dataType)
{
case DataType::Double:
return "Double";
case DataType::Integer:
return "Integer";
case DataType::String:
return "String";
case DataType::None:
return "None";
case DataType::Number:
return "Number";
case DataType::Any:
return "Any";
}
return ""; // silence compiler warning (doesn't reach here at run-time)
}
// function to convert token type enumerator to string
static const char *tokenTypeName(Type type)
{
switch (type)
{
case Type::Command:
return "Command";
case Type::Operator:
return "Operator";
case Type::IntFunc:
return "IntFunc";
case Type::Constant:
return "Constant";
case Type::DefFunc:
return "DefFunc";
case Type::NoParen:
return "NoParen";
case Type::Paren:
return "Paren";
}
return ""; // silence compiler warning (doesn't reach here at run-time)
}
// function to print the contents of a token
void Tester::printToken(const TokenPtr &token)
{
m_cout << '\t' << std::setw(2) << std::right << token->column() << std::left
<< ": " << std::setw(10) << tokenTypeName(token->type());
switch (token->type())
{
case Type::DefFunc:
m_cout << (token->index() == DefFuncN_Code
|| token->index() == DefFuncNInt_Code
|| token->index() == DefFuncNStr_Code ? " " : "()");
break;
case Type::IntFunc:
m_cout << (token->operandCount() == 0 ? " " : "()");
break;
case Type::Constant:
case Type::NoParen:
m_cout << " ";
break;
case Type::Paren:
m_cout << "()";
break;
case Type::Operator:
case Type::Command:
m_cout << "Op";
break;
}
if (!token->isType(Type::Command))
{
m_cout << ' ' << std::setw(7) << dataTypeName(token->dataType());
}
switch (token->type())
{
case Type::DefFunc:
m_cout << " |" << token->stringWithDataType();
if (token->index() == DefFuncP_Code
|| token->index() == DefFuncPInt_Code
|| token->index() == DefFuncPStr_Code)
{
m_cout << '(';
}
m_cout << '|';
break;
case Type::NoParen:
m_cout << " |" << token->stringWithDataType() << '|';
break;
case Type::Paren:
m_cout << " |" << token->stringWithDataType() << "(|";
break;
case Type::Constant:
switch (token->dataType())
{
case DataType::Integer:
m_cout << ' ' << token->valueInt();
break;
case DataType::Double:
m_cout << ' ';
if (token->hasSubCode(IntConst_SubCode))
{
m_cout << token->valueInt() << ",";
}
m_cout << token->value();
break;
default:
break;
}
m_cout << " |" << token->string() << '|';
break;
case Type::Operator:
case Type::IntFunc:
case Type::Command:
m_cout << " " << token->debugName();
if (token->isCode(Rem_Code) || token->isCode(RemOp_Code))
{
m_cout << " |" << token->string() << '|';
}
break;
}
m_cout << std::endl;
}
// function to print a token with an error
void Tester::printError(TokenError &error)
{
m_cout << std::string(7 + error.m_column, ' ')
<< std::string(error.m_length, '^') << "-- "
<< StatusMessage::text(error.m_status).toStdString() << std::endl;
}
// end: test_ibcp.cpp