Search in sources :

Example 1 with CFunction

use of com.laytonsmith.core.constructs.CFunction in project CommandHelper by EngineHub.

the class Math method doIncrementDecrement.

/**
 * If we have the case {@code @array[0]++}, we have to increment it as though it were a variable, so we have to do
 * that with execs. This method consolidates the code to do so.
 *
 * @return
 */
protected static Construct doIncrementDecrement(ParseTree[] nodes, Script parent, Environment env, Target t, Function func, boolean pre, boolean inc) {
    if (nodes[0].getData() instanceof CFunction) {
        Function f;
        try {
            f = ((CFunction) nodes[0].getData()).getFunction();
        } catch (ConfigCompileException ex) {
            // This can't really happen, as the compiler would have already caught this
            throw new Error(ex);
        }
        if (f.getName().equals(new ArrayHandling.array_get().getName())) {
            // Ok, so, this is it, we're in charge here.
            long temp;
            long newVal;
            // First, pull out the current value. We're gonna do this manually though, and we will actually
            // skip the whole array_get execution.
            ParseTree eval = nodes[0];
            Construct array = parent.seval(eval.getChildAt(0), env);
            Construct index = parent.seval(eval.getChildAt(1), env);
            Construct cdelta = new CInt(1, t);
            if (nodes.length == 2) {
                cdelta = parent.seval(nodes[1], env);
            }
            long delta = Static.getInt(cdelta, t);
            // First, error check, then get the old value, and store it in temp.
            if (!(array instanceof CArray) && !(array instanceof ArrayAccess)) {
                // Let's just evaluate this like normal with array_get, so it will
                // throw the appropriate exception.
                new ArrayHandling.array_get().exec(t, env, array, index);
                throw ConfigRuntimeException.CreateUncatchableException("Shouldn't have gotten here. Please report this error, and how you got here.", t);
            } else if (!(array instanceof CArray)) {
                // own exception.
                throw new CRECastException("Cannot increment/decrement a non-array array" + " accessed value. (The value passed in was \"" + array.val() + "\")", t);
            } else {
                // Ok, we're good. Data types should all be correct.
                CArray myArray = ((CArray) array);
                Construct value = myArray.get(index, t);
                if (value instanceof CInt || value instanceof CDouble) {
                    temp = Static.getInt(value, t);
                    if (inc) {
                        newVal = temp + delta;
                    } else {
                        newVal = temp - delta;
                    }
                    new ArrayHandling.array_set().exec(t, env, array, index, new CInt(newVal, t));
                } else {
                    throw new CRECastException("Cannot increment/decrement a non numeric value.", t);
                }
            }
            long valueToReturn;
            if (pre) {
                valueToReturn = newVal;
            } else {
                valueToReturn = temp;
            }
            return new CInt(valueToReturn, t);
        }
    }
    Construct[] args = new Construct[nodes.length];
    for (int i = 0; i < args.length; i++) {
        args[i] = parent.eval(nodes[i], env);
    }
    return func.exec(t, env, args);
}
Also used : CRECastException(com.laytonsmith.core.exceptions.CRE.CRECastException) CArray(com.laytonsmith.core.constructs.CArray) CFunction(com.laytonsmith.core.constructs.CFunction) CDouble(com.laytonsmith.core.constructs.CDouble) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) CFunction(com.laytonsmith.core.constructs.CFunction) ArrayAccess(com.laytonsmith.core.natives.interfaces.ArrayAccess) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) ParseTree(com.laytonsmith.core.ParseTree)

Example 2 with CFunction

use of com.laytonsmith.core.constructs.CFunction in project CommandHelper by EngineHub.

the class Procedure method execute.

/**
 * Executes this procedure, with the arguments that were passed in
 *
 * @param args
 * @param env
 * @param t
 * @return
 */
