Search in sources :

Example 1 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType 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 ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class ParameterListParser method setTypeVarargs.

protected void setTypeVarargs() {
    // Ellipsis after the type automatically converts it to an array type (see #333)
    this.type = new ArrayType(this.type);
    this.flags |= VARARGS;
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType)

Example 3 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class Template method makeMainMethod.

private void makeMainMethod() {
    // func main(args: [String]) -> void = new TemplateName().mainImpl(args)
    final ParameterList params = this.mainMethod.getParameters();
    final CodeParameter argsParam = new CodeParameter(this.mainMethod, null, Name.fromRaw("args"), new ArrayType(Types.STRING));
    params.add(argsParam);
    final IValue newTemplate = new ConstructorCall(null, this.templateClass.getClassType(), ArgumentList.EMPTY);
    this.mainMethod.setValue(new MethodCall(null, newTemplate, Name.fromRaw("mainImpl"), new ArgumentList(new FieldAccess(argsParam))));
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IValue(dyvilx.tools.compiler.ast.expression.IValue) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) ConstructorCall(dyvilx.tools.compiler.ast.expression.access.ConstructorCall) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall)

Example 4 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class CaseClasses method writeToString.

public static void writeToString(MethodWriter writer, IValue value) throws BytecodeException {
    final IType type = value.getType();
    // If someValue is a String, write it as is
    if (Types.isSuperType(Types.STRING, type)) {
        value.writeExpression(writer, Types.STRING);
        return;
    }
    // Otherwise, generate an appropriate call to String.valueOf or ObjectArray.toString
    value.writeExpression(writer, type);
    switch(type.getTypecode()) {
        case PrimitiveType.BOOLEAN_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "toString", "(Z)Ljava/lang/String;", false);
            return;
        case PrimitiveType.BYTE_CODE:
        case PrimitiveType.SHORT_CODE:
        case PrimitiveType.CHAR_CODE:
        case PrimitiveType.INT_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;", false);
            return;
        case PrimitiveType.LONG_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "toString", "(J)Ljava/lang/String;", false);
            return;
        case PrimitiveType.FLOAT_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "toString", "(F)Ljava/lang/String;", false);
            return;
        case PrimitiveType.DOUBLE_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "toString", "(D)Ljava/lang/String;", false);
            return;
    }
    final ArrayType arrayType = type.extract(ArrayType.class);
    if (arrayType != null) {
        writeArrayToString(writer, arrayType);
        return;
    }
    writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;", false);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IType(dyvilx.tools.compiler.ast.type.IType)

Example 5 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class ArrayExpr method getTypeMatch.

@Override
public int getTypeMatch(IType type, IImplicitContext implicitContext) {
    final ArrayType arrayType = type.extract(ArrayType.class);
    if (arrayType == null) {
        if (type.getTheClass() == Types.OBJECT_CLASS) {
            return SUBTYPE_MATCH;
        }
        if (type.getAnnotation(LazyFields.ARRAY_CONVERTIBLE) != null) {
            return CONVERSION_MATCH;
        }
        return MISMATCH;
    }
    final IType elementType = arrayType.getElementType();
    return this.values.getTypeMatch(elementType, implicitContext);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)15 IType (dyvilx.tools.compiler.ast.type.IType)6 SourcePosition (dyvil.source.position.SourcePosition)2 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)2 IValue (dyvilx.tools.compiler.ast.expression.IValue)2 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)2 Label (dyvilx.tools.asm.Label)1 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)1 IClass (dyvilx.tools.compiler.ast.classes.IClass)1 IConstructor (dyvilx.tools.compiler.ast.constructor.IConstructor)1 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)1 Field (dyvilx.tools.compiler.ast.field.Field)1 IField (dyvilx.tools.compiler.ast.field.IField)1 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)1 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)1 Mutability (dyvilx.tools.compiler.ast.type.Mutability)1 Marker (dyvilx.tools.parsing.marker.Marker)1