-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoorExcelVisitor.cs
161 lines (154 loc) · 5.93 KB
/
PoorExcelVisitor.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Antlr4.Runtime.Misc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PoorExcel
{
class PoorExcelVisitor : PoorExcelBaseVisitor<double>
{
Dictionary<string, double> tableIdentifier = new Dictionary<string, double>();
public override double VisitCompileUnit(PoorExcelParser.CompileUnitContext context)
{
return Visit(context.expression());
}
public override double VisitNumberExpr(PoorExcelParser.NumberExprContext context)
{
var result = double.Parse(context.GetText());
Debug.WriteLine(result);
return result;
}
public override double VisitIdentifierExpr(PoorExcelParser.IdentifierExprContext context)
{
var result = context.GetText();
double value;
if (tableIdentifier.TryGetValue(result.ToString(), out value))
return value;
else
return 0.0;
}
public override double VisitParenthesizedExpr(PoorExcelParser.ParenthesizedExprContext context)
{
return Visit(context.expression());
}
public override double VisitAdditiveExpr([NotNull] PoorExcelParser.AdditiveExprContext context)
{
var left = WalkLeft(context);
var right = WalkRight(context);
if (context.operatorToken.Type == PoorExcelLexer.ADD)
{
Debug.WriteLine("{0}+{1}", left, right);
return left + right;
}
else
{
Debug.WriteLine("{0}-{1}", left, right);
return left - right;
}
}
public override double VisitMultiplicativeExpr([NotNull] PoorExcelParser.MultiplicativeExprContext context)
{
var left = WalkLeft(context);
var right = WalkRight(context);
if (context.operatorToken.Type == PoorExcelLexer.MULTIPLY)
{
Debug.WriteLine("{0}*{1}", left, right);
return left * right;
}
else
{
Debug.WriteLine("{0} / {1}", left, right);
return left / right;
}
}
public override double VisitUnaryMExpr([NotNull] PoorExcelParser.UnaryMExprContext context)
{
var number = WalkLeft(context);
return number - 1;
}
public override double VisitUnaryPExpr([NotNull] PoorExcelParser.UnaryPExprContext context)
{
var number = WalkLeft(context);
return number + 1;
}
public override double VisitMmaxMminExpr([NotNull] PoorExcelParser.MmaxMminExprContext context)
{
List<double> list = new List<double>();
int idx = 0;
while (WalkN(context, idx) != 1.0101)
{
list.Add(WalkN(context, idx));
idx++;
}
var sortedList = from l in list orderby l select l;
if (context.operatorToken.Type == PoorExcelLexer.MMAX)
{
Debug.WriteLine("mmax(");
for (idx = 0; idx < list.Count(); ++idx)
{
Debug.WriteLine("{0}", list.ElementAt(idx));
if (idx != list.Count() - 1)
Debug.WriteLine(" , ");
}
Debug.WriteLine(")");
return sortedList.ElementAt(sortedList.Count() - 1);
}
else
{
Debug.WriteLine("mmin(");
for (idx = 0; idx < list.Count(); ++idx)
{
Debug.WriteLine("{0}", list.ElementAt(idx));
if (idx != list.Count() - 1)
Debug.WriteLine(" , ");
}
Debug.WriteLine(")");
return sortedList.ElementAt(0);
}
}
public override double VisitEqualExpr([NotNull] PoorExcelParser.EqualExprContext context)
{
var left = WalkLeft(context);
var right = WalkRight(context);
Debug.WriteLine("{0} = {1}", left, right);
return Convert.ToDouble(left == right);
}
public override double VisitLessExpr([NotNull] PoorExcelParser.LessExprContext context)
{
var left = WalkLeft(context);
var right = WalkRight(context);
Debug.WriteLine("{0} < {1}", left, right);
return Convert.ToDouble(left < right);
}
public override double VisitGreaterExpr([NotNull] PoorExcelParser.GreaterExprContext context)
{
var left = WalkLeft(context);
var right = WalkRight(context);
Debug.WriteLine("{0} > {1}", left, right);
return Convert.ToDouble(left > right);
}
public override double VisitNotExpr([NotNull] PoorExcelParser.NotExprContext context)
{
return Convert.ToDouble(!Convert.ToBoolean(WalkN(context, 0)));
}
private double WalkLeft(PoorExcelParser.ExpressionContext context)
{
return Visit(context.GetRuleContext<PoorExcelParser.ExpressionContext>(0));
}
private double WalkRight(PoorExcelParser.ExpressionContext context)
{
return Visit(context.GetRuleContext<PoorExcelParser.ExpressionContext>(1));
}
private double WalkN(PoorExcelParser.ExpressionContext context,int idx)
{
try
{
return Visit(context.GetRuleContext<PoorExcelParser.ExpressionContext>(idx));
}
catch (NullReferenceException) { return 1.0101; }
}
}
}