-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBASICInterpreter.cpp
More file actions
628 lines (521 loc) · 14.2 KB
/
BASICInterpreter.cpp
File metadata and controls
628 lines (521 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include<iostream>
#include<stdlib.h>
#include<map>
#include <string>
#include <sstream>
#include <vector>
#include<cstring>
#include<vector>
#include<fstream>
#include<ctype.h>
#include<bits/stdc++.h>
using namespace std;
//Exception declarations
class EndQuotesException{};
class InitialQuotesException{};
class NumberInputException{};
class ENDIFException{};
class GOTOEXException{};
class WrongstatementException{};
class Nonexistentvariable{};
//Abstract base class//
class base{
public:
//virtual function is overidden by each of the derived classes
virtual void execute(std::vector<std::vector<string> >arr,map<string, double> &variables,int &line,int lines)=0;
};
//Derived IF statement Class//
class IF:public base
{
private:
string input; //Input string to IF
string A,B; //string to store separated arguments
int A1,A2; //Arguments converted into integer for comparison
int operator1; //operator
public:
//execute Function shared with all the derived classes and base class//
void execute(std::vector<std::vector<string> >arr,map<string, double> &variables,int &line,int lines);
void checkOperator();
int checkCondition(int A,int B,int Operator);
inline bool isInteger(const std::string & s);
int checkVariable(map<string, double> variables);
int findEndIF(std::vector<std::vector<string> >arr,int line,int lines);
~IF();
};
//Checks the conditions once the operator and the Arguments are assigned as integers
int IF::checkCondition(int A,int B,int Operator){
if (Operator==1){if(A>B){return 1;} else{return 0;}}
if(Operator==2){if(A<B){return 1;} else{return 0;}}
if(Operator==3){if(A==B){return 1;} else{return 0;}}
return 0;
}
//Checks the operator present within the string 'input'
void IF::checkOperator(){
char str[input.length()];
strcpy(str,input.c_str());
int i;
char operators1[3]={'>','<','='};
char *pch;
string words[2];
int found;
for(i=0;i<3;i++){
found=input.find(operators1[i]);
if(input[found]>0){
operator1=i+1;
}}
i=0;
pch=strtok(str,">=<");
while(pch !=NULL){
words[i]=pch;
pch=strtok(NULL,">=<");
i++;
}
A=words[0];
B=words[1];
}
//checks whether an argument is an integer or not
inline bool IF:: isInteger(const std::string & s)
{
if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;
char * p ;
strtol(s.c_str(), &p, 10) ;
return (*p == 0) ;
}
//check variables uses isInteger to check whether the argument is a variable or number and assigns A and B accordignly
int IF::checkVariable(map<string, double> variables){
if(isInteger(A)==false){
A1=variables[A];
}
else{
std::istringstream ss(A);
ss >> A1;
}
if(isInteger(B)==false){
A2=variables[B];
}
else{
std::istringstream ss(B);
ss >> A2;
}
}
//Counts EndiF from the IF line and checks whether there are enough and ENDIF and returns the positions of ENDIF
int IF::findEndIF(std::vector<std::vector<string> >arr,int line,int lines){
int countifs=1;
int i;
for(i=line+1;i<lines;i++)
{
if(arr[i][1]=="IF")
{
countifs++;
}
if(arr[i][1]=="ENDIF"){
countifs--;
if(countifs==0){return i;}
}
}
if(countifs!=0){
line=lines+1; //increments line in program class to end of program to exit//
throw ENDIFException{}; //throws exception if not enough ENDIFs found//
return 0;
}
}
//Execute function checks the operator first and assigns the string A1 and B1 and checks if these are variables//
//The checkCondition is then called to evaluate the condition//
void IF::execute(std::vector<std::vector<string> >arr,map<string, double> &variables,int &line,int lines){
input=arr[line][2];
checkOperator();
checkVariable(variables);
if(checkCondition(A1,A2,operator1)==1&&findEndIF(arr,line,lines)>0){
line++; //uses the reference from execute to increment line number in program class to proceed to the next line
}
if(checkCondition(A1,A2,operator1)==0&&findEndIF(arr,line,lines)>0){
line=findEndIF(arr,line,lines); //if If statement not satisfied, line number set to the corresponding ENDIF
}
}
//LET FUNCTION
class let:public base{
private:
string input;
int operator1;
string A; //Arguments extracted as strings/
string B;
int A1; //Arguments assigned as integers//
int A2;
string Result; //Variable being assigned the value used as the key for MAP//
int inresult; //assigned value for key in map//
public:
void checkOperator();
inline bool isInteger(const std::string & s);
int checkVariable(map<string,double> &variables,int Operator,int &line ,int lines);
int compute(int A,int B,int Operator);
void execute(std::vector<std::vector<string> >arr,map<string, double> &variables,int &line,int lines);
~let();
};
//Same process as the one in IF, checks Operator such as *,+..//
void let::checkOperator(){
char str[input.length()];
strcpy(str,input.c_str());
int i;
char operators1[4]={'+','*','-','/'};
char *pch;
int equal;
string words[3];
int found;
found=0;
for(i=0;i<4;i++){
found=input.find(operators1[i]);
if(input[found]>0){
operator1=i+1;
}}
i=0;
equal=input.find('=');//
pch=strtok(str,"+*-/=");
while(pch !=NULL){
words[i]=pch;
pch=strtok(NULL,"+*-/=");
i++;
}
if (found==0){
Result=words[0];
operator1=5;
A=words[1];
}
else{
Result=words[0];
A=words[1]; //assigns arguments to strings A and B
B=words[2];
}
}
//checks whether an argument is an integer or not
inline bool let:: isInteger(const std::string & s)
{
if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;
char * p ;
strtol(s.c_str(), &p, 10) ;
return (*p == 0) ;
}
//check variables uses isInteger to check whether the argument is a variable or number and assigns A and B accordignly
int let::checkVariable(map<string,double> &variables,int Operator,int &line,int lines){
if(isInteger(A)==false)
{
line=lines+1; throw Nonexistentvariable();
}
else{
std::istringstream ss(A);
ss >> A1;
}
if(Operator!=5){
if(isInteger(B)==false)
{
A2=variables[B];
}
else{
std::istringstream ss(B);
ss >> A2;
}
}
}
//uses the operator assigned to compute the let
int let::compute(int A,int B,int Operator){
if (Operator==1){return A+B;}
if(Operator==2){return A*B;}
if(Operator==3){return A-B;}
if(Operator==4){return A/B;}
if(Operator==0){return A;}
return -1;
//else{return A;}
}
//execute function
void let::execute(std::vector<std::vector<string> >arr,map<string,double> &variables,int &line,int lines){
input=arr[line][2]; //assigns string Input
checkOperator();
checkVariable(variables,operator1,line,lines);
inresult=compute(A1,A2,operator1); //stores the computed result into inresult
variables[Result]=inresult; //inresult is assigned to map with key//
if(inresult!=-1){
line++;
}
else{line=lines+1;} //increments line to the end of the program//
}
//INPUT FUNCTION
class INPUT:public base
{
public:
void execute(std::vector<std::vector<string> >arr,map<string, double> &variables,int &line,int lines){
//extract value from string and assign to map
string key = arr[line][2];
//checks if inputed value is integer//
if (isInteger(key)==false){
double val;
cin >> val;
variables[key] = val;
line++;
}
else{
line=lines+1;
throw NumberInputException(); //throws exeception
}
}
inline bool isInteger(const std::string & s);
~INPUT();
};
//checks whether an argument is an integer or not
inline bool INPUT:: isInteger(const std::string & s)
{
if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;
char * p ;
strtol(s.c_str(), &p, 10) ;
return (*p == 0) ;
}
//GOTO CLASS
class GOTO:public base{
public:
inline bool isInteger(const std::string & s);
void execute(std::vector<std::vector<string> >arr,map<string, double> &variables,int &line,int lines);
~GOTO();
};
//checks whether an argument is an integer or not
inline bool GOTO::isInteger(const std::string & s)
{
if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;
char * p ;
strtol(s.c_str(), &p, 10) ;
return (*p == 0) ;
}
//execute checks if line number is a number and finds the corresponding line with the same characters in the stored vector
void GOTO::execute(std::vector<std::vector<string> >arr,map<string, double> &variables,int &line,int lines)
{
string element = arr[line][2];
int temp;
temp=line;
int i;
if (isInteger(element)==1)
{
for(i=0;i<lines;i++)
{
if(arr[i][0]==element)
{
line=i;
}
}
if(temp==line)
{
line=lines+1;
throw GOTOEXException();
}
}
else{throw GOTOEXException();}
}
//PRINT Class
class Print:public base{
private:
int i=2; //counter
public:
void execute(std::vector<std::vector<string> >arr,map<string, double> &variables,int &line,int lines){
int length = arr[line].size()-1;
if(variables.find(arr[line][2])==variables.end()){
//Check that there are quotation marks in the beginning of the statement
if(arr[line][2].at(0)== '\"'){
//Check that there are quotation marks at the end of the statement
if (arr[line][length][arr[line][length].size() - 1]== '\"'){
//remove quotes from first element of string to print
arr[line][2].erase(
remove( arr[line][2].begin(), arr[line][2].end(), '\"' ),
arr[line][2].end()
);
//remove quotes from last element of string to print
arr[line][length].erase(
remove(arr[line][length].begin(), arr[line][length].end(), '\"' ),
arr[line][length].end()
);
//Check for newline character and print correct strings
while (i != length+1){
string str2 = arr[line][i];
int pos = 0;
bool state = true;
while (pos != str2.length())
{
if(str2.at(pos) == '\\' && str2.at(pos+1) == 'n')
{
cout << endl;
state =false;
pos++;
}
else
{
cout << str2.at(pos);
}
pos++;
}
if (state == true)
{
cout << " ";
}
i++;
}
}
else
{
line=lines+1;
throw EndQuotesException();
}
}
else{
line=lines+1;
throw InitialQuotesException();
}
}
else{
cout <<variables[arr[line][2]];
}
line++;
}
~Print();
};
//MAIN PROGRAM CLASS
class program{
private:
string Filename;
std::vector<std::vector<string> >arr;
map<string, double> mymap;
std::map<int,base*> lines;
int line=0;
int length=0;
public:
void File(const char* const FileName);
string *separate(string line,int *count2);
void create();
int errorcheck(int i);
void execute();
};
//Create function checks the second element within the vector and assigns a pointer to a new (derived Class) for functions.
// The the pointer is then stored within the map(for base class pointers) and line executed with the execute function //
//Create passes on the required references and variables to the base class pointer execute//
//The execute function within the derived class overrides this and carries out its process.//
void program::create(){
if(arr[line][1]=="LET")
{
let* ptr=new let();
lines[line]=ptr;
lines[line]->execute(arr,mymap,line,length);
}
if(arr[line][1]=="IF")
{
IF* ptr=new IF();
lines[line]=ptr;
lines[line]->execute(arr,mymap,line,length);
}
if(arr[line][1]=="ENDIF")
{
line++;
}
if(arr[line][1]=="INPUT")
{
INPUT* ptr=new INPUT();
lines[line]=ptr;
lines[line]->execute(arr,mymap,line,length);
}
if(arr[line][1]=="GOTO")
{
GOTO* ptr=new GOTO();
lines[line]=ptr;
lines[line]->execute(arr,mymap,line,length);
}
if(arr[line][1]=="PRINT")
{
Print* ptr=new Print();
lines[line]=ptr;
lines[line]->execute(arr,mymap,line,length);
}
}
//separates the each line by spaces and returns the words within a line as an array.
string *program::separate(string line,int *count2)
{
string buff;
string *words;
int count1=0;
int i;
stringstream ss(line);
vector<string>tokens;
while(ss>>buff){
tokens.push_back((buff));
count1++;
}
words=new string[count1];
for(i=0;i<count1;i++)
{
words[i]=tokens[i];
}
*count2=count1;
return words;
}
//uses the function separate to create a 2D vector and does this for each of the lines
void program::File(const char* const FileName)
{
std::vector<string> sub;
std::ifstream infile(FileName);
string *words;
string line1;
int words2;
length=0;
while (std::getline(infile, line1))
{
length++;
string *words=separate(line1 ,&words2);
for(int j=0;j<words2;j++)
{
sub.push_back(words[j]);
}
arr.push_back(sub);
sub.clear();
if(errorcheck((length-1))==0){
line=length+1;
throw WrongstatementException();
}
}
}
//INITIAL ERRORCHECKING
int program::errorcheck(int i){
if(arr[i][1]=="PRINT"||"LET"||"GOTO"||"IF"||"INPUT"||"ENDIF") //checks that the statements entered are correct
{
return 1;
}
return 0;
}
//EXECUTE FUNCTION- STARTS THE LINE READING AND STATEMENT EXECUTION
void program::execute()
{
while(line<length)
{ //while loop goes through each line and executes the statement
create(); //creates the nodes dynamically
}
lines.clear(); //clears the map once program in is finished//
}
int main(int argc, char** argv)
{
program a;
//catches exception for errors in different classes
try {
a.File(argv[1]);
a.execute();
}
catch (EndQuotesException& e){
cerr << "Missing end quotes" << endl;
}
catch (InitialQuotesException& e){
cerr << "Missing beginning quotes"<<endl;
}
catch (NumberInputException& e){
cerr << "Invalid key, cannot be integer"<<endl;
}
catch (ENDIFException& e){
cerr << "Non matching IFs and ENDIFs"<<endl;
}
catch (GOTOEXException& e){
cerr << "LINE number does not exist"<<endl;
}
catch (WrongstatementException& e)
{
cerr << "Incorrect statement"<<endl; //catches exception for wrong statement
}
}