Mercurial Hosting > luan
comparison src/luan/LuanBit.java @ 88:6ca02b188dba
add LuanBit to clean up code;
add repr();
git-svn-id: https://luan-java.googlecode.com/svn/trunk@89 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 27 Feb 2013 23:50:32 +0000 |
parents | |
children | 2428ecfed375 |
comparison
equal
deleted
inserted
replaced
87:eaf37cfa30c2 | 88:6ca02b188dba |
---|---|
1 package luan; | |
2 | |
3 import java.util.List; | |
4 | |
5 | |
6 public final class LuanBit { | |
7 public final LuanState luan; | |
8 public final LuanElement el; | |
9 | |
10 LuanBit(LuanState luan,LuanElement el) { | |
11 this.luan = luan; | |
12 this.el = el; | |
13 } | |
14 | |
15 public LuanException exception(Object msg) { | |
16 return new LuanException(this,msg); | |
17 } | |
18 | |
19 public Object[] call(LuanFunction fn,String fnName,Object... args) throws LuanException { | |
20 List<StackTraceElement> stackTrace = luan.stackTrace; | |
21 stackTrace.add( new StackTraceElement(el,fnName) ); | |
22 try { | |
23 return fn.call(luan,args); | |
24 } finally { | |
25 stackTrace.remove(stackTrace.size()-1); | |
26 } | |
27 } | |
28 | |
29 public String checkString(Object obj) throws LuanException { | |
30 String s = Luan.asString(obj); | |
31 if( s == null ) | |
32 throw exception( "attempt to use a " + Luan.type(obj) + " as a string" ); | |
33 return s; | |
34 } | |
35 | |
36 public Number checkNumber(Object obj) throws LuanException { | |
37 Number n = Luan.toNumber(obj); | |
38 if( n == null ) | |
39 throw exception( "attempt to perform arithmetic on a " + Luan.type(obj) + " value" ); | |
40 return n; | |
41 } | |
42 | |
43 public LuanFunction checkFunction(Object obj) throws LuanException { | |
44 if( obj instanceof LuanFunction ) | |
45 return (LuanFunction)obj; | |
46 throw exception( "attempt to call a " + Luan.type(obj) + " value" ); | |
47 } | |
48 | |
49 public String toString(Object obj) throws LuanException { | |
50 LuanFunction fn = getHandlerFunction("__tostring",obj); | |
51 if( fn != null ) | |
52 return checkString( Luan.first( call(fn,"__tostring",obj) ) ); | |
53 return Luan.toString(obj); | |
54 } | |
55 | |
56 public String repr(Object obj) throws LuanException { | |
57 LuanFunction fn = getHandlerFunction("__repr",obj); | |
58 if( fn != null ) | |
59 return checkString( Luan.first( call(fn,"__repr",obj) ) ); | |
60 String repr = Luan.repr(obj); | |
61 if( repr==null ) | |
62 throw exception( "value '" + obj + "' doesn't support repr()" ); | |
63 return repr; | |
64 } | |
65 | |
66 public LuanFunction getHandlerFunction(String op,Object obj) throws LuanException { | |
67 Object f = luan.getHandler(op,obj); | |
68 if( f == null ) | |
69 return null; | |
70 return checkFunction(f); | |
71 } | |
72 | |
73 public LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException { | |
74 LuanFunction f1 = getHandlerFunction(op,o1); | |
75 if( f1 != null ) | |
76 return f1; | |
77 return getHandlerFunction(op,o2); | |
78 } | |
79 | |
80 public boolean isLessThan(Object o1,Object o2) throws LuanException { | |
81 if( o1 instanceof Number && o2 instanceof Number ) { | |
82 Number n1 = (Number)o1; | |
83 Number n2 = (Number)o2; | |
84 return n1.doubleValue() < n2.doubleValue(); | |
85 } | |
86 if( o1 instanceof String && o2 instanceof String ) { | |
87 String s1 = (String)o1; | |
88 String s2 = (String)o2; | |
89 return s1.compareTo(s2) < 0; | |
90 } | |
91 LuanFunction fn = getBinHandler("__lt",o1,o2); | |
92 if( fn != null ) | |
93 return Luan.toBoolean( Luan.first(call(fn,"__lt",o1,o2)) ); | |
94 throw exception( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); | |
95 } | |
96 | |
97 public Object arithmetic(String op,Object o1,Object o2) throws LuanException { | |
98 LuanFunction fn = getBinHandler(op,o1,o2); | |
99 if( fn != null ) | |
100 return Luan.first(call(fn,op,o1,o2)); | |
101 String type = Luan.toNumber(o1)==null ? Luan.type(o1) : Luan.type(o2); | |
102 throw exception("attempt to perform arithmetic on a "+type+" value"); | |
103 } | |
104 | |
105 } |