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