Mercurial Hosting > luan
annotate core/src/luan/impl/$Luan.java @ 652:067d9470184d
compile SetStmt and ForStmt
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 04 Apr 2016 23:26:06 -0600 |
parents | 140cc5191b7a |
children |
rev | line source |
---|---|
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 package luan.impl; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
3 import java.util.Arrays; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 import java.util.List; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 import java.util.ArrayList; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 import luan.Luan; |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
7 import luan.LuanState; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 import luan.LuanTable; |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
9 import luan.LuanFunction; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 import luan.LuanException; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 public final class $Luan { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 private $Luan() {} // never |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 private static List<Expressions> listExpressions = new ArrayList<Expressions>(); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 static int addExpressions(Expressions exp) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 int i = listExpressions.size(); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 listExpressions.add(exp); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 return i; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 public static Expressions getExpressions(int i) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 return listExpressions.get(i); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 |
651
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
30 private static List<Stmt> listStmt = new ArrayList<Stmt>(); |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
31 |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
32 static int addStmt(Stmt stmt) { |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
33 int i = listStmt.size(); |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
34 listStmt.add(stmt); |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
35 return i; |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
36 } |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
37 |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
38 public static Stmt getStmt(int i) { |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
39 return listStmt.get(i); |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
40 } |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
41 |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
42 |
652
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
43 private static List<Settable> listSettable = new ArrayList<Settable>(); |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
44 |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
45 static int addSettable(Settable settable) { |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
46 int i = listSettable.size(); |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
47 listSettable.add(settable); |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
48 return i; |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
49 } |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
50 |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
51 public static Settable getSettable(int i) { |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
52 return listSettable.get(i); |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
53 } |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
54 |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
55 |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
56 public static Object first(Object obj) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 return Luan.first(obj); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
60 public static int len(LuanState luan,Object o) throws LuanException { |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 if( o instanceof String ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 String s = (String)o; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 return s.length(); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
64 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
65 if( o instanceof byte[] ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 byte[] a = (byte[])o; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 return a.length; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
68 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
69 if( o instanceof LuanTable ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
70 LuanTable t = (LuanTable)o; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 return t.length(luan); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
72 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
73 throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" ); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
74 } |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
75 |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
76 public static Object unm(LuanState luan,Object o) throws LuanException { |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
77 if( o instanceof Number ) |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
78 return -((Number)o).doubleValue(); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
79 if( o instanceof LuanTable ) { |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
80 LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
81 if( fn != null ) { |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
82 return Luan.first(fn.call(luan,new Object[]{o})); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
83 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
84 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
85 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
86 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
87 |
651
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
88 public static boolean checkBoolean(Object o) throws LuanException { |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
89 return Luan.checkBoolean(o); |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
90 } |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
91 |
652
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
92 public static LuanFunction checkFunction(Object o) throws LuanException { |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
93 return Luan.checkFunction(o); |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
94 } |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
95 |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
96 public static boolean not(Object o) throws LuanException { |
651
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
97 return !checkBoolean(o); |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
98 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
99 |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
100 private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
101 LuanFunction fn = Luan.getBinHandler(op,o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
102 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
103 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
104 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
105 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
106 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
107 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
108 public static Object pow(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
109 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
110 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
111 return arithmetic(luan,"__pow",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
112 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
113 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
114 public static Object mul(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
115 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
116 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
117 return arithmetic(luan,"__mul",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
118 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
119 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
120 public static Object div(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
121 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
122 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
123 return arithmetic(luan,"__div",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
124 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
125 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
126 public static Object mod(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
127 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
128 double d1 = ((Number)o1).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
129 double d2 = ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
130 return d1 - Math.floor(d1/d2)*d2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
131 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
132 return arithmetic(luan,"__mod",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
133 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
134 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
135 public static Object add(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
136 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
137 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
138 return arithmetic(luan,"__add",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
139 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
140 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
141 public static Object sub(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
142 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
143 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
144 return arithmetic(luan,"__sub",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
145 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
146 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
147 public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
148 LuanFunction fn = Luan.getBinHandler("__concat",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
149 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
150 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
151 String s1 = luan.toString(o1); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
152 String s2 = luan.toString(o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
153 return s1 + s2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
154 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
155 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
156 public static boolean eq(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
157 if( o1 == o2 || o1 != null && o1.equals(o2) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
158 return true; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
159 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
160 Number n1 = (Number)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
161 Number n2 = (Number)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
162 return n1.doubleValue() == n2.doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
163 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
164 if( o1 instanceof byte[] && o2 instanceof byte[] ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
165 byte[] b1 = (byte[])o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
166 byte[] b2 = (byte[])o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
167 return Arrays.equals(b1,b2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
168 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
169 if( !(o1 instanceof LuanTable && o2 instanceof LuanTable) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
170 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
171 LuanTable t1 = (LuanTable)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
172 LuanTable t2 = (LuanTable)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
173 LuanTable mt1 = t1.getMetatable(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
174 LuanTable mt2 = t2.getMetatable(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
175 if( mt1==null || mt2==null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
176 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
177 Object f = mt1.rawGet("__eq"); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
178 if( f == null || !f.equals(mt2.rawGet("__eq")) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
179 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
180 LuanFunction fn = Luan.checkFunction(f); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
181 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
182 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
183 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
184 public static boolean le(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
185 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
186 Number n1 = (Number)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
187 Number n2 = (Number)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
188 return n1.doubleValue() <= n2.doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
189 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
190 if( o1 instanceof String && o2 instanceof String ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
191 String s1 = (String)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
192 String s2 = (String)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
193 return s1.compareTo(s2) <= 0; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
194 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
195 LuanFunction fn = Luan.getBinHandler("__le",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
196 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
197 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
198 fn = Luan.getBinHandler("__lt",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
199 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
200 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
201 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
202 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
203 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
204 public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
205 return Luan.isLessThan(luan,o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
206 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
207 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
208 public static boolean cnd(Object o) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
209 return !(o == null || Boolean.FALSE.equals(o)); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
210 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
211 |
651
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
212 public static void nop(Object o) {} |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
213 |
652
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
214 |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
215 public static void set(LuanStateImpl luan,Settable[] vars,Object obj) throws LuanException { |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
216 if( obj instanceof Object[] ) { |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
217 Object[] vals = (Object[])obj; |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
218 for( int i=0; i<vars.length; i++ ) { |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
219 Object val = i < vals.length ? vals[i] : null; |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
220 vars[i].set(luan,val); |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
221 } |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
222 } else { |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
223 vars[0].set(luan,obj); |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
224 for( int i=1; i<vars.length; i++ ) { |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
225 vars[i].set(luan,null); |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
226 } |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
227 } |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
228 } |
067d9470184d
compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents:
651
diff
changeset
|
229 |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
230 } |