diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-01-06 20:07:28 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-01-06 20:07:28 +0530 |
| commit | a56f093bf08780ab7ed7ef196f031c193a522922 (patch) | |
| tree | f7328d5c154cd7d4521b3bc9575cf89fe6671d5e /src | |
| parent | d0baefe81d90d44f61322716e20850a7f6f5eaa5 (diff) | |
| download | ts-types-lang-a56f093bf08780ab7ed7ef196f031c193a522922.tar.gz ts-types-lang-a56f093bf08780ab7ed7ef196f031c193a522922.zip | |
feat: we got custom effects baebeeyyy
Diffstat (limited to '')
| -rw-r--r-- | src/index.ts | 13 | ||||
| -rw-r--r-- | src/runtime.ts | 44 | ||||
| -rw-r--r-- | src/stdlib/fs.ts | 6 | ||||
| -rw-r--r-- | src/stdlib/io.ts | 17 | ||||
| -rw-r--r-- | src/stdlib/stdio.ts | 10 | ||||
| -rw-r--r-- | src/stdlib/sys.ts | 8 |
6 files changed, 48 insertions, 50 deletions
diff --git a/src/index.ts b/src/index.ts index df2643a..e764623 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { Program, Print, PutString, Bind, Debug, Kind1, GetEnv, JsExpr, ReadFile, GetArgs, ReadLine, Seq } from './stdlib' +import { Print, PutString, Bind, Debug, Kind1, GetEnv, JsExpr, ReadFile, GetArgs, ReadLine, Seq, Effect, DefineEffect } from './stdlib' interface PrintK<Label extends string = ""> extends Kind1<unknown> { return: Debug<Label, this['input']> @@ -12,7 +12,14 @@ interface StartGuessing extends Kind1<number> { return: Bind<Seq<[PutString<"Your guess? ">, ReadLine]>, AskForGuess<this['input']>> } -export type main = Program<[ +interface Wow<_A, _B> extends Effect { } + +export type main = [ + DefineEffect<"Wow", `(a, b) => { + console.log(typeToString(a), '-->', typeToString(b)) + }`>, + Wow<"a", 69>, + Bind<GetArgs, PrintK>, PutString<"1,2,3? ">, [1, 2, 3] extends infer Res ? Print<Res> : never, @@ -34,5 +41,5 @@ export type main = Program<[ Print<"Wow">, Bind<JsExpr<"200 * 2">, PrintK<"200 * 2 =">>, ]>, -]> +] diff --git a/src/runtime.ts b/src/runtime.ts index 164e66e..bac9415 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -58,10 +58,22 @@ const addResult = (name: string, ty: string): Node | undefined => { const match = <K extends string, R>(k: K | undefined, pattern: { [key in K | '_']: () => R }) => k && pattern[k] ? pattern[k]() : pattern._() +const customEffects: Record<string, (...args: Type[]) => void> = {} + const accumulateResults = async (effTyp: Type, node: Node): Promise<string[]> => { const name = effTyp.getSymbol()?.getName() return match(name, { + DefineEffect: async () => { + const [nameTyp, exprTyp] = effTyp.getTypeArguments() + const name = nameTyp.getLiteralValue() as string + const exprStr = exprTyp.getLiteralValue() as string + const func = eval(exprStr) + + Object.assign(customEffects, { [name]: func }) + return [] + }, + Print: async () => { console.log(...effTyp.getTypeArguments().map(typeToString)); return [] @@ -160,38 +172,25 @@ const accumulateResults = async (effTyp: Type, node: Node): Promise<string[]> => }, _: async () => { - console.log(`${name} result effect is unhandled`) + if (name && customEffects[name]) { + customEffects[name](...effTyp.getTypeArguments()) + } else { + console.log(`${name} result effect is unhandled`) + } return [] }, }) } -// const evalAccumulator = async (effNode: Node, node: Node) => { -// const effTyp = effNode.getType() -// await accumulateResults(effTyp, node) -// } - const main = async () => { if (typeRefNode) { const resultType = entryPoint?.getType() - if (typeRefNode && entryPoint && resultType?.getSymbol()?.getName() === 'Program') { - const exitCodeTy = getPropertyType(typeRefNode, 'exitCode') - const effectTypes = getPropertyType(typeRefNode, 'effects') - if (effectTypes?.isTuple()) { - for (const typ of effectTypes.getTupleElements()) { - await accumulateResults(typ, typeRefNode) - } - } - - const exitCode = exitCodeTy?.getLiteralValue() as number - - if (exitCode !== 0) { - process.exit(exitCode) + if (resultType) { + const effects = resultType.isTuple() ? resultType.getTupleElements() : [resultType] + for (const typ of effects) { + await accumulateResults(typ, typeRefNode) } - } else { - const ty = typeChecker.getTypeAtLocation(typeRefNode) - console.log(typeToString(ty)) } } } @@ -200,6 +199,7 @@ main() .then(() => { // console.log(entryPoint?.print()) // console.log(resultTypeNode?.print()) + rl.close() process.exit(0) }) .catch(e => (console.error(e), process.exit(1))) diff --git a/src/stdlib/fs.ts b/src/stdlib/fs.ts index 0bd6a2c..1374da5 100644 --- a/src/stdlib/fs.ts +++ b/src/stdlib/fs.ts @@ -1,6 +1,6 @@ -import { EffectAtom } from './io' +import { Effect } from './io' -export interface WriteFile<_Path extends string, _Content extends string> extends EffectAtom { } +export interface WriteFile<_Path extends string, _Content extends string> extends Effect { } -export interface ReadFile<_Path extends string> extends EffectAtom<string> { } +export interface ReadFile<_Path extends string> extends Effect<string> { } diff --git a/src/stdlib/io.ts b/src/stdlib/io.ts index c1e4574..d8e3ed2 100644 --- a/src/stdlib/io.ts +++ b/src/stdlib/io.ts @@ -1,22 +1,13 @@ -export interface EffectAtom<T = unknown> { output: T } -export type Effect = EffectAtom[] +export interface Effect<T = unknown> { output: T } export interface Kind1<Inp = unknown, Out = unknown> { input: Inp return: Out } -export interface Program<Effs extends Effect, ExitCode extends number = 0> { - effects: Effs, - exitCode: ExitCode, -} +export interface Bind<_Eff extends Effect, _Fn extends Kind1> extends Effect { } -export interface Bind<Eff extends EffectAtom, Fn extends Kind1> extends EffectAtom { - input: Eff - chainTo: Fn -} +export interface Seq<_Effs extends Effect[]> extends Effect { } -export interface Seq<Effs extends EffectAtom[]> extends EffectAtom { - effects: Effs -} +export interface DefineEffect<_Name extends string, _Func extends string> extends Effect { } diff --git a/src/stdlib/stdio.ts b/src/stdlib/stdio.ts index 96c27f8..b20d2da 100644 --- a/src/stdlib/stdio.ts +++ b/src/stdlib/stdio.ts @@ -1,10 +1,10 @@ -import { EffectAtom } from './io' +import { Effect } from './io' -export interface PutString<_ extends string> extends EffectAtom { } +export interface PutString<_ extends string> extends Effect { } -export interface Print<_ extends any> extends EffectAtom { } +export interface Print<_ extends any> extends Effect { } -export interface Debug<_ extends string, T> extends EffectAtom<T> { } +export interface Debug<_ extends string, T> extends Effect<T> { } -export interface ReadLine extends EffectAtom<string> { } +export interface ReadLine extends Effect<string> { } diff --git a/src/stdlib/sys.ts b/src/stdlib/sys.ts index 1a9b18e..03541be 100644 --- a/src/stdlib/sys.ts +++ b/src/stdlib/sys.ts @@ -1,8 +1,8 @@ -import { EffectAtom } from './io' +import { Effect } from './io' -export interface GetEnv<_Name extends string> extends EffectAtom<string> { } +export interface GetEnv<_Name extends string> extends Effect<string> { } -export interface GetArgs extends EffectAtom<string[]> { } +export interface GetArgs extends Effect<string[]> { } -export interface JsExpr<_Expr extends string> extends EffectAtom<any> { } +export interface JsExpr<_Expr extends string> extends Effect<any> { } |
