use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.
the class OptimizationUtilities method optimize.
/**
* This function takes a string script, and returns an equivalent, optimized script.
*
* @param script
* @return
* @throws ConfigCompileException
*/
public static String optimize(String script, File source) throws ConfigCompileException, ConfigCompileGroupException {
ParseTree tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, source, true));
StringBuilder b = new StringBuilder();
// The root always contains null.
for (ParseTree child : tree.getChildren()) {
b.append(optimize0(child));
}
return b.toString();
}
use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.
the class OptimizationUtilities method optimize0.
private static String optimize0(ParseTree node) {
if (node.getData() instanceof CFunction) {
StringBuilder b = new StringBuilder();
boolean first = true;
b.append(((CFunction) node.getData()).val()).append("(");
for (ParseTree child : node.getChildren()) {
if (!first) {
b.append(",");
}
first = false;
b.append(optimize0(child));
}
b.append(")");
return b.toString();
} else if (node.getData() instanceof CString) {
// strings
return new StringBuilder().append("'").append(node.getData().val().replaceAll("\t", "\\t").replaceAll("\n", "\\n").replace("\\", "\\\\").replace("'", "\\'")).append("'").toString();
} else if (node.getData() instanceof IVariable) {
return ((IVariable) node.getData()).getVariableName();
} else if (node.getData() instanceof Variable) {
return ((Variable) node.getData()).getVariableName();
} else if (node.getData() instanceof CSlice) {
return node.getData().val();
} else if (node.getData() instanceof CArray) {
// It's a hardcoded array. This only happens in the course of optimization, if
// the optimizer adds a new array. We still need to handle it appropriately though.
// The values in the array will be constant, guaranteed.
StringBuilder b = new StringBuilder();
b.append("array(");
boolean first = true;
CArray n = (CArray) node.getData();
for (String key : n.stringKeySet()) {
if (!first) {
b.append(",");
}
first = false;
b.append(optimize0(new ParseTree(n.get(key, Target.UNKNOWN), node.getFileOptions())));
}
b.append(")");
return b.toString();
} else {
// static
return node.getData().toString();
}
}
use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.
the class BoundEvent method execute.
private void execute(Environment env, ActiveEvent activeEvent) throws EventException {
ParseTree superRoot = new ParseTree(null);
superRoot.addChild(tree);
Event myDriver = this.getEventDriver();
myDriver.execute(superRoot, this, env, activeEvent);
}
use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.
the class MethodScriptStaticCompiler method compile.
/**
* Compiles the script, converting it into mid level object code, or in the case of a language compiler, the other
* language's source code.
*
* @param script
* @param platform
* @return
*/
public static String compile(String script, api.Platforms platform, File file) throws ConfigCompileException, ConfigCompileGroupException {
// First, we optimize. The "core" functions are always run through
// the native interpreter's compiler for optimization.
ParseTree tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, file, true));
StringBuilder b = new StringBuilder();
for (ParseTree node : tree.getChildren()) {
go(node, b, platform);
}
return b.toString();
}
use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.
the class OptimizerObject method optimize01.
/**
* This optimization level removes all the __autoconcat__s (and inadvertently several other constructs as well)
*
* @param tree
* @param compilerEnvironment
* @throws ConfigCompileException
*/
private void optimize01(ParseTree tree, CompilerEnvironment compilerEnvironment) throws ConfigCompileException {
com.laytonsmith.core.functions.Compiler.__autoconcat__ autoconcat = (com.laytonsmith.core.functions.Compiler.__autoconcat__) FunctionList.getFunction("__autoconcat__", Target.UNKNOWN);
if (tree.getData() instanceof CFunction && tree.getData().val().equals("__autoconcat__")) {
ParseTree tempNode = autoconcat.optimizeSpecial(tree.getChildren(), true);
tree.setData(tempNode.getData());
tree.setChildren(tempNode.getChildren());
}
for (int i = 0; i < tree.getChildren().size(); i++) {
ParseTree node = tree.getChildren().get(i);
optimize01(node, compilerEnvironment);
}
}
Aggregations