Space Lua: Improve numeric types semantics (#1803)
* Space Lua: Improve numeric types semantics Add numeric result normalization for arithmetic operations: * Integer operations preserve int type, collapse -0 to +0 * Float operations preserve float type, maintain -0.0 * Division always produces float results Fix for-loop variable type tracking: * Loop variable type determined by start/step parameters * Integer loops produce integer variables * Float/mixed loops produce float-tagged variables Implement table key normalization: * Integer-valued floats (1.0, 2.0) normalize to integer keys * All zero variants (0, 0.0, -0.0) map to same key * Non-integer floats (1.5) remain distinct keys Add tests for: * Arithmetic type preservation across operators * For-loop type modes (int/float/mixed) * Table key equivalence and normalization * Edge cases with zero, -0.0, and special values * Standard library `type` and `math.type` Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> * Code refactor and deduplication Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> --------- Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { LuaBuiltinFunction, LuaRuntimeError, LuaTable } from "../runtime.ts";
|
||||
import { getZeroBoxKind, isFloatTag, isNegativeZero } from "../numeric.ts";
|
||||
|
||||
export const mathApi = new LuaTable({
|
||||
// math constants
|
||||
@@ -14,8 +15,34 @@ export const mathApi = new LuaTable({
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof x === "number" || x instanceof Number) {
|
||||
if (isFloatTag(x)) {
|
||||
return "float";
|
||||
}
|
||||
|
||||
if (x instanceof Number) {
|
||||
const kind = getZeroBoxKind(x);
|
||||
if (kind === "float") {
|
||||
return "float";
|
||||
}
|
||||
if (kind === "int") {
|
||||
return "integer";
|
||||
}
|
||||
|
||||
const n = Number(x);
|
||||
if (!Number.isFinite(n)) {
|
||||
return "float";
|
||||
}
|
||||
|
||||
return Number.isInteger(n) ? "integer" : "float";
|
||||
}
|
||||
|
||||
if (typeof x === "number") {
|
||||
const n = Number(x);
|
||||
|
||||
// Negative zero is a float
|
||||
if (n === 0 && isNegativeZero(n)) {
|
||||
return "float";
|
||||
}
|
||||
|
||||
// NaN and +Inf/-Inf are floats
|
||||
if (!Number.isFinite(n)) {
|
||||
|
||||
Reference in New Issue
Block a user