comparison src/luan/impl/LuanImpl.java @ 1578:c922446f53aa

immutable threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 08 Feb 2021 14:16:19 -0700
parents 364859d29ff5
children 9ef19f5ea973
comparison
equal deleted inserted replaced
1577:60e5c324adf9 1578:c922446f53aa
31 31
32 public static Object unm(Luan luan,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(luan,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(Luan luan,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(luan,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 }
87 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); 87 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue();
88 return arithmetic(luan,"__sub",o1,o2); 88 return arithmetic(luan,"__sub",o1,o2);
89 } 89 }
90 90
91 public static Object concat(Luan luan,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(luan,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;
134 if( o1 instanceof String && o2 instanceof String ) { 134 if( o1 instanceof String && o2 instanceof String ) {
135 String s1 = (String)o1; 135 String s1 = (String)o1;
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(luan,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(luan,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