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;
}
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);
}
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;
}
Aggregations