Mercurial Hosting > luan
comparison core/src/luan/LuanState.java @ 576:4723d22062ce
remove LuanBit
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 13 Jul 2015 20:38:26 -0600 |
parents | 7c3ad6db8ac3 |
children | 60c549d43988 |
comparison
equal
deleted
inserted
replaced
575:7c3ad6db8ac3 | 576:4723d22062ce |
---|---|
62 public final Object eval(String cmd,LuanTable env) throws LuanException { | 62 public final Object eval(String cmd,LuanTable env) throws LuanException { |
63 LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true); | 63 LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true); |
64 return fn.call(this); | 64 return fn.call(this); |
65 } | 65 } |
66 | 66 |
67 public final LuanBit bit(LuanElement el) { | |
68 return new LuanBit(this,el); | |
69 } | |
70 | 67 |
71 // convenience methods | |
72 | |
73 private final LuanBit JAVA = bit(null); | |
74 | 68 |
75 public LuanException exception(Object msg) throws LuanException { | 69 public LuanException exception(Object msg) throws LuanException { |
76 return JAVA.exception(msg); | 70 return new LuanException(this,msg); |
77 } | 71 } |
78 | 72 |
79 public Boolean checkBoolean(Object obj) throws LuanException { | 73 public Boolean checkBoolean(Object obj) throws LuanException { |
80 return JAVA.checkBoolean(obj); | 74 if( obj instanceof Boolean ) |
75 return (Boolean)obj; | |
76 throw exception( "attempt to use '"+context()+"' (a " + Luan.type(obj) + " value) as a boolean" ); | |
77 } | |
78 | |
79 public Boolean checkBoolean(Object obj,LuanElement el) throws LuanException { | |
80 push(el,null); | |
81 try { | |
82 return checkBoolean(obj); | |
83 } finally { | |
84 pop(); | |
85 } | |
81 } | 86 } |
82 | 87 |
83 public String checkString(Object obj) throws LuanException { | 88 public String checkString(Object obj) throws LuanException { |
84 return JAVA.checkString(obj); | 89 if( obj instanceof String ) |
90 return (String)obj; | |
91 throw exception( "attempt to use '"+context()+"' (a " + Luan.type(obj) + " value) as a string" ); | |
85 } | 92 } |
86 | 93 |
87 public LuanFunction checkFunction(Object obj) throws LuanException { | 94 public LuanFunction checkFunction(Object obj) throws LuanException { |
88 return JAVA.checkFunction(obj); | 95 if( obj instanceof LuanFunction ) |
96 return (LuanFunction)obj; | |
97 throw exception( "attempt to call '"+context()+"' (a " + Luan.type(obj) + " value)" ); | |
89 } | 98 } |
90 | 99 |
91 public boolean isLessThan(Object o1,Object o2) throws LuanException { | 100 public boolean isLessThan(Object o1,Object o2) throws LuanException { |
92 return JAVA.isLessThan(o1,o2); | 101 if( o1 instanceof Number && o2 instanceof Number ) { |
102 Number n1 = (Number)o1; | |
103 Number n2 = (Number)o2; | |
104 return n1.doubleValue() < n2.doubleValue(); | |
105 } | |
106 if( o1 instanceof String && o2 instanceof String ) { | |
107 String s1 = (String)o1; | |
108 String s2 = (String)o2; | |
109 return s1.compareTo(s2) < 0; | |
110 } | |
111 LuanFunction fn = getBinHandler("__lt",o1,o2); | |
112 if( fn != null ) | |
113 return checkBoolean( Luan.first(fn.call(this,new Object[]{o1,o2})) ); | |
114 throw exception( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); | |
93 } | 115 } |
94 | 116 |
95 public String toString(Object obj) throws LuanException { | 117 public String toString(Object obj) throws LuanException { |
96 if( obj instanceof LuanTable ) { | 118 if( obj instanceof LuanTable ) { |
97 LuanTable tbl = (LuanTable)obj; | 119 LuanTable tbl = (LuanTable)obj; |
104 if( obj instanceof byte[] ) | 126 if( obj instanceof byte[] ) |
105 return "binary: " + Integer.toHexString(obj.hashCode()); | 127 return "binary: " + Integer.toHexString(obj.hashCode()); |
106 return obj.toString(); | 128 return obj.toString(); |
107 } | 129 } |
108 | 130 |
131 public String toString(Object obj,LuanElement el) throws LuanException { | |
132 push(el,null); | |
133 try { | |
134 return toString(obj); | |
135 } finally { | |
136 pop(); | |
137 } | |
138 } | |
139 | |
109 public Object index(Object obj,Object key) throws LuanException { | 140 public Object index(Object obj,Object key) throws LuanException { |
110 if( obj instanceof LuanTable ) { | 141 if( obj instanceof LuanTable ) { |
111 LuanTable tbl = (LuanTable)obj; | 142 LuanTable tbl = (LuanTable)obj; |
112 return tbl.get(this,key); | 143 return tbl.get(this,key); |
113 } | 144 } |
117 } | 148 } |
118 | 149 |
119 public String context() { | 150 public String context() { |
120 return stackTrace.get(stackTrace.size()-1).call.text(); | 151 return stackTrace.get(stackTrace.size()-1).call.text(); |
121 } | 152 } |
153 | |
154 public void push(LuanElement el,String fnName) { | |
155 if( el == null ) throw new RuntimeException(); | |
156 stackTrace.add( new StackTraceElement(el,fnName) ); | |
157 } | |
158 | |
159 public void pop() { | |
160 stackTrace.remove(stackTrace.size()-1); | |
161 } | |
162 | |
163 public LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException { | |
164 if( o1 instanceof LuanTable ) { | |
165 LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1); | |
166 if( f1 != null ) | |
167 return f1; | |
168 } | |
169 return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null; | |
170 } | |
171 | |
172 public LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException { | |
173 Object f = t.getHandler(op); | |
174 if( f == null ) | |
175 return null; | |
176 return checkFunction(f); | |
177 } | |
178 | |
179 public void dumpStack() { | |
180 System.err.println( stackTrace ); | |
181 } | |
182 /* | |
183 public Number checkNumber(Object obj) throws LuanException { | |
184 if( obj instanceof Number ) | |
185 return (Number)obj; | |
186 throw exception( "attempt to perform arithmetic on '"+context()+"' (a " + Luan.type(obj) + " value)" ); | |
187 } | |
188 */ | |
122 } | 189 } |