Search in sources :

Example 16 with SourcePosition

use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.

the class IDataMember method checkAssign.

default IValue checkAssign(MarkerList markers, IContext context, SourcePosition position, IValue receiver, IValue newValue) {
    if (this.hasModifier(Modifiers.FINAL) && !context.isConstructor()) {
        markers.add(Markers.semanticError(position, this.getKind().getName() + ".assign.final", this.getName()));
    }
    final IType type = this.getType();
    final ITypeContext typeContext = receiver == null ? ITypeContext.NULL : receiver.getType();
    final TypeChecker.MarkerSupplier markerSupplier = (errorPosition, expected, actual) -> {
        final String kindName = this.getKind().getName();
        final Marker marker = Markers.semanticError(errorPosition, kindName + ".assign.type", this.getName());
        marker.addInfo(Markers.getSemantic(kindName + ".type", expected));
        marker.addInfo(Markers.getSemantic("value.type", actual));
        return marker;
    };
    return TypeChecker.convertValue(newValue, type, typeContext, markers, context, markerSupplier);
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) ITypeContext(dyvilx.tools.compiler.ast.generic.ITypeContext) IValueConsumer(dyvilx.tools.compiler.ast.consumer.IValueConsumer) IContext(dyvilx.tools.compiler.ast.context.IContext) TypeChecker(dyvilx.tools.compiler.transform.TypeChecker) IType(dyvilx.tools.compiler.ast.type.IType) MemberKind(dyvilx.tools.compiler.ast.member.MemberKind) WriteableExpression(dyvilx.tools.compiler.ast.expression.WriteableExpression) IMember(dyvilx.tools.compiler.ast.member.IMember) Types(dyvilx.tools.compiler.ast.type.builtin.Types) Formatting(dyvilx.tools.compiler.config.Formatting) Marker(dyvilx.tools.parsing.marker.Marker) SourcePosition(dyvil.source.position.SourcePosition) Markers(dyvilx.tools.compiler.util.Markers) BytecodeException(dyvilx.tools.compiler.backend.exception.BytecodeException) MarkerList(dyvilx.tools.parsing.marker.MarkerList) NonNull(dyvil.annotation.internal.NonNull) Modifiers(dyvil.reflect.Modifiers) MethodWriter(dyvilx.tools.compiler.backend.MethodWriter) IClass(dyvilx.tools.compiler.ast.classes.IClass) TypeChecker(dyvilx.tools.compiler.transform.TypeChecker) Marker(dyvilx.tools.parsing.marker.Marker) ITypeContext(dyvilx.tools.compiler.ast.generic.ITypeContext) IType(dyvilx.tools.compiler.ast.type.IType)

Example 17 with SourcePosition

use of dyvil.source.position.SourcePosition 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 18 with SourcePosition

use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.

the class LambdaExpr method inferTypes.

private void inferTypes(MarkerList markers) {
    if (!this.method.hasTypeVariables()) {
        for (int i = 0, count = this.parameters.size(); i < count; i++) {
            final IParameter parameter = this.parameters.get(i);
            if (parameter.getType().isUninferred()) {
                final IType type = this.method.getParameters().get(i).getType().atPosition(parameter.getPosition());
                parameter.setType(type);
            }
        }
        this.checkReturnType(markers, this.method.getType());
        return;
    }
    for (int i = 0, count = this.parameters.size(); i < count; i++) {
        final IParameter parameter = this.parameters.get(i);
        if (!parameter.getType().isUninferred()) {
            continue;
        }
        final SourcePosition position = parameter.getPosition();
        final IType methodParamType = this.method.getParameters().get(i).getType();
        final IType concreteType = methodParamType.getConcreteType(this.type);
        // Can't infer parameter type
        if ((this.flags & IMPLICIT_PARAMETERS) == 0 && concreteType.isUninferred()) {
            // markers.add(Markers.semanticError(position, "lambda.parameter.implicit"));
            markers.add(Markers.semanticError(position, "lambda.parameter.type", parameter.getName()));
            continue;
        }
        // asReturnType is required for Wildcard Types
        parameter.setType(concreteType.atPosition(position));
    }
    this.checkReturnType(markers, this.method.getType().getConcreteType(this.type));
}
Also used : SourcePosition(dyvil.source.position.SourcePosition) IType(dyvilx.tools.compiler.ast.type.IType)

