-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexprtest.cpp
More file actions
78 lines (67 loc) · 1.7 KB
/
exprtest.cpp
File metadata and controls
78 lines (67 loc) · 1.7 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
// $Id: exprtest.cc 18 2007-08-19 19:51:52Z tb $
#include <iostream>
#include <fstream>
#include "driver.hpp"
#include "expression.hpp"
int main(int argc, char *argv[])
{
CalcContext calc;
example::Driver driver(calc);
bool readfile = false;
for(int ai = 1; ai < argc; ++ai)
{
if (argv[ai] == std::string ("-p")) {
driver.trace_parsing = true;
}
else if (argv[ai] == std::string ("-s")) {
driver.trace_scanning = true;
}
else
{
// read a file with expressions
std::fstream infile(argv[ai]);
if (!infile.good())
{
std::cerr << "Could not open file: " << argv[ai] << std::endl;
return 0;
}
calc.clearExpressions();
bool result = driver.parse_stream(infile, argv[ai]);
if (result)
{
std::cout << "Expressions:" << std::endl;
for (unsigned int ei = 0; ei < calc.expressions.size(); ++ei)
{
std::cout << "[" << ei << "]:" << std::endl;
std::cout << "tree:" << std::endl;
calc.expressions[ei]->print(std::cout);
std::cout << "evaluated: "
<< calc.expressions[ei]->evaluate()
<< std::endl;
}
}
readfile = true;
}
}
if (readfile) return 0;
std::cout << "Reading expressions from stdin" << std::endl;
std::string line;
while( std::cout << "input: " &&
std::getline(std::cin, line) &&
!line.empty() )
{
calc.clearExpressions();
bool result = driver.parse_string(line, "input");
if (result)
{
for (unsigned int ei = 0; ei < calc.expressions.size(); ++ei)
{
std::cout << "tree:" << std::endl;
calc.expressions[ei]->print(std::cout);
std::cout << "evaluated: "
<< calc.expressions[ei]->evaluate()
<< std::endl;
}
}
}
}