Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brianush1 committed Jan 21, 2020
0 parents commit 908e2f6
Show file tree
Hide file tree
Showing 45 changed files with 9,867 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.dub
.vscode
docs.json
__dummy.html
docs/
/zua
/cmdline/zua
zua.so
zua.dylib
zua.dll
zua.a
zua.lib
zua-test-*
*.exe
*.o
*.obj
*.lst
trace.log
trace.def
trace.dot
trace.svg
libzua.a
37 changes: 37 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
= Zua
brianush1 <brianush1@outlook.com>
:icons: font

WARNING: This project is very much in an alpha stage and should not yet be used in production code

A slow, not yet fully functioning implementation of Lua 5.1

== Usage

To build Zua, first cd to the cmdline directory under this folder. Then, run `dub build -b release`. This will build the Zua command-line utility in release mode. Run `./zua --help` for usage.

== The path to full* Lua 5.1 support
* [x] `coroutine` library
* [x] `math` library
* [x] `table` library
* [ ] `io` library
* [ ] `debug` library (maybe diverge from Lua 5.1)
* [ ] Pattern-matching functions in `string` library
* [ ] Module system (`require`, `module`, and `package` globals)
* [ ] Add userdata type
* [ ] `newproxy` function

=== *Deliberate inconsistencies
Zua does make some decisions about where to deliberately diverge from Lua 5.1. Here is a comperehensive list of where this is done:

* There is no such thing as the "main thread." All code, even toplevel code, runs in a coroutine.
* There is no arbitrary compilation at runtime. This means the globals `dofile`, `load`, `loadfile`, and `loadstring` are not available.
* The `string.dump` (and consequently `("").dump`) methods are not available; what use were they, anyway?
* Errors do not include line numbers; the second argument to the `error` function is ignored.

Any inconsistency between Lua 5.1 and Zua that is not listed above should be regarded as a bug and filed as an issue.

== Additions

* Full error position is kept track of, with greater than line precision.
* The parser includes error recovery.
127 changes: 127 additions & 0 deletions benchmark.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
-- The Computer Language Benchmarks Game
-- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
-- contributed by Mike Pall

local startClock = os.clock()
local sqrt = math.sqrt

local PI = 3.141592653589793
local SOLAR_MASS = 4 * PI * PI
local DAYS_PER_YEAR = 365.24
local bodies = {
{ -- Sun
x = 0,
y = 0,
z = 0,
vx = 0,
vy = 0,
vz = 0,
mass = SOLAR_MASS
},
{ -- Jupiter
x = 4.84143144246472090e+00,
y = -1.16032004402742839e+00,
z = -1.03622044471123109e-01,
vx = 1.66007664274403694e-03 * DAYS_PER_YEAR,
vy = 7.69901118419740425e-03 * DAYS_PER_YEAR,
vz = -6.90460016972063023e-05 * DAYS_PER_YEAR,
mass = 9.54791938424326609e-04 * SOLAR_MASS
},
{ -- Saturn
x = 8.34336671824457987e+00,
y = 4.12479856412430479e+00,
z = -4.03523417114321381e-01,
vx = -2.76742510726862411e-03 * DAYS_PER_YEAR,
vy = 4.99852801234917238e-03 * DAYS_PER_YEAR,
vz = 2.30417297573763929e-05 * DAYS_PER_YEAR,
mass = 2.85885980666130812e-04 * SOLAR_MASS
},
{ -- Uranus
x = 1.28943695621391310e+01,
y = -1.51111514016986312e+01,
z = -2.23307578892655734e-01,
vx = 2.96460137564761618e-03 * DAYS_PER_YEAR,
vy = 2.37847173959480950e-03 * DAYS_PER_YEAR,
vz = -2.96589568540237556e-05 * DAYS_PER_YEAR,
mass = 4.36624404335156298e-05 * SOLAR_MASS
},
{ -- Neptune
x = 1.53796971148509165e+01,
y = -2.59193146099879641e+01,
z = 1.79258772950371181e-01,
vx = 2.68067772490389322e-03 * DAYS_PER_YEAR,
vy = 1.62824170038242295e-03 * DAYS_PER_YEAR,
vz = -9.51592254519715870e-05 * DAYS_PER_YEAR,
mass = 5.15138902046611451e-05 * SOLAR_MASS
}
}

local function advance(bodies, nbody, dt)
for i=1,nbody do
local bi = bodies[i]
local bix, biy, biz, bimass = bi.x, bi.y, bi.z, bi.mass
local bivx, bivy, bivz = bi.vx, bi.vy, bi.vz
for j=i+1,nbody do
local bj = bodies[j]
local dx, dy, dz = bix-bj.x, biy-bj.y, biz-bj.z
local distance = sqrt(dx*dx + dy*dy + dz*dz)
local mag = dt / (distance * distance * distance)
local bim, bjm = bimass*mag, bj.mass*mag
bivx = bivx - (dx * bjm)
bivy = bivy - (dy * bjm)
bivz = bivz - (dz * bjm)
bj.vx = bj.vx + (dx * bim)
bj.vy = bj.vy + (dy * bim)
bj.vz = bj.vz + (dz * bim)
end
bi.vx = bivx
bi.vy = bivy
bi.vz = bivz
end
for i=1,nbody do
local bi = bodies[i]
bi.x = bi.x + (dt * bi.vx)
bi.y = bi.y + (dt * bi.vy)
bi.z = bi.z + (dt * bi.vz)
end
end

