summaryrefslogtreecommitdiff
path: root/src/lang/ks-lang
diff options
context:
space:
mode:
Diffstat (limited to 'src/lang/ks-lang')
-rw-r--r--src/lang/ks-lang/index.ts27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/lang/ks-lang/index.ts b/src/lang/ks-lang/index.ts
new file mode 100644
index 0000000..c60203f
--- /dev/null
+++ b/src/lang/ks-lang/index.ts
@@ -0,0 +1,27 @@
+import { callFn, emptyStackFrame, evaluate, lex, parse } from "../js-lang";
+import {
+ Evaluate,
+ FnPrim,
+ Lex,
+ Parse,
+ EvalError,
+ CallFn,
+ EmptyStackFrame,
+} from "../ts-lang";
+
+export const createFn = <Program extends string>(
+ program: Program
+): Evaluate<Parse<Lex<Program>>> extends [infer ProgramFn extends FnPrim]
+ ? <const Args extends any[]>(
+ ...args: Args
+ ) => CallFn<ProgramFn, Args, EmptyStackFrame>
+ : EvalError<"Cannot create a function from a program that does not eval to a function"> => {
+ const [programFn] = evaluate(parse(lex(program))) as Array<FnPrim>;
+ if (!programFn.fn) {
+ throw new Error(
+ "Cannot create a function from a program that does not eval to a function"
+ );
+ }
+
+ return ((...args: any[]) => callFn(programFn, args, emptyStackFrame)) as any;
+};