diff options
| -rw-r--r-- | examples/custom-effect.ts | 15 | ||||
| -rw-r--r-- | examples/ffi.ts | 9 | ||||
| -rw-r--r-- | examples/guess-number.ts | 6 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | src/runtime.ts | 15 | ||||
| -rw-r--r-- | src/stdlib/io.ts | 2 |
6 files changed, 32 insertions, 17 deletions
diff --git a/examples/custom-effect.ts b/examples/custom-effect.ts index 948caf4..6a4b8b0 100644 --- a/examples/custom-effect.ts +++ b/examples/custom-effect.ts @@ -1,10 +1,15 @@ -import { DefineEffect, Effect } from '../src/stdlib' +import { Bind, DefineEffect, Effect, Kind1, Print } from '../src/stdlib' -interface Wow<_A, _B> extends Effect { } +interface Mathemagic<_A, _B> extends Effect { } + +interface PrintK extends Kind1 { + return: Print<this['input']> +} export type main = [ - DefineEffect<"Wow", `(a, b) => { - console.log(typeToString(a), '-->', typeToString(b)) + DefineEffect<"Mathemagic", `(a, b) => { + console.log(typeToString(a)) + return { result: 5 * b.getLiteralValue() } }`>, - Wow<"a", 69>, + Bind<Mathemagic<"a", 69>, PrintK>, ] diff --git a/examples/ffi.ts b/examples/ffi.ts index 7a73a0a..3310712 100644 --- a/examples/ffi.ts +++ b/examples/ffi.ts @@ -1,4 +1,4 @@ -import { Bind, Debug, JsExpr, Kind1 } from "../src/stdlib" +import { Bind, Debug, DefineEffect, Effect, JsExpr, Kind1 } from "../src/stdlib" interface PrintK<Label extends string = ""> extends Kind1 { return: Debug<Label, this['input']> @@ -6,7 +6,14 @@ interface PrintK<Label extends string = ""> extends Kind1 { type Square<N extends number> = JsExpr<`${N} ** 2`> +interface Mathemagic<_A, _B extends number> extends Effect { } + export type main = [ Bind<JsExpr<"{ boobaa: [5 * 3, 5 * 2] }">, PrintK<'|'>>, Bind<Square<69>, PrintK<"69^2 =">>, + DefineEffect<"Mathemagic", `(a, b) => { + console.log(typeToString(a)) + return { result: 5 * b.getLiteralValue() } + }`>, + Bind<Mathemagic<"a", 69>, PrintK<'69 * 5 ='>>, ] diff --git a/examples/guess-number.ts b/examples/guess-number.ts index 8f5d535..79f6ad1 100644 --- a/examples/guess-number.ts +++ b/examples/guess-number.ts @@ -1,4 +1,4 @@ -import { Print, PutString, Bind, Kind1, JsExpr, ReadLine, Then, PutStringLn } from '../src/stdlib' +import { Print, PutString, Bind, Kind1, JsExpr, ReadLine, Do, PutStringLn } from '../src/stdlib' export type main = [ PutStringLn<"You have 5 guesses">, @@ -11,7 +11,7 @@ export type main = [ interface AskForGuess<N extends number, Attempts extends 0[]> extends Kind1<string> { return: `${this['input']}` extends `${N}` ? PutStringLn<"Yay! You got it right!"> - : Then<[ + : Do<[ Print<`Wrong guess. Total attempts: ${ [...Attempts, 0] extends infer Ls extends 0[] ? Ls['length'] : 0 }`>, @@ -23,7 +23,7 @@ interface StartGuessing<Attempts extends 0[] = []> extends Kind1<number> { return: Attempts['length'] extends 5 ? PutStringLn<"Max attempts exceeded. Game over!"> : Bind< - Then<[ + Do<[ PutString<"Your guess? ">, ReadLine ]>, diff --git a/package.json b/package.json index 0028d3d..1f98a54 100644 --- a/package.json +++ b/package.json @@ -11,13 +11,13 @@ "watch": "nodemon --exec 'clear && yarn runtime' -e .ts" }, "dependencies": { - "@types/node": "^18.11.18", "ts-morph": "^17.0.1", "ts-node": "^10.9.1", "typescript": "^4.9.4", "uuid": "^9.0.0" }, "devDependencies": { + "@types/node": "^18.11.18", "@types/uuid": "^9.0.0", "nodemon": "^2.0.20" } diff --git a/src/runtime.ts b/src/runtime.ts index 91393be..0a65bf3 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -50,7 +50,7 @@ const addResult = (name: string, ty: string): Node | undefined => { } } -const customEffects: Record<string, (...args: Type[]) => void> = {} +const customEffects: Record<string, (...args: Type[]) => any> = {} const evaluateType = async (effTyp: Type, node: Node): Promise<string[]> => { const name = effTyp.getSymbol()?.getName() @@ -158,7 +158,7 @@ const evaluateType = async (effTyp: Type, node: Node): Promise<string[]> => { return [hash] }, - Then: async () => { + Do: async () => { const [effectTyps] = effTyp.getTypeArguments() const effectResults = await evalList(effectTyps?.getTupleElements() ?? [], node) const resultKey = effectResults[effectResults.length - 1] @@ -169,7 +169,12 @@ const evaluateType = async (effTyp: Type, node: Node): Promise<string[]> => { _: async () => { if (name && customEffects[name]) { - customEffects[name](...effTyp.getTypeArguments()) + const out = await customEffects[name](...effTyp.getTypeArguments()) + if (out) { + const hash = uuid() + addResult(hash, `${JSON.stringify(out)}`) + return [hash] + } } else { console.log(`${name} result effect is unhandled`) } @@ -192,9 +197,7 @@ const main = async () => { if (resultType) { const effects = resultType.isTuple() ? resultType.getTupleElements() : [resultType] - for (const typ of effects) { - await evaluateType(typ, typeRefNode) - } + await evalList(effects, typeRefNode) } } } diff --git a/src/stdlib/io.ts b/src/stdlib/io.ts index d256628..c78204f 100644 --- a/src/stdlib/io.ts +++ b/src/stdlib/io.ts @@ -9,7 +9,7 @@ export interface Bind<_Eff extends Effect, _Fn extends Kind1> extends Effect { } export interface Seq<_Effs extends Effect[]> extends Effect { } -export interface Then<_Effs extends Effect[]> extends Effect { } +export interface Do<_Effs extends Effect[]> extends Effect { } export interface DefineEffect<_Name extends string, _Func extends string> extends Effect { } |
