From 1dc3e9ae80abeda3d9e938062a61ddabda0bea54 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Tue, 4 Jun 2024 19:18:53 +0200 Subject: [PATCH] fixed errors detected by new type-checker --- LICENSE => LICENSE.md | 0 src/lang/flybytes/api/JavaLang.rsc | 48 +++++++++---------- src/lang/flybytes/demo/pico/Compiler.rsc | 12 +++-- src/lang/flybytes/demo/protol/Compiler.rsc | 10 ++-- src/lang/flybytes/internal/ClassCompiler.java | 2 - src/lang/flybytes/tests/ComparisonTests.rsc | 22 ++++----- src/lang/flybytes/tests/MirrorTests.rsc | 8 ++-- 7 files changed, 52 insertions(+), 50 deletions(-) rename LICENSE => LICENSE.md (100%) diff --git a/LICENSE b/LICENSE.md similarity index 100% rename from LICENSE rename to LICENSE.md diff --git a/src/lang/flybytes/api/JavaLang.rsc b/src/lang/flybytes/api/JavaLang.rsc index 055ac47..b7484ab 100644 --- a/src/lang/flybytes/api/JavaLang.rsc +++ b/src/lang/flybytes/api/JavaLang.rsc @@ -28,31 +28,31 @@ Mirror LongMirror() = classMirror("java.lang.Long"); Mirror ShortMirror() = classMirror("java.lang.Short"); Mirror FloatMirror() = classMirror("java.lang.Float"); -@doc{the maximal value for an arithmetic type on the JVM} -real maxValue(float()) = FloatMirror().getStatic("MAX_VALUE").toValue(#real); -real maxValue(double()) = DoubleMirror().getStatic("MAX_VALUE").toValue(#real); -int maxValue(short()) = ShortMirror().getStatic("MAX_VALUE").toValue(#int); -int maxValue(character()) = CharacterMirror().getStatic("MAX_VALUE").toValue(#int); -int maxValue(integer()) = IntegerMirror().getStatic("MAX_VALUE").toValue(#int); -int maxValue(long()) = LongMirror().getStatic("MAX_VALUE").toValue(#int); -int maxValue(byte()) = ByteMirror().getStatic("MAX_VALUE").toValue(#int); - -@doc{the minimal value for an arithmetic type on the JVM} -int minValue(byte()) = ByteMirror().getStatic("MIN_VALUE").toValue(#int); -int minValue(long()) = LongMirror().getStatic("MIN_VALUE").toValue(#int); -int minValue(integer()) = IntegerMirror().getStatic("MIN_VALUE").toValue(#int); -real minValue(float()) = -1 * maxValue(float()); -real minValue(double()) = -1 * maxValue(double()); -int minValue(short()) = ShortMirror().getStatic("MIN_VALUE").toValue(#int); -int minValue(character()) = CharacterMirror().getStatic("MIN_VALUE").toValue(#int); +@synopsis{the maximal value for an arithmetic type on the JVM} +real maxRealValue(float()) = FloatMirror().getStatic("MAX_VALUE").toValue(#real); +real maxRealValue(double()) = DoubleMirror().getStatic("MAX_VALUE").toValue(#real); +int maxIntValue(short()) = ShortMirror().getStatic("MAX_VALUE").toValue(#int); +int maxIntValue(character()) = CharacterMirror().getStatic("MAX_VALUE").toValue(#int); +int maxIntValue(integer()) = IntegerMirror().getStatic("MAX_VALUE").toValue(#int); +int maxIntValue(long()) = LongMirror().getStatic("MAX_VALUE").toValue(#int); +int maxIntValue(byte()) = ByteMirror().getStatic("MAX_VALUE").toValue(#int); + +@synopsis{the minimal value for an arithmetic type on the JVM} +real minRealValue(float()) = -1 * maxValue(float()); +real minRealValue(double()) = -1 * maxValue(double()); +int minIntValue(byte()) = ByteMirror().getStatic("MIN_VALUE").toValue(#int); +int minIntValue(long()) = LongMirror().getStatic("MIN_VALUE").toValue(#int); +int minIntValue(integer()) = IntegerMirror().getStatic("MIN_VALUE").toValue(#int); +int minIntValue(short()) = ShortMirror().getStatic("MIN_VALUE").toValue(#int); +int minIntValue(character()) = CharacterMirror().getStatic("MIN_VALUE").toValue(#int); @doc{the minimal increment for an arithmetic type on the JVM} -real epsilon(float()) = FloatMirror().getStatic("MIN_VALUE").toValue(#real); // misnomer in the Java library -real epsilon(double()) = DoubleMirror().getStatic("MIN_VALUE").toValue(#real); // misnomer in the Java library -int epsilon(short()) = 1; -int epsilon(character()) = 1; -int epsilon(integer()) = 1; -int epsilon(long()) = 1; -int epsilon(byte()) = 1; +num epsilon(float()) = FloatMirror().getStatic("MIN_VALUE").toValue(#real); // misnomer in the Java library +num epsilon(double()) = DoubleMirror().getStatic("MIN_VALUE").toValue(#real); // misnomer in the Java library +num epsilon(short()) = 1; +num epsilon(character()) = 1; +num epsilon(integer()) = 1; +num epsilon(long()) = 1; +num epsilon(byte()) = 1; Exp Integer_parseInt(Exp e, int radix) = invokeStatic(Integer(), methodDesc(integer(), "parseInt", [string(), integer()]), [e, iconst(radix)]); diff --git a/src/lang/flybytes/demo/pico/Compiler.rsc b/src/lang/flybytes/demo/pico/Compiler.rsc index cbb2ad5..3b7d83e 100644 --- a/src/lang/flybytes/demo/pico/Compiler.rsc +++ b/src/lang/flybytes/demo/pico/Compiler.rsc @@ -60,15 +60,19 @@ list[Stat] stats({Statement ";"}* stats) = [stat(s)[src=s@\loc] | s <- stats]; Stat stat(s:(Statement) ` := `) = store("", expr(val)); - + + Stat stat(s:(Statement) `if then ' <{Statement ";"}* thenPart> 'else ' <{Statement ";"}* elsePart> - 'fi`) - = \if(ne(expr(cond), iconst(0)), stats(thenPart), stats(elsePart)); - + 'fi`) + = \if(ne(expr(cond), iconst(0)), stats(thenPart), stats(elsePart)) when /Id x := thenPart + + + + Stat stat(s:(Statement) `while do ' <{Statement ";"}* body> diff --git a/src/lang/flybytes/demo/protol/Compiler.rsc b/src/lang/flybytes/demo/protol/Compiler.rsc index e603109..0ff1460 100644 --- a/src/lang/flybytes/demo/protol/Compiler.rsc +++ b/src/lang/flybytes/demo/protol/Compiler.rsc @@ -62,7 +62,7 @@ data Type = prototype(str name, list[Method] methods, list[Field] fields); list[Class] compile(Program p, str name) { progClass = class(object(name), methods=[ - main("args", [*compile(p.commands), \return()])[src=p@\loc] + main("args", [*compileAll(p.commands), \return()])[src=p@\loc] ] )[src=p@\loc]; @@ -71,7 +71,7 @@ list[Class] compile(Program p, str name) { return declareVariables(allClasses); } -list[Stat] compile(Command* commands) = [compile(c)[src=c@\loc] | c <- commands]; +list[Stat] compileAll(Command* commands) = [compile(c)[src=c@\loc] | c <- commands]; Stat compile((Command) ` = ;`) = store("", compile(v)); @@ -85,10 +85,10 @@ Stat compile((Command) `[] = ;`) = astore(compile(array), getInt(compile(index)), compile(v)); Stat compile((Command) `if() { } else { }`) - = \if(compile(cond), compile(thenPart), compile(elsePart)); + = \if(compile(cond), compileAll(thenPart), compileAll(elsePart)); Stat compile((Command) `while() { }`) - = \while(compile(cond), compile(body)); + = \while(compile(cond), compileAll(body)); Stat compile((Command) `;`) = \do(compile(e)); @@ -191,7 +191,7 @@ list[Field] fields(Definition* defs) = [ field("", val)[src=name@\loc] | (Definition) ` = ` <- defs]; Method method(str name, {Id ","}* args, Command* commands) - = method(\public(), Prototype, name, [var(Prototype, "") | a <- args], compile(commands)); + = method(\public(), Prototype, name, [var(Prototype, "") | a <- args], compileAll(commands)); Field field(str name, Expr val) = field(Prototype, name, init=compile(val), modifiers={\public()}); diff --git a/src/lang/flybytes/internal/ClassCompiler.java b/src/lang/flybytes/internal/ClassCompiler.java index 042dbec..3041cb5 100644 --- a/src/lang/flybytes/internal/ClassCompiler.java +++ b/src/lang/flybytes/internal/ClassCompiler.java @@ -26,8 +26,6 @@ */ package lang.flybytes.internal; -import static org.junit.Assert.fail; - import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; diff --git a/src/lang/flybytes/tests/ComparisonTests.rsc b/src/lang/flybytes/tests/ComparisonTests.rsc index 2ca3d23..8482c72 100644 --- a/src/lang/flybytes/tests/ComparisonTests.rsc +++ b/src/lang/flybytes/tests/ComparisonTests.rsc @@ -32,17 +32,17 @@ list[Type] intTypes = [integer(), short(), byte(), long()]; test bool testEqTrue(int i) = all (t <- intTypes, - I := prim(t, abs(i) % maxValue(t)), + I := prim(t, abs(i) % maxIntValue(t)), testCmpOp(cmpOpClass(t, eq), t, I, I, true)); test bool testEqFalse(int i) = all (t <- intTypes, - I := abs(i) % maxValue(t), + I := abs(i) % maxIntValue(t), testCmpOp(cmpOpClass(t, eq), t, prim(t, I), prim(t, I - 1), false)); test bool testNEqTrue(int i) = all (t <- intTypes, - I := abs(i) % maxValue(t), + I := abs(i) % maxIntValue(t), testCmpOp(cmpOpClass(t, ne), t, prim(t, I), prim(t, I - 1), true)); test bool testNEqFalse(int i) @@ -52,26 +52,26 @@ test bool testNEqFalse(int i) test bool testLt(int i, int j) = all (t <- intTypes, - I := (i % maxValue(t)), - J := (j % maxValue(t)), + I := (i % maxIntValue(t)), + J := (j % maxIntValue(t)), testCmpOp(cmpOpClass(t, lt), t, prim(t, I), prim(t, J), I < J)); test bool testGt(int i, int j) = all (t <- intTypes, - I := (i % maxValue(t)), - J := (j % maxValue(t)), + I := (i % maxIntValue(t)), + J := (j % maxIntValue(t)), testCmpOp(cmpOpClass(t, gt), t, prim(t, I), prim(t, J), I > J)); test bool testGeInt(int i, int j) = all (t <- intTypes, - I := (i % maxValue(t)), - J := (j % maxValue(t)), + I := (i % maxIntValue(t)), + J := (j % maxIntValue(t)), testCmpOp(cmpOpClass(t, ge), t, prim(t, I), prim(t, J), I >= J)); test bool testLe(int i, int j) = all (t <- intTypes, - I := (i % maxValue(t)), - J := (j % maxValue(t)), + I := (i % maxIntValue(t)), + J := (j % maxIntValue(t)), testCmpOp(cmpOpClass(t, le), t, prim(t, I), prim(t, J), I <= J)); list[Type] floatTypes = [float(), double()]; diff --git a/src/lang/flybytes/tests/MirrorTests.rsc b/src/lang/flybytes/tests/MirrorTests.rsc index ed6f8eb..5411433 100644 --- a/src/lang/flybytes/tests/MirrorTests.rsc +++ b/src/lang/flybytes/tests/MirrorTests.rsc @@ -4,10 +4,10 @@ import lang::flybytes::Mirror; import lang::flybytes::api::JavaLang; import lang::flybytes::Syntax; -test bool intId(int v) = v % maxValue(integer()) == integer(integer(v % maxValue(integer()))); -test bool longId(int v) = v % maxValue(long()) == long(long(v % maxValue(long()))); -test bool byteId(int v) = v % maxValue(byte()) == byte(byte(v % maxValue(byte()))); -test bool shortId(int v) = v % maxValue(short()) == short(short(v % maxValue(short()))); +test bool intId(int v) = v % maxIntValue(integer()) == integer(integer(v % maxIntValue(integer()))); +test bool longId(int v) = v % maxIntValue(long()) == long(long(v % maxIntValue(long()))); +test bool byteId(int v) = v % maxIntValue(byte()) == byte(byte(v % maxIntValue(byte()))); +test bool shortId(int v) = v % maxIntValue(short()) == short(short(v % maxIntValue(short()))); test bool stringId(str x) = x == string(string(x)); // due to loss of precision we can not have a general isomorphism between reals and doubles