Skip to content

Commit

Permalink
[types] Fix equals() + lambda type + static field call
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Jul 15, 2023
1 parent b0bf7a9 commit af31aba
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/main/java/leekscript/common/ArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,17 @@ public boolean isIndexable() {
public boolean canBeIndexable() {
return true;
}

@Override
public int hashCode() {
return this.type.hashCode() * 31 + 1;
}

@Override
public boolean equals(Object object) {
if (object instanceof ArrayType at) {
return this.type.equals(at.type);
}
return false;
}
}
18 changes: 18 additions & 0 deletions src/main/java/leekscript/common/CompoundType.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,22 @@ public Complete complete() {
}
return complete;
}


@Override
public int hashCode() {
int hashCode = 7;
for (var t : types) {
hashCode = hashCode * 31 + t.hashCode();
}
return hashCode;
}

@Override
public boolean equals(Object object) {
if (object instanceof CompoundType mt) {
return this.types.equals(mt.types);
}
return false;
}
}
13 changes: 13 additions & 0 deletions src/main/java/leekscript/common/MapType.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,17 @@ public boolean isIndexable() {
public boolean canBeIndexable() {
return true;
}

@Override
public int hashCode() {
return (this.key.hashCode() * 31 + this.value.hashCode()) * 31 + 2;
}

@Override
public boolean equals(Object object) {
if (object instanceof MapType mt) {
return this.key.equals(mt.key) && this.value.equals(mt.value);
}
return false;
}
}
9 changes: 9 additions & 0 deletions src/main/java/leekscript/common/TemplateType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package leekscript.common;

public class TemplateType extends Type {

public TemplateType(String name) {
super(name, "t", "", "", "");
}

}
5 changes: 5 additions & 0 deletions src/main/java/leekscript/common/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,9 @@ public boolean isIntOrReal() {
public boolean isPrimitive() {
return this == Type.INT || this == Type.BOOL || this == Type.REAL;
}

@Override
public int hashCode() {
return this.signature.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public void compileL(MainLeekBlock mainblock, JavaWriter writer) {
} else {
// Champ statique
// writer.addCode("execute(" + v.getClassDeclaration().getName() + "." + field);
if (type != Type.ANY) {
writer.addCode("(" + type.getJavaPrimitiveName(mainblock.getVersion()) + ") ");
}
writer.addCode("u_" + v.getClassDeclaration().getName() + ".callStaticField(\"" + field + "\", " + mainblock.getWordCompiler().getCurrentClassVariable());
}
} else if (object instanceof LeekVariable v && v.getVariableType() == VariableType.THIS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ public void preAnalyze(WordCompiler compiler) throws LeekCompilerException {
// System.out.println("VD preAnalyze " + token.getWord());
this.function = compiler.getCurrentFunction();
if (mValue != null && mValue.getType() instanceof FunctionType) {
var vt = compiler.getMainBlock().isStrict() ? mValue.getType() : Type.ANY;
registerVariable(compiler, vt);
registerVariable(compiler, this.type);
mValue.preAnalyze(compiler);
} else {
if (mValue != null) mValue.preAnalyze(compiler);
Expand Down
1 change: 1 addition & 0 deletions src/test/java/test/TestFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@ public void run() {
code_v2_("function Functor() => Function < => string> { return function() => string { return 'yea' } } class Temp { Function < => Function> functor; Function result;constructor() { this.functor = Functor; this.result = this.functor(); } } var t = new Temp() return t.result()").equals("\"yea\"");
DISABLED_code_v1("Function< => integer> f function test(Function< => any> _) {} test(f)").equals("null");
code_v2_("Function< => integer> f function test(Function< => any> _) {} test(f)").equals("null");
code("Function<integer => boolean> t = function(integer b) => boolean { return true }").equals("null");
}
}
4 changes: 4 additions & 0 deletions src/test/java/test/TestObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ public void run() {
code_v2_("class A { static a = -> 12 } return A.a()").equals("12");
code_v2_("class A { private static a = -> 12 } return A.a()").error(Error.PRIVATE_STATIC_FIELD);
code_v2_("class A { static a = -> 12 } return A.b()").error(Error.CLASS_STATIC_MEMBER_DOES_NOT_EXIST);
code_v2_("class A { public static Function< => integer> f = function () => integer { return 1 } } return A.f()").equals("1");
code_v2_("class A { public static Function< => integer> f = function () => integer { return 1 } } function g() => integer { return A.f() } return g()").equals("1");
code_v3_("class A { public static Function< => Array<integer>> | Function<integer => Array<integer>?> f = function (integer a) => Array<integer> { return new Array() as Array<integer> } } function g() => Array<integer>? { return A.f(1) } return g()").equals("[]");
code_v3_("class A { public static Function< => Array<Array<integer | boolean>>> | Function<integer? => Array<Array<integer | boolean>>?> f = function (integer? a) => Array<Array<integer | boolean>>? { return new Array() as Array<Array<integer | boolean>>? } } function g() => Array<Array<integer|boolean>>? { return A.f(1) } return g()").equals("[]");

section("Final fields");
code_strict_v2_("class A { final a = 12 } var a = new A() a.a = 15 return a.a").error(Error.CANNOT_ASSIGN_FINAL_FIELD);
Expand Down

0 comments on commit af31aba

Please sign in to comment.