comparison 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
comparison
equal deleted inserted replaced
578:60c549d43988 579:f22a09e98b04
10 import luan.LuanFunction; 10 import luan.LuanFunction;
11 import luan.LuanException; 11 import luan.LuanException;
12 import luan.LuanElement; 12 import luan.LuanElement;
13 import luan.LuanSource; 13 import luan.LuanSource;
14 import luan.DeepCloner; 14 import luan.DeepCloner;
15 import luan.StackTraceElement;
15 16
16 17
17 final class LuanStateImpl extends LuanState { 18 final class LuanStateImpl extends LuanState {
18 19
19 private static class Frame { 20 private static class Frame {
122 if( frame==null ) 123 if( frame==null )
123 return null; 124 return null;
124 return frame.closure.fnDef.el().source; 125 return frame.closure.fnDef.el().source;
125 } 126 }
126 127
128
129
130 @Override public boolean isLessThan(Object o1,Object o2) throws LuanException {
131 if( o1 instanceof Number && o2 instanceof Number ) {
132 Number n1 = (Number)o1;
133 Number n2 = (Number)o2;
134 return n1.doubleValue() < n2.doubleValue();
135 }
136 if( o1 instanceof String && o2 instanceof String ) {
137 String s1 = (String)o1;
138 String s2 = (String)o2;
139 return s1.compareTo(s2) < 0;
140 }
141 LuanFunction fn = getBinHandler("__lt",o1,o2);
142 if( fn != null )
143 return checkBoolean( Luan.first(fn.call(this,new Object[]{o1,o2})) );
144 throw new LuanException(this, "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
145 }
146
147 LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
148 if( o1 instanceof LuanTable ) {
149 LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
150 if( f1 != null )
151 return f1;
152 }
153 return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
154 }
155
156 LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
157 Object f = t.getHandler(op);
158 if( f == null )
159 return null;
160 return checkFunction(f);
161 }
162
163 Boolean checkBoolean(Object obj,LuanElement el) throws LuanException {
164 push(el,null);
165 try {
166 return checkBoolean(obj);
167 } finally {
168 pop();
169 }
170 }
171
172 String toString(Object obj,LuanElement el) throws LuanException {
173 push(el,null);
174 try {
175 return toString(obj);
176 } finally {
177 pop();
178 }
179 }
180
181 void push(LuanElement el,String fnName) {
182 if( el == null ) throw new RuntimeException();
183 stackTrace.add( new StackTraceElement(el,fnName) );
184 }
185
186 void pop() {
187 stackTrace.remove(stackTrace.size()-1);
188 }
189
127 } 190 }