Search in sources :

Example 6 with ParseTree

use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.

the class NotInstanceofKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    if (keywordPosition == 0) {
        throw new ConfigCompileException("Expected value to proceed \"notinstanceof\" keyword, but no identifiers were found.", list.get(keywordPosition).getTarget());
    }
    if (list.size() <= keywordPosition + 1) {
        throw new ConfigCompileException("Expected type to follow \"notinstanceof\" keyword, but no type was found.", list.get(keywordPosition).getTarget());
    }
    ParseTree node = new ParseTree(new CFunction(INSTANCEOF, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
    node.addChild(list.get(keywordPosition - 1));
    node.addChild(list.get(keywordPosition + 1));
    ParseTree not = new ParseTree(new CFunction(NOT, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
    not.addChild(node);
    // Overwrite the LHS
    list.set(keywordPosition - 1, not);
    // Remove the keyword
    list.remove(keywordPosition);
    // Remove the RHS
    list.remove(keywordPosition);
    return keywordPosition;
}
Also used : CFunction(com.laytonsmith.core.constructs.CFunction) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 7 with ParseTree

use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.

the class ClosureKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    try {
        if (list.get(keywordPosition).getData() instanceof CFunction) {
            // It's a function, so do the old processing
            SimpleBlockKeywordFunction.doProcess(this.getKeywordName(), null, true, list, keywordPosition);
            // easiest if we do the conversion here.
            try {
                if (list.get(keywordPosition - 1).getData() instanceof CClassType) {
                    ParseTree type = list.remove(keywordPosition - 1);
                    List<ParseTree> children = list.get(keywordPosition - 1).getChildren();
                    children.add(0, type);
                    list.get(keywordPosition - 1).setChildren(children);
                    return keywordPosition - 1;
                }
            } catch (IndexOutOfBoundsException ex) {
            // Ignore, it's not a typed closure
            }
            return keywordPosition;
        } else {
            // Else it's standalone, so this should be treated as the closure ClassType
            list.set(keywordPosition, new ParseTree(CClosure.TYPE, list.get(keywordPosition).getFileOptions()));
            return keywordPosition;
        }
    } catch (IndexOutOfBoundsException ex) {
        throw new ConfigCompileException("Unexpected \"closure\" reference", list.get(keywordPosition).getTarget());
    }
}
Also used : CFunction(com.laytonsmith.core.constructs.CFunction) CClassType(com.laytonsmith.core.constructs.CClassType) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 8 with ParseTree

use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.

the class DoKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    // We expect the format to be "do" __cbracket__ while, so if this is not the case, we will
    // always throw an exception.
    Target t = list.get(keywordPosition).getTarget();
    try {
        ParseTree code = list.get(keywordPosition + 1);
        ParseTree _while = list.get(keywordPosition + 2);
        this.validateCodeBlock(code, "Missing brace following \"do\" keyword");
        if (!(_while.getData() instanceof CFunction) || !_while.getData().val().equals(WHILE)) {
            throw new ConfigCompileException("Missing while clause following \"do\" keyword", t);
        }
        if (_while.getChildren().isEmpty()) {
            throw new ConfigCompileException("Missing argument to while clause", _while.getTarget());
        }
        ParseTree dowhile = new ParseTree(new CFunction(DOWHILE, t), list.get(keywordPosition).getFileOptions());
        dowhile.addChild(this.getArgumentOrNull(code));
        dowhile.addChild(_while.getChildAt(0));
        list.set(keywordPosition, dowhile);
        list.remove(keywordPosition + 2);
        list.remove(keywordPosition + 1);
    } catch (IndexOutOfBoundsException ex) {
        throw new ConfigCompileException("Unexpected keyword \"do\"", t);
    }
    return keywordPosition;
}
Also used : Target(com.laytonsmith.core.constructs.Target) CFunction(com.laytonsmith.core.constructs.CFunction) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 9 with ParseTree

use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.

the class ForKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    ParseTree _for = list.get(keywordPosition);
    Target t = _for.getTarget();
    if (list.size() > keywordPosition + 1) {
        // This portion handles the initial code block, i.e. foreach(...){ }
        ParseTree codeBlock = list.get(keywordPosition + 1);
        if (isCodeBlock(codeBlock)) {
            validateCodeBlock(codeBlock, "");
            _for.addChild(getArgumentOrNull(codeBlock));
            list.remove(keywordPosition + 1);
        }
    }
    if (list.size() > keywordPosition + 1) {
        // This part handles the else keyword, i.e. foreach(...){ } else { }
        ParseTree elseKeyword = list.get(keywordPosition + 1);
        // If it's not an else keyword, then we'll leave it alone, and be done.
        if (elseKeyword.getData() instanceof CKeyword && elseKeyword.getData().val().equals("else")) {
            list.remove(keywordPosition + 1);
            ParseTree codeBlock = list.get(keywordPosition + 1);
            if (isCodeBlock(codeBlock)) {
                validateCodeBlock(codeBlock, "");
                _for.addChild(getArgumentOrNull(codeBlock));
            }
            // We also have to refactor this into a foreachelse, instead of a foreach.
            list.get(keywordPosition).setData(new CFunction(FORELSE, t));
            list.remove(keywordPosition + 1);
        }
    }
    return keywordPosition;
}
Also used : Target(com.laytonsmith.core.constructs.Target) CFunction(com.laytonsmith.core.constructs.CFunction) CKeyword(com.laytonsmith.core.constructs.CKeyword) ParseTree(com.laytonsmith.core.ParseTree)

Example 10 with ParseTree

use of com.laytonsmith.core.ParseTree in project CommandHelper by EngineHub.

the class ForeachKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    ParseTree foreach = list.get(keywordPosition);
    Target t = foreach.getTarget();
    if (list.size() > keywordPosition + 1) {
        // This portion handles the initial code block, i.e. foreach(...){ }
        ParseTree codeBlock = list.get(keywordPosition + 1);
        if (isCodeBlock(codeBlock)) {
            validateCodeBlock(codeBlock, "");
            foreach.addChild(getArgumentOrNull(codeBlock));
            list.remove(keywordPosition + 1);
        }
    }
    if (list.size() > keywordPosition + 1) {
        // This part handles the else keyword, i.e. foreach(...){ } else { }
        ParseTree elseKeyword = list.get(keywordPosition + 1);
        // If it's not an else keyword, then we'll leave it alone, and be done.
        if (elseKeyword.getData() instanceof CKeyword && elseKeyword.getData().val().equals("else")) {
            list.remove(keywordPosition + 1);
            ParseTree codeBlock = list.get(keywordPosition + 1);
            if (isCodeBlock(codeBlock)) {
                validateCodeBlock(codeBlock, "");
                foreach.addChild(getArgumentOrNull(codeBlock));
            }
            // We also have to refactor this into a foreachelse, instead of a foreach.
            list.get(keywordPosition).setData(new CFunction(FOREACHELSE, t));
            list.remove(keywordPosition + 1);
        }
    }
    return keywordPosition;
}
Also used : Target(com.laytonsmith.core.constructs.Target) CFunction(com.laytonsmith.core.constructs.CFunction) CKeyword(com.laytonsmith.core.constructs.CKeyword) 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