annotate core/src/luan/impl/LuanImpl.java @ 670:58ebfec6178b

all luan now compiles
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Apr 2016 01:05:57 -0600
parents 71f8f5075df8
children d3e5414bdf4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
660
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
11 import luan.modules.JavaLuan;
648
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
654
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
14 public final class LuanImpl {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
15 private LuanImpl() {} // never
648
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
670
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
17 /*
664
71f8f5075df8 compile FnDef
Franklin Schmidt <fschmidt@gmail.com>
parents: 663
diff changeset
18 private static List list = new ArrayList();
648
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
664
71f8f5075df8 compile FnDef
Franklin Schmidt <fschmidt@gmail.com>
parents: 663
diff changeset
20 static int addObj(Object obj) {
71f8f5075df8 compile FnDef
Franklin Schmidt <fschmidt@gmail.com>
parents: 663
diff changeset
21 int i = list.size();
71f8f5075df8 compile FnDef
Franklin Schmidt <fschmidt@gmail.com>
parents: 663
diff changeset
22 list.add(obj);
648
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 return i;
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
664
71f8f5075df8 compile FnDef
Franklin Schmidt <fschmidt@gmail.com>
parents: 663
diff changeset
26 public static Object getObj(int i) {
71f8f5075df8 compile FnDef
Franklin Schmidt <fschmidt@gmail.com>
parents: 663
diff changeset
27 return list.get(i);
652
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
28 }
670
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
29 */
652
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
30
649
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
31 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
32 if( o instanceof String ) {
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 String s = (String)o;
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return s.length();
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 if( o instanceof byte[] ) {
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 byte[] a = (byte[])o;
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 return a.length;
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 if( o instanceof LuanTable ) {
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 LuanTable t = (LuanTable)o;
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 return t.length(luan);
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 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
45 }
649
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
46
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
47 public static Object unm(LuanState luan,Object o) throws LuanException {
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
48 if( o instanceof Number )
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
49 return -((Number)o).doubleValue();
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
50 if( o instanceof LuanTable ) {
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
51 LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o);
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
52 if( fn != null ) {
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
53 return Luan.first(fn.call(luan,new Object[]{o}));
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
54 }
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
55 }
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
56 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
57 }
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
58
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
59 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
60 LuanFunction fn = Luan.getBinHandler(op,o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
61 if( fn != null )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
62 return Luan.first(fn.call(luan,new Object[]{o1,o2}));
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
63 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
64 throw new LuanException("attempt to perform arithmetic on a "+type+" value");
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
65 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
66
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
67 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
68 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
69 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() );
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
70 return arithmetic(luan,"__pow",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
71 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
72
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
73 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
74 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
75 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
76 return arithmetic(luan,"__mul",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
77 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
78
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
79 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
80 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
81 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
82 return arithmetic(luan,"__div",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
83 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
84
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
85 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
86 if( o1 instanceof Number && o2 instanceof Number ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
87 double d1 = ((Number)o1).doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
88 double d2 = ((Number)o2).doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
89 return d1 - Math.floor(d1/d2)*d2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
90 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
91 return arithmetic(luan,"__mod",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
92 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
93
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
94 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
95 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
96 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
97 return arithmetic(luan,"__add",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
98 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
99
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
100 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
101 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
102 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
103 return arithmetic(luan,"__sub",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
104 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
105
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
106 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
107 LuanFunction fn = Luan.getBinHandler("__concat",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
108 if( fn != null )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
109 return Luan.first(fn.call(luan,new Object[]{o1,o2}));
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
110 String s1 = luan.toString(o1);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
111 String s2 = luan.toString(o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
112 return s1 + s2;
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
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
115 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
116 if( o1 == o2 || o1 != null && o1.equals(o2) )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
117 return true;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
118 if( o1 instanceof Number && o2 instanceof Number ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
119 Number n1 = (Number)o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
120 Number n2 = (Number)o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
121 return n1.doubleValue() == n2.doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
122 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
123 if( o1 instanceof byte[] && o2 instanceof byte[] ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
124 byte[] b1 = (byte[])o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
125 byte[] b2 = (byte[])o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
126 return Arrays.equals(b1,b2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
127 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
128 if( !(o1 instanceof LuanTable && o2 instanceof LuanTable) )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
129 return false;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
130 LuanTable t1 = (LuanTable)o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
131 LuanTable t2 = (LuanTable)o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
132 LuanTable mt1 = t1.getMetatable();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
133 LuanTable mt2 = t2.getMetatable();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
134 if( mt1==null || mt2==null )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
135 return false;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
136 Object f = mt1.rawGet("__eq");
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
137 if( f == null || !f.equals(mt2.rawGet("__eq")) )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
138 return false;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
139 LuanFunction fn = Luan.checkFunction(f);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
140 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
141 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
142
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
143 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
144 if( o1 instanceof Number && o2 instanceof Number ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
145 Number n1 = (Number)o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
146 Number n2 = (Number)o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
147 return n1.doubleValue() <= n2.doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
148 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
149 if( o1 instanceof String && o2 instanceof String ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
150 String s1 = (String)o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
151 String s2 = (String)o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
152 return s1.compareTo(s2) <= 0;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
153 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
154 LuanFunction fn = Luan.getBinHandler("__le",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
155 if( fn != null )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
156 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
157 fn = Luan.getBinHandler("__lt",o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
158 if( fn != null )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
159 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
160 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
161 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
162
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
163 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
164 return Luan.isLessThan(luan,o1,o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
165 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
166
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
167 public static boolean cnd(Object o) throws LuanException {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
168 return !(o == null || Boolean.FALSE.equals(o));
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
169 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
170
651
140cc5191b7a start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents: 650
diff changeset
171 public static void nop(Object o) {}
140cc5191b7a start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents: 650
diff changeset
172
670
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
173 /*
652
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
174 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
175 if( obj instanceof Object[] ) {
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
176 Object[] vals = (Object[])obj;
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
177 for( int i=0; i<vars.length; i++ ) {
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
178 Object val = i < vals.length ? vals[i] : null;
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
179 vars[i].set(luan,val);
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
180 }
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
181 } else {
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
182 vars[0].set(luan,obj);
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
183 for( int i=1; i<vars.length; i++ ) {
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
184 vars[i].set(luan,null);
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
185 }
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
186 }
067d9470184d compile SetStmt and ForStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 651
diff changeset
187 }
670
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
188 */
660
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
189 public static void put(LuanState luan,Object t,Object key,Object value) throws LuanException {
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
190 if( t instanceof LuanTable ) {
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
191 LuanTable tbl = (LuanTable)t;
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
192 tbl.put(luan,key,value);
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
193 return;
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
194 }
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
195 if( t != null && luan.hasJava() )
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
196 JavaLuan.__new_index(luan,t,key,value);
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
197 else
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
198 throw new LuanException( "attempt to index a " + Luan.type(t) + " value" );
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
199 }
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
200
662
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
201 public static Object pick(Object o,int i) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
202 if( i < 1 )
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
203 throw new RuntimeException();
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
204 if( !(o instanceof Object[]) )
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
205 return null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
206 Object[] a = (Object[])o;
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
207 return i<a.length ? a[i] : null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
208 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
209
670
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
210 public static Object[] varArgs(Object o,int i) {
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
211 if( !(o instanceof Object[]) )
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
212 return i==0 ? new Object[]{o} : LuanFunction.NOTHING;
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
213 Object[] a = (Object[])o;
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
214 if( i >= a.length )
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
215 return LuanFunction.NOTHING;
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
216 Object[] rtn = new Object[a.length - i];
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
217 System.arraycopy(a,i,rtn,0,rtn.length);
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
218 return rtn;
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
219 }
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
220
654
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
221 public static Object[] concatArgs(Object o1,Object o2) {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
222 if( o1 instanceof Object[] ) {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
223 Object[] a1 = (Object[])o1;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
224 if( o2 instanceof Object[] ) {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
225 Object[] a2 = (Object[])o2;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
226 Object[] rtn = new Object[a1.length+a2.length];
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
227 System.arraycopy(a1,0,rtn,0,a1.length);
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
228 System.arraycopy(a2,0,rtn,a1.length,a2.length);
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
229 return rtn;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
230 } else {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
231 Object[] rtn = new Object[a1.length+1];
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
232 System.arraycopy(a1,0,rtn,0,a1.length);
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
233 rtn[a1.length] = o2;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
234 return rtn;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
235 }
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
236 } else {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
237 if( o2 instanceof Object[] ) {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
238 Object[] a2 = (Object[])o2;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
239 Object[] rtn = new Object[1+a2.length];
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
240 rtn[0] = o1;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
241 System.arraycopy(a2,0,rtn,1,a2.length);
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
242 return rtn;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
243 } else {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
244 Object[] rtn = new Object[2];
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
245 rtn[0] = o1;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
246 rtn[2] = o2;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
247 return rtn;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
248 }
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
249 }
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
250 }
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
251
655
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
252 public static LuanTable table(Object o) {
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
253 LuanTable table = new LuanTable();
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
254 int i = 0;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
255 for( Object fld : Luan.array(o) ) {
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
256 if( fld instanceof TableField ) {
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
257 TableField tblFld = (TableField)fld;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
258 Object key = tblFld.key;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
259 Object value = tblFld.value;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
260 if( key != null && value != null )
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
261 table.rawPut(key,value);
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
262 } else {
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
263 i++;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
264 if( fld != null )
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
265 table.rawPut(i,fld);
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
266 }
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
267 }
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
268 return table;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
269 }
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
270
648
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271 }