public Construct execute(List<Construct> args, Environment env, Target t) {
    env.getEnv(GlobalEnv.class).SetVarList(new IVariableList());
    // This is what will become our @arguments var
    CArray arguments = new CArray(Target.UNKNOWN);
    for (String key : originals.keySet()) {
        Construct c = originals.get(key);
        env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(Auto.TYPE, key, c, Target.UNKNOWN));
        arguments.push(c, t);
    }
    // new Script(null, null);
    Script fakeScript = Script.GenerateScript(tree, env.getEnv(GlobalEnv.class).GetLabel());
    for (int i = 0; i < args.size(); i++) {
        Construct c = args.get(i);
        arguments.set(i, c, t);
        if (varIndex.size() > i) {
            String varname = varIndex.get(i).getVariableName();
            if (c instanceof CNull || InstanceofUtil.isInstanceof(c, varIndex.get(i).getDefinedType()) || varIndex.get(i).getDefinedType().equals(Auto.TYPE)) {
                env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(varIndex.get(i).getDefinedType(), varname, c, c.getTarget()));
            } else {
                throw new CRECastException("Procedure \"" + name + "\" expects a value of type " + varIndex.get(i).getDefinedType().val() + " in argument " + (i + 1) + ", but" + " a value of type " + c.typeof() + " was found instead.", c.getTarget());
            }
        }
    }
    env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(CArray.TYPE, "@arguments", arguments, Target.UNKNOWN));
    StackTraceManager stManager = env.getEnv(GlobalEnv.class).GetStackTraceManager();
    stManager.addStackTraceElement(new ConfigRuntimeException.StackTraceElement("proc " + name, getTarget()));
    try {
        if (tree.getData() instanceof CFunction && "sconcat".equals(tree.getData().val())) {
            // compiler proper.
            for (ParseTree child : tree.getChildren()) {
                fakeScript.eval(child, env);
            }
        } else {
            fakeScript.eval(tree, env);
        }
    } catch (FunctionReturnException e) {
        // Normal exit
        stManager.popStackTraceElement();
        Construct ret = e.getReturn();
        if (!InstanceofUtil.isInstanceof(ret, returnType)) {
            throw new CRECastException("Expected procedure \"" + name + "\" to return a value of type " + returnType.val() + " but a value of type " + ret.typeof() + " was returned instead", ret.getTarget());
        }
        return ret;
    } catch (LoopManipulationException ex) {
        // Not exactly normal, but pop anyways
        stManager.popStackTraceElement();
        // a compile error.
        throw ConfigRuntimeException.CreateUncatchableException("Loop manipulation operations (e.g. break() or continue()) cannot" + " bubble up past procedures.", t);
    } catch (ConfigRuntimeException e) {
        if (e instanceof AbstractCREException) {
            ((AbstractCREException) e).freezeStackTraceElements(stManager);
        }
        stManager.popStackTraceElement();
        throw e;
    } catch (Throwable th) {
        // Not sure. Pop, but rethrow
        stManager.popStackTraceElement();
        throw th;
    }
    // Normal exit, but no return.
    stManager.popStackTraceElement();
    // If we got here, then there was no return value. This is fine, but only for returnType void or auto.
    if (!(returnType.equals(Auto.TYPE) || returnType.equals(CVoid.TYPE))) {
        throw new CRECastException("Expecting procedure \"" + name + "\" to return a value of type " + returnType.val() + "," + " but no value was returned.", tree.getTarget());
    }
    return CVoid.VOID;
}
Also used : AbstractCREException(com.laytonsmith.core.exceptions.CRE.AbstractCREException) CRECastException(com.laytonsmith.core.exceptions.CRE.CRECastException) IVariable(com.laytonsmith.core.constructs.IVariable) IVariableList(com.laytonsmith.core.constructs.IVariableList) CArray(com.laytonsmith.core.constructs.CArray) CFunction(com.laytonsmith.core.constructs.CFunction) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) StackTraceManager(com.laytonsmith.core.exceptions.StackTraceManager) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) LoopManipulationException(com.laytonsmith.core.exceptions.LoopManipulationException) CNull(com.laytonsmith.core.constructs.CNull)

Example 3 with CFunction

use of com.laytonsmith.core.constructs.CFunction in project CommandHelper by EngineHub.

the class MethodScriptCompiler method compile.

/**
 * Compiles the token stream into a valid ParseTree. This also includes optimization and reduction.
 *
 * @param stream The token stream, as generated by {@link #lex(String, File, boolean) lex}
 * @return A fully compiled, optimized, and reduced parse tree. If {@code stream} is null or empty, null is
 * returned.
 * @throws ConfigCompileException If the script contains syntax errors. Additionally, during optimization, certain
 * methods may cause compile errors. Any function that can optimize static occurrences and throws a
 * {@link ConfigRuntimeException} will have that exception converted to a ConfigCompileException.
 */