local function energy(bodies, nbody)
local e = 0
for i=1,nbody do
local bi = bodies[i]
local vx, vy, vz, bim = bi.vx, bi.vy, bi.vz, bi.mass
e = e + (0.5 * bim * (vx*vx + vy*vy + vz*vz))
for j=i+1,nbody do
local bj = bodies[j]
local dx, dy, dz = bi.x-bj.x, bi.y-bj.y, bi.z-bj.z
local distance = sqrt(dx*dx + dy*dy + dz*dz)
e = e - ((bim * bj.mass) / distance)
end
end
return e
end

local function offsetMomentum(b, nbody)
local px, py, pz = 0, 0, 0
for i=1,nbody do
local bi = b[i]
local bim = bi.mass
px = px + (bi.vx * bim)
py = py + (bi.vy * bim)
pz = pz + (bi.vz * bim)
end
b[1].vx = -px / SOLAR_MASS
b[1].vy = -py / SOLAR_MASS
b[1].vz = -pz / SOLAR_MASS
end

local N = tonumber(arg and arg[1]) or 2000
local nbody = #bodies

offsetMomentum(bodies, nbody)
print( energy(bodies, nbody) )
for i=1,N do advance(bodies, nbody, 0.01) end
print( energy(bodies, nbody) )

print("Took " .. (os.clock() - startClock) * 1000 .. "ms")
18 changes: 18 additions & 0 deletions cmdline/dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"Brian"
],
"dependencies": {
"zua": {
"path": ".."
}
},
"stringImportPaths": [
"../tests"
],
"copyright": "Copyright © 2019, Brian",
"description": "Command line interface to Zua",
"license": "proprietary",
"name": "zua-cmd",
"targetName": "zua"
}
6 changes: 6 additions & 0 deletions cmdline/dub.selections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"fileVersion": 1,
"versions": {
"zua": {"path":".."}
}
}
12 changes: 12 additions & 0 deletions cmdline/profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env sh

# Script that can be used to profile Zua
# Depends on profdump and graphviz

rm -f trace.def
rm -f trace.dot
rm -f trace.log
rm -f trace.svg
dub run -b profile -- -f ../benchmark.lua
dub run profdump -- -d -t 2 -f trace.log trace.dot
dot -Tsvg trace.dot -o trace.svg
108 changes: 108 additions & 0 deletions cmdline/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import std.stdio;
import std.algorithm;
import std.typecons;
import std.conv;
import std.getopt;
import std.file;
import zua.diagnostic;
import zua;

private void execute(string filename, string source) {
Common c = new Common(GlobalOptions.FullAccess);

try {
auto res = c.run(filename, source);
Diagnostic[] diag = res[0];
foreach (Diagnostic d; diag) {
stderr.writeln(d.type.to!string ~ ": " ~ d.message);
foreach (size_t[2] range; d.ranges) {
auto t = decodeIndex(range[0], source);
auto k = decodeIndex(range[1], source);
t.line++;
k.line++;
if (t.line == k.line) {
stderr.writeln(" Line " ~ t.line.to!string);
}
else {
stderr.writeln(" Lines " ~ t.line.to!string ~ "-" ~ k.line.to!string);
}
}
}
}
catch (LuaError e) {
string str = e.data.toString;
stderr.writeln("Error: " ~ str);
ResolvedTraceframe[] stack = c.resolve(e.stack);
stderr.writeln(" Trace begin");
foreach_reverse (frame; stack) {
if (!frame.filename.isNull) {
auto t = decodeIndex(frame.start, source);
auto k = decodeIndex(frame.end, source);
t.line++;
k.line++;
if (t.line == k.line) {
stderr.writeln(" Line " ~ t.line.to!string);
}
else {
stderr.writeln(" Lines " ~ t.line.to!string ~ "-" ~ k.line.to!string);
}
}
}
stderr.writeln(" Trace end");
}
}

void main(string[] args) {
string file;
bool runTests = false;

GetoptResult helpInformation;

try {
helpInformation = getopt(
args,
"file|f", "A path to the file to execute", &file,
"tests", "If provided, runs the Zua test suite", &runTests
);
}
catch (Exception e) {
stderr.writeln("Error: " ~ e.msg);
return;
}

if (helpInformation.helpWanted) {
defaultGetoptPrinter(q"(Zua command-line utility.
For bug reporting instructions, please see:
<https://github.com/brianush1/zua>.
Options:)", helpInformation.options);
return;
}

if (runTests) {
const tests = [
import("cond.lua"),
import("assign.lua"),
import("tables.lua"),
import("upvalues.lua"),
import("execorder.lua"),
import("op.lua"),
import("basic.lua"),
import("coroutine.lua"),
import("globalenv.lua"),
import("tablelib.lua"),
import("stringlib.lua"),
import("bit32lib.lua"),
];

foreach (s; tests) {
execute("test.lua", s);
}
}

if (file != "") {
const data = readText(file);
execute(file, data);
}
}
Loading

0 comments on commit 908e2f6

Please sign in to comment.