Initial draft of interpreter. Lua compiled "chunks" can be unmarshalled. Approximately half of bytecodes implemented in some form or another.

This commit is contained in:
James Roseborough
2007-06-08 05:11:37 +00:00
commit 70dfc20f57
31 changed files with 1368 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package lua.value;
public final class LBoolean extends LValue {
public static final LBoolean TRUE = new LBoolean("true",true);
public static final LBoolean FALSE = new LBoolean("false",false);
private final String m_name;
private final boolean m_value;
private LBoolean( String name, boolean value ) {
this.m_name = name;
this.m_value = value;
}
public final String luaAsString() {
return m_name;
}
public final boolean luaAsBoolean() {
return m_value;
}
public final static LBoolean valueOf(boolean value) {
return value? TRUE: FALSE;
}
}

View File

@@ -0,0 +1,48 @@
package lua.value;
import lua.Lua;
public class LDouble extends LNumber {
private final double m_value;
public LDouble(double value) {
this.m_value = value;
}
public String luaAsString() {
return String.valueOf(m_value);
}
// binary operations on integers, first dispatch
public LValue luaBinOpUnknown(int opcode, LValue lhs) {
return lhs.luaBinOpDouble( opcode, this.m_value );
}
// binary operations on mixtures of doubles and integers
public LValue luaBinOpInteger(int opcode, int rhs) {
return luaBinOpDoubleDouble( opcode, m_value, (double) rhs );
}
// binary operations on doubles
public LValue luaBinOpDouble(int opcode, double rhs) {
return luaBinOpDoubleDouble( opcode, m_value, rhs );
}
public static LValue luaBinOpDoubleDouble( int opcode, double lhs, double rhs ) {
switch ( opcode ) {
case Lua.OP_ADD: return new LDouble( lhs + rhs );
case Lua.OP_SUB: return new LDouble( lhs - rhs );
case Lua.OP_MUL: return new LDouble( lhs * rhs );
case Lua.OP_DIV: return new LDouble( lhs / rhs );
case Lua.OP_MOD: return new LDouble( lhs % rhs );
case Lua.OP_POW: return new LDouble( Math.pow(lhs, rhs) );
}
return luaUnsupportedOperation();
}
public int luaAsInt() {
return (int) m_value;
}
}

View File

@@ -0,0 +1,7 @@
package lua.value;
public class LFunction extends LValue {
}

View File

@@ -0,0 +1,43 @@
package lua.value;
import lua.Lua;
public class LInteger extends LNumber {
private final int m_value;
public LInteger(int value) {
this.m_value = value;
}
public int luaAsInt() {
return m_value;
}
public String luaAsString() {
return String.valueOf(m_value);
}
// binary operations on integers, first dispatch
public LValue luaBinOpUnknown(int opcode, LValue lhs) {
return lhs.luaBinOpInteger( opcode, this.m_value );
}
// binary operations on integers
public LValue luaBinOpInteger(int opcode, int rhs) {
switch ( opcode ) {
case Lua.OP_ADD: return new LInteger( m_value + rhs );
case Lua.OP_SUB: return new LInteger( m_value - rhs );
case Lua.OP_MUL: return new LInteger( m_value * rhs );
case Lua.OP_DIV: return new LInteger( m_value / rhs );
case Lua.OP_MOD: return new LInteger( m_value % rhs );
case Lua.OP_POW: return new LInteger( (int) Math.pow(m_value, rhs) );
}
return luaUnsupportedOperation();
}
// binary operations on mixed integer, double
public LValue luaBinOpDouble(int opcode, double rhs) {
return LDouble.luaBinOpDoubleDouble(opcode, (double) m_value, rhs );
}
}

View File

@@ -0,0 +1,13 @@
package lua.value;
public final class LNil extends LValue {
public static final LNil NIL = new LNil();
public final String luaAsString() {
return "nil";
}
public boolean luaAsBoolean() {
return false;
}
}

View File

@@ -0,0 +1,6 @@
package lua.value;
abstract
public class LNumber extends LValue {
}

View File

@@ -0,0 +1,21 @@
package lua.value;
import lua.StackState;
public class LString extends LValue {
final String m_string;
public LString(String string) {
this.m_string = string;
}
// TODO: what to do with LuaState?
public LString(StackState l, String string) {
this(string);
}
public String luaAsString() {
return m_string;
}
}

View File

@@ -0,0 +1,29 @@
package lua.value;
import java.util.Hashtable;
import java.util.Vector;
public class LTable extends LValue {
private Hashtable m_hash = new Hashtable();
private Vector m_array = new Vector();
public LTable() {
}
public LTable(int narray, int nhash) {
}
public void luaSetTable(LValue key, LValue val) {
m_hash.put( key.luaAsString(), val );
m_array.add( val );
}
public LValue luaGetTable(LValue key) {
return (LValue) m_hash.get( key.luaAsString() );
}
public String luaAsString() {
return m_hash.toString();
}
}

View File

@@ -0,0 +1,5 @@
package lua.value;
public class LThread extends LValue {
}

View File

@@ -0,0 +1,65 @@
package lua.value;
import lua.StackState;
abstract
public class LValue {
protected static LValue luaUnsupportedOperation() {
throw new java.lang.UnsupportedOperationException();
}
// test if value is true
public boolean luaAsBoolean() {
return true;
}
// perform a lua call
public void luaStackCall(StackState state, int base, int nresults) {
luaUnsupportedOperation();
}
// unsupported except for numbers
public LValue luaBinOpUnknown(int opcode, LValue lhs) {
return luaUnsupportedOperation();
}
// unsupported except for numbers
public LValue luaBinOpInteger(int opcode, int m_value) {
return luaUnsupportedOperation();
}
// unsupported except for numbers
public LValue luaBinOpDouble(int opcode, double m_value) {
return luaUnsupportedOperation();
}
/** set a value in a table
*/
public void luaSetTable(LValue key, LValue value) {
luaUnsupportedOperation();
}
/** Get a value from a table */
public LValue luaGetTable(LValue value) {
return luaUnsupportedOperation();
}
/** Get the value as a String
*/
public String luaAsString() {
return super.toString();
}
/** Override standard toString with lua String conversion by default */
public String toString() {
return luaAsString();
}
/** Return value as an integer */
public int luaAsInt() {
luaUnsupportedOperation();
return 0;
}
}