aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/index.ts22
-rw-r--r--src/runtime.ts37
2 files changed, 52 insertions, 7 deletions
diff --git a/src/index.ts b/src/index.ts
index f7e00b7..6097752 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -3,13 +3,17 @@ export type Effect = EffectAtom[]
export interface PrintString<_ extends string> extends EffectAtom { }
export interface Print<_ extends any> extends EffectAtom { }
-// interface Debug<_ extends string, T> extends EffectAtom<T> { }
+export interface Debug<_ extends string, T> extends EffectAtom<T> { }
export interface WriteFile<_Path extends string, _Content extends string> extends EffectAtom { }
export interface ReadFile<_Path extends string> extends EffectAtom<string> { }
export interface GetEnv<_Name extends string> extends EffectAtom<string> { }
+export interface ReadLine extends EffectAtom<string> { }
+
+export interface JsExpr<_Expr extends string> extends EffectAtom<any> {}
+
export interface Program<Effs extends Effect, ExitCode extends number = 0> {
effects: Effs,
exitCode: ExitCode,
@@ -25,13 +29,21 @@ export interface ChainIO<Eff extends EffectAtom, Fn extends Kind1> extends Effec
chainTo: Fn
}
-export interface PrintK extends Kind1<string | undefined> {
- return: Print<this['input']>
+interface PrintK<Label extends string = ""> extends Kind1<unknown> {
+ return: Debug<Label, this['input']>
}
+// interface WithInputK extends Kind1<string> {
+// return: ChainIO<ReadLine, PrintK<this['input']>>
+// }
+
+// ChainIO<ReadLine, WithInputK>,
+// ChainIO<ReadFile<"./default.nix">, PrintK>,
+// Print<"Your name?:">,
+// ChainIO<ReadLine, PrintK<"Hello,">>,
+
export type main = Program<[
[1, 2, 3] extends infer Res ? Print<Res> : never,
- Print<`wow: ${string} -> ${'helo'}`>,
ChainIO<GetEnv<"NODE_ENV">, PrintK>,
- ChainIO<ReadFile<"./default.nix">, PrintK>,
+ ChainIO<JsExpr<"{ boobaa: [5 * 3, 5 * 2] }">, PrintK>,
]>
diff --git a/src/runtime.ts b/src/runtime.ts
index 231dcd3..11a7e61 100644
--- a/src/runtime.ts
+++ b/src/runtime.ts
@@ -1,6 +1,17 @@
import { Project, ScriptTarget, Type, Node, StringLiteral, TypeFormatFlags, SyntaxKind } from 'ts-morph'
import path from 'path'
import { promises as fs } from 'fs'
+import readline from 'readline';
+
+const rl = readline.createInterface({
+ input: process.stdin,
+ output: process.stdout,
+ terminal: false
+});
+
+const readLineFromStdin = (): Promise<string> => new Promise((res) => {
+ rl.on('line', res)
+})
const project = new Project({
compilerOptions: {
@@ -75,6 +86,22 @@ const accumulateResults = async (effTyp: Type, node: Node): Promise<string[]> =>
return [hash]
},
+ ReadLine: async () => {
+ const line = await readLineFromStdin()
+ const hash = createHash()
+ addResult(hash, `${JSON.stringify(line)}`)
+ return [hash]
+ },
+
+ JsExpr: async () => {
+ const [exprTyp] = effTyp.getTypeArguments()
+ const exprStr = JSON.parse(typeToString(exprTyp))
+ const result = eval(exprStr)
+ const hash = createHash()
+ addResult(hash, `${JSON.stringify(result)}`)
+ return [hash]
+ },
+
_: async () => {
console.log(`${name} result effect is unhandled`)
return []
@@ -91,6 +118,13 @@ const evalAccumulator = async (effNode: Node, node: Node) => {
console.log(...effTyp.getTypeArguments().map(typeToString));
},
+ Debug: async () => {
+ const [labelTyp, valueTyp] = effTyp.getTypeArguments()
+ const label = JSON.parse(typeToString(labelTyp))
+ const value = JSON.parse(typeToString(valueTyp))
+ console.log(label, value)
+ },
+
ReadFile: async () => {
const [hash] = await accumulateResults(effTyp, node)
effNode.replaceWithText(`${RESULT_TYPE_NAME}[${JSON.stringify(hash)}]`)
@@ -104,9 +138,8 @@ const evalAccumulator = async (effNode: Node, node: Node) => {
},
ChainIO: async () => {
- const inputTyp = effTyp.getProperty('input')?.getTypeAtLocation(node)
const chainToKind = effTyp.getProperty('chainTo')?.getTypeAtLocation(node)
- const [hashRes] = inputTyp ? await accumulateResults(inputTyp, node) : []
+ const [hashRes] = await accumulateResults(effTyp, node)
const chainRes = `(${typeToString(chainToKind)} & { input: ${RESULT_TYPE_NAME}[${JSON.stringify(hashRes)}]['output'] })['return']`
const updateEffNode = effNode.replaceWithText(chainRes)
await evalAccumulator(updateEffNode, node)