view core/src/luan/impl/$Luan.java @ 652:067d9470184d

compile SetStmt and ForStmt
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 Apr 2016 23:26:06 -0600
parents 140cc5191b7a
children
line wrap: on
line source

package luan.impl;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import luan.Luan;
import luan.LuanState;
import luan.LuanTable;
import luan.LuanFunction;
import luan.LuanException;


public final class $Luan {
	private $Luan() {}  // never


	private static List<Expressions> listExpressions = new ArrayList<Expressions>();

	static int addExpressions(Expressions exp) {
		int i = listExpressions.size();
		listExpressions.add(exp);
		return i;
	}

	public static Expressions getExpressions(int i) {
		return listExpressions.get(i);
	}


	private static List<Stmt> listStmt = new ArrayList<Stmt>();

	static int addStmt(Stmt stmt) {
		int i = listStmt.size();
		listStmt.add(stmt);
		return i;
	}

	public static Stmt getStmt(int i) {
		return listStmt.get(i);
	}


	private static List<Settable> listSettable = new ArrayList<Settable>();

	static int addSettable(Settable settable) {
		int i = listSettable.size();
		listSettable.add(settable);
		return i;
	}

	public static Settable getSettable(int i) {
		return listSettable.get(i);
	}


	public static Object first(Object obj) {
		return Luan.first(obj);
	}

	public static int len(LuanState luan,Object o) throws LuanException {
		if( o instanceof String ) {
			String s = (String)o;
			return s.length();
		}
		if( o instanceof byte[] ) {
			byte[] a = (byte[])o;
			return a.length;
		}
		if( o instanceof LuanTable ) {
			LuanTable t = (LuanTable)o;
			return t.length(luan);
		}
		throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" );
	}

	public static Object unm(LuanState luan,Object o) throws LuanException {
		if( o instanceof Number )
			return -((Number)o).doubleValue();
		if( o instanceof LuanTable ) {
			LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o);
			if( fn != null ) {
				return Luan.first(fn.call(luan,new Object[]{o}));
			}
		}
		throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value");
	}

	public static boolean checkBoolean(Object o) throws LuanException {
		return Luan.checkBoolean(o);
	}

	public static LuanFunction checkFunction(Object o) throws LuanException {
		return Luan.checkFunction(o);
	}

	public static boolean not(Object o) throws LuanException {
		return !checkBoolean(o);
	}

	private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException {
		LuanFunction fn = Luan.getBinHandler(op,o1,o2);
		if( fn != null )
			return Luan.first(fn.call(luan,new Object[]{o1,o2}));
		String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2);
		throw new LuanException("attempt to perform arithmetic on a "+type+" value");
	}

	public static Object pow(LuanState luan,Object o1,Object o2) throws LuanException {
		if( o1 instanceof Number && o2 instanceof Number )
			return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() );
		return arithmetic(luan,"__pow",o1,o2);
	}

	public static Object mul(LuanState luan,Object o1,Object o2) throws LuanException {
		if( o1 instanceof Number && o2 instanceof Number )
			return ((Number)o1).doubleValue() * ((Number)o2).doubleValue();
		return arithmetic(luan,"__mul",o1,o2);
	}

	public static Object div(LuanState luan,Object o1,Object o2) throws LuanException {
		if( o1 instanceof Number && o2 instanceof Number )
			return ((Number)o1).doubleValue() / ((Number)o2).doubleValue();
		return arithmetic(luan,"__div",o1,o2);
	}

	public static Object mod(LuanState luan,Object o1,Object o2) throws LuanException {
		if( o1 instanceof Number && o2 instanceof Number ) {
			double d1 = ((Number)o1).doubleValue();
			double d2 = ((Number)o2).doubleValue();
			return d1 - Math.floor(d1/d2)*d2;
		}
		return arithmetic(luan,"__mod",o1,o2);
	}

	public static Object add(LuanState luan,Object o1,Object o2) throws LuanException {
		if( o1 instanceof Number && o2 instanceof Number )
			return ((Number)o1).doubleValue() + ((Number)o2).doubleValue();
		return arithmetic(luan,"__add",o1,o2);
	}

	public static Object sub(LuanState luan,Object o1,Object o2) throws LuanException {
		if( o1 instanceof Number && o2 instanceof Number )
			return ((Number)o1).doubleValue() - ((Number)o2).doubleValue();
		return arithmetic(luan,"__sub",o1,o2);
	}

	public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException {
		LuanFunction fn = Luan.getBinHandler("__concat",o1,o2);
		if( fn != null )
			return Luan.first(fn.call(luan,new Object[]{o1,o2}));
		String s1 = luan.toString(o1);
		String s2 = luan.toString(o2);
		return s1 + s2;
	}

	public static boolean eq(LuanState luan,Object o1,Object o2) throws LuanException {
		if( o1 == o2 || o1 != null && o1.equals(o2) )
			return true;
		if( o1 instanceof Number && o2 instanceof Number ) {
			Number n1 = (Number)o1;
			Number n2 = (Number)o2;
			return n1.doubleValue() == n2.doubleValue();
		}
		if( o1 instanceof byte[] && o2 instanceof byte[] ) {
			byte[] b1 = (byte[])o1;
			byte[] b2 = (byte[])o2;
			return Arrays.equals(b1,b2);
		}
		if( !(o1 instanceof LuanTable && o2 instanceof LuanTable) )
			return false;
		LuanTable t1 = (LuanTable)o1;
		LuanTable t2 = (LuanTable)o2;
		LuanTable mt1 = t1.getMetatable();
		LuanTable mt2 = t2.getMetatable();
		if( mt1==null || mt2==null )
			return false;
		Object f = mt1.rawGet("__eq");
		if( f == null || !f.equals(mt2.rawGet("__eq")) )
			return false;
		LuanFunction fn = Luan.checkFunction(f);
		return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
	}

	public static boolean le(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 = Luan.getBinHandler("__le",o1,o2);
		if( fn != null )
			return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
		fn = Luan.getBinHandler("__lt",o1,o2);
		if( fn != null )
			return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) );
		throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
	}

	public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException {
		return Luan.isLessThan(luan,o1,o2);
	}

	public static boolean cnd(Object o) throws LuanException {
		return !(o == null || Boolean.FALSE.equals(o));
	}

	public static void nop(Object o) {}


	public static void set(LuanStateImpl luan,Settable[] vars,Object obj) throws LuanException {
		if( obj instanceof Object[] ) {
			Object[] vals = (Object[])obj;
			for( int i=0; i<vars.length; i++ ) {
				Object val = i < vals.length ? vals[i] : null;
				vars[i].set(luan,val);
			}
		} else {
			vars[0].set(luan,obj);
			for( int i=1; i<vars.length; i++ ) {
				vars[i].set(luan,null);
			}
		}
	}

}