Search in sources :

Example 61 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class Property method resolve.

@Override
public void resolve(MarkerList markers, IContext context) {
    super.resolve(markers, context);
    if (this.getter != null) {
        this.getter.resolve(markers, context);
        // Infer Type if necessary
        if (this.type == Types.UNKNOWN) {
            this.type = this.getter.getType();
            if (this.setterParameter != null) {
                this.setterParameter.setType(this.type);
            }
        }
    }
    if (this.type == Types.UNKNOWN) {
        markers.add(Markers.semanticError(this.position, "property.type.infer", this.name));
    }
    if (this.setter != null) {
        this.setter.resolve(markers, context);
    }
    if (this.initializer != null) {
        final IContext context1 = new CombiningContext(this, context);
        final IValue resolved = this.initializer.resolve(markers, context1);
        this.initializer = TypeChecker.convertValue(resolved, Types.VOID, Types.VOID, markers, context1, TypeChecker.markerSupplier("property.initializer.type"));
    }
}
Also used : IContext(dyvilx.tools.compiler.ast.context.IContext) IValue(dyvilx.tools.compiler.ast.expression.IValue) CombiningContext(dyvilx.tools.compiler.ast.context.CombiningContext)

Example 62 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class Property method formatGetter.

private static void formatGetter(IMethod getter, String prefix, StringBuilder buffer) {
    final String indent = Formatting.getIndent("property.getter.indent", prefix);
    final IValue value = getter.getValue();
    buffer.append('\n').append(indent);
    getter.getAttributes().toInlineString(indent, buffer);
    buffer.append("get");
    if (value != null) {
        if (Util.formatStatementList(indent, buffer, value)) {
            return;
        }
        // Separator
        if (Formatting.getBoolean("property.getter.separator.space_before")) {
            buffer.append(' ');
        }
        buffer.append(':');
        if (Formatting.getBoolean("property.getter.separator.newline_after")) {
            buffer.append('\n').append(indent);
        } else if (Formatting.getBoolean("property.getter.separator.space_after")) {
            buffer.append(' ');
        }
        value.toString(indent, buffer);
    }
    if (Formatting.getBoolean("property.getter.semicolon")) {
        buffer.append(';');
    }
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue)

Example 63 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue 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)

Example 64 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class ExternalField method resolveConstantValue.

private void resolveConstantValue() {
    if ((this.resolved & RETURN_TYPE) == 0) {
        this.resolveReturnType();
    }
    this.resolved |= CONSTANT_VALUE;
    final IValue value = IValue.fromObject(this.constantValue);
    if (value != null) {
        this.value = value.withType(this.type, null, null, this.getCombiningContext());
    }
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue)

Example 65 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class MethodCall method foldConstants.

@Override
public IValue foldConstants() {
    if (!this.arguments.isEmpty()) {
        IValue argument;
        if (this.receiver != null) {
            if (this.receiver.isConstant()) {
                if (this.arguments.size() == 1 && (argument = this.arguments.getFirst()).isConstant()) {
                    // Binary Infix Operators
                    final IValue folded = ConstantFolder.applyInfix(this.receiver, this.name, argument);
                    if (folded != null) {
                        return folded;
                    }
                }
            } else {
                this.receiver = this.receiver.foldConstants();
            }
        } else if (this.arguments.size() == 1 && (argument = this.arguments.getFirst()).isConstant()) {
            // Unary Prefix Operators
            final IValue folded = ConstantFolder.applyUnary(this.name, argument);
            if (folded != null) {
                return folded;
            }
        }
        this.arguments.foldConstants();
        return this;
    }
    if (this.receiver != null) {
        if (this.receiver.isConstant()) {
            // Unary Postfix Operators (and some Prefix Operators)
            final IValue folded = ConstantFolder.applyUnary(this.name, this.receiver);
            if (folded != null) {
                return folded;
            }
        }
        this.receiver = this.receiver.foldConstants();
    }
    return this;
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue)

Aggregations

IValue (dyvilx.tools.compiler.ast.expression.IValue)79 IType (dyvilx.tools.compiler.ast.type.IType)20 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)17 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)11 SourcePosition (dyvil.source.position.SourcePosition)10 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)9 ArrayExpr (dyvilx.tools.compiler.ast.expression.ArrayExpr)7 Marker (dyvilx.tools.parsing.marker.Marker)7 IMethod (dyvilx.tools.compiler.ast.method.IMethod)6 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)5 IContext (dyvilx.tools.compiler.ast.context.IContext)5 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)5 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)5 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)4 IClass (dyvilx.tools.compiler.ast.classes.IClass)4 Variable (dyvilx.tools.compiler.ast.field.Variable)4 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)4 CombiningContext (dyvilx.tools.compiler.ast.context.CombiningContext)3 IVariable (dyvilx.tools.compiler.ast.field.IVariable)3 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)3