-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·62 lines (54 loc) · 1.42 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·62 lines (54 loc) · 1.42 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
/**
* File: main.cpp
* --------------
* Manages the central read-eval-loop that
* interacts with the user and evaluates all
* expressions.
*/
#include <iostream>
#include <string>
#include "genlib.h"
#include "expression.h"
#include "evalstate.h"
#include "scanner.h"
/**
* The main function does little more than manage the
* high level read-eval-print loop.
*/
int main() {
EvalState state;
state.addBuiltins();
Scanner scanner;
scanner.setSpaceOption(Scanner::IgnoreSpaces);
scanner.setStringOption(Scanner::ScanQuotesAsStrings);
int line = 1;
while (true) {
cout << "little-schemer " << line++ << "> ";
string expression;
getline(cin, expression);
scanner.setInput(expression);
try {
Expression *exp = Expression::readExpression(scanner);
if (exp != NULL) {
Expression *result = exp->eval(state);
cout << result->toString() << endl;
}
} catch (string errorMsg) {
cout << "Error: " + errorMsg << endl;
}
}
return 0;
}
/**
* Throws the supplied error message as an exception, designed
* to be caught near the bottom of the main function. This
* version of Error displaces the built-in version of Error,
* which ends the program whenever it's called.
*
* @param string errorMsg the error message that should be
* printed to signal a problem before moving on and
* prompting the user to enter another expression.
*/
void Error(string errorMsg) {
throw errorMsg;
}