Skip to content

Commit

Permalink
builtin function aliases, MID->SUBSTRING
Browse files Browse the repository at this point in the history
  • Loading branch information
BalaM314 committed Aug 13, 2024
1 parent 59c8b50 commit 6aca7e0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions build/builtin_functions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type PreprocesssedBuiltinFunctionData<TArgs extends BuiltinFunctionArg[], TRetur
args: TArgs;
returnType: TReturn;
impl(this: Runtime, ...args: FunctionArgs<TArgs>): PrimitiveVariableTypeMapping<TReturn>;
aliases?: string[];
};
export declare const getBuiltinFunctions: () => Record<keyof typeof preprocessedBuiltinFunctions, BuiltinFunctionData> & Partial<Record<string, BuiltinFunctionData>>;
export declare const preprocessedBuiltinFunctions: {
Expand Down
30 changes: 19 additions & 11 deletions build/builtin_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down
1 change: 1 addition & 0 deletions build/runtime-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export type BuiltinFunctionData = {
returnType: VariableType | null;
name: string;
impl: (this: Runtime, ...args: (RangeAttached<BoxPrimitive<VariableValue>>)[]) => VariableValue;
aliases: string[];
};
export type ClassMethodStatement = ClassFunctionStatement | ClassProcedureStatement;
export type ClassMethodData = ProgramASTBranchNode & {
Expand Down
18 changes: 13 additions & 5 deletions src/builtin_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type PreprocesssedBuiltinFunctionData<TArgs extends BuiltinFunctionArg[], TRetur
args: TArgs;
returnType: TReturn;
impl(this:Runtime, ...args:FunctionArgs<TArgs>):PrimitiveVariableTypeMapping<TReturn>;
aliases?: string[];
};
/** Wrapper function used to get the correct type definitions */
function fn<const T extends BuiltinFunctionArg[], const S extends PrimitiveVariableTypeName>(data:PreprocesssedBuiltinFunctionData<T, S>){
Expand All @@ -75,23 +76,29 @@ function fn<const T extends BuiltinFunctionArg[], const S extends PrimitiveVaria

let builtinFunctions; //cache
export const getBuiltinFunctions = ():Record<keyof typeof preprocessedBuiltinFunctions, BuiltinFunctionData> & Partial<Record<string, BuiltinFunctionData>> => builtinFunctions ??= ( // eslint-disable-line @typescript-eslint/no-unsafe-return
<T extends string>(d:Record<T, PreprocesssedBuiltinFunctionData<any, any>>):Record<T, BuiltinFunctionData> & Partial<Record<string, BuiltinFunctionData>> =>
Object.fromEntries(Object.entries(d).map(([name, data]) =>
<T extends string>(d:Record<T, PreprocesssedBuiltinFunctionData<any, any>>) => {
const obj:Record<string, BuiltinFunctionData> = Object.fromEntries(Object.entries(d).map(([name, data]) =>
[name, {
args: new Map((data.args as BuiltinFunctionArg[]).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)
)
}])),
),
}] as const)),
aliases: data.aliases ?? [],
name,
//Unsound cast
impl: data.impl as never as ((this:Runtime, ...args:RangeAttached<BoxPrimitive<VariableValue>>[]) => VariableTypeMapping<PrimitiveVariableType>),
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
Expand Down Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions src/runtime-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ export type BuiltinFunctionData = {
returnType:VariableType | null;
name:string;
impl: (this:Runtime, ...args:(RangeAttached<BoxPrimitive<VariableValue>>)[]) => VariableValue;
aliases: string[];
};
export type ClassMethodStatement = ClassFunctionStatement | ClassProcedureStatement;
export type ClassMethodData = ProgramASTBranchNode & {
Expand Down

0 comments on commit 6aca7e0

Please sign in to comment.