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.platform.J2sePlatform;
import org.luaj.vm.LFunction;
import org.luaj.vm.LString;
import org.luaj.vm.LTable;
import org.luaj.vm.Lua;
import org.luaj.vm.LuaState;
import org.luaj.vm.Platform;
@@ -40,7 +42,7 @@ import org.luaj.vm.Platform;
* lua command for use in java se environments.
*/
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 =
"usage: java -cp luaj-j2se.jar lua [options] [script [args]].\n" +
@@ -116,10 +118,16 @@ public class lua {
processing = true;
for ( int i=0; i<args.length; i++ ) {
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;
} else if ( args[i].length() <= 1 ) {
processScript( vm, System.in, "-", args, i+1 );
processScript( vm, System.in, "-", args, i );
break;
} else {
switch ( args[i].charAt(1) ) {
@@ -128,7 +136,7 @@ public class lua {
break;
case 'e':
++i;
processScript( vm, new ByteArrayInputStream(args[i].getBytes()), args[i], null, 0 );
processScript( vm, new ByteArrayInputStream(args[i].getBytes()), args[i], args, i );
break;
case '-':
processing = false;
@@ -173,7 +181,7 @@ public class lua {
if ( args != null )
for ( int i=offset; i<args.length; i++ )
vm.pushstring(args[i]);
vm.call(vm.gettop()-1, 0);
vm.call(args.length-offset, 0);
break;
case LuaState.LUA_ERRMEM:
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/
-- contributed by Mike Pall
-- modified for luaj to remove dependency on io
local function fannkuch(n)
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
-- Print max. 30 permutations.
if check < 30 then
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
end
-- Copy and flip.
@@ -18,14 +17,16 @@ local function fannkuch(n)
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 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]
if qq == 1 then -- ... until 1st element is 1.
if flips > maxflips then maxflips = flips end -- New maximum?
break
end
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
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.
for i=3,n do
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.
s[i] = i
-- Rotate 1<-...<-i+1.
@@ -46,6 +47,5 @@ local function fannkuch(n)
until false
end
local arg = {...}
local n = tonumber(arg and arg[1]) or 5
print("Pfannkuchen("..n..") = "..fannkuch(n))
local n = tonumber(arg and arg[1]) or 1
io.write("Pfannkuchen(", n, ") = ", fannkuch(n), "\n")

View File

@@ -1,40 +1,91 @@
-- The Computer Language Shootout
-- The Computer Language Benchmarks Game
-- http://shootout.alioth.debian.org/
-- 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 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 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 Ci = 2*y / height - 1
for xb=0,width-1,8 do
local bits = 0
local xbb = xb+7
for x=xb,xbb < width and xbb or width-1 do
bits = bits + bits
local Zr, Zi, Zrq, Ziq = 0.0, 0.0, 0.0, 0.0
local Cr = x * wscale - 1.5
for i=1,m do
local Zri = Zr*Zi
Zr = Zrq - Ziq + Cr
Zi = Zri + Zri + Ci
Zrq = Zr*Zr
Ziq = Zi*Zi
if Zrq + Ziq > limit2 then
bits = bits + 1
break
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
for xb=0,width-1,8 do
local bits = 0
local xbb = xb+7
for x=xb,xbb < width and xbb or width-1 do
bits = bits + bits
local Zr, Zi, Zrq, Ziq = 0.0, 0.0, 0.0, 0.0
local Cr = x * wscale - 1.5
for i=1,m do
local Zri = Zr*Zi
Zr = Zrq - Ziq + Cr
Zi = Zri + Zri + Ci
Zrq = Zr*Zr
Ziq = Zi*Zi
if Zrq + Ziq > limit2 then
bits = bits + 1
break
end
end
end
end
if xbb >= width then
for x=width,xbb do bits = bits + bits + 1 end
end
write(char(255-bits))
end
if xbb >= width then
for x=width,xbb do bits = bits + bits + 1 end
end
obuff[#obuff + 1] = char(255 - bits)
end
end
write(table.concat(obuff))
end

View File

@@ -1,7 +1,6 @@
-- The Great Computer Language Shootout
-- http://shootout.alioth.debian.org/
-- contributed by Isaac Gouy, tuned by Mike Pall
-- modified for luaj to remove dependency on io
local sqrt = math.sqrt
@@ -114,12 +113,11 @@ local function offsetMomentum(b, nbody)
b[1].vz = -pz / SOLAR_MASS
end
local arg = {...}
local N = tonumber(arg and arg[1]) or 1000
local bodies = { Sun, Jupiter, Saturn, Uranus, Neptune }
local nbody = #bodies
local nbody = table.getn(bodies)
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
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