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:
49
src/test/java/LuacRunner.java
Normal file
49
src/test/java/LuacRunner.java
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import lua.StackState;
|
||||
import lua.io.Closure;
|
||||
import lua.io.LoadState;
|
||||
import lua.io.Proto;
|
||||
import lua.value.LString;
|
||||
|
||||
/**
|
||||
* Program to run a compiled lua chunk for test purposes
|
||||
*
|
||||
* @author jim_roseborough
|
||||
*/
|
||||
public class LuacRunner {
|
||||
|
||||
public static void main( String[] args ) throws IOException {
|
||||
|
||||
// get script name
|
||||
String script = (args.length>0? args[0]: "src/test/res/test1.luac");
|
||||
System.out.println("loading '"+script+"'");
|
||||
|
||||
// new lua state
|
||||
StackState state = new StackState();
|
||||
|
||||
// push args onto stack
|
||||
for ( int i=1; i<args.length; i++ )
|
||||
state.push(new LString(args[i]));
|
||||
|
||||
// load the file
|
||||
InputStream is = new FileInputStream( script );
|
||||
Proto p = LoadState.undump(state, is, script);
|
||||
|
||||
// create closure to execute
|
||||
Closure c = new Closure( state, p );
|
||||
state.push( c );
|
||||
for ( int i=0; i<args.length; i++ )
|
||||
state.push( new LString(args[i]) );
|
||||
state.docall(args.length, false);
|
||||
|
||||
// print result?
|
||||
System.out.println("stack:");
|
||||
for ( int i=0; i<state.top; i++ )
|
||||
System.out.println(" ["+i+"]="+state.stack[i] );
|
||||
|
||||
}
|
||||
}
|
||||
7
src/test/res/compile.sh
Normal file
7
src/test/res/compile.sh
Normal file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
LUA_HOME=/cygdrive/c/programs/lua5.1
|
||||
for x in test1 test2 test3 test4
|
||||
do
|
||||
echo compiling $x
|
||||
${LUA_HOME}/luac5.1.exe -l -o ${x}.luac ${x}.lua
|
||||
done
|
||||
5
src/test/res/test1.lua
Normal file
5
src/test/res/test1.lua
Normal file
@@ -0,0 +1,5 @@
|
||||
a = 123 + 456
|
||||
print( a )
|
||||
|
||||
|
||||
|
||||
BIN
src/test/res/test1.luac
Normal file
BIN
src/test/res/test1.luac
Normal file
Binary file not shown.
10
src/test/res/test2.lua
Normal file
10
src/test/res/test2.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
function sum(a,b,c,d) -- "sum" method
|
||||
return a+b+c+d -- return sum
|
||||
end
|
||||
print( sum( 1, 2, 3, 4 ) )
|
||||
print( sum( 5, 6, 7 ) )
|
||||
print( sum( 9, 10, 11, 12, 13, 14 ) )
|
||||
print( sum( sum(1,2,3,4), sum(5,6,7), sum(9,10,11,12,13,14), 15 ) )
|
||||
|
||||
|
||||
|
||||
BIN
src/test/res/test2.luac
Normal file
BIN
src/test/res/test2.luac
Normal file
Binary file not shown.
6
src/test/res/test3.lua
Normal file
6
src/test/res/test3.lua
Normal file
@@ -0,0 +1,6 @@
|
||||
a = func(1, 2, 3)
|
||||
a = func(3, 2, 1)
|
||||
a = func(func(),func(),func())
|
||||
|
||||
|
||||
|
||||
BIN
src/test/res/test3.luac
Normal file
BIN
src/test/res/test3.luac
Normal file
Binary file not shown.
8
src/test/res/test4.lua
Normal file
8
src/test/res/test4.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
function Point(x, y) -- "Point" object constructor
|
||||
return { x = x, y = y } -- Creates and returns a new object (table)
|
||||
end
|
||||
array = { Point(10, 20), Point(30, 40), Point(50, 60) } -- Creates array of points
|
||||
print(array[2].y) -- Prints 40
|
||||
|
||||
|
||||
|
||||
BIN
src/test/res/test4.luac
Normal file
BIN
src/test/res/test4.luac
Normal file
Binary file not shown.
Reference in New Issue
Block a user