-
Notifications
You must be signed in to change notification settings - Fork 35
/
ExpressionParser.cs
102 lines (86 loc) · 2.53 KB
/
ExpressionParser.cs
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
using sly.lexer;
using sly.parser.generator;
namespace expressionparser
{
public class ExpressionParser
{
[NodeName("integer")]
[Production("primary: INT")]
public int Primary(Token<ExpressionToken> intToken)
{
return intToken.IntValue;
}
[NodeName("group")]
[Production("primary: LPAREN [d] expression RPAREN [d]")]
public int Group(int groupValue)
{
return groupValue;
}
[NodeName("addOrSubstract")]
[Production("expression : term PLUS expression")]
[Production("expression : term MINUS expression")]
public int Expression(int left, Token<ExpressionToken> operatorToken, int right)
{
var result = 0;
switch (operatorToken.TokenID)
{
case ExpressionToken.PLUS:
{
result = left + right;
break;
}
case ExpressionToken.MINUS:
{
result = left - right;
break;
}
}
return result;
}
[NodeName("expression")]
[Production("expression : term")]
public int Expression_Term(int termValue)
{
return termValue;
}
[NodeName("multOrDivide")]
[Production("term : factor TIMES term")]
[Production("term : factor DIVIDE term")]
public int Term(int left, Token<ExpressionToken> operatorToken, int right)
{
var result = 0;
switch (operatorToken.TokenID)
{
case ExpressionToken.TIMES:
{
result = left * right;
break;
}
case ExpressionToken.DIVIDE:
{
result = left / right;
break;
}
}
return result;
}
[Production("term : factor")]
[NodeName("term")]
public int Term_Factor(int factorValue)
{
return factorValue;
}
[Production("factor : primary")]
[NodeName("primary")]
public int primaryFactor(int primValue)
{
return primValue;
}
[NodeName("negate")]
[Production("factor : MINUS factor")]
public int Factor(Token<ExpressionToken> discardedMinus, int factorValue)
{
return -factorValue;
}
}
}