Search in sources :

Example 11 with IVariable

use of dyvilx.tools.compiler.ast.field.IVariable in project Dyvil by Dyvil.

the class ForStatement method writeStatement.

@Override
public void writeStatement(MethodWriter writer) throws BytecodeException {
    dyvilx.tools.asm.Label startLabel = this.startLabel.getTarget();
    dyvilx.tools.asm.Label updateLabel = this.updateLabel.getTarget();
    dyvilx.tools.asm.Label endLabel = this.endLabel.getTarget();
    IVariable var = this.variable;
    int locals = writer.localCount();
    // Variable
    if (var != null) {
        var.writeInit(writer);
    }
    writer.visitTargetLabel(startLabel);
    // Condition
    if (this.condition != null) {
        this.condition.writeInvJump(writer, endLabel);
    }
    // Action
    if (this.action != null) {
        this.action.writeExpression(writer, Types.VOID);
    }
    // Update
    writer.visitLabel(updateLabel);
    if (this.update != null) {
        this.update.writeExpression(writer, Types.VOID);
    }
    // Go back to Condition
    writer.visitJumpInsn(Opcodes.GOTO, startLabel);
    writer.resetLocals(locals);
    writer.visitLabel(endLabel);
    // Variable
    if (var != null) {
        var.writeLocal(writer, startLabel, endLabel);
    }
}
Also used : IVariable(dyvilx.tools.compiler.ast.field.IVariable)

Example 12 with IVariable

use of dyvilx.tools.compiler.ast.field.IVariable in project Dyvil by Dyvil.

the class StatementList method writeExpression.

@Override
public void writeExpression(MethodWriter writer, IType type) throws BytecodeException {
    int statementCount = this.valueCount - 1;
    if (statementCount < 0) {
        return;
    }
    dyvilx.tools.asm.Label start = new dyvilx.tools.asm.Label();
    dyvilx.tools.asm.Label end = new dyvilx.tools.asm.Label();
    writer.visitLabel(start);
    int localCount = writer.localCount();
    if (this.labels == null) {
        // Write all statements except the last one
        for (int i = 0; i < statementCount; i++) {
            this.values[i].writeExpression(writer, Types.VOID);
        }
        // Write the last expression
        this.values[statementCount].writeExpression(writer, type);
    } else {
        final int labelCount = this.labels.length;
        Label label;
        // Write all statements except the last one
        for (int i = 0; i < statementCount; i++) {
            if (i < labelCount && (label = this.labels[i]) != null) {
                writer.visitLabel(label.getTarget());
            }
            this.values[i].writeExpression(writer, Types.VOID);
        }
        // Write last expression (and label)
        if (statementCount < labelCount && (label = this.labels[statementCount]) != null) {
            writer.visitLabel(label.getTarget());
        }
        this.values[statementCount].writeExpression(writer, type);
    }
    writer.resetLocals(localCount);
    writer.visitLabel(end);
    if (this.variables == null) {
        return;
    }
    for (IVariable variable : this.variables) {
        variable.writeLocal(writer, start, end);
    }
}
Also used : IVariable(dyvilx.tools.compiler.ast.field.IVariable) Label(dyvilx.tools.compiler.ast.statement.control.Label)

Aggregations

IVariable (dyvilx.tools.compiler.ast.field.IVariable)12 IType (dyvilx.tools.compiler.ast.type.IType)4 IValue (dyvilx.tools.compiler.ast.expression.IValue)3 Name (dyvil.lang.Name)1 CastOperator (dyvilx.tools.compiler.ast.expression.CastOperator)1 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)1 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)1 OptionalUnwrapOperator (dyvilx.tools.compiler.ast.expression.optional.OptionalUnwrapOperator)1 IDataMember (dyvilx.tools.compiler.ast.field.IDataMember)1 Closure (dyvilx.tools.compiler.ast.statement.Closure)1 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)1 Label (dyvilx.tools.compiler.ast.statement.control.Label)1 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)1 ParameterListParser (dyvilx.tools.compiler.parser.method.ParameterListParser)1 Marker (dyvilx.tools.parsing.marker.Marker)1 IToken (dyvilx.tools.parsing.token.IToken)1