Mercurial Hosting > luan
comparison src/luan/impl/LuanImpl.java @ 1334:c88b486a9511
make some Luan methods static
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Feb 2019 22:53:57 -0700 |
parents | 25746915a241 |
children | e0cf0d108a77 |
comparison
equal
deleted
inserted
replaced
1333:25746915a241 | 1334:c88b486a9511 |
---|---|
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,new Object[]{o})); | 38 return Luan.first(fn.call(luan,new Object[]{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,new Object[]{o1,o2})); | 47 return Luan.first(fn.call(luan,new Object[]{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,new Object[]{o1,o2})); | 94 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
95 String s1 = luan.toString(o1); | 95 String s1 = Luan.luanToString(o1); |
96 String s2 = luan.toString(o2); | 96 String s2 = Luan.luanToString(o2); |
97 return s1 + s2; | 97 return s1 + s2; |
98 } | 98 } |
99 | 99 |
100 public static boolean eq(Luan luan,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) ) |
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,new Object[]{o1,o2})) ); | 141 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{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,new Object[]{o2,o1})) ); | 144 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{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 |