This patch fixes the Lua arithmetic model at parser level only. Runtime level fixtures are left for future work. MOTIVATION Space Lua treats all numbers as IEEE-754 doubles (JS semantics), so it preserves negative zero. Lua uses integers and floats. The literal "-0" in Lua is parsed as unary minus applied to the integer literal "0", which yields the integer "0" (no sign). This difference flips the sign of the divisor in "1/-0" and "-1/-0". Although it could be fixed without touching the parser by changing only unary minus evaluation, this commit goes a little further and provides integer vs float differenciating at the parser level as well uses it to fix the unary minus evaluation. Extensive tests are added to test this exact change works and the Space Lua arithmetic model is aligned with Lua. Note that things like `-(expr)` in general may still produce results inconsistent with Lua. This cannot be fixed at parser level and must be fixed at runtime level which will need far more complex change to the Space Lua interpreter code. Therefor these cases we omitted from the tests. Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { parse } from "./parse.ts";
|
|
import { luaBuildStandardEnv } from "./stdlib.ts";
|
|
import { LuaEnv, LuaRuntimeError, LuaStackFrame } from "./runtime.ts";
|
|
import { evalStatement } from "./eval.ts";
|
|
import { assert } from "@std/assert/assert";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
Deno.test("[Lua] Core language", async () => {
|
|
await runLuaTest("./language_core_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] Core language", async () => {
|
|
await runLuaTest("./arithmetic_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] Table tests", async () => {
|
|
await runLuaTest("./stdlib/table_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] String to number tests", async () => {
|
|
await runLuaTest("./tonumber_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] String tests", async () => {
|
|
await runLuaTest("./stdlib/string_test.lua");
|
|
// await runLuaTest("./stdlib/string_test2.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] Space Lua tests", async () => {
|
|
await runLuaTest("./stdlib/space_lua_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] OS tests", async () => {
|
|
await runLuaTest("./stdlib/os_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] Math tests", async () => {
|
|
await runLuaTest("./stdlib/math_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] JS tests", async () => {
|
|
await runLuaTest("./stdlib/js_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] Global functions tests", async () => {
|
|
await runLuaTest("./stdlib/global_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] Encoding functions tests", async () => {
|
|
await runLuaTest("./stdlib/encoding_test.lua");
|
|
});
|
|
|
|
Deno.test("[Lua] Lume functions tests", async () => {
|
|
await runLuaTest("./lume_test.lua");
|
|
});
|
|
|
|
async function runLuaTest(luaPath: string) {
|
|
const luaFile = await Deno.readTextFile(
|
|
fileURLToPath(new URL(luaPath, import.meta.url)),
|
|
);
|
|
const chunk = parse(luaFile, {});
|
|
const env = new LuaEnv(luaBuildStandardEnv());
|
|
const sf = LuaStackFrame.createWithGlobalEnv(env, chunk.ctx);
|
|
|
|
try {
|
|
await evalStatement(chunk, env, sf);
|
|
} catch (e: any) {
|
|
if (e instanceof LuaRuntimeError) {
|
|
console.error(`Error evaluating script:`, e.toPrettyString(luaFile));
|
|
} else {
|
|
console.error(`Error evaluating script:`, e);
|
|
}
|
|
assert(false);
|
|
}
|
|
}
|