-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.cpp
More file actions
executable file
·130 lines (78 loc) · 2.41 KB
/
Interpreter.cpp
File metadata and controls
executable file
·130 lines (78 loc) · 2.41 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
#include <iostream>
#include <fstream>
#include <string>
#include "DataStructures/SinglyLinkedList.hpp"
#include <regex>
#include <random>
#include "Program.hpp"
#include <cstdlib>
int main(int argc, char** argv) {
std::string fileName;
if(argc > 2) {
std::cout << "Please run with the following form: " << std::endl;
std::cout << "ibscript.exe <filename>" << std::endl;
std::cout << std::endl;
std::cout << "For an IDE, please run IBScript.jar using:" << std::endl;
std::cout << "java -jar IBScript.jar" << std::endl;
std::exit(1);
} else if(argc == 2) { //Run from file
fileName = argv[1];
std::ifstream file(fileName);
std::string line;
//std::regex whitespace("^\\s*$");
SinglyLinkedList<std::string> linesList;
std::string* lines;
int linesCount;
while(!file.eof()) {
std::getline(file, line);
//std::cout << line << std::endl;
linesList.add(new std::string(line));
}
file.close();
linesCount = linesList.size();
lines = new std::string[linesCount];
//linesList->extractToArray(lines);
ListIterator<std::string>* lIt = linesList.getIterator();
int idx = 0;
while(lIt->hasMore()) {
lines[idx] = *(lIt->getValue());
idx++;
lIt->next();
}
Program prg(lines, linesCount);
//variables.status();
delete[] lines;
} else { //Live interpreter
SinglyLinkedList<std::string> linesList;
std::string* lines;
int level = 1;
while(true) {
for(int i = 0; i < level; i++) {
std::cout << "> ";
}
std::string line;
std::getline(std::cin, line);
std::cout << line << std::endl;
linesList.add(new std::string(line));
int linesCount = linesList.size();
lines = new std::string[linesCount];
ListIterator<std::string>* lIt = linesList.getIterator();
int idx = 0;
while(lIt->hasMore()) {
lines[idx] = *(lIt->getValue());
std::cout << ": " << lines[idx] << std::endl;
idx++;
lIt->next();
}
try {
Program prg(lines, linesCount);
} catch(RuntimeException) { //If output fails, remove last line
linesList.removeTail();
}
//Remove all outputs, so that unwanted outputs aren't given next program run
if(line.find("output ") != std::string::npos)
linesList.removeTail();
delete[] lines;
}
}
}