-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserAST.cs
214 lines (189 loc) · 6.93 KB
/
ParserAST.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CS252Project
{
public class ParserAST
{
private IList<object> iList;
int initCount = 0; // keep track of number of token to be parsed
private readonly Statement result;
public ParserAST(IList<object> iList)
{
// TODO: Complete member initialization
this.iList = iList;
this.initCount = 0;
this.result = this.parseStatement();
}
public Statement Result
{
get { return result; }
}
public Statement parseStatement()
{
Statement stmt = null;
if (this.initCount == iList.Count)
{
// if token is empty
}
if (this.iList[this.initCount].ToString().Equals("print"))
{
this.initCount++;
//string value = ((System.Text.StringBuilder)this.iList[this.initCount]).ToString();
//Stringvalue stringLiteral = new Stringvalue();
//stringLiteral.value = value;
Print pobj = new Print();
pobj.Expr = this.GetExpr();// stringLiteral;
// this.initCount++;
stmt = pobj;
}
else if (this.iList[this.initCount].ToString().Equals("var"))
{
DVar var = new DVar();
this.initCount++;
var.Var = this.iList[this.initCount].ToString();
this.initCount++;
if (this.iList[this.initCount].Equals(ScanFile.op.Equal))
{
this.initCount++;
var.Expr = this.GetExpr();
}
stmt = var;
}
else if (this.iList[this.initCount].ToString().Equals("read_int"))
{
this.initCount++;
ReadInt readInt = new ReadInt();
if (!string.IsNullOrEmpty(this.iList[this.initCount].ToString()))
{
readInt.Ident = this.iList[this.initCount++].ToString();
stmt = readInt;
}
}
else if (this.iList[this.initCount].ToString().Equals("for"))
{
this.initCount++;
Forlp forLoop = new Forlp();
forLoop.Var = this.iList[this.initCount].ToString();
this.initCount++;
if (this.iList[this.initCount].Equals(ScanFile.op.Equal))
{
this.initCount++;
forLoop.From = this.GetExpr();
}
if (this.iList[this.initCount].ToString().Equals("to"))
{
this.initCount++;
forLoop.To = this.GetExpr();
}
if (this.iList[this.initCount].ToString().Equals("do"))
{
this.initCount++;
forLoop.stmt = this.parseStatement();
this.initCount++; // for semicolon
}
stmt = forLoop;
if (this.iList[this.initCount].ToString().Equals("end"))
{
//this.initCount++;
}
//this.initCount++;
}
else if (!string.IsNullOrEmpty(this.iList[this.initCount].ToString()))
{
Assign assign = new Assign();
assign.Var = this.iList[this.initCount++].ToString();
this.initCount++; // to expect = sign
// assign.Expr = this.GetExpr();
assign.Expr = GetBinop(this.GetExpr());
stmt = assign;
}
if (this.initCount < this.iList.Count && this.iList[this.initCount].Equals(ScanFile.op.SemiColon))
{
this.initCount++;
if (this.initCount < this.iList.Count && !this.iList[this.initCount].ToString().Equals("end"))
{
Sequence seq = new Sequence();
seq.stmt1 = stmt;
seq.stmt2 = this.parseStatement();
stmt = seq;
}
}
return stmt;
}
private Expr GetBinop(Expr exp1)
{
Expr expr=null;
BinExpr bexpr = new BinExpr();
bexpr.left = exp1;
//this.initCount++;
switch ((CS252Project.ScanFile.op)(this.iList[this.initCount]))
{
case CS252Project.ScanFile.op.Add:
bexpr.op = BinOp.Add;
this.initCount++;
break;
case CS252Project.ScanFile.op.Sub:
bexpr.op = BinOp.Sub;
this.initCount++;
break;
case CS252Project.ScanFile.op.Mult:
bexpr.op = BinOp.Mult;
this.initCount++;
break;
case CS252Project.ScanFile.op.Div:
bexpr.op = BinOp.Div;
this.initCount++;
break;
default:
break;
}
// bexpr.op = BinOp.Sub;
bexpr.right = this.GetExpr();
if (!this.iList[this.initCount].Equals(ScanFile.op.SemiColon))
{
expr = GetBinop(bexpr);
}
else
{
if (bexpr.right == null) // if single assignment
{
return bexpr.left;
}
return bexpr;
}
return expr;
}
private Expr GetExpr()
{
if (this.iList[this.initCount] is System.Text.StringBuilder && this.iList[this.initCount].ToString().StartsWith("\""))
{
string value = ((System.Text.StringBuilder)this.iList[this.initCount++]).ToString();
Stringvalue stringLiteral = new Stringvalue();
stringLiteral.value = value.Replace("\"","");
return stringLiteral;
}
else if (this.iList[this.initCount] is float)
{
float intValue = (float)this.iList[this.initCount++];
IntFloatValue intLiteral = new IntFloatValue();
intLiteral.value = intValue;
return intLiteral;
}
else if (this.iList[this.initCount] is StringBuilder)
{
string ident = this.iList[this.initCount++].ToString(); // considering that is a variable
Variable var = new Variable();
var.Ident = ident;
return var;
}
else
{
// to throw exception
return null;
}
}
public BinExpr bexpr { get; set; }
}
}