return "unknown:??";
}
+ public static boolean toBoolean(Object o) {
+ if (o == null) return false;
+ if (o instanceof Boolean) return ((Boolean)o).booleanValue();
+ if (o instanceof Number) return o.equals(new Integer(0));
+ return true;
+ }
+ public static long toLong(Object o) { return toNumber(o).longValue(); }
+ public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
+ public static Number toNumber(Object o) {
+ if (o == null) return new Long(0);
+ if (o instanceof Number) return ((Number)o);
+ if (o instanceof String) try { return new Double((String)o); } catch (NumberFormatException e) { return new Double(0); }
+ if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? new Long(1) : new Long(0);
+ if (o instanceof JS) return ((JS)o).coerceToNumber();
+ // FIXME
+ throw new Error("toNumber() got object of type " + o.getClass().getName());
+ }
// Instance Methods ////////////////////////////////////////////////////////////////////
}
}
public void put(Object key, Object val) {
- if (key.equals("length")) vec.setSize(Parser.toNumber(val).intValue());
+ if (key.equals("length")) vec.setSize(toNumber(val).intValue());
int i = intVal(key);
if (i == Integer.MIN_VALUE) super.put(key, val);
else {
public static class Script extends Function {
Vector e = null;
private Script(Vector e) { this.e = e; }
- public String getSourceName() throws JS.Exn { return ((Parser.Expr)e.elementAt(0)).sourceName; }
+ public String getSourceName() throws JS.Exn { return ((ForthBlock)e.elementAt(0)).sourceName; }
public Object _call(JS.Array args) throws JS.Exn {
Scope rootScope = (Scope)args.elementAt(0);
Function saved = (Function)currentFunction.get(Thread.currentThread());
currentFunction.put(Thread.currentThread(), this);
try {
for(int i=0; i<e.size(); i++)
- ((Parser.Expr)e.elementAt(i)).eval(rootScope);
- } catch (Parser.ReturnException e) {
+ ((ForthBlock)e.elementAt(i)).eval(rootScope);
+ } catch (ForthBlock.ReturnException e) {
// ignore
- } catch (Parser.ControlTransferException e) {
+ } catch (ForthBlock.ControlTransferException e) {
throw new JS.Exn("block threw a ControlTransferException: " + e);
} finally {
if (saved == null) currentFunction.remove(Thread.currentThread());
try {
Vector exprs = new Vector();
while(true) {
- Parser.Expr ret = p.parseBlock(false);
+ ForthBlock ret = p.parseStatement(false);
if (ret == null) break;
exprs.addElement(ret);
}