aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/file.ts16
-rw-r--r--src/eval.ts22
-rw-r--r--stdlib/exception.ts5
3 files changed, 40 insertions, 3 deletions
diff --git a/examples/file.ts b/examples/file.ts
index d91b68a..fd5728b 100644
--- a/examples/file.ts
+++ b/examples/file.ts
@@ -1,9 +1,23 @@
import { Bind, Kind1 } from '../stdlib/effect'
import { PutStringLn } from '../stdlib/stdio'
import { ReadFile } from '../stdlib/fs'
+import { Try } from '../stdlib/exception'
interface PrintK extends Kind1<string> {
return: PutStringLn<this['input']>
}
-export type main = Bind<ReadFile<'./default.nix'>, PrintK>
+interface ConstK<Val> extends Kind1<unknown, Val> {
+ return: Val
+}
+
+export type main = [
+ Bind<ReadFile<'./default.nix'>, PrintK>,
+ Bind<
+ Try<
+ ReadFile<'./unicorn'>,
+ ConstK<"hello world">
+ >,
+ PrintK
+ >,
+]
diff --git a/src/eval.ts b/src/eval.ts
index c34e911..12997b1 100644
--- a/src/eval.ts
+++ b/src/eval.ts
@@ -4,6 +4,7 @@ import readline from 'readline'
import { match } from './util'
import { Ctx } from './types'
+import { assert } from 'console'
const rl = readline.createInterface({
input: process.stdin,
@@ -117,8 +118,25 @@ export const evaluateType = async (
},
Exit: async () => {
- const exitCode = args[0] && ctx.getTypeValue(args[0])
- process.exit(exitCode)
+ process.exit(args[0] && ctx.getTypeValue(args[0]))
+ },
+
+ Try: async () => {
+ const [effTyp, catchK] = args
+
+ try {
+ if (!effTyp) throw new Error('wow')
+ return await evaluateType(ctx, effTyp)
+ } catch(e) {
+ const error = JSON.stringify((e as any)?.message ?? e)
+ const catchResExpr = `(${ctx.typeToString(catchK)} & { input: ${error} })['return']`
+ const [resultKey, _] = ctx.createResult(catchResExpr)
+ return [resultKey]
+ }
+ },
+
+ Throw: async () => {
+ throw args[0] && ctx.getTypeValue(args[0])
},
ReadLine: async () => {
diff --git a/stdlib/exception.ts b/stdlib/exception.ts
new file mode 100644
index 0000000..731b3b0
--- /dev/null
+++ b/stdlib/exception.ts
@@ -0,0 +1,5 @@
+import { Effect, Kind1 } from "./effect";
+
+export interface Try<_E extends Effect, _Catch extends Kind1> extends Effect {}
+
+export interface Throw<_E> extends Effect {}