Skip to content

Commit

Permalink
fixed errors detected by new type-checker
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Jun 4, 2024
1 parent 9aa3d6a commit 1dc3e9a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 50 deletions.
File renamed without changes.
48 changes: 24 additions & 24 deletions src/lang/flybytes/api/JavaLang.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
12 changes: 8 additions & 4 deletions src/lang/flybytes/demo/pico/Compiler.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ list[Stat] stats({Statement ";"}* stats) = [stat(s)[src=s@\loc] | s <- stats];

Stat stat(s:(Statement) `<Id var> := <Expression val>`)
= store("<var>", expr(val));



Stat stat(s:(Statement)
`if <Expression cond> 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 <Expression cond> do
' <{Statement ";"}* body>
Expand Down
10 changes: 5 additions & 5 deletions src/lang/flybytes/demo/protol/Compiler.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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) `<Id id> = <Expr v>;`)
= store("<id>", compile(v));
Expand All @@ -85,10 +85,10 @@ Stat compile((Command) `<Expr array>[<Expr index>] = <Expr v>;`)
= astore(compile(array), getInt(compile(index)), compile(v));

Stat compile((Command) `if(<Expr cond >) { <Command* thenPart> } else { <Command* elsePart> }`)
= \if(compile(cond), compile(thenPart), compile(elsePart));
= \if(compile(cond), compileAll(thenPart), compileAll(elsePart));

Stat compile((Command) `while(<Expr cond>) { <Command* body> }`)
= \while(compile(cond), compile(body));
= \while(compile(cond), compileAll(body));

Stat compile((Command) `<Expr e>;`) = \do(compile(e));

Expand Down Expand Up @@ -191,7 +191,7 @@ list[Field] fields(Definition* defs)
= [ field("<name>", val)[src=name@\loc] | (Definition) `<Id name> = <Expr val>` <- defs];

Method method(str name, {Id ","}* args, Command* commands)
= method(\public(), Prototype, name, [var(Prototype, "<a>") | a <- args], compile(commands));
= method(\public(), Prototype, name, [var(Prototype, "<a>") | a <- args], compileAll(commands));

Field field(str name, Expr val)
= field(Prototype, name, init=compile(val), modifiers={\public()});
Expand Down
2 changes: 0 additions & 2 deletions src/lang/flybytes/internal/ClassCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions src/lang/flybytes/tests/ComparisonTests.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()];
Expand Down
8 changes: 4 additions & 4 deletions src/lang/flybytes/tests/MirrorTests.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1dc3e9a

Please sign in to comment.