diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-01-05 18:49:34 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-01-05 18:49:34 +0530 |
| commit | 9842f84ecd47011149c928287e63fb5de399e161 (patch) | |
| tree | cd243838ac7cba673d053855b3818b9ec0b4b3a6 | |
| parent | 1e2b8780906744ad51248ee7021bd5d1d1e86ebb (diff) | |
| download | ts-types-lang-9842f84ecd47011149c928287e63fb5de399e161.tar.gz ts-types-lang-9842f84ecd47011149c928287e63fb5de399e161.zip | |
feat: js expr in types now. aaaa. kill me pliz
Diffstat (limited to '')
| -rw-r--r-- | src/index.ts | 22 | ||||
| -rw-r--r-- | src/runtime.ts | 37 |
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) |
