ContextAnalysis: Make naught comparable to struct again.

This commit is contained in:
2023-03-21 22:44:21 +01:00
parent 8b17ced533
commit cce58b6e38
2 changed files with 73 additions and 1 deletions

View File

@@ -21,7 +21,7 @@ public class NamedType extends Type {
@Override
public Type combine(Type that) {
if(this.equals(that)) {
if(this.equals(that) || (that instanceof NullType)) {
return this;
}

View File

@@ -0,0 +1,72 @@
import de.hsrm.compiler.Klang.ContextAnalysis;
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class NaughtTest {
@Test
void shouldBeComparableToStruct() {
ParseTree tree = Helper.prepareParser("""
struct bar { a: int; }
function foo(): int {
let a: bar = create bar(1);
if (a == naught) {
return -1;
}
return 1;
}
foo();
""");
var ctxAnal = new ContextAnalysis(
Helper.getFuncs(tree),
Helper.getStructs(tree),
Helper.getEnums(tree)
);
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldBeAssignableToStruct() {
ParseTree tree = Helper.prepareParser("""
struct bar { a: int; }
function foo(): int {
let a: bar = naught;
return 1;
}
foo();
""");
var ctxAnal = new ContextAnalysis(
Helper.getFuncs(tree),
Helper.getStructs(tree),
Helper.getEnums(tree)
);
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldBeReturnable() {
ParseTree tree = Helper.prepareParser("""
struct bar { a: int; }
function foo(): bar {
return naught;
}
foo();
""");
var ctxAnal = new ContextAnalysis(
Helper.getFuncs(tree),
Helper.getStructs(tree),
Helper.getEnums(tree)
);
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
}