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