Search in sources :

Example 1 with IValue

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

the class ForEachStatement method resolve.

@Override
public IValue resolve(MarkerList markers, IContext context) {
    IType varType = this.variable.getType();
    final IValue value = this.resolveValue(markers, context);
    final IType valueType = value.getType();
    if (value.valueTag() == IValue.METHOD_CALL) {
        final MethodCall rangeOperator = (MethodCall) value;
        if (RangeForStatement.isRangeOperator(rangeOperator)) {
            final IType elementType = RangeForStatement.getElementType(rangeOperator);
            if (varType == Types.UNKNOWN) {
                this.inferVariableType(markers, elementType);
            } else if (!Types.isAssignable(varType, elementType)) {
                final Marker marker = Markers.semanticError(value.getPosition(), "for.range.type");
                marker.addInfo(Markers.getSemantic("range.type", valueType));
                marker.addInfo(Markers.getSemantic("variable.type", varType));
                markers.add(marker);
            }
            final RangeForStatement rangeForStatement = new RangeForStatement(this.position, this.variable, elementType);
            rangeForStatement.resolveAction(this.action, markers, context);
            return rangeForStatement;
        }
    }
    final ArrayType arrayType = valueType.extract(ArrayType.class);
    if (arrayType != null) {
        if (varType == Types.UNKNOWN) {
            this.inferVariableType(markers, arrayType.getElementType());
        } else if (!Types.isAssignable(varType, arrayType.getElementType())) {
            final Marker marker = Markers.semanticError(value.getPosition(), "for.array.type");
            marker.addInfo(Markers.getSemantic("array.type", valueType));
            marker.addInfo(Markers.getSemantic("variable.type", varType));
            markers.add(marker);
        }
        final ArrayForStatement arrayForStatement = new ArrayForStatement(this.position, this.variable, arrayType);
        arrayForStatement.resolveAction(this.action, markers, context);
        return arrayForStatement;
    }
    if (Types.isAssignable(IterableForStatement.LazyFields.ITERATOR, valueType)) {
        return this.toIteratorLoop(markers, context, varType, value, valueType);
    }
    if (Types.isAssignable(IterableForStatement.LazyFields.ITERABLE, valueType)) {
        return this.toIterable(markers, context, varType, value, valueType);
    }
    if (Types.isAssignable(Types.STRING, valueType)) {
        return this.toStringLoop(markers, context, varType, value);
    }
    final Marker marker = Markers.semanticError(this.variable.getPosition(), "for.each.invalid");
    marker.addInfo(Markers.getSemantic("value.type", valueType));
    markers.add(marker);
    this.resolveAction(this.action, markers, context);
    return this;
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IValue(dyvilx.tools.compiler.ast.expression.IValue) Marker(dyvilx.tools.parsing.marker.Marker) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall) IType(dyvilx.tools.compiler.ast.type.IType)

Example 2 with IValue

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

the class ForEachStatement method resolveValue.

private IValue resolveValue(MarkerList markers, IContext context) {
    IValue value = this.variable.getValue().resolve(markers, context);
    IType valueType = value.getType();
    value = TypeChecker.convertValue(value, valueType, valueType, markers, context, TypeChecker.markerSupplier("for.each.type"));
    this.variable.setValue(value);
    return value;
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) IType(dyvilx.tools.compiler.ast.type.IType)

Example 3 with IValue

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

the class Field method resolve.

