forked from heavy3/programming-abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
59 lines (51 loc) · 1.65 KB
/
Copy pathmain.cpp
File metadata and controls
59 lines (51 loc) · 1.65 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
//
// main.cpp
//
// --------------------------------------------------------------------------
// Attribution: "Programming Abstractions in C++" by Eric Roberts
// Chapter 19, Exercise 07
// Stanford University, Autumn Quarter 2012
// http://web.stanford.edu/class/archive/cs/cs106b/cs106b.1136/materials/CS106BX-Reader.pdf
// --------------------------------------------------------------------------
//
// Created by Glenn Streiff on 3/27/17
// Copyright © 2017 Glenn Streiff. All rights reserved.
//
#include <iostream>
#include <string>
#include "error.h"
#include "exp.h"
#include "parser.h"
#include "tokenscanner.h"
using namespace std;
const std::string HEADER = "CS106B Programming Abstractions in C++: Ex 19.07\n";
const std::string DETAIL = "Inheritance: Arithmetic interpreter with % operator";
const std::string BANNER = HEADER + DETAIL;
const std::string PROMPT = "=> ";
// Main program
int main() {
cout << BANNER << endl << endl;
cout << "Enter an arithmetic expression to evaluate (e.g., x = 3 + 4)";
cout << endl;
EvaluationContext context;
TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.scanNumbers();
while (true) {
Expression* exp = NULL;
try {
string line;
cout << PROMPT;
getline(cin, line);
if (line == "quit") break;
scanner.setInput(line);
exp = parseExp(scanner);
int value = exp->eval(context);
cout << value << endl;
} catch (ErrorException& ex) {
cerr << "Error: " << ex.getMessage() << endl;
}
if (exp != NULL) delete exp;
}
return 0;
}