diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/Luan.java	Fri Aug 26 14:36:40 2016 -0600
@@ -0,0 +1,157 @@
+package luan;
+
+import java.util.List;
+import luan.modules.BasicLuan;
+import luan.impl.LuanCompiler;
+
+
+public final class Luan {
+
+	public static void main(String[] args) throws LuanException {
+		LuanState luan = new LuanState();
+		LuanFunction standalone = (LuanFunction)BasicLuan.load_file(luan,"classpath:luan/cmd_line.luan");
+		standalone.call(luan,args);
+	}
+
+	public static Object first(Object obj) {
+		if( !(obj instanceof Object[]) )
+			return obj;
+		Object[] a = (Object[])obj;
+		return a.length==0 ? null : a[0];
+	}
+
+	public static Object[] array(Object obj) {
+		return obj instanceof Object[] ? (Object[])obj : new Object[]{obj};
+	}
+
+	public static String type(Object obj) {
+		if( obj == null )
+			return "nil";
+		if( obj instanceof String )
+			return "string";
+		if( obj instanceof Boolean )
+			return "boolean";
+		if( obj instanceof Number )
+			return "number";
+		if( obj instanceof LuanTable )
+			return "table";
+		if( obj instanceof LuanFunction )
+			return "function";
+		if( obj instanceof byte[] )
+			return "binary";
+		return "java";
+	}
+
+	public static String toString(Number n) {
+		if( n instanceof Integer )
+			return n.toString();
+		int i = n.intValue();
+		if( i == n.doubleValue() )
+			return Integer.toString(i);
+		String s = n.toString();
+		int iE = s.indexOf('E');
+		String ending  = null;
+		if( iE != -1 ) {
+			ending = s.substring(iE);
+			s = s.substring(0,iE);
+		}
+		if( s.endsWith(".0") )
+			s = s.substring(0,s.length()-2);
+		if( ending != null )
+			s += ending;
+		return s;
+	}
+
+	public static Integer asInteger(Object obj) {
+		if( obj instanceof Integer )
+			return (Integer)obj;
+		if( !(obj instanceof Number) )
+			return null;
+		Number n = (Number)obj;
+		int i = n.intValue();
+		return i==n.doubleValue() ? Integer.valueOf(i) : null;
+	}
+
+	public static String stringEncode(String s) {
+		s = s.replace("\\","\\\\");
+		s = s.replace("\u0007","\\a");
+		s = s.replace("\b","\\b");
+		s = s.replace("\f","\\f");
+		s = s.replace("\n","\\n");
+		s = s.replace("\r","\\r");
+		s = s.replace("\t","\\t");
+		s = s.replace("\u000b","\\v");
+		s = s.replace("\"","\\\"");
+		s = s.replace("\'","\\'");
+		return s;
+	}
+
+
+	// from LuanState
+
+	public static Boolean checkBoolean(Object obj) throws LuanException {
+		if( obj instanceof Boolean )
+			return (Boolean)obj;
+		throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a boolean" );
+	}
+
+	public static String checkString(Object obj) throws LuanException {
+		if( obj instanceof String )
+			return (String)obj;
+		throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a string" );
+	}
+
+	public static LuanFunction checkFunction(Object obj) throws LuanException {
+		if( obj instanceof LuanFunction )
+			return (LuanFunction)obj;
+		throw new LuanException("attempt to call a " + Luan.type(obj) + " value" );
+	}
+
+	public static boolean isLessThan(LuanState luan,Object o1,Object o2) throws LuanException {
+		if( o1 instanceof Number && o2 instanceof Number ) {
+			Number n1 = (Number)o1;
+			Number n2 = (Number)o2;
+			return n1.doubleValue() < n2.doubleValue();
+		}
+		if( o1 instanceof String && o2 instanceof String ) {
+			String s1 = (String)o1;
+			String s2 = (String)o2;
+			return s1.compareTo(s2) < 0;
+		}
+		LuanFunction fn = getBinHandler("__lt",o1,o2);
+		if( fn != null )
+			return checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
+		throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
+	}
+
+	public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
+		if( o1 instanceof LuanTable ) {
+			LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
+			if( f1 != null )
+				return f1;
+		}
+		return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
+	}
+
+	public static LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
+		Object f = t.getHandler(op);
+		if( f == null )
+			return null;
+		return checkFunction(f);
+	}
+
+	public static LuanFunction load(String text,String sourceName,LuanTable env)
+		throws LuanException
+	{
+		return LuanCompiler.compile(text,sourceName,env);
+	}
+
+	public static LuanFunction load(String text,String sourceName)
+		throws LuanException
+	{
+		return load(text,sourceName,null);
+	}
+
+
+	private Luan() {}  // never
+}