Repository navigation
Expand file tree
/
Copy pathStatement.java
More file actions
174 lines (148 loc) · 4.27 KB
/
Copy pathStatement.java
File metadata and controls
174 lines (148 loc) · 4.27 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
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
import java.util.ArrayList;
public abstract class Statement
{
protected ArrayList<String> tokens = new ArrayList<String>();
protected ArrayList<String> executedTokens = new ArrayList<String>();
public Statement(ArrayList<String> _tokens)
{
tokens.addAll(_tokens);
}
protected String getCurrentToken()
{
return tokens.get(0);
}
protected Statement createStatement() throws ParserException
{
String currenttoken = this.getCurrentToken();
if(currenttoken.equals("print"))
{
Print p = new Print(tokens);
return p;
}
else if (TokenHandler.isVariable(currenttoken))
{
Assignment a = new Assignment(tokens);
return a;
}
else if(currenttoken.equals("if"))
{
IF i = new IF(tokens);
return i;
}
else if(currenttoken.equals("while"))
{
While w = new While(tokens);
return w;
}
else if(currenttoken.equals("begin"))
{
CompoundStatement s = new CompoundStatement(tokens);
return s;
}
else
throw new ParserException("No valid syntax: " + currenttoken);
}
protected boolean ConditionIsTrue(ArrayList<String> tokens, ArrayList<String> outCondition) throws VariableException, ParserException
{
String currenttoken = tokens.get(0);
int leftvalue = TokenHandler.readTokenValue(currenttoken);
outCondition.add(currenttoken);
match(currenttoken, tokens);
currenttoken = tokens.get(0);
if(currenttoken.equals("<"))
{
outCondition.add(currenttoken);
match(currenttoken, tokens);
currenttoken = tokens.get(0);
int rightvalue = TokenHandler.readTokenValue(currenttoken);
outCondition.add(currenttoken);
match(currenttoken, tokens);
return (leftvalue < rightvalue);
}
if(currenttoken.equals(">"))
{
outCondition.add(currenttoken);
match(currenttoken, tokens);
currenttoken = tokens.get(0);
int rightvalue = TokenHandler.readTokenValue(currenttoken);
outCondition.add(currenttoken);
match(currenttoken, tokens);
return (leftvalue > rightvalue);
}
if(currenttoken.equals("<="))
{
outCondition.add(currenttoken);
match(currenttoken, tokens);
currenttoken = tokens.get(0);
int rightvalue = TokenHandler.readTokenValue(currenttoken);
outCondition.add(currenttoken);
match(currenttoken, tokens);
return (leftvalue <= rightvalue);
}
if(currenttoken.equals(">="))
{
outCondition.add(currenttoken);
match(currenttoken, tokens);
currenttoken = tokens.get(0);
int rightvalue = TokenHandler.readTokenValue(currenttoken);
outCondition.add(currenttoken);
match(currenttoken, tokens);
return (leftvalue >= rightvalue);
}
if(currenttoken.equals("="))
{
outCondition.add(currenttoken);
match(currenttoken, tokens);
currenttoken = tokens.get(0);
int rightvalue = TokenHandler.readTokenValue(currenttoken);
outCondition.add(currenttoken);
match(currenttoken, tokens);
return (leftvalue == rightvalue);
}
if(currenttoken.equals("/="))
{
outCondition.add(currenttoken);
match(currenttoken, tokens);
currenttoken = tokens.get(0);
int rightvalue = TokenHandler.readTokenValue(currenttoken);
outCondition.add(currenttoken);
match(currenttoken, tokens);
return (leftvalue != rightvalue);
}
throw new ParserException("Not a valid condition");
}
private void match(String token, ArrayList<String> matchTokens) throws ParserException
{
if(token.equals(matchTokens.get(0)))
{
executedTokens.add(token);
matchTokens.remove(0);
}
else
throw new ParserException("Unexpected token");
}
protected void match(String token) throws ParserException
{
match(token, tokens);
}
protected int resetTokens()
{
tokens.clear();
tokens.addAll(executedTokens);
int tokensSize = executedTokens.size();
executedTokens.clear();
return tokensSize;
}
protected void moveAhead(int numberofElements) throws ParserException
{
for(int i = 0; i < numberofElements; i++)
{
match(getCurrentToken());
}
}
public abstract int execute(boolean skip) throws ParserException, VariableException;
public int execute() throws ParserException, VariableException
{
return execute(false);
}
}