From 6aca7e0e0c0d4e6254684ac399b1f9c657f344fc Mon Sep 17 00:00:00 2001 From: BalaM314 <71201189+BalaM314@users.noreply.github.com> Date: Tue, 13 Aug 2024 21:38:13 +0530 Subject: [PATCH] builtin function aliases, MID->SUBSTRING --- build/builtin_functions.d.ts | 1 + build/builtin_functions.js | 30 +++++++++++++++++++----------- build/runtime-types.d.ts | 1 + src/builtin_functions.ts | 18 +++++++++++++----- src/runtime-types.ts | 1 + 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/build/builtin_functions.d.ts b/build/builtin_functions.d.ts index 86e4fed..e756f4b 100644 --- a/build/builtin_functions.d.ts +++ b/build/builtin_functions.d.ts @@ -14,6 +14,7 @@ type PreprocesssedBuiltinFunctionData): PrimitiveVariableTypeMapping; + aliases?: string[]; }; export declare const getBuiltinFunctions: () => Record & Partial>; export declare const preprocessedBuiltinFunctions: { diff --git a/build/builtin_functions.js b/build/builtin_functions.js index 14ed3dd..bf39939 100644 --- a/build/builtin_functions.js +++ b/build/builtin_functions.js @@ -4,17 +4,24 @@ function fn(data) { return data; } let builtinFunctions; -export const getBuiltinFunctions = () => builtinFunctions ?? (builtinFunctions = ((d) => Object.fromEntries(Object.entries(d).map(([name, data]) => [name, { - args: new Map(data.args.map(([name, type]) => [name, { - passMode: "reference", - type: (Array.isArray(type) ? type : [type]).map(t => Array.isArray(t) - ? new ArrayVariableType(null, null, t[0] == "ANY" ? null : PrimitiveVariableType.get(t[0]), [-1, -1]) - : PrimitiveVariableType.get(t)) - }])), - name, - impl: data.impl, - returnType: PrimitiveVariableType.get(data.returnType) - }])))(preprocessedBuiltinFunctions)); +export const getBuiltinFunctions = () => builtinFunctions ?? (builtinFunctions = ((d) => { + const obj = Object.fromEntries(Object.entries(d).map(([name, data]) => [name, { + args: new Map(data.args.map(([name, type]) => [name, { + passMode: "reference", + type: (Array.isArray(type) ? type : [type]).map(t => Array.isArray(t) + ? new ArrayVariableType(null, null, t[0] == "ANY" ? null : PrimitiveVariableType.get(t[0]), [-1, -1]) + : PrimitiveVariableType.get(t)), + }])), + aliases: data.aliases ?? [], + name, + impl: data.impl, + returnType: PrimitiveVariableType.get(data.returnType) + }])); + Object.values(obj).filter(v => v.aliases && v.aliases.length > 0).forEach(v => { + v.aliases.forEach(alias => obj[alias] = v); + }); + return obj; +})(preprocessedBuiltinFunctions)); export const preprocessedBuiltinFunctions = ({ LEFT: fn({ args: [ @@ -63,6 +70,7 @@ export const preprocessedBuiltinFunctions = ({ fail(`End of slice (${length} + ${start}) is greater than the length of the string (${chars.length})`, str); return chars.slice(start - 1, start + length - 1).join(""); }, + aliases: ["SUBSTRING"] }), LENGTH: fn({ args: [ diff --git a/build/runtime-types.d.ts b/build/runtime-types.d.ts index 1284b2d..62b79d1 100644 --- a/build/runtime-types.d.ts +++ b/build/runtime-types.d.ts @@ -259,6 +259,7 @@ export type BuiltinFunctionData = { returnType: VariableType | null; name: string; impl: (this: Runtime, ...args: (RangeAttached>)[]) => VariableValue; + aliases: string[]; }; export type ClassMethodStatement = ClassFunctionStatement | ClassProcedureStatement; export type ClassMethodData = ProgramASTBranchNode & { diff --git a/src/builtin_functions.ts b/src/builtin_functions.ts index be683cc..34527ea 100644 --- a/src/builtin_functions.ts +++ b/src/builtin_functions.ts @@ -67,6 +67,7 @@ type PreprocesssedBuiltinFunctionData):PrimitiveVariableTypeMapping; + aliases?: string[]; }; /** Wrapper function used to get the correct type definitions */ function fn(data:PreprocesssedBuiltinFunctionData){ @@ -75,8 +76,8 @@ function fn & Partial> => builtinFunctions ??= ( // eslint-disable-line @typescript-eslint/no-unsafe-return - (d:Record>):Record & Partial> => - Object.fromEntries(Object.entries(d).map(([name, data]) => + (d:Record>) => { + const obj:Record = Object.fromEntries(Object.entries(d).map(([name, data]) => [name, { args: new Map((data.args as BuiltinFunctionArg[]).map(([name, type]) => [name, { passMode: "reference", @@ -84,14 +85,20 @@ export const getBuiltinFunctions = ():Record>[]) => VariableTypeMapping), returnType: PrimitiveVariableType.get(data.returnType as PrimitiveVariableTypeName) }] - )) + )); + Object.values(obj).filter(v => v.aliases && v.aliases.length > 0).forEach(v => { + v.aliases.forEach(alias => obj[alias] = v); + }); + return obj; + } )(preprocessedBuiltinFunctions); export const preprocessedBuiltinFunctions = ({ //Source: s23 P22 insert @@ -138,6 +145,7 @@ export const preprocessedBuiltinFunctions = ({ if(length + start - 1 > chars.length) fail(`End of slice (${length} + ${start}) is greater than the length of the string (${chars.length})`, str); return chars.slice(start - 1, start + length - 1).join(""); }, + aliases: ["SUBSTRING"] }), //source: spec 5.5 LENGTH: fn({ diff --git a/src/runtime-types.ts b/src/runtime-types.ts index 3c0965b..108b2da 100644 --- a/src/runtime-types.ts +++ b/src/runtime-types.ts @@ -830,6 +830,7 @@ export type BuiltinFunctionData = { returnType:VariableType | null; name:string; impl: (this:Runtime, ...args:(RangeAttached>)[]) => VariableValue; + aliases: string[]; }; export type ClassMethodStatement = ClassFunctionStatement | ClassProcedureStatement; export type ClassMethodData = ProgramASTBranchNode & {