Mercurial Hosting > luan
comparison src/luan/impl/LuanImpl.java @ 1563:8fbcc4747091
remove LuanFunction.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 09 Nov 2020 01:37:57 -0700 |
parents | b89212fd04b5 |
children | 364859d29ff5 |
comparison
equal
deleted
inserted
replaced
1562:b89212fd04b5 | 1563:8fbcc4747091 |
---|---|
11 | 11 |
12 | 12 |
13 public final class LuanImpl { | 13 public final class LuanImpl { |
14 private LuanImpl() {} // never | 14 private LuanImpl() {} // never |
15 | 15 |
16 public static int len(Object o) throws LuanException { | 16 public static int len(Luan luan,Object o) throws LuanException { |
17 if( o instanceof String ) { | 17 if( o instanceof String ) { |
18 String s = (String)o; | 18 String s = (String)o; |
19 return s.length(); | 19 return s.length(); |
20 } | 20 } |
21 if( o instanceof byte[] ) { | 21 if( o instanceof byte[] ) { |
22 byte[] a = (byte[])o; | 22 byte[] a = (byte[])o; |
23 return a.length; | 23 return a.length; |
24 } | 24 } |
25 if( o instanceof LuanTable ) { | 25 if( o instanceof LuanTable ) { |
26 LuanTable t = (LuanTable)o; | 26 LuanTable t = (LuanTable)o; |
27 return t.length(); | 27 return t.length(luan); |
28 } | 28 } |
29 throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" ); | 29 throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" ); |
30 } | 30 } |
31 | 31 |
32 public static Object unm(Object o) throws LuanException { | 32 public static Object unm(Luan luan,Object o) throws LuanException { |
33 if( o instanceof Number ) | 33 if( o instanceof Number ) |
34 return -((Number)o).doubleValue(); | 34 return -((Number)o).doubleValue(); |
35 if( o instanceof LuanTable ) { | 35 if( o instanceof LuanTable ) { |
36 LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o); | 36 LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o); |
37 if( fn != null ) { | 37 if( fn != null ) { |
38 return Luan.first(fn.call(o)); | 38 return Luan.first(fn.call(luan,o)); |
39 } | 39 } |
40 } | 40 } |
41 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); | 41 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); |
42 } | 42 } |
43 | 43 |
44 private static Object arithmetic(String op,Object o1,Object o2) throws LuanException { | 44 private static Object arithmetic(Luan luan,String op,Object o1,Object o2) throws LuanException { |
45 LuanFunction fn = Luan.getBinHandler(op,o1,o2); | 45 LuanFunction fn = Luan.getBinHandler(op,o1,o2); |
46 if( fn != null ) | 46 if( fn != null ) |
47 return Luan.first(fn.call(o1,o2)); | 47 return Luan.first(fn.call(luan,o1,o2)); |
48 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); | 48 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); |
49 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); | 49 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); |
50 } | 50 } |
51 | 51 |
52 public static Object pow(Object o1,Object o2) throws LuanException { | 52 public static Object pow(Luan luan,Object o1,Object o2) throws LuanException { |
53 if( o1 instanceof Number && o2 instanceof Number ) | 53 if( o1 instanceof Number && o2 instanceof Number ) |
54 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); | 54 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); |
55 return arithmetic("__pow",o1,o2); | 55 return arithmetic(luan,"__pow",o1,o2); |
56 } | 56 } |
57 | 57 |
58 public static Object mul(Object o1,Object o2) throws LuanException { | 58 public static Object mul(Luan luan,Object o1,Object o2) throws LuanException { |
59 if( o1 instanceof Number && o2 instanceof Number ) | 59 if( o1 instanceof Number && o2 instanceof Number ) |
60 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue(); | 60 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue(); |
61 return arithmetic("__mul",o1,o2); | 61 return arithmetic(luan,"__mul",o1,o2); |
62 } | 62 } |
63 | 63 |
64 public static Object div(Object o1,Object o2) throws LuanException { | 64 public static Object div(Luan luan,Object o1,Object o2) throws LuanException { |
65 if( o1 instanceof Number && o2 instanceof Number ) | 65 if( o1 instanceof Number && o2 instanceof Number ) |
66 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue(); | 66 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue(); |
67 return arithmetic("__div",o1,o2); | 67 return arithmetic(luan,"__div",o1,o2); |
68 } | 68 } |
69 | 69 |
70 public static Object mod(Object o1,Object o2) throws LuanException { | 70 public static Object mod(Luan luan,Object o1,Object o2) throws LuanException { |
71 if( o1 instanceof Number && o2 instanceof Number ) { | 71 if( o1 instanceof Number && o2 instanceof Number ) { |
72 double d1 = ((Number)o1).doubleValue(); | 72 double d1 = ((Number)o1).doubleValue(); |
73 double d2 = ((Number)o2).doubleValue(); | 73 double d2 = ((Number)o2).doubleValue(); |
74 return d1 - Math.floor(d1/d2)*d2; | 74 return d1 - Math.floor(d1/d2)*d2; |
75 } | 75 } |
76 return arithmetic("__mod",o1,o2); | 76 return arithmetic(luan,"__mod",o1,o2); |
77 } | 77 } |
78 | 78 |
79 public static Object add(Object o1,Object o2) throws LuanException { | 79 public static Object add(Luan luan,Object o1,Object o2) throws LuanException { |
80 if( o1 instanceof Number && o2 instanceof Number ) | 80 if( o1 instanceof Number && o2 instanceof Number ) |
81 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); | 81 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); |
82 return arithmetic("__add",o1,o2); | 82 return arithmetic(luan,"__add",o1,o2); |
83 } | 83 } |
84 | 84 |
85 public static Object sub(Object o1,Object o2) throws LuanException { | 85 public static Object sub(Luan luan,Object o1,Object o2) throws LuanException { |
86 if( o1 instanceof Number && o2 instanceof Number ) | 86 if( o1 instanceof Number && o2 instanceof Number ) |
87 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); | 87 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); |
88 return arithmetic("__sub",o1,o2); | 88 return arithmetic(luan,"__sub",o1,o2); |
89 } | 89 } |
90 | 90 |
91 public static Object concat(Object o1,Object o2) throws LuanException { | 91 public static Object concat(Luan luan,Object o1,Object o2) throws LuanException { |
92 LuanFunction fn = Luan.getBinHandler("__concat",o1,o2); | 92 LuanFunction fn = Luan.getBinHandler("__concat",o1,o2); |
93 if( fn != null ) | 93 if( fn != null ) |
94 return Luan.first(fn.call(o1,o2)); | 94 return Luan.first(fn.call(luan,o1,o2)); |
95 String s1 = Luan.luanToString(o1); | 95 String s1 = luan.luanToString(o1); |
96 String s2 = Luan.luanToString(o2); | 96 String s2 = luan.luanToString(o2); |
97 return s1 + s2; | 97 return s1 + s2; |
98 } | 98 } |
99 | 99 |
100 public static boolean eq(Object o1,Object o2) throws LuanException { | 100 public static boolean eq(Luan luan,Object o1,Object o2) throws LuanException { |
101 if( o1 == o2 || o1 != null && o1.equals(o2) ) | 101 if( o1 == o2 || o1 != null && o1.equals(o2) ) |
102 return true; | 102 return true; |
103 if( o1 instanceof Number && o2 instanceof Number ) { | 103 if( o1 instanceof Number && o2 instanceof Number ) { |
104 Number n1 = (Number)o1; | 104 Number n1 = (Number)o1; |
105 Number n2 = (Number)o2; | 105 Number n2 = (Number)o2; |
120 return false; | 120 return false; |
121 Object f = mt1.rawGet("__eq"); | 121 Object f = mt1.rawGet("__eq"); |
122 if( f == null || !f.equals(mt2.rawGet("__eq")) ) | 122 if( f == null || !f.equals(mt2.rawGet("__eq")) ) |
123 return false; | 123 return false; |
124 LuanFunction fn = Luan.checkFunction(f); | 124 LuanFunction fn = Luan.checkFunction(f); |
125 return Luan.checkBoolean( Luan.first(fn.call(o1,o2)) ); | 125 return Luan.checkBoolean( Luan.first(fn.call(luan,o1,o2)) ); |
126 } | 126 } |
127 | 127 |
128 public static boolean le(Object o1,Object o2) throws LuanException { | 128 public static boolean le(Luan luan,Object o1,Object o2) throws LuanException { |
129 if( o1 instanceof Number && o2 instanceof Number ) { | 129 if( o1 instanceof Number && o2 instanceof Number ) { |
130 Number n1 = (Number)o1; | 130 Number n1 = (Number)o1; |
131 Number n2 = (Number)o2; | 131 Number n2 = (Number)o2; |
132 return n1.doubleValue() <= n2.doubleValue(); | 132 return n1.doubleValue() <= n2.doubleValue(); |
133 } | 133 } |
136 String s2 = (String)o2; | 136 String s2 = (String)o2; |
137 return s1.compareTo(s2) <= 0; | 137 return s1.compareTo(s2) <= 0; |
138 } | 138 } |
139 LuanFunction fn = Luan.getBinHandler("__le",o1,o2); | 139 LuanFunction fn = Luan.getBinHandler("__le",o1,o2); |
140 if( fn != null ) | 140 if( fn != null ) |
141 return Luan.checkBoolean( Luan.first(fn.call(o1,o2)) ); | 141 return Luan.checkBoolean( Luan.first(fn.call(luan,o1,o2)) ); |
142 fn = Luan.getBinHandler("__lt",o1,o2); | 142 fn = Luan.getBinHandler("__lt",o1,o2); |
143 if( fn != null ) | 143 if( fn != null ) |
144 return !Luan.checkBoolean( Luan.first(fn.call(o2,o1)) ); | 144 return !Luan.checkBoolean( Luan.first(fn.call(luan,o2,o1)) ); |
145 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); | 145 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); |
146 } | 146 } |
147 | 147 |
148 public static boolean lt(Object o1,Object o2) throws LuanException { | 148 public static boolean lt(Luan luan,Object o1,Object o2) throws LuanException { |
149 return Luan.isLessThan(o1,o2); | 149 return luan.isLessThan(o1,o2); |
150 } | 150 } |
151 | 151 |
152 public static boolean cnd(Object o) throws LuanException { | 152 public static boolean cnd(Object o) throws LuanException { |
153 return !(o == null || Boolean.FALSE.equals(o)); | 153 return !(o == null || Boolean.FALSE.equals(o)); |
154 } | 154 } |