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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
package bjc.dicelang.v1.examples;
import bjc.dicelang.v1.ast.DiceASTEvaluator;
import bjc.dicelang.v1.ast.DiceASTInliner;
import bjc.dicelang.v1.ast.DiceASTOptimizer;
import bjc.dicelang.v1.ast.DiceASTParser;
import bjc.dicelang.v1.ast.DiceASTReferenceSanitizer;
import bjc.dicelang.v1.ast.IResult;
import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
import bjc.dicelang.v1.ast.optimization.ConstantCollapser;
import bjc.dicelang.v1.ast.optimization.OperationCondenser;
import bjc.utils.data.ITree;
import bjc.utils.funcdata.FunctionalMap;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.IMap;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Test interface for AST-based dice language
*
* @author ben
*
*/
public class DiceASTLanguageTest {
private static IMap<String, DiceASTPragma> actions;
private static DiceASTOptimizer optimizer;
// Set up things that need to be configured
static {
actions = new FunctionalMap<>();
// Inline all the variables in a given expression
actions.put("inline", DiceASTLanguageTest::handleInlineAction);
// Print out the enviroment
actions.put("env", (tokenizer, enviroment) -> {
enviroment.forEach((varName, varValue) -> {
System.out.println(varName + " is bound to " + varValue);
});
});
// Create and configure the optimizer
optimizer = new DiceASTOptimizer();
optimizer.addPass(new ConstantCollapser());
}
// Read in a command
private static String getNextCommand(Scanner inputSource, int commandNumber) {
// Print a prompt using the current command number
System.out.print("\ndice-lang-" + commandNumber + "> ");
// Read in the next command
return inputSource.nextLine();
}
private static void handleInlineAction(FunctionalStringTokenizer tokenizer,
IMap<String, ITree<IDiceASTNode>> enviroment) {
// Skip the pragma name
tokenizer.nextToken();
// Get the pragma arguments
IList<String> pragmaArgs = tokenizer.toList();
if(pragmaArgs.getSize() < 3) {
// Complain about pragma arguments not being valid
System.err.println("ERROR: Inline requires at least 3 parameters. They are:"
+ "\n\t1. The name of the expression to inline."
+ "\n\t2. The name of the variable to bind the result to."
+ "\n\t3 and onwards. Names of variables to inline in the expression.");
} else {
// Get arguments
String inlineExpression = pragmaArgs.getByIndex(0);
String variableName = pragmaArgs.getByIndex(1);
// Grab the variables we want to inline
IList<String> inlinedVariables = pragmaArgs.tail().tail();
// Actually inline the variable
ITree<IDiceASTNode> inlinedExpression = DiceASTInliner.selectiveInline(
enviroment.get(inlineExpression), enviroment, inlinedVariables);
// Stick the inlined variable into the enviroment
enviroment.put(variableName, inlinedExpression);
}
}
/**
* Main method of class
*
* @param args
* Unused CLI args
*/
public static void main(String[] args) {
// Prepare the things we need for input
Scanner inputSource = new Scanner(System.in);
int commandNumber = 0;
// Grab the initial command
String currentLine = getNextCommand(inputSource, commandNumber);
// The enviroment for variables
IMap<String, ITree<IDiceASTNode>> enviroment = new FunctionalMap<>();
// Handle commands
while(!currentLine.equalsIgnoreCase("quit")) {
// Get the name of a possible action
String possibleActionName = currentLine.split(" ")[0];
// Check and see if we're executing an action
if(actions.containsKey(possibleActionName)) {
// Execute action
FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer(currentLine);
// Execute the action
actions.get(possibleActionName).accept(tokenizer, enviroment);
// Get the next command
currentLine = getNextCommand(inputSource, commandNumber);
continue;
}
// The AST we are going to build
ITree<IDiceASTNode> builtAST;
// Time command preparation
long time = System.nanoTime();
// Prepare the command
IList<String> preparedTokens = DiceExpressionPreparer.prepareCommand(currentLine);
System.out.println("Command prepared in " + (double) (System.nanoTime() - time) / 1000000000
+ " s");
try {
// Time the AST creation
time = System.nanoTime();
// Create the AST
builtAST = DiceASTParser.createFromString(preparedTokens);
System.out.println("Command parsed in "
+ (double) (System.nanoTime() - time) / 1000000000 + " s");
} catch(InputMismatchException | IllegalStateException | UnsupportedOperationException ex) {
// Tell the user there was an error in parsing
System.out.println("PARSING ERROR: " + ex.getLocalizedMessage());
// Move onto the next command
currentLine = getNextCommand(inputSource, commandNumber);
continue;
}
// Print out parsed AST
System.out.println("\tParsed: " + builtAST.toString());
// Time AST transformation
time = System.nanoTime();
// Transform the AST
ITree<IDiceASTNode> transformedAST = transformAST(builtAST, enviroment);
System.out.println("Command transformed in " + (double) (System.nanoTime() - time) / 1000000000
+ " s");
// Print out the transformed AST
System.out.println("\tTransformed: " + transformedAST.toString());
try {
// Time the evaluation
time = System.nanoTime();
// Evaluate the expression once
IResult sampleResult = DiceASTEvaluator.evaluateAST(transformedAST, enviroment);
System.out.println("Command evaluated in "
+ (double) (System.nanoTime() - time) / 1000000000 + " s");
// Print out the result of evaluating the
// expression
System.out.println("\t\tSample Result: " + sampleResult);
// Update the 'last' meta-variable
enviroment.put("last", transformedAST);
} catch(UnsupportedOperationException usex) {
// Tell the user there was an error in
// evaluation
System.out.println("EVALUATION ERROR: " + usex.getLocalizedMessage());
// Get the next command
currentLine = getNextCommand(inputSource, commandNumber);
// Process it
continue;
}
// Increase the number of commands
commandNumber++;
// Get the next command
currentLine = getNextCommand(inputSource, commandNumber);
}
System.out.println("Bye.");
// Cleanup after ourselves
inputSource.close();
}
// Transform a parsed AST
private static ITree<IDiceASTNode> transformAST(ITree<IDiceASTNode> builtAST,
IMap<String, ITree<IDiceASTNode>> enviroment) {
// Optimize the tree first
ITree<IDiceASTNode> optimizedTree = optimizer.optimizeTree(builtAST, enviroment);
// Then, condense unnecessary operations
ITree<IDiceASTNode> condensedTree = OperationCondenser.condense(optimizedTree);
// Next, sanitize references
ITree<IDiceASTNode> sanitizedTree = DiceASTReferenceSanitizer.sanitize(condensedTree, enviroment);
// Re-optimize the sanitized & condensed tree
optimizedTree = optimizer.optimizeTree(sanitizedTree, enviroment);
// Re-condense the newly optimized tree
condensedTree = OperationCondenser.condense(optimizedTree);
return condensedTree;
}
}
|