-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
executable file
·200 lines (189 loc) · 4.41 KB
/
main.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
/**
* \file main.cpp
*
* Driver code implementing the main read-parse-eval-print loop.
* Supports both (1) an interactive mode, and (2) a batch mode where
* input expressions are read from the file specified by the first
* command-line argument.
*/
#include <stdexcept>
#include "parse.hpp"
#include "eval.hpp"
#include <sstream>
using namespace std;
/**
* \brief Parse and evaluate the s-expression, and print the result.
* \param sexpr The string vaule holding the s-expression.
*/
void parse_eval_print(string sexpr)
{
try {
Cell* root = parse(sexpr);
// cout << endl;
// cout << *root << endl;
Cell* result = eval(root);
if ( result == nil ) {
cout << "()" << endl;
} else {
cout << *result << endl;
}
// delete root;
// delete result;
} catch (runtime_error &e) {
cerr << "ERROR: " << e.what() << endl;
} catch (logic_error &e) {
cerr << "LOGIC ERROR: " << e.what() << endl;
exit(1);
}
}
/**
* \brief Read single single symbol into the end of a string buffer.
* \param fin The input file stream.
* \param str The string buffer.
*/
void readsinglesymbol(ifstream& fin, string& str)
{
char currentchar;
fin.get(currentchar);
if (fin.eof()) {
return;
}
if (currentchar == '\"') {
// read a string literal
do {
str += currentchar;
fin.get(currentchar);
} while (currentchar != '\"');
str += currentchar;
} else {
do {
str += currentchar;
fin.get(currentchar);
} while ((false == iswhitespace(currentchar))
&& ('(' != currentchar)
&& (false == fin.eof()));
fin.putback(currentchar);
}
}
/**
* \brief Read, parse, evaluate, and print the expression one by one from
* the input stream.
*
* \param fin The input file stream.
*/
void readfile(ifstream& fin)
{
string sexp;
bool isstartsexp = false;
int inumleftparenthesis = 0;
// check whether to read the end
while (!fin.eof()) {
// read char by char
char currentchar;
fin.get(currentchar);
if (fin.eof()) {
break;
}
// skip some white space before new s-expression occurs
if ((true == iswhitespace(currentchar))&&(false == isstartsexp)) {
continue;
}
// run across a new s-expression
if ((false == isstartsexp)&&(false == iswhitespace(currentchar))) {
// check whether single symbol
if ('(' != currentchar) {
// read a single symbol
fin.putback(currentchar);
readsinglesymbol(fin, sexp);
// call function
parse_eval_print(sexp);
sexp.clear();
} else {
// start new expression
isstartsexp = true;
// read left parenthesis
sexp += currentchar;
inumleftparenthesis = 1;
}
} else {
// in the process of reading the current s-expression
if (true == isstartsexp) {
if (true == iswhitespace(currentchar)) {
// append a blankspace
//sexp += ' ';
sexp += currentchar;
} else {
// append current character
sexp += currentchar;
// count left parenthesis
if ('(' == currentchar) {
inumleftparenthesis ++;
}
if (')' == currentchar) {
inumleftparenthesis --;
// check whether current s-expression ends
if (0 == inumleftparenthesis) {
// current s-expression ends
isstartsexp = false;
// call functions
parse_eval_print(sexp);
sexp.clear();
}
}
}
}
}
}
}
/**
* \brief Read the expressions from the file.
* \param fn The file name.
*/
void readfile(char* fn)
{
ifstream fin(fn);
readfile(fin);
fin.close();
}
/**
* \brief Read, parse, evaluate, and print the expression one by one from
* the standard input, interactively.
*/
void readconsole()
{
string sexpr;
// read the input
do {
cout << "> ";
getline(cin, sexpr);
if (cin.eof()) {
break;
}
if ("(exit)" == sexpr) {
return;
}
parse_eval_print(sexpr);
} while (true);
}
/**
* \brief Call either the batch or interactive main drivers.
*/
int main(int argc, char* argv[])
{
// read from the standard input
readfile("library.scm");
switch(argc) {
case 1:
readconsole();
exit(0);
break;
case 2:
// read from a file
readfile(argv[1]);
break;
default:
cout << "too many arguments!" << endl;
exit(0);
}
return 0;
}