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