Search in sources :

Example 21 with ParseTree

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();
}
Also used : ParseTree(com.laytonsmith.core.ParseTree)

Example 22 with ParseTree

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();
    }
}
Also used : Variable(com.laytonsmith.core.constructs.Variable) IVariable(com.laytonsmith.core.constructs.IVariable) IVariable(com.laytonsmith.core.constructs.IVariable) CSlice(com.laytonsmith.core.constructs.CSlice) CArray(com.laytonsmith.core.constructs.CArray) CFunction(com.laytonsmith.core.constructs.CFunction) CString(com.laytonsmith.core.constructs.CString) ParseTree(com.laytonsmith.core.ParseTree) CString(com.laytonsmith.core.constructs.CString)

Example 23 with ParseTree

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);
}
Also used : ParseTree(com.laytonsmith.core.ParseTree)

Example 24 with ParseTree

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();
}
Also used : ParseTree(com.laytonsmith.core.ParseTree)

Example 25 with ParseTree

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);
    }
}
Also used : CFunction(com.laytonsmith.core.constructs.CFunction) ParseTree(com.laytonsmith.core.ParseTree)

Aggregations

ParseTree (com.laytonsmith.core.ParseTree)30 CFunction (com.laytonsmith.core.constructs.CFunction)16 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)16 CString (com.laytonsmith.core.constructs.CString)5 Target (com.laytonsmith.core.constructs.Target)5 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)5 ArrayList (java.util.ArrayList)5 CArray (com.laytonsmith.core.constructs.CArray)4 Construct (com.laytonsmith.core.constructs.Construct)4 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)4 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)4 CKeyword (com.laytonsmith.core.constructs.CKeyword)3 IVariable (com.laytonsmith.core.constructs.IVariable)3 Variable (com.laytonsmith.core.constructs.Variable)3 Environment (com.laytonsmith.core.environments.Environment)3 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)3 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)3 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)3 MethodScriptComplete (com.laytonsmith.core.MethodScriptComplete)2 TokenStream (com.laytonsmith.core.compiler.TokenStream)2