Search in sources :

Example 6 with Variable

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

the class FuncDirective method convertBlock.

protected static StatementList convertBlock(StatementList block) {
    final StatementList value = new StatementList();
    // new StringWriter()
    final ConstructorCall newStringWriter = new ConstructorCall(null, Template.LazyTypes.StringWriter, ArgumentList.EMPTY);
    // let writer = new StringWriter()
    final Variable writer = new Variable(Name.fromRaw("writer"), Template.LazyTypes.Writer, newStringWriter);
    writer.getAttributes().addFlag(Modifiers.FINAL | Modifiers.GENERATED);
    // { let writer = new StringWriter; { ... }; writer.toString }
    value.add(new VariableStatement(writer));
    value.add(block);
    value.add(new MethodCall(null, new FieldAccess(writer), Names.toString));
    return value;
}
Also used : Variable(dyvilx.tools.compiler.ast.field.Variable) VariableStatement(dyvilx.tools.compiler.ast.statement.VariableStatement) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) ConstructorCall(dyvilx.tools.compiler.ast.expression.access.ConstructorCall) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall)

Example 7 with Variable

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

the class MethodAssignment method writeExpression.

@Override
public void writeExpression(MethodWriter writer, IType type) throws BytecodeException {
    if (Types.isVoid(type)) {
        super.writeExpression(writer, type);
        return;
    }
    final IValue expression = this.arguments.getFirst();
    final Variable receiverVar = new Variable(null, this.receiver.getType(), this.receiver);
    final Variable expressionVar = new Variable(null, expression.getType(), expression);
    receiverVar.writeInit(writer);
    expressionVar.writeInit(writer);
    this.method.writeCall(writer, new FieldAccess(receiverVar), new ArgumentList(new FieldAccess(expressionVar)), this.genericData, Types.VOID, this.lineNumber());
    expressionVar.writeGet(writer);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) Variable(dyvilx.tools.compiler.ast.field.Variable) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList)

Example 8 with Variable

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

the class OptionalChainAware method transform.

static IValue transform(OptionalChainAware oca) {
    // oca = receiver?.access
    // <- oco ->
    final IValue oco = oca.getReceiver();
    if (oco == null || oco.valueTag() != IValue.OPTIONAL_CHAIN) {
        // no transformation needed as it is actually not an optional chain
        return oca;
    }
    final SourcePosition position = oca.getPosition();
    final IValue receiver = ((OptionalChainOperator) oco).getReceiver();
    // receiver is now the actual receiver of the optional chain operator
    BindingIfStatement bindingIf;
    if (receiver instanceof BindingIfStatement && (bindingIf = (BindingIfStatement) receiver).getElse() == NullValue.NULL) {
        // safe bet that the receiver used to be an optional chain
        // Perform the following transformation (the entire statement is the receiver):
        // if let $0 = oldReceiver { $0.oldAccess } else null
        // becomes
        // if (let $0 = oldReceiver, let $1 = $0.oldAccess) { $1.access } else null
        final Variable var = newVar(position, bindingIf.getThen());
        bindingIf.addVariable(var);
        oca.setReceiver(new FieldAccess(var));
        bindingIf.setThen(oca);
        return bindingIf;
    }
    // oca = receiver?.access, and receiver is not an optional chain
    // receiver?.access
    // becomes
    // if let $0 = receiver { $0.access } else null
    final Variable var = newVar(position, receiver);
    bindingIf = new BindingIfStatement(position);
    bindingIf.addVariable(var);
    oca.setReceiver(new FieldAccess(var));
    bindingIf.setThen(oca);
    bindingIf.setElse(NullValue.NULL);
    return bindingIf;
}
Also used : BindingIfStatement(dyvilx.tools.compiler.ast.statement.BindingIfStatement) IValue(dyvilx.tools.compiler.ast.expression.IValue) Variable(dyvilx.tools.compiler.ast.field.Variable) SourcePosition(dyvil.source.position.SourcePosition) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess)

Aggregations

Variable (dyvilx.tools.compiler.ast.field.Variable)8 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)5 IValue (dyvilx.tools.compiler.ast.expression.IValue)4 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)3 SourcePosition (dyvil.source.position.SourcePosition)2 BindingIfStatement (dyvilx.tools.compiler.ast.statement.BindingIfStatement)2 VariableStatement (dyvilx.tools.compiler.ast.statement.VariableStatement)2 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)1 IVariable (dyvilx.tools.compiler.ast.field.IVariable)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 IType (dyvilx.tools.compiler.ast.type.IType)1 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)1 TypeParser (dyvilx.tools.compiler.parser.type.TypeParser)1 ForEachDirective (dyvilx.tools.gensrc.ast.directive.ForEachDirective)1