public static ParseTree compile(TokenStream stream) throws ConfigCompileException, ConfigCompileGroupException {
    Set<ConfigCompileException> compilerErrors = new HashSet<>();
    if (stream == null || stream.isEmpty()) {
        return null;
    }
    Target unknown;
    try {
        // Instead of using Target.UNKNOWN, we can at least set the file.
        unknown = new Target(0, stream.get(0).target.file(), 0);
    } catch (Exception e) {
        unknown = Target.UNKNOWN;
    }
    // Remove all newlines and whitespaces.
    ListIterator<Token> it = stream.listIterator(0);
    while (it.hasNext()) {
        if (it.next().type.isWhitespace()) {
            it.remove();
        }
    }
    // Get the file options.
    final FileOptions fileOptions = stream.getFileOptions();
    ParseTree tree = new ParseTree(fileOptions);
    tree.setData(CNull.NULL);
    Stack<ParseTree> parents = new Stack<>();
    /**
     * constructCount is used to determine if we need to use autoconcat when reaching a FUNC_END. The previous
     * constructs, if the count is greater than 1, will be moved down into an autoconcat.
     */
    Stack<AtomicInteger> constructCount = new Stack<>();
    constructCount.push(new AtomicInteger(0));
    parents.push(tree);
    tree.addChild(new ParseTree(new CFunction("__autoconcat__", unknown), fileOptions));
    parents.push(tree.getChildAt(0));
    tree = tree.getChildAt(0);
    constructCount.push(new AtomicInteger(0));
    /**
     * The array stack is used to keep track of the number of square braces in use.
     */
    Stack<AtomicInteger> arrayStack = new Stack<>();
    arrayStack.add(new AtomicInteger(-1));
    Stack<AtomicInteger> minusArrayStack = new Stack<>();
    Stack<AtomicInteger> minusFuncStack = new Stack<>();
    int parens = 0;
    Token t = null;
    int bracketCount = 0;
    // Create a Token array to iterate over, rather than using the LinkedList's O(n) get() method.
    Token[] tokenArray = stream.toArray(new Token[stream.size()]);
    for (int i = 0; i < tokenArray.length; i++) {
        t = tokenArray[i];
        Token prev1 = i - 1 >= 0 ? tokenArray[i - 1] : new Token(TType.UNKNOWN, "", t.target);
        Token next1 = i + 1 < stream.size() ? tokenArray[i + 1] : new Token(TType.UNKNOWN, "", t.target);
        Token next2 = i + 2 < stream.size() ? tokenArray[i + 2] : new Token(TType.UNKNOWN, "", t.target);
        Token next3 = i + 3 < stream.size() ? tokenArray[i + 3] : new Token(TType.UNKNOWN, "", t.target);
        // Brace handling
        if (t.type == TType.LCURLY_BRACKET) {
            ParseTree b = new ParseTree(new CFunction("__cbrace__", t.getTarget()), fileOptions);
            tree.addChild(b);
            tree = b;
            parents.push(b);
            bracketCount++;
            constructCount.push(new AtomicInteger(0));
            continue;
        }
        if (t.type == TType.RCURLY_BRACKET) {
            bracketCount--;
            if (constructCount.peek().get() > 1) {
                // We need to autoconcat some stuff
                int stacks = constructCount.peek().get();
                int replaceAt = tree.getChildren().size() - stacks;
                ParseTree c = new ParseTree(new CFunction("__autoconcat__", tree.getTarget()), fileOptions);
                List<ParseTree> subChildren = new ArrayList<>();
                for (int b = replaceAt; b < tree.numberOfChildren(); b++) {
                    subChildren.add(tree.getChildAt(b));
                }
                c.setChildren(subChildren);
                if (replaceAt > 0) {
                    List<ParseTree> firstChildren = new ArrayList<>();
                    for (int d = 0; d < replaceAt; d++) {
                        firstChildren.add(tree.getChildAt(d));
                    }
                    tree.setChildren(firstChildren);
                } else {
                    tree.removeChildren();
                }
                tree.addChild(c);
            }
            parents.pop();
            tree = parents.peek();
            constructCount.pop();
            try {
                constructCount.peek().incrementAndGet();
            } catch (EmptyStackException e) {
                throw new ConfigCompileException("Unexpected end curly brace", t.target);
            }
            continue;
        }
        // Associative array/label handling
        if (t.type == TType.LABEL && tree.getChildren().size() > 0) {
            // If it's not an atomic identifier it's an error.
            if (!prev1.type.isAtomicLit() && prev1.type != TType.IVARIABLE && prev1.type != TType.KEYWORD) {
                ConfigCompileException error = new ConfigCompileException("Invalid label specified", t.getTarget());
                if (prev1.type == TType.FUNC_END) {
                    // and stop compilation at this point.
                    throw error;
                }
                compilerErrors.add(error);
            }
            // Wrap previous construct in a CLabel
            ParseTree cc = tree.getChildren().get(tree.getChildren().size() - 1);
            tree.removeChildAt(tree.getChildren().size() - 1);
            tree.addChild(new ParseTree(new CLabel(cc.getData()), fileOptions));
            continue;
        }
        // Array notation handling
        if (t.type.equals(TType.LSQUARE_BRACKET)) {
            arrayStack.push(new AtomicInteger(tree.getChildren().size() - 1));
            continue;
        } else if (t.type.equals(TType.RSQUARE_BRACKET)) {
            boolean emptyArray = false;
            if (prev1.type.equals(TType.LSQUARE_BRACKET)) {
                emptyArray = true;
            }
            if (arrayStack.size() == 1) {
                throw new ConfigCompileException("Mismatched square bracket", t.target);
            }
            // array is the location of the array
            int array = arrayStack.pop().get();
            // index is the location of the first node with the index
            int index = array + 1;
            if (!tree.hasChildren() || array == -1) {
                throw new ConfigCompileException("Brackets are illegal here", t.target);
            }
            ParseTree myArray = tree.getChildAt(array);
            ParseTree myIndex;
            if (!emptyArray) {
                myIndex = new ParseTree(new CFunction("__autoconcat__", myArray.getTarget()), fileOptions);
                for (int j = index; j < tree.numberOfChildren(); j++) {
                    myIndex.addChild(tree.getChildAt(j));
                }
            } else {
                myIndex = new ParseTree(new CSlice("0..-1", t.target), fileOptions);
            }
            tree.setChildren(tree.getChildren().subList(0, array));
            ParseTree arrayGet = new ParseTree(new CFunction("array_get", t.target), fileOptions);
            arrayGet.addChild(myArray);
            arrayGet.addChild(myIndex);
            // Check if the @var[...] had a negating "-" in front. If so, add a neg().
            if (!minusArrayStack.isEmpty() && arrayStack.size() + 1 == minusArrayStack.peek().get()) {
                if (!next1.type.equals(TType.LSQUARE_BRACKET)) {
                    // Wait if there are more array_get's comming.
                    ParseTree negTree = new ParseTree(new CFunction("neg", unknown), fileOptions);
                    negTree.addChild(arrayGet);
                    tree.addChild(negTree);
                    minusArrayStack.pop();
                } else {
                    // Negate the next array_get instead, so just add this one to the tree.
                    tree.addChild(arrayGet);
                }
            } else {
                tree.addChild(arrayGet);
            }
            constructCount.peek().set(constructCount.peek().get() - myIndex.numberOfChildren());
            continue;
        }
        // Smart strings
        if (t.type == TType.SMART_STRING) {
            if (t.val().contains("@")) {
                ParseTree function = new ParseTree(fileOptions);
                function.setData(new CFunction(new Compiler.smart_string().getName(), t.target));
                ParseTree string = new ParseTree(fileOptions);
                string.setData(new CString(t.value, t.target));
                function.addChild(string);
                tree.addChild(function);
            } else {
                tree.addChild(new ParseTree(new CString(t.val(), t.target), fileOptions));
            }
            constructCount.peek().incrementAndGet();
            continue;
        }
        if (t.type == TType.DEREFERENCE) {
            // Currently unimplemented, but going ahead and making it strict
            compilerErrors.add(new ConfigCompileException("The '" + t.val() + "' symbol is not currently allowed in raw strings. You must quote all" + " symbols.", t.target));
        }
        if (t.type.equals(TType.FUNC_NAME)) {
            CFunction func = new CFunction(t.val(), t.target);
            ParseTree f = new ParseTree(func, fileOptions);
            tree.addChild(f);
            constructCount.push(new AtomicInteger(0));
            tree = f;
            parents.push(f);
        } else if (t.type.equals(TType.FUNC_START)) {
            if (!prev1.type.equals(TType.FUNC_NAME)) {
                throw new ConfigCompileException("Unexpected parenthesis", t.target);
            }
            parens++;
        } else if (t.type.equals(TType.FUNC_END)) {
            if (parens <= 0) {
                throw new ConfigCompileException("Unexpected parenthesis", t.target);
            }
            parens--;
            // Pop function.
            parents.pop();
            if (constructCount.peek().get() > 1) {
                // We need to autoconcat some stuff
                int stacks = constructCount.peek().get();
                int replaceAt = tree.getChildren().size() - stacks;
                ParseTree c = new ParseTree(new CFunction("__autoconcat__", tree.getTarget()), fileOptions);
                List<ParseTree> subChildren = new ArrayList<>();
                for (int b = replaceAt; b < tree.numberOfChildren(); b++) {
                    subChildren.add(tree.getChildAt(b));
                }
                c.setChildren(subChildren);
                if (replaceAt > 0) {
                    List<ParseTree> firstChildren = new ArrayList<>();
                    for (int d = 0; d < replaceAt; d++) {
                        firstChildren.add(tree.getChildAt(d));
                    }
                    tree.setChildren(firstChildren);
                } else {
                    tree.removeChildren();
                }
                tree.addChild(c);
            }
            constructCount.pop();
            try {
                constructCount.peek().incrementAndGet();
            } catch (EmptyStackException e) {
                throw new ConfigCompileException("Unexpected end parenthesis", t.target);
            }
            try {
                tree = parents.peek();
            } catch (EmptyStackException e) {
                throw new ConfigCompileException("Unexpected end parenthesis", t.target);
            }
            // Handle "-func(args)" and "-func(args)[index]".
            if (!minusFuncStack.isEmpty() && minusFuncStack.peek().get() == parens + 1) {
                if (next1.type.equals(TType.LSQUARE_BRACKET)) {
                    // Move the negation to the array_get which contains this function.
                    // +1 because the bracket isn't counted yet.
                    minusArrayStack.push(new AtomicInteger(arrayStack.size() + 1));
                } else {
                    // Negate this function.
                    ParseTree negTree = new ParseTree(new CFunction("neg", unknown), fileOptions);
                    negTree.addChild(tree.getChildAt(tree.numberOfChildren() - 1));
                    tree.removeChildAt(tree.numberOfChildren() - 1);
                    tree.addChildAt(tree.numberOfChildren(), negTree);
                }
                minusFuncStack.pop();
            }
        } else if (t.type.equals(TType.COMMA)) {
            if (constructCount.peek().get() > 1) {
                int stacks = constructCount.peek().get();
                int replaceAt = tree.getChildren().size() - stacks;
                ParseTree c = new ParseTree(new CFunction("__autoconcat__", unknown), fileOptions);
                List<ParseTree> subChildren = new ArrayList<>();
                for (int b = replaceAt; b < tree.numberOfChildren(); b++) {
                    subChildren.add(tree.getChildAt(b));
                }
                c.setChildren(subChildren);
                if (replaceAt > 0) {
                    List<ParseTree> firstChildren = new ArrayList<>();
                    for (int d = 0; d < replaceAt; d++) {
                        firstChildren.add(tree.getChildAt(d));
                    }
                    tree.setChildren(firstChildren);
                } else {
                    tree.removeChildren();
                }
                tree.addChild(c);
            }
            constructCount.peek().set(0);
            continue;
        }
        if (t.type == TType.SLICE) {
            // "empty first" slice notation. Compare this to the code below.
            try {
                CSlice slice;
                String value = next1.val();
                if (next1.type == TType.MINUS || next1.type == TType.PLUS) {
                    value = next1.val() + next2.val();
                    i++;
                }
                slice = new CSlice(".." + value, t.getTarget());
                i++;
                tree.addChild(new ParseTree(slice, fileOptions));
                constructCount.peek().incrementAndGet();
                continue;
            } catch (ConfigRuntimeException ex) {
                // turn them into a CCE.
                throw new ConfigCompileException(ex);
            }
        }
        if (next1.type.equals(TType.SLICE)) {
            // Slice notation handling
            try {
                CSlice slice;
                if (t.type.isSeparator() || (t.type.isWhitespace() && prev1.type.isSeparator()) || t.type.isKeyword()) {
                    // empty first
                    String value = next2.val();
                    i++;
                    if (next2.type == TType.MINUS || next2.type == TType.PLUS) {
                        value = next2.val() + next3.val();
                        i++;
                    }
                    slice = new CSlice(".." + value, next1.getTarget());
                    if (t.type.isKeyword()) {
                        tree.addChild(new ParseTree(new CKeyword(t.val(), t.getTarget()), fileOptions));
                        constructCount.peek().incrementAndGet();
                    }
                } else if (next2.type.isSeparator() || next2.type.isKeyword()) {
                    // empty last
                    String modifier = "";
                    if (prev1.type == TType.MINUS || prev1.type == TType.PLUS) {
                        // The negative would have already been inserted into the tree
                        modifier = prev1.val();
                        tree.removeChildAt(tree.getChildren().size() - 1);
                    }
                    slice = new CSlice(modifier + t.value + "..", t.target);
                } else {
                    // both are provided
                    String modifier1 = "";
                    if (prev1.type == TType.MINUS || prev1.type == TType.PLUS) {
                        // It's a negative, incorporate that here, and remove the
                        // minus from the tree
                        modifier1 = prev1.val();
                        tree.removeChildAt(tree.getChildren().size() - 1);
                    }
                    Token first = t;
                    if (first.type.isWhitespace()) {
                        first = prev1;
                    }
                    Token second = next2;
                    i++;
                    String modifier2 = "";
                    if (next2.type == TType.MINUS || next2.type == TType.PLUS) {
                        modifier2 = next2.val();
                        second = next3;
                        i++;
                    }
                    slice = new CSlice(modifier1 + first.value + ".." + modifier2 + second.value, t.target);
                }
                i++;
                tree.addChild(new ParseTree(slice, fileOptions));
                constructCount.peek().incrementAndGet();
                continue;
            } catch (ConfigRuntimeException ex) {
                // turn them into a CCE.
                throw new ConfigCompileException(ex);
            }
        } else if (t.type == TType.LIT) {
            Construct c = Static.resolveConstruct(t.val(), t.target);
            if (c instanceof CString && fileOptions.isStrict()) {
                compilerErrors.add(new ConfigCompileException("Bare strings are not allowed in strict mode", t.target));
            } else if ((c instanceof CInt || c instanceof CDecimal) && next1.type == TType.DOT && next2.type == TType.LIT) {
                // minus zero before decimals and leading zeroes after decimals
                try {
                    if (t.value.startsWith("0m")) {
                        // CDecimal
                        String neg = "";
                        if (prev1.value.equals("-")) {
                            neg = "-";
                        }
                        c = new CDecimal(neg + t.value.substring(2) + '.' + next2.value, t.target);
                    } else {
                        // CDouble
                        c = new CDouble(Double.parseDouble(t.val() + '.' + next2.val()), t.target);
                    }
                    i += 2;
                } catch (NumberFormatException e) {
                // Not a double
                }
            }
            tree.addChild(new ParseTree(c, fileOptions));
            constructCount.peek().incrementAndGet();
        } else if (t.type.equals(TType.STRING) || t.type.equals(TType.COMMAND)) {
            tree.addChild(new ParseTree(new CString(t.val(), t.target), fileOptions));
            constructCount.peek().incrementAndGet();
        } else if (t.type.equals(TType.IDENTIFIER)) {
            tree.addChild(new ParseTree(new CPreIdentifier(t.val(), t.target), fileOptions));
            constructCount.peek().incrementAndGet();
        } else if (t.type.isKeyword()) {
            tree.addChild(new ParseTree(new CKeyword(t.val(), t.getTarget()), fileOptions));
            constructCount.peek().incrementAndGet();
        } else if (t.type.equals(TType.IVARIABLE)) {
            tree.addChild(new ParseTree(new IVariable(t.val(), t.target), fileOptions));
            constructCount.peek().incrementAndGet();
        } else if (t.type.equals(TType.UNKNOWN)) {
            tree.addChild(new ParseTree(Static.resolveConstruct(t.val(), t.target), fileOptions));
            constructCount.peek().incrementAndGet();
        } else if (t.type.isSymbol()) {
            // Also handles "-function()" and "-@var[index]".
            if (t.type.equals(TType.MINUS) && !prev1.type.isAtomicLit() && !prev1.type.equals(TType.IVARIABLE) && !prev1.type.equals(TType.VARIABLE) && !prev1.type.equals(TType.RCURLY_BRACKET) && !prev1.type.equals(TType.RSQUARE_BRACKET) && !prev1.type.equals(TType.FUNC_END) && (next1.type.equals(TType.IVARIABLE) || next1.type.equals(TType.VARIABLE) || next1.type.equals(TType.FUNC_NAME))) {
                // Check if we are negating a value from an array, function or variable.
                if (next2.type.equals(TType.LSQUARE_BRACKET)) {
                    // +1 because the bracket isn't counted yet.
                    minusArrayStack.push(new AtomicInteger(arrayStack.size() + 1));
                } else if (next1.type.equals(TType.FUNC_NAME)) {
                    // +1 because the function isn't counted yet.
                    minusFuncStack.push(new AtomicInteger(parens + 1));
                } else {
                    ParseTree negTree = new ParseTree(new CFunction("neg", unknown), fileOptions);
                    negTree.addChild(new ParseTree(new IVariable(next1.value, next1.target), fileOptions));
                    tree.addChild(negTree);
                    constructCount.peek().incrementAndGet();
                    // Skip the next variable as we've just handled it.
                    i++;
                }
            } else {
                tree.addChild(new ParseTree(new CSymbol(t.val(), t.type, t.target), fileOptions));
                constructCount.peek().incrementAndGet();
            }
        } else if (t.type == TType.DOT) {
            // Check for doubles that start with a decimal, otherwise concat
            Construct c = null;
            if (next1.type == TType.LIT && prev1.type != TType.STRING && prev1.type != TType.SMART_STRING) {
                try {
                    c = new CDouble(Double.parseDouble('.' + next1.val()), t.target);
                    i++;
                } catch (NumberFormatException e) {
                // Not a double
                }
            }
            if (c == null) {
                c = new CSymbol(".", TType.CONCAT, t.target);
            }
            tree.addChild(new ParseTree(c, fileOptions));
            constructCount.peek().incrementAndGet();
        } else if (t.type.equals(TType.VARIABLE) || t.type.equals(TType.FINAL_VAR)) {
            tree.addChild(new ParseTree(new Variable(t.val(), null, false, t.type.equals(TType.FINAL_VAR), t.target), fileOptions));
            constructCount.peek().incrementAndGet();
        // right_vars.add(new Variable(t.val(), null, t.line_num));
        }
    }
    assert t != null;
    if (arrayStack.size() != 1) {
        throw new ConfigCompileException("Mismatched square brackets", t.target);
    }
    if (parens != 0) {
        throw new ConfigCompileException("Mismatched parenthesis", t.target);
    }
    if (bracketCount != 0) {
        throw new ConfigCompileException("Mismatched curly braces", t.target);
    }
    Stack<List<Procedure>> procs = new Stack<>();
    procs.add(new ArrayList<Procedure>());
    processKeywords(tree);
    optimizeAutoconcats(tree, compilerErrors);
    optimize(tree, procs, compilerErrors);
    link(tree, compilerErrors);
    checkLabels(tree, compilerErrors);
    checkBreaks(tree, compilerErrors);
    if (!compilerErrors.isEmpty()) {
        if (compilerErrors.size() == 1) {
            // Just throw the one CCE
            for (ConfigCompileException e : compilerErrors) {
                throw e;
            }
        } else {
            throw new ConfigCompileGroupException(compilerErrors);
        }
    }
    parents.pop();
    tree = parents.pop();
    return tree;
}
Also used : CLabel(com.laytonsmith.core.constructs.CLabel) IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) CSymbol(com.laytonsmith.core.constructs.CSymbol) CPreIdentifier(com.laytonsmith.core.constructs.CPreIdentifier) IVariable(com.laytonsmith.core.constructs.IVariable) ArrayList(java.util.ArrayList) Token(com.laytonsmith.core.constructs.Token) CString(com.laytonsmith.core.constructs.CString) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) CString(com.laytonsmith.core.constructs.CString) EmptyStackException(java.util.EmptyStackException) Target(com.laytonsmith.core.constructs.Target) CSlice(com.laytonsmith.core.constructs.CSlice) KeywordList(com.laytonsmith.core.compiler.KeywordList) FunctionList(com.laytonsmith.core.functions.FunctionList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) CDecimal(com.laytonsmith.core.constructs.CDecimal) HashSet(java.util.HashSet) Compiler(com.laytonsmith.core.functions.Compiler) CFunction(com.laytonsmith.core.constructs.CFunction) CDouble(com.laytonsmith.core.constructs.CDouble) URISyntaxException(java.net.URISyntaxException) ProgramFlowManipulationException(com.laytonsmith.core.exceptions.ProgramFlowManipulationException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) DataSourceException(com.laytonsmith.persistence.DataSourceException) NoSuchElementException(java.util.NoSuchElementException) EmptyStackException(java.util.EmptyStackException) IOException(java.io.IOException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) Stack(java.util.Stack) CInt(com.laytonsmith.core.constructs.CInt) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Construct(com.laytonsmith.core.constructs.Construct) CKeyword(com.laytonsmith.core.constructs.CKeyword) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) FileOptions(com.laytonsmith.core.compiler.FileOptions)