@Override
public void resolve(MarkerList markers, IContext context) {
    super.resolve(markers, context);
    if (this.value != null) {
        final IContext context1 = new CombiningContext(this, context);
        this.value = this.value.resolve(markers, context1);
        boolean inferType = false;
        if (this.type == Types.UNKNOWN) {
            inferType = true;
            this.type = this.value.getType();
        }
        final TypeChecker.MarkerSupplier markerSupplier = TypeChecker.markerSupplier("field.type.incompatible", "field.type", "value.type", this.name);
        this.value = TypeChecker.convertValue(this.value, this.type, this.type, markers, context1, markerSupplier);
        if (inferType) {
            this.type = this.value.getType();
            if (this.type == Types.UNKNOWN && this.value.isResolved()) {
                markers.add(Markers.semantic(this.position, "field.type.infer", this.name.unqualified));
                this.type = Types.ANY;
            }
        }
    } else if (this.type == Types.UNKNOWN) {
        markers.add(Markers.semantic(this.position, "field.type.infer.novalue", this.name.unqualified));
        this.type = Types.ANY;
    }
    if (this.property == null) {
        return;
    }
    this.property.setType(this.type);
    final IMethod getter = this.property.getGetter();
    final IMethod setter = this.property.getSetter();
    final IValue receiver = this.hasModifier(Modifiers.STATIC) ? null : new ThisExpr(this.enclosingClass.getThisType(), VariableThis.DEFAULT);
    if (getter != null) {
        getter.setType(this.type);
        if (getter.getValue() == null) {
            // get: this.FIELD_NAME
            getter.setValue(new FieldAccess(getter.getPosition(), receiver, this));
        }
    }
    if (setter != null) {
        final IParameter setterParameter = setter.getParameters().get(0);
        setterParameter.setType(this.type);
        if (setter.getValue() == null) {
            final SourcePosition setterPosition = setter.getPosition();
            if (this.hasModifier(Modifiers.FINAL)) {
                markers.add(Markers.semanticError(setterPosition, "field.property.setter.final", this.name));
                // avoid abstract method error
                setter.setValue(new VoidValue(setterPosition));
            } else {
                // set: this.FIELD_NAME = newValue
                setter.setValue(new FieldAssignment(setterPosition, receiver, this, new FieldAccess(setterPosition, null, setterParameter)));
            }
        }
    }
    this.property.resolve(markers, context);
}
Also used : VoidValue(dyvilx.tools.compiler.ast.expression.constant.VoidValue) IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IContext(dyvilx.tools.compiler.ast.context.IContext) TypeChecker(dyvilx.tools.compiler.transform.TypeChecker) CombiningContext(dyvilx.tools.compiler.ast.context.CombiningContext) FieldAssignment(dyvilx.tools.compiler.ast.expression.access.FieldAssignment) IValue(dyvilx.tools.compiler.ast.expression.IValue) SourcePosition(dyvil.source.position.SourcePosition) IMethod(dyvilx.tools.compiler.ast.method.IMethod) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) ThisExpr(dyvilx.tools.compiler.ast.expression.ThisExpr)

Example 4 with IValue

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

the class Property method formatBody.

public static void formatBody(IProperty property, String prefix, StringBuilder buffer) {
    // Block Start
    if (Formatting.getBoolean("property.block.newline")) {
        buffer.append('\n').append(prefix);
    } else {
        buffer.append(' ');
    }
    buffer.append('{');
    // Initializer
    final IValue initializer = property.getInitializer();
    final IMethod getter = property.getGetter();
    final IMethod setter = property.getSetter();
    if (initializer != null) {
        formatInitializer(initializer, prefix, buffer);
        if (getter != null || setter != null) {
            buffer.append('\n').append(prefix);
        }
    }
    // Getter
    if (getter != null) {
        formatGetter(getter, prefix, buffer);
        if (setter != null) {
            buffer.append('\n').append(prefix);
        }
    }
    // Setter
    if (setter != null) {
        formatSetter(setter, prefix, buffer);
    }
    // Block End
    buffer.append('\n').append(prefix).append('}');
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) IMethod(dyvilx.tools.compiler.ast.method.IMethod)

Example 5 with IValue

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

the class Property method formatSetter.

private static void formatSetter(IMethod setter, String prefix, StringBuilder buffer) {
    final String indent = Formatting.getIndent("property.setter.indent", prefix);
    final IValue value = setter.getValue();
    final Name setterParameterName = setter.getParameters().get(0).getName();
    buffer.append('\n').append(indent);
    setter.getAttributes().toInlineString(indent, buffer);
    buffer.append("set");
    if (setterParameterName != Names.newValue) {
        Formatting.appendSeparator(buffer, "property.setter.parameter.open_paren", '(');
        buffer.append(setterParameterName);
        Formatting.appendSeparator(buffer, "property.setter.parameter.close_paren", ')');
    }
    if (value != null) {
        if (Util.formatStatementList(indent, buffer, value)) {
            return;
        }
        // Separator
        if (Formatting.getBoolean("property.setter.separator.space_before")) {
            buffer.append(' ');
        }
        buffer.append(':');
        if (Formatting.getBoolean("property.setter.separator.newline_after")) {
            buffer.append('\n').append(indent);
        } else if (Formatting.getBoolean("property.setter.separator.space_after")) {
            buffer.append(' ');
        }
        value.toString(indent, buffer);
    }
    if (Formatting.getBoolean("property.setter.semicolon")) {
        buffer.append(';');
    }
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) Name(dyvil.lang.Name)

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