Mercurial Hosting > luan
diff core/src/luan/impl/LuanStateImpl.java @ 579:f22a09e98b04
clean up LuanState
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 16 Jul 2015 15:14:31 -0600 |
parents | 8e51d6071b67 |
children | 859c0dedc8b6 |
line wrap: on
line diff
--- a/core/src/luan/impl/LuanStateImpl.java Tue Jul 14 17:40:48 2015 -0600 +++ b/core/src/luan/impl/LuanStateImpl.java Thu Jul 16 15:14:31 2015 -0600 @@ -12,6 +12,7 @@ import luan.LuanElement; import luan.LuanSource; import luan.DeepCloner; +import luan.StackTraceElement; final class LuanStateImpl extends LuanState { @@ -124,4 +125,66 @@ return frame.closure.fnDef.el().source; } + + + @Override public boolean isLessThan(Object o1,Object o2) throws LuanException { + if( o1 instanceof Number && o2 instanceof Number ) { + Number n1 = (Number)o1; + Number n2 = (Number)o2; + return n1.doubleValue() < n2.doubleValue(); + } + if( o1 instanceof String && o2 instanceof String ) { + String s1 = (String)o1; + String s2 = (String)o2; + return s1.compareTo(s2) < 0; + } + LuanFunction fn = getBinHandler("__lt",o1,o2); + if( fn != null ) + return checkBoolean( Luan.first(fn.call(this,new Object[]{o1,o2})) ); + throw new LuanException(this, "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); + } + + LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException { + if( o1 instanceof LuanTable ) { + LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1); + if( f1 != null ) + return f1; + } + return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null; + } + + LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException { + Object f = t.getHandler(op); + if( f == null ) + return null; + return checkFunction(f); + } + + Boolean checkBoolean(Object obj,LuanElement el) throws LuanException { + push(el,null); + try { + return checkBoolean(obj); + } finally { + pop(); + } + } + + String toString(Object obj,LuanElement el) throws LuanException { + push(el,null); + try { + return toString(obj); + } finally { + pop(); + } + } + + void push(LuanElement el,String fnName) { + if( el == null ) throw new RuntimeException(); + stackTrace.add( new StackTraceElement(el,fnName) ); + } + + void pop() { + stackTrace.remove(stackTrace.size()-1); + } + }