Search in sources :

Example 1 with Expression

use of stanhebben.zenscript.expression.Expression in project ZenScript by CraftTweaker.

the class StatementForeach method compile.

@Override
public void compile(IEnvironmentMethod environment) {
    Expression cList = list.compile(environment, ZenType.ANYARRAY).eval(environment);
    ZenType listType = cList.getType();
    IZenIterator iterator = listType.makeIterator(varnames.length, environment);
    if (iterator == null) {
        environment.error(getPosition(), "No iterator with " + varnames.length + " variables");
        return;
    }
    MethodOutput methodOutput = environment.getOutput();
    environment.getOutput().position(getPosition());
    IEnvironmentMethod local = new EnvironmentScope(environment);
    int[] localVariables = new int[varnames.length];
    for (int i = 0; i < localVariables.length; i++) {
        SymbolLocal localVar = new SymbolLocal(iterator.getType(i), true);
        local.putValue(varnames[i], localVar, getPosition());
        localVariables[i] = local.getLocal(localVar);
    }
    cList.compile(true, environment);
    iterator.compileStart(localVariables);
    Label repeat = new Label();
    Label exit = new Label();
    // Allows for break statements, sets the exit label!
    for (Statement statement : body.getSubStatements()) {
        if (statement instanceof StatementBreak)
            ((StatementBreak) statement).setExit(exit);
    }
    methodOutput.label(repeat);
    iterator.compilePreIterate(localVariables, exit);
    body.compile(local);
    iterator.compilePostIterate(localVariables, exit, repeat);
    methodOutput.label(exit);
    iterator.compileEnd();
}
Also used : Expression(stanhebben.zenscript.expression.Expression) ParsedExpression(stanhebben.zenscript.parser.expression.ParsedExpression) SymbolLocal(stanhebben.zenscript.symbols.SymbolLocal) Label(org.objectweb.asm.Label)

Example 2 with Expression

use of stanhebben.zenscript.expression.Expression in project ZenScript by CraftTweaker.

the class StatementVar method compile.

@Override
public void compile(IEnvironmentMethod environment) {
    environment.getOutput().position(getPosition());
    Expression cInitializer = initializer == null ? null : initializer.compile(environment, type).eval(environment);
    ZenType cType = type == null ? (cInitializer == null ? ZenTypeAny.INSTANCE : cInitializer.getType()) : type;
    SymbolLocal symbol = new SymbolLocal(cType, isFinal);
    environment.putValue(name, symbol, getPosition());
    if (cInitializer != null) {
        cInitializer.compile(true, environment);
        environment.getOutput().store(symbol.getType().toASMType(), environment.getLocal(symbol));
    }
}
Also used : Expression(stanhebben.zenscript.expression.Expression) ParsedExpression(stanhebben.zenscript.parser.expression.ParsedExpression) SymbolLocal(stanhebben.zenscript.symbols.SymbolLocal)

Example 3 with Expression

use of stanhebben.zenscript.expression.Expression in project ZenScript by CraftTweaker.

the class ParsedExpressionCompare method compile.

@Override
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
    Expression cLeft = left.compile(environment, null).eval(environment);
    Expression cRight = right.compile(environment, null).eval(environment);
    return cLeft.getType().compare(getPosition(), environment, cLeft, cRight, type);
}
Also used : Expression(stanhebben.zenscript.expression.Expression) IPartialExpression(stanhebben.zenscript.expression.partial.IPartialExpression)

Example 4 with Expression

use of stanhebben.zenscript.expression.Expression in project ZenScript by CraftTweaker.

the class ParsedExpressionIndexSet method compile.

@Override
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
    // TODO: improve prediction in this expression
    Expression cValue = value.compile(environment, null).eval(environment);
    Expression cIndex = index.compile(environment, null).eval(environment);
    Expression cSetValue = setValue.compile(environment, null).eval(environment);
    return cValue.getType().trinary(getPosition(), environment, cValue, cIndex, cSetValue, OperatorType.INDEXSET);
}
Also used : Expression(stanhebben.zenscript.expression.Expression) IPartialExpression(stanhebben.zenscript.expression.partial.IPartialExpression)

Example 5 with Expression

use of stanhebben.zenscript.expression.Expression in project ZenScript by CraftTweaker.

the class StatementReturn method compile.

@Override
public void compile(IEnvironmentMethod environment) {
    environment.getOutput().position(getPosition());
    if (expression == null) {
        environment.getOutput().ret();
    } else {
        Expression cExpression = expression.compile(environment, returnType).eval(environment);
        cExpression.compile(true, environment);
        Type returnType = cExpression.getType().toASMType();
        environment.getOutput().returnType(returnType);
    }
}
Also used : ZenType(stanhebben.zenscript.type.ZenType) Type(org.objectweb.asm.Type) Expression(stanhebben.zenscript.expression.Expression) ParsedExpression(stanhebben.zenscript.parser.expression.ParsedExpression)

Aggregations

Expression (stanhebben.zenscript.expression.Expression)11 IPartialExpression (stanhebben.zenscript.expression.partial.IPartialExpression)6 ParsedExpression (stanhebben.zenscript.parser.expression.ParsedExpression)4 ZenType (stanhebben.zenscript.type.ZenType)4 Label (org.objectweb.asm.Label)2 SymbolLocal (stanhebben.zenscript.symbols.SymbolLocal)2 Annotation (java.lang.annotation.Annotation)1 Type (org.objectweb.asm.Type)1