view core/src/luan/impl/$Luan.java @ 649:37f0cf43f191

UnaryExpr now compiled
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Mar 2016 22:42:27 -0600
parents e387e4021afe
children d658eab7bf4c
line wrap: on
line source

package luan.impl;

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);
	}


	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 not(Object o) throws LuanException {
		return !Luan.checkBoolean(o);
	}

}