- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
Latest commit
67 lines (63 loc) · 1.51 KB
/
Copy pathparser.cpp
File metadata and controls
67 lines (63 loc) · 1.51 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
#include"parser_impl.hpp"
#include"table.hpp"
doubleParser::prim(bool get){
using Lexer::number_value;
using Lexer::string_value;
if(get) get_token();
switch (curr_tok) {
caseNUMBER: // Floating-point constant
{
double v = number_value;
get_token();
return v;
}
caseNAME:
{
double& v = table[string_value];
if(Lexer::get_token() == ASSIGN) v = expr(true);
return v;
}
caseMINUS: // Unary minus
return -prim(true);
caseLP:
{
double e = expr(true);
if (curr_tok != RP) throwSyntax_error(") expected");
get_token(); // Eat ')'
return e;
}
default:
throwSyntax_error("primary expected");
}
}
doubleParser::term(bool get) {
double left = prim(get);
for(;;)
switch (curr_tok) {
caseMUL:
left *= prim(true);
break;
caseDIV:
if (double d = prim(true)) {
left /= d;
break;
}
throwZero_divide();
default:
return left;
}
}
doubleParser::expr(bool get) {
double left = term(get);
for(;;)
switch (curr_tok) {
casePLUS:
left += term(true);
break;
caseMINUS:
left -= term(true);
break;
default:
return left;
}
}