diff options
Diffstat (limited to 'src/lang/js-lang/builtin')
| -rw-r--r-- | src/lang/js-lang/builtin/builtin.ts | 39 | ||||
| -rw-r--r-- | src/lang/js-lang/builtin/index.ts | 2 | ||||
| -rw-r--r-- | src/lang/js-lang/builtin/sbuiltin.ts | 46 |
3 files changed, 87 insertions, 0 deletions
diff --git a/src/lang/js-lang/builtin/builtin.ts b/src/lang/js-lang/builtin/builtin.ts new file mode 100644 index 0000000..dde91b6 --- /dev/null +++ b/src/lang/js-lang/builtin/builtin.ts @@ -0,0 +1,39 @@ +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/lang/js-lang/builtin/index.ts b/src/lang/js-lang/builtin/index.ts new file mode 100644 index 0000000..00e77f7 --- /dev/null +++ b/src/lang/js-lang/builtin/index.ts @@ -0,0 +1,2 @@ +export * from "./builtin"; +export * from "./sbuiltin"; diff --git a/src/lang/js-lang/builtin/sbuiltin.ts b/src/lang/js-lang/builtin/sbuiltin.ts new file mode 100644 index 0000000..44c969d --- /dev/null +++ b/src/lang/js-lang/builtin/sbuiltin.ts @@ -0,0 +1,46 @@ +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, +}; |
