Add "arg" global value to provide command line arguments.

This commit is contained in:
James Roseborough
2009-05-05 00:29:20 +00:00
parent 5048824909
commit a55ddfa2d9
5 changed files with 108 additions and 51 deletions

View File

@@ -31,6 +31,8 @@ import org.luaj.compiler.LuaC;
import org.luaj.lib.DebugLib; import org.luaj.lib.DebugLib;
import org.luaj.platform.J2sePlatform; import org.luaj.platform.J2sePlatform;
import org.luaj.vm.LFunction; import org.luaj.vm.LFunction;
import org.luaj.vm.LString;
import org.luaj.vm.LTable;
import org.luaj.vm.Lua; import org.luaj.vm.Lua;
import org.luaj.vm.LuaState; import org.luaj.vm.LuaState;
import org.luaj.vm.Platform; import org.luaj.vm.Platform;
@@ -40,7 +42,7 @@ import org.luaj.vm.Platform;
* lua command for use in java se environments. * lua command for use in java se environments.
*/ */
public class lua { public class lua {
private static final String version = Lua._VERSION + "Copyright (C) 2008 luaj.org"; private static final String version = Lua._VERSION + " Copyright (C) 2009 luaj.org";
private static final String usage = private static final String usage =
"usage: java -cp luaj-j2se.jar lua [options] [script [args]].\n" + "usage: java -cp luaj-j2se.jar lua [options] [script [args]].\n" +
@@ -116,10 +118,16 @@ public class lua {
processing = true; processing = true;
for ( int i=0; i<args.length; i++ ) { for ( int i=0; i<args.length; i++ ) {
if ( ! processing || ! args[i].startsWith("-") ) { if ( ! processing || ! args[i].startsWith("-") ) {
processScript( vm, new FileInputStream(args[i]), args[i], args, i+1 ); LTable arg = new LTable();
for ( int j=0; j<args.length; j++ )
arg.put( j-i, new LString(args[j]) );
arg.put( -i-1, new LString("lua") );
arg.put( -i-2, new LString("java") );
vm._G.put( "arg", arg );
processScript( vm, new FileInputStream(args[i]), args[i], args, i );
break; break;
} else if ( args[i].length() <= 1 ) { } else if ( args[i].length() <= 1 ) {
processScript( vm, System.in, "-", args, i+1 ); processScript( vm, System.in, "-", args, i );
break; break;
} else { } else {
switch ( args[i].charAt(1) ) { switch ( args[i].charAt(1) ) {
@@ -128,7 +136,7 @@ public class lua {
break; break;
case 'e': case 'e':
++i; ++i;
processScript( vm, new ByteArrayInputStream(args[i].getBytes()), args[i], null, 0 ); processScript( vm, new ByteArrayInputStream(args[i].getBytes()), args[i], args, i );
break; break;
case '-': case '-':
processing = false; processing = false;
@@ -173,7 +181,7 @@ public class lua {
if ( args != null ) if ( args != null )
for ( int i=offset; i<args.length; i++ ) for ( int i=offset; i<args.length; i++ )
vm.pushstring(args[i]); vm.pushstring(args[i]);
vm.call(vm.gettop()-1, 0); vm.call(args.length-offset, 0);
break; break;
case LuaState.LUA_ERRMEM: case LuaState.LUA_ERRMEM:
System.out.println("out of memory during chunk load"); System.out.println("out of memory during chunk load");

View File

@@ -1,16 +1,15 @@
-- The Computer Language Shootout -- The Computer Language Benchmarks Game
-- http://shootout.alioth.debian.org/ -- http://shootout.alioth.debian.org/
-- contributed by Mike Pall -- contributed by Mike Pall
-- modified for luaj to remove dependency on io
local function fannkuch(n) local function fannkuch(n)
local p, q, s, odd, check, maxflips = {}, {}, {}, true, 0, 0 local p, q, s, odd, check, maxflips = {}, {}, {}, true, 0, 0
for i=1,n do p[i] = i; s[i] = i end for i=1,n do p[i] = i; q[i] = i; s[i] = i end
repeat repeat
-- Print max. 30 permutations. -- Print max. 30 permutations.
if check < 30 then if check < 30 then
if not p[n] then return maxflips end -- Catch n = 0, 1, 2. if not p[n] then return maxflips end -- Catch n = 0, 1, 2.
for i=1,n do print(p[i]) end; io.write(unpack(p)); io.write("\n")
check = check + 1 check = check + 1
end end
-- Copy and flip. -- Copy and flip.
@@ -18,14 +17,16 @@ local function fannkuch(n)
if p[n] ~= n and q1 ~= 1 then -- Avoid useless work. if p[n] ~= n and q1 ~= 1 then -- Avoid useless work.
for i=2,n do q[i] = p[i] end -- Work on a copy. for i=2,n do q[i] = p[i] end -- Work on a copy.
for flips=1,1000000 do -- Flip ... for flips=1,1000000 do -- Flip ...
local j = q1 - 1
for i=2,q1/2 do q[i], q[j] = q[j], q[i]; j = j - 1 end
local qq = q[q1] local qq = q[q1]
if qq == 1 then -- ... until 1st element is 1. if qq == 1 then -- ... until 1st element is 1.
if flips > maxflips then maxflips = flips end -- New maximum? if flips > maxflips then maxflips = flips end -- New maximum?
break break
end end
q[q1] = q1 q[q1] = q1
if q1 >= 4 then
local i, j = 2, q1 - 1
repeat q[i], q[j] = q[j], q[i]; i = i + 1; j = j - 1; until i >= j
end
q1 = qq q1 = qq
end end
end end
@@ -36,7 +37,7 @@ local function fannkuch(n)
p[2], p[3] = p[3], p[2]; odd = true -- Rotate 1<-2 and 1<-2<-3. p[2], p[3] = p[3], p[2]; odd = true -- Rotate 1<-2 and 1<-2<-3.
for i=3,n do for i=3,n do
local sx = s[i] local sx = s[i]
if sx ~= 1 then sx = sx - 1; s[i] = sx; break end if sx ~= 1 then s[i] = sx-1; break end
if i == n then return maxflips end -- Out of permutations. if i == n then return maxflips end -- Out of permutations.
s[i] = i s[i] = i
-- Rotate 1<-...<-i+1. -- Rotate 1<-...<-i+1.
@@ -46,6 +47,5 @@ local function fannkuch(n)
until false until false
end end
local arg = {...} local n = tonumber(arg and arg[1]) or 1
local n = tonumber(arg and arg[1]) or 5 io.write("Pfannkuchen(", n, ") = ", fannkuch(n), "\n")
print("Pfannkuchen("..n..") = "..fannkuch(n))

View File

@@ -1,17 +1,65 @@
-- The Computer Language Shootout -- The Computer Language Benchmarks Game
-- http://shootout.alioth.debian.org/ -- http://shootout.alioth.debian.org/
-- contributed by Mike Pall -- contributed by Mike Pall
-- modified for luaj to remove dependency on io -- modified by Rob Kendrick to be parallel
-- modified by Isaac Gouy
-- called with the following arguments on the command line;
-- 1: size of mandelbrot to generate
-- 2: number of children to spawn (defaults to 6, which works well on 4-way)
-- If this is a child, then there will be additional parameters;
-- 3: start row
-- 4: end row
--
-- Children buffer up their output and emit it to stdout when
-- finished, to avoid stalling due to a full pipe.
local arg = {...}
local width = tonumber(arg and arg[1]) or 100 local width = tonumber(arg and arg[1]) or 100
local children = tonumber(arg and arg[2]) or 6
local srow = tonumber(arg and arg[3])
local erow = tonumber(arg and arg[4])
local height, wscale = width, 2/width local height, wscale = width, 2/width
local m, limit2 = 50, 4.0 local m, limit2 = 50, 4.0
local write, char = print, string.char local write, char = io.write, string.char
write("P4\n", width, " ", height, "\n") if not srow then
-- we are the parent process. emit the header, and then spawn children
for y=0,height-1 do local workunit = math.floor(width / (children + 1))
local handles = { }
write("P4\n", width, " ", height, "\n")
children = children - 1
for i = 0, children do
local cs, ce
if i == 0 then
cs = 0
ce = workunit
elseif i == children then
cs = (workunit * i) + 1
ce = width - 1
else
cs = (workunit * i) + 1
ce = cs + workunit - 1
end
handles[i + 1] = io.popen(("%s %s %d %d %d %d"):format(
arg[-1], arg[0], width, children + 1, cs, ce))
end
-- collect answers, and emit
for i = 0, children do
write(handles[i + 1]:read "*a")
end
else
-- we are a child process. do the work allocated to us.
local obuff = { }
for y=srow,erow do
local Ci = 2*y / height - 1 local Ci = 2*y / height - 1
for xb=0,width-1,8 do for xb=0,width-1,8 do
local bits = 0 local bits = 0
@@ -35,6 +83,9 @@ for y=0,height-1 do
if xbb >= width then if xbb >= width then
for x=width,xbb do bits = bits + bits + 1 end for x=width,xbb do bits = bits + bits + 1 end
end end
write(char(255-bits)) obuff[#obuff + 1] = char(255 - bits)
end end
end
write(table.concat(obuff))
end end

View File

@@ -1,7 +1,6 @@
-- The Great Computer Language Shootout -- The Great Computer Language Shootout
-- http://shootout.alioth.debian.org/ -- http://shootout.alioth.debian.org/
-- contributed by Isaac Gouy, tuned by Mike Pall -- contributed by Isaac Gouy, tuned by Mike Pall
-- modified for luaj to remove dependency on io
local sqrt = math.sqrt local sqrt = math.sqrt
@@ -114,12 +113,11 @@ local function offsetMomentum(b, nbody)
b[1].vz = -pz / SOLAR_MASS b[1].vz = -pz / SOLAR_MASS
end end
local arg = {...}
local N = tonumber(arg and arg[1]) or 1000 local N = tonumber(arg and arg[1]) or 1000
local bodies = { Sun, Jupiter, Saturn, Uranus, Neptune } local bodies = { Sun, Jupiter, Saturn, Uranus, Neptune }
local nbody = #bodies local nbody = table.getn(bodies)
offsetMomentum(bodies, nbody) offsetMomentum(bodies, nbody)
print( tostring(energy(bodies, nbody))) io.write( string.format("%0.9f",energy(bodies, nbody)), "\n")
for i=1,N do advance(bodies, nbody, 0.01) end for i=1,N do advance(bodies, nbody, 0.01) end
print( tostring(energy(bodies, nbody))) io.write( string.format("%0.9f",energy(bodies, nbody)), "\n")

View File

@@ -1 +1 @@
version: 0.96 version: 0.97