Lua cleanup

main
Zef Hemel 2025-01-08 11:39:00 +01:00
parent fc1ff7dc5b
commit fc9213c7ca
1 changed files with 44 additions and 58 deletions

View File

@ -376,9 +376,25 @@ function evalPrefixExpression(
} }
} }
// Mapping table of operators meta-methods to their corresponding operator // Helper functions to reduce duplication
function evalMetamethod(
left: any,
right: any,
metaMethod: string,
ctx: ASTCtx,
sf: LuaStackFrame,
): LuaValue | undefined {
if (left?.metatable?.has(metaMethod)) {
const fn = left.metatable.get(metaMethod);
return luaCall(fn, [left, right], ctx, sf);
} else if (right?.metatable?.has(metaMethod)) {
const fn = right.metatable.get(metaMethod);
return luaCall(fn, [left, right], ctx, sf);
}
}
type LuaMetaMethod = Record<string, { // Simplified operator definitions
const operatorsMetaMethods: Record<string, {
metaMethod?: string; metaMethod?: string;
nativeImplementation: ( nativeImplementation: (
a: LuaValue, a: LuaValue,
@ -386,46 +402,24 @@ type LuaMetaMethod = Record<string, {
ctx: ASTCtx, ctx: ASTCtx,
sf: LuaStackFrame, sf: LuaStackFrame,
) => LuaValue; ) => LuaValue;
}>; }> = {
"+": { metaMethod: "__add", nativeImplementation: (a, b) => a + b },
const operatorsMetaMethods: LuaMetaMethod = { "-": { metaMethod: "__sub", nativeImplementation: (a, b) => a - b },
"+": { "*": { metaMethod: "__mul", nativeImplementation: (a, b) => a * b },
metaMethod: "__add", "/": { metaMethod: "__div", nativeImplementation: (a, b) => a / b },
nativeImplementation: (a, b) => a + b,
},
"-": {
metaMethod: "__sub",
nativeImplementation: (a, b) => a - b,
},
"*": {
metaMethod: "__mul",
nativeImplementation: (a, b) => a * b,
},
"/": {
metaMethod: "__div",
nativeImplementation: (a, b) => a / b,
},
"//": { "//": {
metaMethod: "__idiv", metaMethod: "__idiv",
nativeImplementation: (a, b) => Math.floor(a / b), nativeImplementation: (a, b, ctx, sf) => Math.floor(a / b),
},
"%": {
metaMethod: "__mod",
nativeImplementation: (a, b) => a % b,
},
"^": {
metaMethod: "__pow",
nativeImplementation: (a, b) => a ** b,
}, },
"%": { metaMethod: "__mod", nativeImplementation: (a, b) => a % b },
"^": { metaMethod: "__pow", nativeImplementation: (a, b) => a ** b },
"..": { "..": {
metaMethod: "__concat", metaMethod: "__concat",
nativeImplementation: (a, b) => { nativeImplementation: (a, b) => {
const aString = luaToString(a); const aString = luaToString(a);
const bString = luaToString(b); const bString = luaToString(b);
if (aString instanceof Promise || bString instanceof Promise) { if (aString instanceof Promise || bString instanceof Promise) {
return Promise.all([aString, bString]).then(([aString, bString]) => return Promise.all([aString, bString]).then(([a, b]) => a + b);
aString + bString
);
} else { } else {
return aString + bString; return aString + bString;
} }
@ -443,28 +437,15 @@ const operatorsMetaMethods: LuaMetaMethod = {
metaMethod: "__ne", metaMethod: "__ne",
nativeImplementation: (a, b) => a !== b, nativeImplementation: (a, b) => a !== b,
}, },
"<": { "<": { metaMethod: "__lt", nativeImplementation: (a, b) => a < b },
metaMethod: "__lt", "<=": { metaMethod: "__le", nativeImplementation: (a, b) => a <= b },
nativeImplementation: (a, b) => a < b, ">": { nativeImplementation: (a, b, ctx, sf) => !luaOp("<=", a, b, ctx, sf) },
}, ">=": { nativeImplementation: (a, b, ctx, sf) => !luaOp("<", a, b, ctx, sf) },
"<=": { "and": {
metaMethod: "__le",
nativeImplementation: (a, b) => a <= b,
},
">": {
nativeImplementation: (a, b, ctx, sf) => !luaOp("<=", a, b, ctx, sf),
},
">=": {
nativeImplementation: (a, b, ctx, cf) => !luaOp("<", a, b, ctx, cf),
},
and: {
metaMethod: "__and", metaMethod: "__and",
nativeImplementation: (a, b) => a && b, nativeImplementation: (a, b) => a && b,
}, },
or: { "or": { metaMethod: "__or", nativeImplementation: (a, b) => a || b },
metaMethod: "__or",
nativeImplementation: (a, b) => a || b,
},
}; };
function luaOp( function luaOp(
@ -478,15 +459,20 @@ function luaOp(
if (!operatorHandler) { if (!operatorHandler) {
throw new LuaRuntimeError(`Unknown operator ${op}`, sf.withCtx(ctx)); throw new LuaRuntimeError(`Unknown operator ${op}`, sf.withCtx(ctx));
} }
if (operatorHandler.metaMethod) { if (operatorHandler.metaMethod) {
if (left?.metatable?.has(operatorHandler.metaMethod)) { const result = evalMetamethod(
const fn = left.metatable.get(operatorHandler.metaMethod); left,
return luaCall(fn, [left, right], ctx, sf); right,
} else if (right?.metatable?.has(operatorHandler.metaMethod)) { operatorHandler.metaMethod,
const fn = right.metatable.get(operatorHandler.metaMethod); ctx,
return luaCall(fn, [left, right], ctx, sf); sf,
);
if (result !== undefined) {
return result;
} }
} }
return operatorHandler.nativeImplementation(left, right, ctx, sf); return operatorHandler.nativeImplementation(left, right, ctx, sf);
} }