Imrpove code generator for number expressions.

This commit is contained in:
James Roseborough
2010-07-27 22:19:55 +00:00
parent 9a884549e2
commit bbbae65237
8 changed files with 128 additions and 30 deletions

View File

@@ -85,42 +85,64 @@ public class LuaDouble extends LuaNumber {
public LuaValue add( LuaValue rhs ) { return rhs.add(v); } public LuaValue add( LuaValue rhs ) { return rhs.add(v); }
public LuaValue add( double lhs ) { return LuaDouble.valueOf(lhs + v); } public LuaValue add( double lhs ) { return LuaDouble.valueOf(lhs + v); }
public LuaValue sub( LuaValue rhs ) { return rhs.subFrom(v); } public LuaValue sub( LuaValue rhs ) { return rhs.subFrom(v); }
public LuaValue sub( double rhs ) { return LuaDouble.valueOf(v - rhs); }
public LuaValue sub( int rhs ) { return LuaDouble.valueOf(v - rhs); }
public LuaValue subFrom( double lhs ) { return LuaDouble.valueOf(lhs - v); } public LuaValue subFrom( double lhs ) { return LuaDouble.valueOf(lhs - v); }
public LuaValue mul( LuaValue rhs ) { return rhs.mul(v); } public LuaValue mul( LuaValue rhs ) { return rhs.mul(v); }
public LuaValue mul( double lhs ) { return LuaDouble.valueOf(lhs * v); } public LuaValue mul( double lhs ) { return LuaDouble.valueOf(lhs * v); }
public LuaValue mul( int lhs ) { return LuaDouble.valueOf(lhs * v); } public LuaValue mul( int lhs ) { return LuaDouble.valueOf(lhs * v); }
public LuaValue pow( LuaValue rhs ) { return rhs.powWith(v); } public LuaValue pow( LuaValue rhs ) { return rhs.powWith(v); }
public LuaValue pow( double rhs ) { return MathLib.dpow(v,rhs); }
public LuaValue pow( int rhs ) { return MathLib.dpow(v,rhs); }
public LuaValue powWith( double lhs ) { return MathLib.dpow(lhs,v); } public LuaValue powWith( double lhs ) { return MathLib.dpow(lhs,v); }
public LuaValue powWith( int lhs ) { return MathLib.dpow(lhs,v); } public LuaValue powWith( int lhs ) { return MathLib.dpow(lhs,v); }
public LuaValue div( LuaValue rhs ) { return rhs.divInto(v); } public LuaValue div( LuaValue rhs ) { return rhs.divInto(v); }
public LuaValue div( double rhs ) { return LuaDouble.ddiv(v,rhs); }
public LuaValue div( int rhs ) { return LuaDouble.ddiv(v,rhs); }
public LuaValue divInto( double lhs ) { return LuaDouble.ddiv(lhs,v); } public LuaValue divInto( double lhs ) { return LuaDouble.ddiv(lhs,v); }
public LuaValue mod( LuaValue rhs ) { return rhs.modFrom(v); } public LuaValue mod( LuaValue rhs ) { return rhs.modFrom(v); }
public LuaValue mod( double rhs ) { return LuaDouble.dmod(v,rhs); }
public LuaValue mod( int rhs ) { return LuaDouble.dmod(v,rhs); }
public LuaValue modFrom( double lhs ) { return LuaDouble.dmod(lhs,v); } public LuaValue modFrom( double lhs ) { return LuaDouble.dmod(lhs,v); }
/** lua division is always double, specific values for singularities */ /** lua division is always double, specific values for singularities */
public static LuaValue ddiv(double lhs, double rhs) { public static LuaValue ddiv(double lhs, double rhs) {
return rhs!=0? valueOf( lhs / rhs ): lhs>0? POSINF: lhs==0? NAN: NEGINF; return rhs!=0? valueOf( lhs / rhs ): lhs>0? POSINF: lhs==0? NAN: NEGINF;
} }
public static double ddiv_d(double lhs, double rhs) {
return rhs!=0? lhs / rhs: lhs>0? Double.POSITIVE_INFINITY: lhs==0? Double.NaN: Double.NEGATIVE_INFINITY;
}
/** lua module is always wrt double. */ /** lua module is always wrt double. */
public static LuaValue dmod(double lhs, double rhs) { public static LuaValue dmod(double lhs, double rhs) {
return rhs!=0? valueOf( lhs-rhs*Math.floor(lhs/rhs) ): NAN; return rhs!=0? valueOf( lhs-rhs*Math.floor(lhs/rhs) ): NAN;
} }
public static double dmod_d(double lhs, double rhs) {
return rhs!=0? lhs-rhs*Math.floor(lhs/rhs): Double.NaN;
}
// relational operators // relational operators
public LuaValue lt( LuaValue rhs ) { return rhs.gt_b(v)? LuaValue.TRUE: FALSE; } public LuaValue lt( LuaValue rhs ) { return rhs.gt_b(v)? LuaValue.TRUE: FALSE; }
public LuaValue lt( double rhs ) { return v < rhs? TRUE: FALSE; }
public LuaValue lt( int rhs ) { return v < rhs? TRUE: FALSE; }
public boolean lt_b( LuaValue rhs ) { return rhs.gt_b(v); } public boolean lt_b( LuaValue rhs ) { return rhs.gt_b(v); }
public boolean lt_b( int rhs ) { return v < rhs; } public boolean lt_b( int rhs ) { return v < rhs; }
public boolean lt_b( double rhs ) { return v < rhs; } public boolean lt_b( double rhs ) { return v < rhs; }
public LuaValue lteq( LuaValue rhs ) { return rhs.gteq_b(v)? LuaValue.TRUE: FALSE; } public LuaValue lteq( LuaValue rhs ) { return rhs.gteq_b(v)? LuaValue.TRUE: FALSE; }
public LuaValue lteq( double rhs ) { return v <= rhs? TRUE: FALSE; }
public LuaValue lteq( int rhs ) { return v <= rhs? TRUE: FALSE; }
public boolean lteq_b( LuaValue rhs ) { return rhs.gteq_b(v); } public boolean lteq_b( LuaValue rhs ) { return rhs.gteq_b(v); }
public boolean lteq_b( int rhs ) { return v <= rhs; } public boolean lteq_b( int rhs ) { return v <= rhs; }
public boolean lteq_b( double rhs ) { return v <= rhs; } public boolean lteq_b( double rhs ) { return v <= rhs; }
public LuaValue gt( LuaValue rhs ) { return rhs.lt_b(v)? LuaValue.TRUE: FALSE; } public LuaValue gt( LuaValue rhs ) { return rhs.lt_b(v)? LuaValue.TRUE: FALSE; }
public LuaValue gt( double rhs ) { return v > rhs? TRUE: FALSE; }
public LuaValue gt( int rhs ) { return v > rhs? TRUE: FALSE; }
public boolean gt_b( LuaValue rhs ) { return rhs.lt_b(v); } public boolean gt_b( LuaValue rhs ) { return rhs.lt_b(v); }
public boolean gt_b( int rhs ) { return v > rhs; } public boolean gt_b( int rhs ) { return v > rhs; }
public boolean gt_b( double rhs ) { return v > rhs; } public boolean gt_b( double rhs ) { return v > rhs; }
public LuaValue gteq( LuaValue rhs ) { return rhs.lteq_b(v)? LuaValue.TRUE: FALSE; } public LuaValue gteq( LuaValue rhs ) { return rhs.lteq_b(v)? LuaValue.TRUE: FALSE; }
public LuaValue gteq( double rhs ) { return v >= rhs? TRUE: FALSE; }
public LuaValue gteq( int rhs ) { return v >= rhs? TRUE: FALSE; }
public boolean gteq_b( LuaValue rhs ) { return rhs.lteq_b(v); } public boolean gteq_b( LuaValue rhs ) { return rhs.lteq_b(v); }
public boolean gteq_b( int rhs ) { return v >= rhs; } public boolean gteq_b( int rhs ) { return v >= rhs; }
public boolean gteq_b( double rhs ) { return v >= rhs; } public boolean gteq_b( double rhs ) { return v >= rhs; }

View File

@@ -114,33 +114,49 @@ public class LuaInteger extends LuaNumber {
public LuaValue add( double lhs ) { return LuaDouble.valueOf(lhs + v); } public LuaValue add( double lhs ) { return LuaDouble.valueOf(lhs + v); }
public LuaValue add( int lhs ) { return LuaInteger.valueOf(lhs + (long)v); } public LuaValue add( int lhs ) { return LuaInteger.valueOf(lhs + (long)v); }
public LuaValue sub( LuaValue rhs ) { return rhs.subFrom(v); } public LuaValue sub( LuaValue rhs ) { return rhs.subFrom(v); }
public LuaValue sub( double rhs ) { return LuaDouble.valueOf(v - rhs); }
public LuaValue sub( int rhs ) { return LuaDouble.valueOf(v - rhs); }
public LuaValue subFrom( double lhs ) { return LuaDouble.valueOf(lhs - v); } public LuaValue subFrom( double lhs ) { return LuaDouble.valueOf(lhs - v); }
public LuaValue subFrom( int lhs ) { return LuaInteger.valueOf(lhs - (long)v); } public LuaValue subFrom( int lhs ) { return LuaInteger.valueOf(lhs - (long)v); }
public LuaValue mul( LuaValue rhs ) { return rhs.mul(v); } public LuaValue mul( LuaValue rhs ) { return rhs.mul(v); }
public LuaValue mul( double lhs ) { return LuaDouble.valueOf(lhs * v); } public LuaValue mul( double lhs ) { return LuaDouble.valueOf(lhs * v); }
public LuaValue mul( int lhs ) { return LuaInteger.valueOf(lhs * (long)v); } public LuaValue mul( int lhs ) { return LuaInteger.valueOf(lhs * (long)v); }
public LuaValue pow( LuaValue rhs ) { return rhs.powWith(v); } public LuaValue pow( LuaValue rhs ) { return rhs.powWith(v); }
public LuaValue pow( double rhs ) { return MathLib.dpow(v,rhs); }
public LuaValue pow( int rhs ) { return MathLib.dpow(v,rhs); }
public LuaValue powWith( double lhs ) { return MathLib.dpow(lhs,v); } public LuaValue powWith( double lhs ) { return MathLib.dpow(lhs,v); }
public LuaValue powWith( int lhs ) { return MathLib.dpow(lhs,v); } public LuaValue powWith( int lhs ) { return MathLib.dpow(lhs,v); }
public LuaValue div( LuaValue rhs ) { return rhs.divInto(v); } public LuaValue div( LuaValue rhs ) { return rhs.divInto(v); }
public LuaValue div( double rhs ) { return LuaDouble.ddiv(v,rhs); }
public LuaValue div( int rhs ) { return LuaDouble.ddiv(v,rhs); }
public LuaValue divInto( double lhs ) { return LuaDouble.ddiv(lhs,v); } public LuaValue divInto( double lhs ) { return LuaDouble.ddiv(lhs,v); }
public LuaValue mod( LuaValue rhs ) { return rhs.modFrom(v); } public LuaValue mod( LuaValue rhs ) { return rhs.modFrom(v); }
public LuaValue mod( double rhs ) { return LuaDouble.dmod(v,rhs); }
public LuaValue mod( int rhs ) { return LuaDouble.dmod(v,rhs); }
public LuaValue modFrom( double lhs ) { return LuaDouble.dmod(lhs,v); } public LuaValue modFrom( double lhs ) { return LuaDouble.dmod(lhs,v); }
// relational operators // relational operators
public LuaValue lt( LuaValue rhs ) { return rhs.gt_b(v)? LuaValue.TRUE: FALSE; } public LuaValue lt( LuaValue rhs ) { return rhs.gt_b(v)? TRUE: FALSE; }
public LuaValue lt( double rhs ) { return v < rhs? TRUE: FALSE; }
public LuaValue lt( int rhs ) { return v < rhs? TRUE: FALSE; }
public boolean lt_b( LuaValue rhs ) { return rhs.gt_b(v); } public boolean lt_b( LuaValue rhs ) { return rhs.gt_b(v); }
public boolean lt_b( int rhs ) { return v < rhs; } public boolean lt_b( int rhs ) { return v < rhs; }
public boolean lt_b( double rhs ) { return v < rhs; } public boolean lt_b( double rhs ) { return v < rhs; }
public LuaValue lteq( LuaValue rhs ) { return rhs.gteq_b(v)? LuaValue.TRUE: FALSE; } public LuaValue lteq( LuaValue rhs ) { return rhs.gteq_b(v)? TRUE: FALSE; }
public LuaValue lteq( double rhs ) { return v <= rhs? TRUE: FALSE; }
public LuaValue lteq( int rhs ) { return v <= rhs? TRUE: FALSE; }
public boolean lteq_b( LuaValue rhs ) { return rhs.gteq_b(v); } public boolean lteq_b( LuaValue rhs ) { return rhs.gteq_b(v); }
public boolean lteq_b( int rhs ) { return v <= rhs; } public boolean lteq_b( int rhs ) { return v <= rhs; }
public boolean lteq_b( double rhs ) { return v <= rhs; } public boolean lteq_b( double rhs ) { return v <= rhs; }
public LuaValue gt( LuaValue rhs ) { return rhs.lt_b(v)? LuaValue.TRUE: FALSE; } public LuaValue gt( LuaValue rhs ) { return rhs.lt_b(v)? TRUE: FALSE; }
public LuaValue gt( double rhs ) { return v > rhs? TRUE: FALSE; }
public LuaValue gt( int rhs ) { return v > rhs? TRUE: FALSE; }
public boolean gt_b( LuaValue rhs ) { return rhs.lt_b(v); } public boolean gt_b( LuaValue rhs ) { return rhs.lt_b(v); }
public boolean gt_b( int rhs ) { return v > rhs; } public boolean gt_b( int rhs ) { return v > rhs; }
public boolean gt_b( double rhs ) { return v > rhs; } public boolean gt_b( double rhs ) { return v > rhs; }
public LuaValue gteq( LuaValue rhs ) { return rhs.lteq_b(v)? LuaValue.TRUE: FALSE; } public LuaValue gteq( LuaValue rhs ) { return rhs.lteq_b(v)? TRUE: FALSE; }
public LuaValue gteq( double rhs ) { return v >= rhs? TRUE: FALSE; }
public LuaValue gteq( int rhs ) { return v >= rhs? TRUE: FALSE; }
public boolean gteq_b( LuaValue rhs ) { return rhs.lteq_b(v); } public boolean gteq_b( LuaValue rhs ) { return rhs.lteq_b(v); }
public boolean gteq_b( int rhs ) { return v >= rhs; } public boolean gteq_b( int rhs ) { return v >= rhs; }
public boolean gteq_b( double rhs ) { return v >= rhs; } public boolean gteq_b( double rhs ) { return v >= rhs; }

View File

@@ -61,5 +61,4 @@ public class LuaNumber extends LuaValue {
public LuaValue getmetatable() { public LuaValue getmetatable() {
return s_metatable; return s_metatable;
} }
} }

View File

@@ -263,33 +263,49 @@ public class LuaValue extends Varargs {
public LuaValue add(double rhs) { return aritherror("add"); } public LuaValue add(double rhs) { return aritherror("add"); }
public LuaValue add(int rhs) { return add((double)rhs); } public LuaValue add(int rhs) { return add((double)rhs); }
public LuaValue sub( LuaValue rhs ) { return aritherror("sub"); } public LuaValue sub( LuaValue rhs ) { return aritherror("sub"); }
public LuaValue sub( double rhs ) { return aritherror("sub"); }
public LuaValue sub( int rhs ) { return aritherror("sub"); }
public LuaValue subFrom(double lhs) { return aritherror("sub"); } public LuaValue subFrom(double lhs) { return aritherror("sub"); }
public LuaValue subFrom(int lhs) { return subFrom((double)lhs); } public LuaValue subFrom(int lhs) { return subFrom((double)lhs); }
public LuaValue mul( LuaValue rhs ) { return aritherror("mul"); } public LuaValue mul( LuaValue rhs ) { return aritherror("mul"); }
public LuaValue mul(double rhs) { return aritherror("mul"); } public LuaValue mul(double rhs) { return aritherror("mul"); }
public LuaValue mul(int rhs) { return mul((double)rhs); } public LuaValue mul(int rhs) { return mul((double)rhs); }
public LuaValue pow( LuaValue rhs ) { return aritherror("pow"); } public LuaValue pow( LuaValue rhs ) { return aritherror("pow"); }
public LuaValue pow( double rhs ) { return aritherror("pow"); }
public LuaValue pow( int rhs ) { return aritherror("pow"); }
public LuaValue powWith(double lhs) { return aritherror("mul"); } public LuaValue powWith(double lhs) { return aritherror("mul"); }
public LuaValue powWith(int lhs) { return powWith((double)lhs); } public LuaValue powWith(int lhs) { return powWith((double)lhs); }
public LuaValue div( LuaValue rhs ) { return aritherror("div"); } public LuaValue div( LuaValue rhs ) { return aritherror("div"); }
public LuaValue div( double rhs ) { return aritherror("div"); }
public LuaValue div( int rhs ) { return aritherror("div"); }
public LuaValue divInto(double lhs) { return aritherror("divInto"); } public LuaValue divInto(double lhs) { return aritherror("divInto"); }
public LuaValue mod( LuaValue rhs ) { return aritherror("mod"); } public LuaValue mod( LuaValue rhs ) { return aritherror("mod"); }
public LuaValue mod( double rhs ) { return aritherror("mod"); }
public LuaValue mod( int rhs ) { return aritherror("mod"); }
public LuaValue modFrom(double lhs) { return aritherror("modFrom"); } public LuaValue modFrom(double lhs) { return aritherror("modFrom"); }
// relational operators // relational operators
public LuaValue lt( LuaValue rhs ) { return compareerror(rhs); } public LuaValue lt( LuaValue rhs ) { return compareerror(rhs); }
public LuaValue lt( double rhs ) { return compareerror("number"); }
public LuaValue lt( int rhs ) { return compareerror("number"); }
public boolean lt_b( LuaValue rhs ) { compareerror(rhs); return false; } public boolean lt_b( LuaValue rhs ) { compareerror(rhs); return false; }
public boolean lt_b( int rhs ) { compareerror("number"); return false; } public boolean lt_b( int rhs ) { compareerror("number"); return false; }
public boolean lt_b( double rhs ) { compareerror("number"); return false; } public boolean lt_b( double rhs ) { compareerror("number"); return false; }
public LuaValue lteq( LuaValue rhs ) { return compareerror(rhs); } public LuaValue lteq( LuaValue rhs ) { return compareerror(rhs); }
public LuaValue lteq( double rhs ) { return compareerror("number"); }
public LuaValue lteq( int rhs ) { return compareerror("number"); }
public boolean lteq_b( LuaValue rhs ) { compareerror(rhs); return false; } public boolean lteq_b( LuaValue rhs ) { compareerror(rhs); return false; }
public boolean lteq_b( int rhs ) { compareerror("number"); return false; } public boolean lteq_b( int rhs ) { compareerror("number"); return false; }
public boolean lteq_b( double rhs ) { compareerror("number"); return false; } public boolean lteq_b( double rhs ) { compareerror("number"); return false; }
public LuaValue gt( LuaValue rhs ) { return compareerror(rhs); } public LuaValue gt( LuaValue rhs ) { return compareerror(rhs); }
public LuaValue gt( double rhs ) { return compareerror("number"); }
public LuaValue gt( int rhs ) { return compareerror("number"); }
public boolean gt_b( LuaValue rhs ) { compareerror(rhs); return false; } public boolean gt_b( LuaValue rhs ) { compareerror(rhs); return false; }
public boolean gt_b( int rhs ) { compareerror("number"); return false; } public boolean gt_b( int rhs ) { compareerror("number"); return false; }
public boolean gt_b( double rhs ) { compareerror("number"); return false; } public boolean gt_b( double rhs ) { compareerror("number"); return false; }
public LuaValue gteq( LuaValue rhs ) { return compareerror(rhs); } public LuaValue gteq( LuaValue rhs ) { return compareerror("number"); }
public LuaValue gteq( double rhs ) { return compareerror("number"); }
public LuaValue gteq( int rhs ) { return valueOf(todouble() >= rhs); }
public boolean gteq_b( LuaValue rhs ) { compareerror(rhs); return false; } public boolean gteq_b( LuaValue rhs ) { compareerror(rhs); return false; }
public boolean gteq_b( int rhs ) { compareerror("number"); return false; } public boolean gteq_b( int rhs ) { compareerror("number"); return false; }
public boolean gteq_b( double rhs ) { compareerror("number"); return false; } public boolean gteq_b( double rhs ) { compareerror("number"); return false; }

View File

@@ -158,7 +158,7 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
case 2: { // "setfenv", // (f, table) -> void case 2: { // "setfenv", // (f, table) -> void
LuaTable t = arg2.checktable(); LuaTable t = arg2.checktable();
LuaValue f = getfenvobj(arg1); LuaValue f = getfenvobj(arg1);
if ( f.isfunction() && ! f.isclosure() ) if ( ! f.isfunction() && ! f.isclosure() )
error("'setfenv' cannot change environment of given object"); error("'setfenv' cannot change environment of given object");
f.setfenv(t); f.setfenv(t);
return f.isthread()? NONE: f; return f.isthread()? NONE: f;

View File

@@ -112,14 +112,19 @@ public class MathLib extends OneArgFunction {
public static LuaValue dpow(double a, double b) { public static LuaValue dpow(double a, double b) {
return LuaDouble.valueOf( return LuaDouble.valueOf(
MATHLIB!=null? MATHLIB!=null?
MATHLIB.dpow_d(a,b): MATHLIB.dpow_lib(a,b):
dpow_default(a,b) ); dpow_default(a,b) );
} }
public static double dpow_d(double a, double b) {
return MATHLIB!=null?
MATHLIB.dpow_lib(a,b):
dpow_default(a,b);
}
/** /**
* Hook to override default dpow behavior with faster implementation. * Hook to override default dpow behavior with faster implementation.
*/ */
public double dpow_d(double a, double b) { public double dpow_lib(double a, double b) {
return dpow_default(a,b); return dpow_default(a,b);
} }

View File

@@ -75,7 +75,7 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
} }
/** Faster, better version of pow() used by arithmetic operator ^ */ /** Faster, better version of pow() used by arithmetic operator ^ */
public double dpow_d(double a, double b) { public double dpow_lib(double a, double b) {
return Math.pow(a, b); return Math.pow(a, b);
} }

View File

@@ -355,7 +355,6 @@ public class JavaCodeGen {
public String evalBoolean(Exp exp) { public String evalBoolean(Exp exp) {
Writer x = pushWriter(); Writer x = pushWriter();
callerExpects.put(exp,LuaValue.TBOOLEAN);
exp.accept(new Visitor() { exp.accept(new Visitor() {
public void visit(UnopExp exp) { public void visit(UnopExp exp) {
switch ( exp.op ) { switch ( exp.op ) {
@@ -374,7 +373,7 @@ public class JavaCodeGen {
case Lua.OP_EQ: case Lua.OP_EQ:
case Lua.OP_NEQ: case Lua.OP_NEQ:
op = (exp.op==Lua.OP_EQ? ".eq_b(": ".neq_b("); op = (exp.op==Lua.OP_EQ? ".eq_b(": ".neq_b(");
out("("+evalLuaValue(exp.lhs)+op+evalLuaValue(exp.rhs)+")"); out(evalLuaValue(exp.lhs)+op+evalLuaValue(exp.rhs)+")");
break; break;
case Lua.OP_GT: case Lua.OP_GT:
case Lua.OP_GE: case Lua.OP_GE:
@@ -386,8 +385,18 @@ public class JavaCodeGen {
default: out(evalLuaValue(exp)+".toboolean()"); break; default: out(evalLuaValue(exp)+".toboolean()"); break;
} }
} }
public void visit(Constant exp) {
switch ( exp.value.type() ) {
case LuaValue.TBOOLEAN:
out(exp.value.toboolean()? "true": "false");
break;
default:
out(evalLuaValue(exp)+".toboolean()");
break;
}
}
public void visit(ParensExp exp) { public void visit(ParensExp exp) {
evalBoolean(exp.exp); out(evalBoolean(exp.exp));
} }
public void visit(VarargsExp exp) { public void visit(VarargsExp exp) {
out(evalLuaValue(exp)+".toboolean()"); out(evalLuaValue(exp)+".toboolean()");
@@ -401,13 +410,21 @@ public class JavaCodeGen {
public void visit(NameExp exp) { public void visit(NameExp exp) {
out(evalLuaValue(exp)+".toboolean()"); out(evalLuaValue(exp)+".toboolean()");
} }
public void visit(FuncCall exp) {
out(evalLuaValue(exp)+".toboolean()");
}
public void visit(MethodCall exp) {
out(evalLuaValue(exp)+".toboolean()");
}
public void visit(TableConstructor exp) {
out(evalLuaValue(exp)+".toboolean()");
}
}); });
return popWriter(x); return popWriter(x);
} }
public String evalNumber(Exp exp) { public String evalNumber(Exp exp) {
Writer x = pushWriter(); Writer x = pushWriter();
callerExpects.put(exp,LuaValue.TBOOLEAN);
exp.accept(new Visitor() { exp.accept(new Visitor() {
public void visit(UnopExp exp) { public void visit(UnopExp exp) {
switch ( exp.op ) { switch ( exp.op ) {
@@ -425,14 +442,24 @@ public class JavaCodeGen {
op = (exp.op==Lua.OP_ADD? "+": exp.op==Lua.OP_SUB? "-": "*"); op = (exp.op==Lua.OP_ADD? "+": exp.op==Lua.OP_SUB? "-": "*");
out("("+evalNumber(exp.lhs)+op+evalNumber(exp.rhs)+")"); out("("+evalNumber(exp.lhs)+op+evalNumber(exp.rhs)+")");
break; break;
case Lua.OP_POW: out("MathLib.dpow("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); break; case Lua.OP_POW: out("MathLib.dpow_d("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); break;
//case Lua.OP_DIV: out("LuaDouble.ddiv("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); break; case Lua.OP_DIV: out("LuaDouble.ddiv_d("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); break;
//case Lua.OP_MOD: out("LuaDouble.dmod("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); break; case Lua.OP_MOD: out("LuaDouble.dmod_d("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); break;
default: out(evalLuaValue(exp)+".todouble()"); break; default: out(evalLuaValue(exp)+".todouble()"); break;
} }
} }
public void visit(Constant exp) {
switch ( exp.value.type() ) {
case LuaValue.TNUMBER:
out( evalNumberLiteral(exp.value.todouble()) );
break;
default:
out(evalLuaValue(exp)+".todouble()");
break;
}
}
public void visit(ParensExp exp) { public void visit(ParensExp exp) {
evalNumber(exp.exp); out(evalNumber(exp.exp));
} }
public void visit(VarargsExp exp) { public void visit(VarargsExp exp) {
out(evalLuaValue(exp)+".todouble()"); out(evalLuaValue(exp)+".todouble()");
@@ -446,6 +473,15 @@ public class JavaCodeGen {
public void visit(NameExp exp) { public void visit(NameExp exp) {
out(evalLuaValue(exp)+".todouble()"); out(evalLuaValue(exp)+".todouble()");
} }
public void visit(FuncCall exp) {
out(evalLuaValue(exp)+".todouble()");
}
public void visit(MethodCall exp) {
out(evalLuaValue(exp)+".todouble()");
}
public void visit(TableConstructor exp) {
out(evalLuaValue(exp)+".todouble()");
}
}); });
return popWriter(x); return popWriter(x);
} }
@@ -465,19 +501,19 @@ public class JavaCodeGen {
return; return;
} }
switch ( exp.op ) { switch ( exp.op ) {
case Lua.OP_ADD: out(evalLuaValue(exp.lhs)+".add("+evalNumber(exp.rhs)+")"); return; case Lua.OP_ADD: out("valueOf("+evalNumber(exp.lhs)+"+"+evalNumber(exp.rhs)+")"); return;
case Lua.OP_SUB: out(evalLuaValue(exp.lhs)+".sub("+evalNumber(exp.rhs)+")"); return; case Lua.OP_SUB: out("valueOf("+evalNumber(exp.lhs)+"-"+evalNumber(exp.rhs)+")"); return;
case Lua.OP_MUL: out(evalLuaValue(exp.lhs)+".mul("+evalNumber(exp.rhs)+")"); return; case Lua.OP_MUL: out("valueOf("+evalNumber(exp.lhs)+"*"+evalNumber(exp.rhs)+")"); return;
case Lua.OP_POW: out(evalLuaValue(exp.lhs)+".pow("+evalNumber(exp.rhs)+")"); return; case Lua.OP_POW: out("MathLib.dpow("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); return;
case Lua.OP_DIV: out("LuaDouble.ddiv("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); break; case Lua.OP_DIV: out("LuaDouble.ddiv("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); return;
case Lua.OP_MOD: out("LuaDouble.dmod("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); break; case Lua.OP_MOD: out("LuaDouble.dmod("+evalNumber(exp.lhs)+","+evalNumber(exp.rhs)+")"); return;
case Lua.OP_GT: out(evalLuaValue(exp.lhs)+".gt("+evalNumber(exp.rhs)+")"); return; case Lua.OP_GT: out(evalLuaValue(exp.lhs)+".gt("+evalNumber(exp.rhs)+")"); return;
case Lua.OP_GE: out(evalLuaValue(exp.lhs)+".gteq("+evalNumber(exp.rhs)+")"); return; case Lua.OP_GE: out(evalLuaValue(exp.lhs)+".gteq("+evalNumber(exp.rhs)+")"); return;
case Lua.OP_LT: out(evalLuaValue(exp.lhs)+".lt("+evalNumber(exp.rhs)+")"); return; case Lua.OP_LT: out(evalLuaValue(exp.lhs)+".lt("+evalNumber(exp.rhs)+")"); return;
case Lua.OP_LE: out(evalLuaValue(exp.lhs)+".lteq("+evalNumber(exp.rhs)+")"); return; case Lua.OP_LE: out(evalLuaValue(exp.lhs)+".lteq("+evalNumber(exp.rhs)+")"); return;
case Lua.OP_EQ: out(evalLuaValue(exp.lhs)+".eq("+evalNumber(exp.rhs)+")"); return; case Lua.OP_EQ: out(evalLuaValue(exp.lhs)+".eq("+evalLuaValue(exp.rhs)+")"); return;
case Lua.OP_NEQ: out(evalLuaValue(exp.lhs)+".neq("+evalNumber(exp.rhs)+")"); return; case Lua.OP_NEQ: out(evalLuaValue(exp.lhs)+".neq("+evalLuaValue(exp.rhs)+")"); return;
case Lua.OP_CONCAT: out(evalLuaValue(exp.lhs)+".concat("+evalNumber(exp.rhs)+")"); return; case Lua.OP_CONCAT: out(evalLuaValue(exp.lhs)+".concat("+evalLuaValue(exp.rhs)+")"); return;
default: throw new IllegalStateException("unknown bin op:"+exp.op); default: throw new IllegalStateException("unknown bin op:"+exp.op);
} }
} }
@@ -532,14 +568,18 @@ public class JavaCodeGen {
if ( value == 1 ) return "ONE"; if ( value == 1 ) return "ONE";
if ( numberConstants.containsKey(value) ) if ( numberConstants.containsKey(value) )
return numberConstants.get(value); return numberConstants.get(value);
int ivalue = (int) value; String declvalue = evalNumberLiteral(value);
String declvalue = value==ivalue? String.valueOf(ivalue): String.valueOf(value);
String javaname = javascope.createConstantName(declvalue); String javaname = javascope.createConstantName(declvalue);
constantDeclarations.add( "static final LuaValue "+javaname+" = valueOf("+declvalue+");" ); constantDeclarations.add( "static final LuaValue "+javaname+" = valueOf("+declvalue+");" );
numberConstants.put(value,javaname); numberConstants.put(value,javaname);
return javaname; return javaname;
} }
private String evalNumberLiteral(double value) {
int ivalue = (int) value;
return value==ivalue? String.valueOf(ivalue): String.valueOf(value);
}
public void visit(FieldExp exp) { public void visit(FieldExp exp) {
exp.lhs.accept(this); exp.lhs.accept(this);
out(".get("+evalStringConstant(exp.name.name)+")"); out(".get("+evalStringConstant(exp.name.name)+")");
@@ -877,7 +917,7 @@ public class JavaCodeGen {
} }
public void visit(WhileDo stat) { public void visit(WhileDo stat) {
outb( "while ("+evalLuaValue(stat.exp)+".toboolean()) {"); outb( "while ("+evalBoolean(stat.exp)+") {");
super.visit(stat.block); super.visit(stat.block);
oute( "}" ); oute( "}" );
} }