comparison core/src/luan/LuanBit.java @ 171:3dcb0f9bee82

add core component git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 05:41:22 +0000
parents src/luan/LuanBit.java@c9100f29fae0
children 2456ef7ada02
comparison
equal deleted inserted replaced
170:7c792a328a83 171:3dcb0f9bee82
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 String stackTrace() {
20 StringBuilder buf = new StringBuilder();
21 LuanElement el = this.el;
22 for( int i = luan.stackTrace.size() - 1; i>=0; i-- ) {
23 StackTraceElement stackTraceElement = luan.stackTrace.get(i);
24 buf.append( "\n\t" ).append( el.toString(stackTraceElement.fnName) );
25 el = stackTraceElement.call;
26 }
27 return buf.toString();
28 }
29
30 public void dumpStack() {
31 System.err.println( stackTrace() );
32 }
33
34 public Object call(LuanFunction fn,String fnName,Object[] args) throws LuanException {
35 List<StackTraceElement> stackTrace = luan.stackTrace;
36 stackTrace.add( new StackTraceElement(el,fnName) );
37 try {
38 return fn.call(luan,args);
39 } finally {
40 stackTrace.remove(stackTrace.size()-1);
41 }
42 }
43
44 public String checkString(Object obj) throws LuanException {
45 String s = Luan.asString(obj);
46 if( s == null )
47 throw exception( "attempt to use a " + Luan.type(obj) + " as a string" );
48 return s;
49 }
50
51 public Number checkNumber(Object obj) throws LuanException {
52 Number n = Luan.toNumber(obj);
53 if( n == null )
54 throw exception( "attempt to perform arithmetic on a " + Luan.type(obj) + " value" );
55 return n;
56 }
57
58 public LuanFunction checkFunction(Object obj) throws LuanException {
59 if( obj instanceof LuanFunction )
60 return (LuanFunction)obj;
61 throw exception( "attempt to call a " + Luan.type(obj) + " value" );
62 }
63
64 public Boolean checkBoolean(Object obj) throws LuanException {
65 if( obj instanceof Boolean )
66 return (Boolean)obj;
67 throw exception( "attempt to use a " + Luan.type(obj) + " as a boolean" );
68 }
69
70 public String toString(Object obj) throws LuanException {
71 LuanFunction fn = getHandlerFunction("__tostring",obj);
72 if( fn != null )
73 return checkString( Luan.first( call(fn,"__tostring",new Object[]{obj}) ) );
74 return Luan.toString(obj);
75 }
76
77 public String repr(Object obj) throws LuanException {
78 LuanFunction fn = getHandlerFunction("__repr",obj);
79 if( fn != null )
80 return checkString( Luan.first( call(fn,"__repr",new Object[]{obj}) ) );
81 String repr = Luan.repr(obj);
82 if( repr==null )
83 throw exception( "value '" + obj + "' doesn't support repr()" );
84 return repr;
85 }
86
87 public LuanFunction getHandlerFunction(String op,Object obj) throws LuanException {
88 Object f = luan.getHandler(op,obj);
89 if( f == null )
90 return null;
91 return checkFunction(f);
92 }
93
94 public LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
95 LuanFunction f1 = getHandlerFunction(op,o1);
96 if( f1 != null )
97 return f1;
98 return getHandlerFunction(op,o2);
99 }
100
101 public boolean isLessThan(Object o1,Object o2) throws LuanException {
102 if( o1 instanceof Number && o2 instanceof Number ) {
103 Number n1 = (Number)o1;
104 Number n2 = (Number)o2;
105 return n1.doubleValue() < n2.doubleValue();
106 }
107 if( o1 instanceof String && o2 instanceof String ) {
108 String s1 = (String)o1;
109 String s2 = (String)o2;
110 return s1.compareTo(s2) < 0;
111 }
112 LuanFunction fn = getBinHandler("__lt",o1,o2);
113 if( fn != null )
114 return Luan.toBoolean( Luan.first(call(fn,"__lt",new Object[]{o1,o2})) );
115 throw exception( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
116 }
117
118 public Object arithmetic(String op,Object o1,Object o2) throws LuanException {
119 LuanFunction fn = getBinHandler(op,o1,o2);
120 if( fn != null )
121 return Luan.first(call(fn,op,new Object[]{o1,o2}));
122 String type = Luan.toNumber(o1)==null ? Luan.type(o1) : Luan.type(o2);
123 throw exception("attempt to perform arithmetic on a "+type+" value");
124 }
125
126 }