diff options
Diffstat (limited to 'src/js-lang/builtin')
| -rw-r--r-- | src/js-lang/builtin/builtin.ts | 39 | ||||
| -rw-r--r-- | src/js-lang/builtin/index.ts | 2 | ||||
| -rw-r--r-- | src/js-lang/builtin/sbuiltin.ts | 46 |
3 files changed, 0 insertions, 87 deletions
diff --git a/src/js-lang/builtin/builtin.ts b/src/js-lang/builtin/builtin.ts deleted file mode 100644 index dde91b6..0000000 --- a/src/js-lang/builtin/builtin.ts +++ /dev/null @@ -1,39 +0,0 @@ -type BUILTIN = (args: any[]) => any; - -export const V_BUILTIN_Arr: BUILTIN = (args) => args; - -// FIXME actually implement this properly -export const V_BUILTIN_ToString: BUILTIN = (args) => - args.length === 1 ? JSON.stringify(args[0]) : JSON.stringify(args); - -export const V_BUILTIN_Add: BUILTIN = (args) => { - if (args.every((arg) => ["string", "number"].includes(typeof arg))) { - return args.reduce( - (acc, cur) => acc + cur, - typeof args[0] === "string" ? "" : 0 - ); - } - - throw new Error(`Cannot add operands ${JSON.stringify(args, undefined, 2)}`); -}; - -export const V_BUILTIN_Mul: BUILTIN = (args) => { - if (args.every((arg) => typeof arg === "number") && args.length === 2) { - return args.reduce((acc, cur) => acc * cur, 1); - } - - throw new Error( - `Can only multiply [number, number], but got ${JSON.stringify( - args, - undefined, - 2 - )}` - ); -}; - -export const nameToBUILTIN: Record<string, BUILTIN> = { - arr: V_BUILTIN_Arr, - tostring: V_BUILTIN_ToString, - add: V_BUILTIN_Add, - mul: V_BUILTIN_Mul, -}; diff --git a/src/js-lang/builtin/index.ts b/src/js-lang/builtin/index.ts deleted file mode 100644 index 00e77f7..0000000 --- a/src/js-lang/builtin/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./builtin"; -export * from "./sbuiltin"; diff --git a/src/js-lang/builtin/sbuiltin.ts b/src/js-lang/builtin/sbuiltin.ts deleted file mode 100644 index 44c969d..0000000 --- a/src/js-lang/builtin/sbuiltin.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { callFn, getEvaluatedChildren } from "../core/eval"; -import { ASTNode, FnPrim, StackFrame } from "../../ts-lang"; - -type SBUILTIN = (node: ASTNode, frame: StackFrame) => any; - -export const V_SBUITLIN_Call: SBUILTIN = (node, frame) => { - const children = getEvaluatedChildren(node, frame); - const fn = children[0] as FnPrim | undefined; - - if (!fn?.fn) { - throw new Error( - `Invalid params for function call: ${JSON.stringify( - children, - undefined, - 2 - )}` - ); - } - - return callFn(fn, children.slice(1), frame); -}; - -export const V_SBUILTIN_Map: SBUILTIN = (node, frame) => { - const children = getEvaluatedChildren(node, frame); - const fn = children[1] as FnPrim | undefined; - - if (!fn?.fn) { - throw new Error( - `Invalid params for map: ${JSON.stringify(children, undefined, 2)}` - ); - } - - const values = children[0]; - - if (!Array.isArray(values)) { - // add to ts - throw new Error(`Can't map non-array value: ${values}`); - } - - return values.map((v, i) => callFn(fn, [v, i], frame)); -}; - -export const nameToSBUILTIN: Record<string, SBUILTIN> = { - call: V_SBUITLIN_Call, - map: V_SBUILTIN_Map, -}; |
