comparison src/luan/Luan.java @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents core/src/luan/Luan.java@b620b8e1010f
children 22652f4020fb
comparison
equal deleted inserted replaced
774:3e30cf310e56 775:1a68fc55a80c
1 package luan;
2
3 import java.util.List;
4 import luan.modules.BasicLuan;
5 import luan.impl.LuanCompiler;
6
7
8 public final class Luan {
9
10 public static void main(String[] args) throws LuanException {
11 LuanState luan = new LuanState();
12 LuanFunction standalone = (LuanFunction)BasicLuan.load_file(luan,"classpath:luan/cmd_line.luan");
13 standalone.call(luan,args);
14 }
15
16 public static Object first(Object obj) {
17 if( !(obj instanceof Object[]) )
18 return obj;
19 Object[] a = (Object[])obj;
20 return a.length==0 ? null : a[0];
21 }
22
23 public static Object[] array(Object obj) {
24 return obj instanceof Object[] ? (Object[])obj : new Object[]{obj};
25 }
26
27 public static String type(Object obj) {
28 if( obj == null )
29 return "nil";
30 if( obj instanceof String )
31 return "string";
32 if( obj instanceof Boolean )
33 return "boolean";
34 if( obj instanceof Number )
35 return "number";
36 if( obj instanceof LuanTable )
37 return "table";
38 if( obj instanceof LuanFunction )
39 return "function";
40 if( obj instanceof byte[] )
41 return "binary";
42 return "java";
43 }
44
45 public static String toString(Number n) {
46 if( n instanceof Integer )
47 return n.toString();
48 int i = n.intValue();
49 if( i == n.doubleValue() )
50 return Integer.toString(i);
51 String s = n.toString();
52 int iE = s.indexOf('E');
53 String ending = null;
54 if( iE != -1 ) {
55 ending = s.substring(iE);
56 s = s.substring(0,iE);
57 }
58 if( s.endsWith(".0") )
59 s = s.substring(0,s.length()-2);
60 if( ending != null )
61 s += ending;
62 return s;
63 }
64
65 public static Integer asInteger(Object obj) {
66 if( obj instanceof Integer )
67 return (Integer)obj;
68 if( !(obj instanceof Number) )
69 return null;
70 Number n = (Number)obj;
71 int i = n.intValue();
72 return i==n.doubleValue() ? Integer.valueOf(i) : null;
73 }
74
75 public static String stringEncode(String s) {
76 s = s.replace("\\","\\\\");
77 s = s.replace("\u0007","\\a");
78 s = s.replace("\b","\\b");
79 s = s.replace("\f","\\f");
80 s = s.replace("\n","\\n");
81 s = s.replace("\r","\\r");
82 s = s.replace("\t","\\t");
83 s = s.replace("\u000b","\\v");
84 s = s.replace("\"","\\\"");
85 s = s.replace("\'","\\'");
86 return s;
87 }
88
89
90 // from LuanState
91
92 public static Boolean checkBoolean(Object obj) throws LuanException {
93 if( obj instanceof Boolean )
94 return (Boolean)obj;
95 throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a boolean" );
96 }
97
98 public static String checkString(Object obj) throws LuanException {
99 if( obj instanceof String )
100 return (String)obj;
101 throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a string" );
102 }
103
104 public static LuanFunction checkFunction(Object obj) throws LuanException {
105 if( obj instanceof LuanFunction )
106 return (LuanFunction)obj;
107 throw new LuanException("attempt to call a " + Luan.type(obj) + " value" );
108 }
109
110 public static boolean isLessThan(LuanState luan,Object o1,Object o2) throws LuanException {
111 if( o1 instanceof Number && o2 instanceof Number ) {
112 Number n1 = (Number)o1;
113 Number n2 = (Number)o2;
114 return n1.doubleValue() < n2.doubleValue();
115 }
116 if( o1 instanceof String && o2 instanceof String ) {
117 String s1 = (String)o1;
118 String s2 = (String)o2;
119 return s1.compareTo(s2) < 0;
120 }
121 LuanFunction fn = getBinHandler("__lt",o1,o2);
122 if( fn != null )
123 return checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
124 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
125 }
126
127 public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
128 if( o1 instanceof LuanTable ) {
129 LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
130 if( f1 != null )
131 return f1;
132 }
133 return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
134 }
135
136 public static LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
137 Object f = t.getHandler(op);
138 if( f == null )
139 return null;
140 return checkFunction(f);
141 }
142
143 public static LuanFunction load(String text,String sourceName,LuanTable env)
144 throws LuanException
145 {
146 return LuanCompiler.compile(text,sourceName,env);
147 }
148
149 public static LuanFunction load(String text,String sourceName)
150 throws LuanException
151 {
152 return load(text,sourceName,null);
153 }
154
155
156 private Luan() {} // never
157 }