Example 19 with SourcePosition

use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.

the class Marker method log.

public void log(Source source, String indent, StringBuilder buffer, boolean colors) {
    final String type = BaseMarkers.INSTANCE.getString("marker_level." + this.getLevel().name().toLowerCase());
    final SourcePosition position = this.position;
    final int endLine = position.endLine();
    final String colorString = colors ? this.getColor() : "";
    buffer.append(indent);
    this.appendLine(buffer, source.line(endLine), position.startColumn(), position.endColumn(), colors, colorString);
    buffer.append(indent).append(colorString).append(type);
    if (this.message != null) {
        buffer.append(": ").append(this.message);
    }
    if (colors) {
        buffer.append(Console.ANSI_RESET);
    }
    buffer.append('\n');
    // Append Info (if any)
    if (this.info != null) {
        for (String s : this.info) {
            buffer.append(indent).append('\t').append(s).append('\n');
        }
    }
    buffer.append(indent).append('\n');
}
Also used : SourcePosition(dyvil.source.position.SourcePosition)

Example 20 with SourcePosition

use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.

the class MarkerList method log.

public void log(Source source, StringBuilder buffer, boolean colors) {
    this.sort();
    BitSet lines = new BitSet(source.lineCount());
    int lastLine = 0;
    for (int i = this.markerCount - 1; i >= 0; i--) {
        final Marker marker = this.markers[i];
        final SourcePosition position = marker.getPosition();
        final int endLine = position.endLine();
        for (int l = position.startLine(); l <= endLine; l++) {
            lines.add(l);
        }
        if (endLine > lastLine) {
            lastLine = endLine;
        }
    }
    int nBits = (int) Math.log10(lastLine) + 1;
    final String formatString = "%" + nBits + "d | %s\n";
    final StringBuilder indentBuffer = new StringBuilder();
    for (int i = 0; i < nBits; i++) {
        indentBuffer.append('.');
    }
    indentBuffer.append(' ').append('|').append(' ');
    final String indent = indentBuffer.toString();
    int markerIndex = 0;
    for (int line : lines) {
        buffer.append(String.format(formatString, line, source.line(line)));
        while (markerIndex < this.markerCount && line == this.markers[markerIndex].getPosition().endLine()) {
            this.markers[markerIndex].log(source, indent, buffer, colors);
            markerIndex++;
        }
    }
}
Also used : SourcePosition(dyvil.source.position.SourcePosition) BitSet(dyvil.collection.mutable.BitSet)

Aggregations

SourcePosition (dyvil.source.position.SourcePosition)20 IValue (dyvilx.tools.compiler.ast.expression.IValue)10 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)8 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)8 IType (dyvilx.tools.compiler.ast.type.IType)8 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)7 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)6 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)6 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)5 IContext (dyvilx.tools.compiler.ast.context.IContext)2 ThisExpr (dyvilx.tools.compiler.ast.expression.ThisExpr)2 FieldAssignment (dyvilx.tools.compiler.ast.expression.access.FieldAssignment)2 Variable (dyvilx.tools.compiler.ast.field.Variable)2 IMethod (dyvilx.tools.compiler.ast.method.IMethod)2 BindingIfStatement (dyvilx.tools.compiler.ast.statement.BindingIfStatement)2 ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)2 TypeChecker (dyvilx.tools.compiler.transform.TypeChecker)2 NonNull (dyvil.annotation.internal.NonNull)1 BitSet (dyvil.collection.mutable.BitSet)1 Modifiers (dyvil.reflect.Modifiers)1