Skip to content

Commit

Permalink
[function] Fix function call with multiple versions
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Jun 14, 2023
1 parent 3ea4740 commit c49585d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/main/java/leekscript/common/ClassValueType.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public Type getArgument(int a) {
return Type.compound(types);
}

@Override
public Type getArgument(int argumentCount, int a) {
if (this.clazz == null) return Type.ANY;

var constructor = this.clazz.getConstructor(argumentCount);
if (constructor == null) return Type.ANY;

return constructor.block.getType().getArgument(a);
}

@Override
public String getCode() {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/leekscript/common/CompoundType.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public CastType accepts(Type type) {
for (var t : types) {
var r = t.accepts(type);
if (r.ordinal() < best.ordinal()) best = r;
if (best != CastType.INCOMPATIBLE) ok = true;
if (best != CastType.EQUALS) nok = true;
if (r != CastType.INCOMPATIBLE) ok = true;
if (r != CastType.EQUALS) nok = true;
}
if (best == CastType.INCOMPATIBLE && ok) return CastType.UNSAFE_DOWNCAST;
if (best == CastType.EQUALS && nok) return CastType.UPCAST;
Expand Down Expand Up @@ -152,6 +152,11 @@ public Type getArgument(int a) {
return Type.compound(this.types.stream().map(t -> t.getArgument(a)).collect(Collectors.toCollection(HashSet::new)));
}

@Override
public Type getArgument(int argumentCount, int a) {
return Type.compound(this.types.stream().map(t -> t.getArgument(argumentCount, a)).collect(Collectors.toCollection(HashSet::new)));
}

@Override
public Type returnType() {
return Type.compound(this.types.stream().map(t -> t.returnType()).collect(Collectors.toCollection(HashSet::new)));
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/leekscript/common/FunctionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public Type getArgument(int a) {
return a < arguments.size() ? arguments.get(a) : Type.VOID;
}

@Override
public Type getArgument(int argumentCount, int a) {
return a < arguments.size() ? arguments.get(a) : Type.VOID;
}

@Override
public int getMinArguments() {
return minArguments;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/leekscript/common/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ public Type getArgument(int a) {
return Type.NULL;
}

public Type getArgument(int argumentCount, int a) {
if (this == ANY) {
return Type.ANY;
}
return Type.NULL;
}

public Type returnType() {
if (this == ANY) {
return Type.ANY;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/leekscript/compiler/JavaWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ public void compileClone(MainLeekBlock mainblock, Expression expr) {
}

public void compileConvert(MainLeekBlock mainblock, int index, Expression value, Type type) {
// var v_type = value.getType();
// System.out.println("convert " + v_type + " to " + type);

// System.out.println("convert " + value.getType() + " to " + type);
if (type == Type.REAL && value.getType().isIntOrReal()) {
addCode("(");
value.writeJavaCode(mainblock, this);
Expand All @@ -198,7 +198,9 @@ public void compileConvert(MainLeekBlock mainblock, int index, Expression value,
addCode(").longValue()");
return;
}
if (type.accepts(value.getType()).ordinal() <= CastType.EQUALS.ordinal()) {
var cast = type.accepts(value.getType());
// System.out.println("cast = " + cast);
if (cast.ordinal() <= CastType.EQUALS.ordinal()) {
value.writeJavaCode(mainblock, this);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void compileL(MainLeekBlock mainblock, JavaWriter writer) {
if (system_function != null) {
parameter.writeJavaCode(mainblock, writer);
} else {
writer.compileConvert(mainblock, i, parameter, functionType.getArgument(i));
writer.compileConvert(mainblock, i, parameter, functionType.getArgument(mParameters.size(), i));
}
} else {
if (user_function != null) {
Expand All @@ -303,7 +303,7 @@ public void compileL(MainLeekBlock mainblock, JavaWriter writer) {
if (unsafe) {
writer.compileLoad(mainblock, parameter);
} else {
writer.compileConvert(mainblock, i, parameter, functionType.getArgument(i));
writer.compileConvert(mainblock, i, parameter, functionType.getArgument(mParameters.size(), i));
}
} else {
parameter.compileL(mainblock, writer);
Expand Down Expand Up @@ -370,6 +370,7 @@ else if (!mExpression.getType().canBeCallable()) {
}

this.functionType = mExpression.getType();
// System.out.println("[FC] function type = " + functionType + " args = " + functionType.getArguments();
this.type = functionType.returnType();

for (Expression parameter : mParameters) {
Expand Down

0 comments on commit c49585d

Please sign in to comment.