diff --git a/client/space_lua/arithmetic_test.lua b/client/space_lua/arithmetic_test.lua new file mode 100644 index 00000000..c4b8591c --- /dev/null +++ b/client/space_lua/arithmetic_test.lua @@ -0,0 +1,91 @@ +local function assert_eq(actual, expected, msg) + if actual ~= expected then + error('assert_eq failed: ' .. msg) + end +end + +-- Integer-only (0 and -0 as integers) +-- Expectation: -0 is integer 0, so 1/-0 == +Inf, -1/-0 == +Inf +assert_eq( 1/ 0 == 1/ 0, true, 'int: 1/ 0 == 1/ 0') +assert_eq( 1/ 0 == -1/ 0, false, 'int: 1/ 0 == -1/ 0') +assert_eq( 1/ 0 == 1/-0, true, 'int: 1/ 0 == 1/-0') +assert_eq( 1/ 0 == -1/-0, false, 'int: 1/ 0 == -1/-0') + +assert_eq( -1/ 0 == 1/ 0, false, 'int: -1/ 0 == 1/ 0') +assert_eq( -1/ 0 == -1/ 0, true, 'int: -1/ 0 == -1/ 0') +assert_eq( -1/ 0 == 1/-0, false, 'int: -1/ 0 == 1/-0') +assert_eq( -1/ 0 == -1/-0, true, 'int: -1/ 0 == -1/-0') + +assert_eq( 1/-0 == 1/ 0, true, 'int: 1/-0 == 1/ 0') +assert_eq( 1/-0 == -1/ 0, false, 'int: 1/-0 == -1/ 0') +assert_eq( 1/-0 == 1/-0, true, 'int: 1/-0 == 1/-0') +assert_eq( 1/-0 == -1/-0, false, 'int: 1/-0 == -1/-0') + +assert_eq( -1/-0 == 1/ 0, false, 'int: -1/-0 == 1/ 0') +assert_eq( -1/-0 == -1/ 0, true, 'int: -1/-0 == -1/0 ') +assert_eq( -1/-0 == 1/-0, false, 'int: -1/-0 == 1/-0') +assert_eq( -1/-0 == -1/-0, true, 'int: -1/-0 == -1/-0') + +-- Float-only (0.0 and -0.0 as floats) +-- Expectation: -0.0 is float negative zero, so 1/-0.0 == -Inf, -1/-0.0 == +Inf +assert_eq( 1/ 0.0 == 1/ 0.0, true, 'float: 1/ 0.0 == 1/ 0.0') +assert_eq( 1/ 0.0 == -1/ 0.0, false, 'float: 1/ 0.0 == -1/ 0.0') +assert_eq( 1/ 0.0 == 1/-0.0, false, 'float: 1/ 0.0 == 1/-0.0') +assert_eq( 1/ 0.0 == -1/-0.0, true, 'float: 1/ 0.0 == -1/-0.0') + +assert_eq( -1/ 0.0 == 1/ 0.0, false, 'float: -1/ 0.0 == 1/ 0.0') +assert_eq( -1/ 0.0 == -1/ 0.0, true, 'float: -1/ 0.0 == -1/ 0.0') +assert_eq( -1/ 0.0 == 1/-0.0, true, 'float: -1/ 0.0 == 1/-0.0') +assert_eq( -1/ 0.0 == -1/-0.0, false, 'float: -1/ 0.0 == -1/-0.0') + +assert_eq( 1/-0.0 == 1/ 0.0, false, 'float: 1/-0.0 == 1/ 0.0') +assert_eq( 1/-0.0 == -1/ 0.0, true, 'float: 1/-0.0 == -1/ 0.0') +assert_eq( 1/-0.0 == 1/-0.0, true, 'float: 1/-0.0 == 1/-0.0') +assert_eq( 1/-0.0 == -1/-0.0, false, 'float: 1/-0.0 == -1/-0.0') + +assert_eq( -1/-0.0 == 1/ 0.0, true, 'float: -1/-0.0 == 1/ 0.0') +assert_eq( -1/-0.0 == -1/ 0.0, false, 'float: -1/-0.0 == -1/ 0.0') +assert_eq( -1/-0.0 == 1/-0.0, false, 'float: -1/-0.0 == 1/-0.0') +assert_eq( -1/-0.0 == -1/-0.0, true, 'float: -1/-0.0 == -1/-0.0') + +-- Integer-Float (left int 0/-0; right float 0.0/-0.0) +assert_eq( 1/ 0 == 1/ 0.0, true, 'int-float: 1/0 == 1/ 0.0') +assert_eq( 1/ 0 == -1/ 0.0, false, 'int-float: 1/0 == -1/ 0.0') +assert_eq( 1/ 0 == 1/-0.0, false, 'int-float: 1/0 == 1/-0.0') +assert_eq( 1/ 0 == -1/-0.0, true, 'int-float: 1/0 == -1/-0.0') + +assert_eq( -1/ 0 == 1/ 0.0, false, 'int-float: -1/0 == 1/ 0.0') +assert_eq( -1/ 0 == -1/ 0.0, true, 'int-float: -1/0 == -1/ 0.0') +assert_eq( -1/ 0 == 1/-0.0, true, 'int-float: -1/0 == 1/-0.0') +assert_eq( -1/ 0 == -1/-0.0, false, 'int-float: -1/0 == -1/-0.0') + +assert_eq( 1/-0 == 1/ 0.0, true, 'int-float: 1/-0 == 1/ 0.0') +assert_eq( 1/-0 == -1/ 0.0, false, 'int-float: 1/-0 == -1/ 0.0') +assert_eq( 1/-0 == 1/-0.0, false, 'int-float: 1/-0 == 1/-0.0') +assert_eq( 1/-0 == -1/-0.0, true, 'int-float: 1/-0 == -1/-0.0') + +assert_eq( -1/-0 == 1/ 0.0, false, 'int-float: -1/-0 == 1/ 0.0') +assert_eq( -1/-0 == -1/ 0.0, true, 'int-float: -1/-0 == -1/ 0.0') +assert_eq( -1/-0 == 1/-0.0, true, 'int-float: -1/-0 == 1/-0.0') +assert_eq( -1/-0 == -1/-0.0, false, 'int-float: -1/-0 == -1/-0.0') + +-- Float-Integer (left float 0.0/-0.0; right int 0/-0) +assert_eq( 1/ 0.0 == 1/ 0, true, 'float-int: 1/0.0 == 1/ 0') +assert_eq( 1/ 0.0 == -1/ 0, false, 'float-int: 1/0.0 == -1/ 0') +assert_eq( 1/ 0.0 == 1/-0, true, 'float-int: 1/0.0 == 1/-0') +assert_eq( 1/ 0.0 == -1/-0, false, 'float-int: 1/0.0 == -1/-0') + +assert_eq( -1/ 0.0 == 1/ 0, false, 'float-int: -1/0.0 == 1/ 0') +assert_eq( -1/ 0.0 == -1/ 0, true, 'float-int: -1/0.0 == -1/ 0') +assert_eq( -1/ 0.0 == 1/-0, false, 'float-int: -1/0.0 == 1/-0') +assert_eq( -1/ 0.0 == -1/-0, true, 'float-int: -1/0.0 == -1/-0') + +assert_eq( 1/-0.0 == 1/ 0, false, 'float-int: 1/-0.0 == 1/ 0') +assert_eq( 1/-0.0 == -1/ 0, true, 'float-int: 1/-0.0 == -1/ 0') +assert_eq( 1/-0.0 == 1/-0, false, 'float-int: 1/-0.0 == 1/-0') +assert_eq( 1/-0.0 == -1/-0, true, 'float-int: 1/-0.0 == -1/-0') + +assert_eq( -1/-0.0 == 1/ 0, true, 'float-int: -1/-0.0 == 1/ 0') +assert_eq( -1/-0.0 == -1/ 0, false, 'float-int: -1/-0.0 == -1/ 0') +assert_eq( -1/-0.0 == 1/-0, true, 'float-int: -1/-0.0 == 1/-0') +assert_eq( -1/-0.0 == -1/-0, false, 'float-int: -1/-0.0 == -1/-0') diff --git a/client/space_lua/ast.ts b/client/space_lua/ast.ts index 41503b23..3718386c 100644 --- a/client/space_lua/ast.ts +++ b/client/space_lua/ast.ts @@ -2,6 +2,8 @@ type ASTContext = { ctx: ASTCtx; }; +export type NumericType = "int" | "float"; + export type ASTCtx = { from?: number; to?: number; @@ -165,6 +167,7 @@ export type LuaBooleanLiteral = { export type LuaNumberLiteral = { type: "Number"; value: number; + numericType: NumericType; } & ASTContext; export type LuaStringLiteral = { diff --git a/client/space_lua/eval.ts b/client/space_lua/eval.ts index b774693d..46d7ea96 100644 --- a/client/space_lua/eval.ts +++ b/client/space_lua/eval.ts @@ -154,6 +154,11 @@ export function evalExpression( return value.then((value) => { switch (e.operator) { case "-": + if (e.argument.type === "Number" && e.argument.numericType === "int") { + if (e.argument.value === 0) { + return 0; + } + } return -luaCoerceToNumber(singleResult(value)); case "+": return +singleResult(value); @@ -172,6 +177,11 @@ export function evalExpression( } else { switch (e.operator) { case "-": + if (e.argument.type === "Number" && e.argument.numericType === "int") { + if (e.argument.value === 0) { + return 0; + } + } return -luaCoerceToNumber(singleResult(value)); case "+": return +singleResult(value); diff --git a/client/space_lua/lua.test.ts b/client/space_lua/lua.test.ts index 96f5618b..5ccbd62d 100644 --- a/client/space_lua/lua.test.ts +++ b/client/space_lua/lua.test.ts @@ -9,6 +9,10 @@ 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"); }); diff --git a/client/space_lua/parse.ts b/client/space_lua/parse.ts index 11ce15b0..62566fcf 100644 --- a/client/space_lua/parse.ts +++ b/client/space_lua/parse.ts @@ -404,6 +404,7 @@ function parseExpression(t: ParseTree, ctx: ASTCtx): LuaExpression { type: "Number", // Use the integer parser fox 0x literals value: text.includes("x") ? parseInt(text) : parseFloat(text), + numericType: /[\.eEpP]/.test(text) ? "float" : "int", ctx: context(t, ctx), }; }