Example 4 with CFunction

use of com.laytonsmith.core.constructs.CFunction in project CommandHelper by EngineHub.

the class CompilerObject method compile0.

void compile0() throws ConfigCompileException {
    Token t = consume();
    if (t.type == TType.NEWLINE) {
        return;
    }
    if (t.type == TType.CONST_START) {
        StringBuilder constName = new StringBuilder();
        while ((t = consume()).type != TType.RCURLY_BRACKET) {
            if (t.type != TType.BARE_STRING && t.type != TType.CONCAT) {
                throw new ConfigCompileException("Constant names may only contain names and dots.", t.getTarget());
            }
            constName.append(t.val());
        }
        Construct constant = env.getConstant(constName.toString());
        if (constant == null) {
            throw new ConfigCompileException("Expected the constant ${" + constName.toString() + "} to be provided in the compilation options, but it wasn't.", t.getTarget());
        }
        t = new Token(TType.STRING, constant.val(), constant.getTarget());
    }
    if (t.type == TType.BARE_STRING && peek().type == TType.FUNC_START) {
        consume();
        CFunction f = new CFunction(t.val(), t.getTarget());
        functionLines.add(peek().getTarget());
        pushNode(f);
        return;
    }
    if (t.type == TType.FUNC_END || t.type == TType.COMMA) {
        if (autoConcatCounter > 0) {
            autoConcatCounter--;
            popNode(t.getTarget());
        }
    }
    if (t.type == TType.COMMA) {
        return;
    }
    if (t.type == TType.FUNC_END) {
        // We're done with this child, so push it up
        popNode(t.getTarget());
        functionLines.pop();
        return;
    }
    if (t.type == TType.LSQUARE_BRACKET) {
        CFunction f = new CFunction("__cbracket__", Target.UNKNOWN);
        pushNode(f);
        bracketCounter++;
        bracketLines.push(t.getTarget());
        return;
    }
    if (t.type == TType.RSQUARE_BRACKET) {
        if (bracketCounter == 0) {
            throw new ConfigCompileException("Unexpected right bracket. (Did you have too many right square brackets (]) in your code?)", t.getTarget());
        }
        bracketCounter--;
        bracketLines.pop();
        popNode(t.getTarget());
        return;
    }
    if (t.type == TType.LCURLY_BRACKET) {
        CFunction f = new CFunction("__cbrace__", Target.UNKNOWN);
        pushNode(f);
        braceCounter++;
        braceLines.push(t.getTarget());
        return;
    }
    if (t.type == TType.RCURLY_BRACKET) {
        if (braceCounter == 0) {
            throw new ConfigCompileException("Unexpected right brace. (Did you have too many right braces (}) in your code?)", t.getTarget());
        }
        braceCounter--;
        braceLines.pop();
        popNode(t.getTarget());
        return;
    }
    // If the next token ISN'T a ) , } ] we need to autoconcat this
    if (peek().type != TType.FUNC_END && peek().type != TType.COMMA && peek().type != TType.RCURLY_BRACKET && peek().type != TType.RSQUARE_BRACKET) {
        // ... unless we're already in an autoconcat
        if (!(pointer.getData() instanceof CFunction && ((CFunction) pointer.getData()).val().equals("__autoconcat__"))) {
            CFunction f = new CFunction("__autoconcat__", Target.UNKNOWN);
            pushNode(f);
            autoConcatCounter++;
        }
    }
    if (t.type == TType.BARE_STRING && peek().type == TType.LABEL) {
        consume();
        pointer.addChild(new ParseTree(new CLabel(new CString(t.val(), t.getTarget())), stream.getFileOptions()));
        return;
    }
    if (t.type.isIdentifier()) {
        // If it's an atomic, put it in a construct and parse tree, then add it
        pointer.addChild(new ParseTree(resolveIdentifier(t), stream.getFileOptions()));
        return;
    }
    if (t.type.isSymbol()) {
        pointer.addChild(new ParseTree(new CSymbol(t.val(), t.type, t.getTarget()), stream.getFileOptions()));
        return;
    }
// Now we have to check ahead for commas and other division parameters.
}
Also used : CLabel(com.laytonsmith.core.constructs.CLabel) CSymbol(com.laytonsmith.core.constructs.CSymbol) Construct(com.laytonsmith.core.constructs.Construct) CFunction(com.laytonsmith.core.constructs.CFunction) Token(com.laytonsmith.core.constructs.Token) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree) CString(com.laytonsmith.core.constructs.CString)

