annotate src/luan/impl/LuanImpl.java @ 1802:ca98dee04e08 default tip

add Parsers.json_null
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 21 Apr 2024 21:25:15 -0600
parents 9ef19f5ea973
children
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;
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import luan.LuanTable;
649
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
8 import luan.LuanFunction;
648
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import luan.LuanException;
660
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
10 import luan.modules.JavaLuan;
648
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
654
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
13 public final class LuanImpl {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
14 private LuanImpl() {} // never
648
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
16 public static int len(Luan luan,Object o) throws LuanException {
648
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 if( o instanceof String ) {
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 String s = (String)o;
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 return s.length();
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 if( o instanceof byte[] ) {
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 byte[] a = (byte[])o;
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 return a.length;
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 if( o instanceof LuanTable ) {
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 LuanTable t = (LuanTable)o;
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
27 return t.length(luan);
648
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 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
30 }
649
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
31
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
32 public static Object unm(Luan luan,Object o) throws LuanException {
649
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
33 if( o instanceof Number )
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
34 return -((Number)o).doubleValue();
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
35 if( o instanceof LuanTable ) {
1578
c922446f53aa immutable threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1566
diff changeset
36 LuanFunction fn = luan.getHandlerFunction("__unm",(LuanTable)o);
649
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
37 if( fn != null ) {
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
38 return Luan.first(fn.call(luan,o));
649
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
39 }
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
40 }
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
41 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
42 }
37f0cf43f191 UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents: 648
diff changeset
43
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
44 private static Object arithmetic(Luan luan,String op,Object o1,Object o2) throws LuanException {
1578
c922446f53aa immutable threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1566
diff changeset
45 LuanFunction fn = luan.getBinHandler(op,o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
46 if( fn != null )
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
47 return Luan.first(fn.call(luan,o1,o2));
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
48 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
49 throw new LuanException("attempt to perform arithmetic on a "+type+" value");
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
50 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
51
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
52 public static Object pow(Luan luan,Object o1,Object o2) throws LuanException {
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
53 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
54 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() );
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
55 return arithmetic(luan,"__pow",o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
56 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
57
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
58 public static Object mul(Luan luan,Object o1,Object o2) throws LuanException {
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
59 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
60 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue();
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
61 return arithmetic(luan,"__mul",o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
62 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
63
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
64 public static Object div(Luan luan,Object o1,Object o2) throws LuanException {
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
65 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
66 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue();
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
67 return arithmetic(luan,"__div",o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
68 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
69
1680
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
70 public static Object idiv(Luan luan,Object o1,Object o2) throws LuanException {
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
71 if( o1 instanceof Number && o2 instanceof Number ) {
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
72 double d1 = ((Number)o1).doubleValue();
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
73 double d2 = ((Number)o2).doubleValue();
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
74 return Math.floor(d1/d2);
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
75 }
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
76 return arithmetic(luan,"__idiv",o1,o2);
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
77 }
9ef19f5ea973 add // operator
Franklin Schmidt <fschmidt@gmail.com>
parents: 1578
diff changeset
78
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
79 public static Object mod(Luan luan,Object o1,Object o2) throws LuanException {
650
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 double d1 = ((Number)o1).doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
82 double d2 = ((Number)o2).doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
83 return d1 - Math.floor(d1/d2)*d2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
84 }
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
85 return arithmetic(luan,"__mod",o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
86 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
87
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
88 public static Object add(Luan luan,Object o1,Object o2) throws LuanException {
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
89 if( o1 instanceof Number && o2 instanceof Number )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
90 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue();
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
91 return arithmetic(luan,"__add",o1,o2);
650
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
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
94 public static Object sub(Luan luan,Object o1,Object o2) throws LuanException {
650
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();
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
97 return arithmetic(luan,"__sub",o1,o2);
650
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
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
100 public static Object concat(Luan luan,Object o1,Object o2) throws LuanException {
1578
c922446f53aa immutable threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1566
diff changeset
101 LuanFunction fn = luan.getBinHandler("__concat",o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
102 if( fn != null )
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
103 return Luan.first(fn.call(luan,o1,o2));
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
104 String s1 = luan.luanToString(o1);
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
105 String s2 = luan.luanToString(o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
106 return s1 + s2;
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
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
109 public static boolean eq(Luan luan,Object o1,Object o2) throws LuanException {
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
110 if( o1 == o2 || o1 != null && o1.equals(o2) )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
111 return true;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
112 if( o1 instanceof Number && o2 instanceof Number ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
113 Number n1 = (Number)o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
114 Number n2 = (Number)o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
115 return n1.doubleValue() == n2.doubleValue();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
116 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
117 if( o1 instanceof byte[] && o2 instanceof byte[] ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
118 byte[] b1 = (byte[])o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
119 byte[] b2 = (byte[])o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
120 return Arrays.equals(b1,b2);
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
121 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
122 if( !(o1 instanceof LuanTable && o2 instanceof LuanTable) )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
123 return false;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
124 LuanTable t1 = (LuanTable)o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
125 LuanTable t2 = (LuanTable)o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
126 LuanTable mt1 = t1.getMetatable();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
127 LuanTable mt2 = t2.getMetatable();
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
128 if( mt1==null || mt2==null )
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 Object f = mt1.rawGet("__eq");
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
131 if( f == null || !f.equals(mt2.rawGet("__eq")) )
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
132 return false;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
133 LuanFunction fn = Luan.checkFunction(f);
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
134 return Luan.checkBoolean( Luan.first(fn.call(luan,o1,o2)) );
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
135 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
136
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
137 public static boolean le(Luan luan,Object o1,Object o2) throws LuanException {
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
138 if( o1 instanceof Number && o2 instanceof Number ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
139 Number n1 = (Number)o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
140 Number n2 = (Number)o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
141 return n1.doubleValue() <= n2.doubleValue();
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 if( o1 instanceof String && o2 instanceof String ) {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
144 String s1 = (String)o1;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
145 String s2 = (String)o2;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
146 return s1.compareTo(s2) <= 0;
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
147 }
1578
c922446f53aa immutable threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1566
diff changeset
148 LuanFunction fn = luan.getBinHandler("__le",o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
149 if( fn != null )
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
150 return Luan.checkBoolean( Luan.first(fn.call(luan,o1,o2)) );
1578
c922446f53aa immutable threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1566
diff changeset
151 fn = luan.getBinHandler("__lt",o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
152 if( fn != null )
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
153 return !Luan.checkBoolean( Luan.first(fn.call(luan,o2,o1)) );
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
154 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
155 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
156
1563
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
157 public static boolean lt(Luan luan,Object o1,Object o2) throws LuanException {
8fbcc4747091 remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1562
diff changeset
158 return luan.isLessThan(o1,o2);
650
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
159 }
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
160
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
161 public static boolean cnd(Object o) throws LuanException {
d658eab7bf4c finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents: 649
diff changeset
162 return !(o == null || Boolean.FALSE.equals(o));
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
651
140cc5191b7a start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents: 650
diff changeset
165 public static void nop(Object o) {}
140cc5191b7a start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents: 650
diff changeset
166
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
167 public static void put(Luan luan,Object t,Object key,Object value) throws LuanException {
660
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
168 if( t instanceof LuanTable ) {
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
169 LuanTable tbl = (LuanTable)t;
1562
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1542
diff changeset
170 tbl.put(luan,key,value);
660
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
171 return;
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
172 }
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
173 if( t != null && luan.peek().javaOk )
660
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
174 JavaLuan.__new_index(luan,t,key,value);
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
175 else
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
176 throw new LuanException( "attempt to index a " + Luan.type(t) + " value" );
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
177 }
e064377994b2 compile table put
Franklin Schmidt <fschmidt@gmail.com>
parents: 655
diff changeset
178
662
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
179 public static Object pick(Object o,int i) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
180 if( i < 1 )
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
181 throw new RuntimeException();
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
182 if( !(o instanceof Object[]) )
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
183 return null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
184 Object[] a = (Object[])o;
1089
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
185 return pick(a,i);
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
186 }
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
187
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
188 public static Object pick(Object[] a,int i) {
662
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
189 return i<a.length ? a[i] : null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
190 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 660
diff changeset
191
1089
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
192 public static void noMore(final Object[] a,final int n) throws LuanException {
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
193 if( a.length > n ) {
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
194 for( int i=n; i<a.length; i++ ) {
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
195 if( a[i] != null )
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
196 throw new LuanException("too many arguments");
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
197 }
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
198 }
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
199 }
894786a03d22 check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
200
674
2994e46f62b7 some optimization
Franklin Schmidt <fschmidt@gmail.com>
parents: 672
diff changeset
201 public static Object[] varArgs(Object[] a,int i) {
670
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
202 if( i >= a.length )
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
203 return LuanFunction.NOTHING;
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
204 Object[] rtn = new Object[a.length - i];
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
205 System.arraycopy(a,i,rtn,0,rtn.length);
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
206 return rtn;
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
207 }
58ebfec6178b all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents: 664
diff changeset
208
654
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
209 public static Object[] concatArgs(Object o1,Object o2) {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
210 if( o1 instanceof Object[] ) {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
211 Object[] a1 = (Object[])o1;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
212 if( o2 instanceof Object[] ) {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
213 Object[] a2 = (Object[])o2;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
214 Object[] rtn = new Object[a1.length+a2.length];
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
215 System.arraycopy(a1,0,rtn,0,a1.length);
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
216 System.arraycopy(a2,0,rtn,a1.length,a2.length);
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
217 return rtn;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
218 } else {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
219 Object[] rtn = new Object[a1.length+1];
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
220 System.arraycopy(a1,0,rtn,0,a1.length);
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
221 rtn[a1.length] = o2;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
222 return rtn;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
223 }
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
224 } else {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
225 if( o2 instanceof Object[] ) {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
226 Object[] a2 = (Object[])o2;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
227 Object[] rtn = new Object[1+a2.length];
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
228 rtn[0] = o1;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
229 System.arraycopy(a2,0,rtn,1,a2.length);
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
230 return rtn;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
231 } else {
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
232 Object[] rtn = new Object[2];
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
233 rtn[0] = o1;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
234 rtn[2] = o2;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
235 return rtn;
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
236 }
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
237 }
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
238 }
ea7dbd2dfa65 compile TemplateStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 652
diff changeset
239
1562
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1542
diff changeset
240 public static LuanTable table(Object[] a) throws LuanException {
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1542
diff changeset
241 LuanTable table = new LuanTable();
655
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
242 int i = 0;
674
2994e46f62b7 some optimization
Franklin Schmidt <fschmidt@gmail.com>
parents: 672
diff changeset
243 for( Object fld : a ) {
655
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
244 if( fld instanceof TableField ) {
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
245 TableField tblFld = (TableField)fld;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
246 Object key = tblFld.key;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
247 Object value = tblFld.value;
1566
364859d29ff5 handle nil keys
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
248 if( table.rawPut(key,value) != null )
364859d29ff5 handle nil keys
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
249 throw new LuanException("duplicate key in table constructor: "+key);
655
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
250 } else {
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
251 i++;
1542
d4407e8de707 disallow duplicates in table constructor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1520
diff changeset
252 if( table.rawPut(i,fld) != null )
d4407e8de707 disallow duplicates in table constructor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1520
diff changeset
253 throw new LuanException("duplicate key in table constructor: "+i);
655
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
254 }
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
255 }
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
256 return table;
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
257 }
e2be71451d04 compile TableExpr
Franklin Schmidt <fschmidt@gmail.com>
parents: 654
diff changeset
258
674
2994e46f62b7 some optimization
Franklin Schmidt <fschmidt@gmail.com>
parents: 672
diff changeset
259 public static Object first(Object[] a) {
2994e46f62b7 some optimization
Franklin Schmidt <fschmidt@gmail.com>
parents: 672
diff changeset
260 return a.length==0 ? null : a[0];
2994e46f62b7 some optimization
Franklin Schmidt <fschmidt@gmail.com>
parents: 672
diff changeset
261 }
2994e46f62b7 some optimization
Franklin Schmidt <fschmidt@gmail.com>
parents: 672
diff changeset
262
684
41f791e4206d bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 674
diff changeset
263 public static String strconcat(String... a) {
41f791e4206d bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 674
diff changeset
264 StringBuilder sb = new StringBuilder();
41f791e4206d bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 674
diff changeset
265 for( String s : a ) {
41f791e4206d bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 674
diff changeset
266 sb.append(s);
41f791e4206d bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 674
diff changeset
267 }
41f791e4206d bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 674
diff changeset
268 return sb.toString();
41f791e4206d bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 674
diff changeset
269 }
41f791e4206d bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 674
diff changeset
270
1520
d9a5405a3102 try statement
Franklin Schmidt <fschmidt@gmail.com>
parents: 1464
diff changeset
271 public static void nopTry() throws LuanException {}
d9a5405a3102 try statement
Franklin Schmidt <fschmidt@gmail.com>
parents: 1464
diff changeset
272
648
e387e4021afe start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
273 }