Example 5 with CFunction

use of com.laytonsmith.core.constructs.CFunction in project CommandHelper by EngineHub.

the class TryKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    // If it's the old version, and a function
    if (list.get(keywordPosition).getData() instanceof CFunction && list.get(keywordPosition).getData().val().equals("try")) {
        return keywordPosition;
    }
    // Otherwise it's not, and we can continue on, assuming keyword usage.
    this.validateCodeBlock(list.get(keywordPosition + 1), "Expecting braces after try keyword");
    ParseTree complex_try = new ParseTree(new CFunction(COMPLEX_TRY, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
    complex_try.addChild(getArgumentOrNull(list.get(keywordPosition + 1)));
    // Remove the keyword and the try block
    list.remove(keywordPosition);
    list.remove(keywordPosition);
    // For now, we won't allow try {}, so this must be followed by a catch keyword. This restriction is somewhat artificial, and
    // if we want to remove it in the future, we can do so by removing this code block.
    {
        if (!(list.size() > keywordPosition && (nodeIsCatchKeyword(list.get(keywordPosition)) || nodeIsFinallyKeyword(list.get(keywordPosition))))) {
            throw new ConfigCompileException("Expecting \"catch\" or \"finally\" keyword to follow try, but none found", complex_try.getTarget());
        }
    }
    // We can have any number of catch statements after the try, so we loop through until we run out.
    for (int i = keywordPosition; i < list.size(); i++) {
        if (!nodeIsCatchKeyword(list.get(i)) && !nodeIsFinallyKeyword(list.get(i))) {
            // End of the chain, stop processing.
            break;
        }
        if (list.size() > i + 1) {
            this.validateCodeBlock(list.get(i + 1), "Expecting code block after catch, but none found");
        } else {
            throw new ConfigCompileException("catch must be followed by a code block, but none was found", list.get(i).getTarget());
        }
        if (list.get(i).getData() instanceof CFunction) {
            // We have something like catch(Exception @e) { }
            ParseTree n = list.get(i);
            if (n.getChildren().size() != 1) {
                throw new ConfigCompileException("Unexpected parameters passed to the \"catch\" clause." + " Exactly one argument must be passed.", n.getTarget());
            }
            complex_try.addChild(n.getChildAt(0));
            complex_try.addChild(getArgumentOrNull(list.get(i + 1)));
        } else {
            // clause statement, and we need to verify that there isn't a catch following it.
            if (list.size() > i + 2) {
                if (nodeIsCatchKeyword(list.get(i + 2))) {
                    throw new ConfigCompileException("A finally block must be the final" + " clause in the try/[catch]/finally statement", list.get(i + 2).getTarget());
                }
            }
            // Passed the inspection.
            complex_try.addChild(getArgumentOrNull(list.get(i + 1)));
        }
        // remove the catch keyword and the code block
        list.remove(i);
        list.remove(i);
        --i;
    }
    // Set the new function into place
    list.add(keywordPosition, complex_try);
    return keywordPosition;
}
Also used : CFunction(com.laytonsmith.core.constructs.CFunction) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Aggregations

CFunction (com.laytonsmith.core.constructs.CFunction)22 ParseTree (com.laytonsmith.core.ParseTree)16 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)13 CString (com.laytonsmith.core.constructs.CString)6 Construct (com.laytonsmith.core.constructs.Construct)6 IVariable (com.laytonsmith.core.constructs.IVariable)6 Target (com.laytonsmith.core.constructs.Target)5 CArray (com.laytonsmith.core.constructs.CArray)4 CKeyword (com.laytonsmith.core.constructs.CKeyword)4 CDouble (com.laytonsmith.core.constructs.CDouble)3 CInt (com.laytonsmith.core.constructs.CInt)3 Variable (com.laytonsmith.core.constructs.Variable)3 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)3 FunctionList (com.laytonsmith.core.functions.FunctionList)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 KeywordList (com.laytonsmith.core.compiler.KeywordList)2 CClassType (com.laytonsmith.core.constructs.CClassType)2 CLabel (com.laytonsmith.core.constructs.CLabel)2 CNull (com.laytonsmith.core.constructs.CNull)2