From fccd7917e62a3fbe3b0637ee630d09a9bdbf0678 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 5 Jan 2023 18:10:44 +0530 Subject: feat: adds get env type func --- src/index.ts | 5 ++++- src/runtime.ts | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 87eea0b..f7e00b7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,8 @@ export interface Print<_ extends any> extends EffectAtom { } export interface WriteFile<_Path extends string, _Content extends string> extends EffectAtom { } export interface ReadFile<_Path extends string> extends EffectAtom { } +export interface GetEnv<_Name extends string> extends EffectAtom { } + export interface Program { effects: Effs, exitCode: ExitCode, @@ -23,12 +25,13 @@ export interface ChainIO extends Effec chainTo: Fn } -export interface PrintK extends Kind1 { +export interface PrintK extends Kind1 { return: Print } export type main = Program<[ [1, 2, 3] extends infer Res ? Print : never, Print<`wow: ${string} -> ${'helo'}`>, + ChainIO, PrintK>, ChainIO, PrintK>, ]> diff --git a/src/runtime.ts b/src/runtime.ts index e0a055b..f664a9c 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -42,6 +42,9 @@ const addResult = (name: string, ty: string) => { } } +const createHash = () => + Math.random().toFixed(8).slice(2) + const match = (k: K | undefined, pattern: { [key in K | '_']: () => R }) => k && pattern[k] ? pattern[k]() : pattern._() @@ -53,7 +56,7 @@ const accumulateResults = (effTyp: Type, node: Node): string[] => { const [pathTyp] = effTyp.getTypeArguments() const filePath = JSON.parse(typeToString(pathTyp)) const contents = fs.readFileSync(filePath, 'utf-8') - const hash = Math.random().toFixed(8).slice(2) + const hash = createHash() addResult(hash, JSON.stringify(contents)) return [hash] }, @@ -64,8 +67,16 @@ const accumulateResults = (effTyp: Type, node: Node): string[] => { return [...(inputResults ?? [])] }, + GetEnv: () => { + const [envTyp] = effTyp.getTypeArguments() + const envName = JSON.parse(typeToString(envTyp)) + const hash = createHash() + addResult(hash, `${JSON.stringify(process.env[envName] ?? '')}`) + return [hash] + }, + _: () => { - console.log(`${name} effect is unhandled`) + console.log(`${name} result effect is unhandled`) return [] }, }) @@ -84,24 +95,26 @@ const evalAccumulator = (effNode: Node, node: Node) => { const [hash] = accumulateResults(effTyp, node) effNode.replaceWithText(`${RESULT_TYPE_NAME}[${JSON.stringify(hash)}]`) }, + WriteFile: () => { const [pathTyp, contentsTyp] = effTyp.getTypeArguments() const filePath = JSON.parse(typeToString(pathTyp)) const contents = JSON.parse(typeToString(contentsTyp)) fs.writeFileSync(filePath, contents) }, + ChainIO: () => { const inputTyp = effTyp.getProperty('input')?.getTypeAtLocation(node) const chainToKind = effTyp.getProperty('chainTo')?.getTypeAtLocation(node) const [hashRes] = inputTyp ? accumulateResults(inputTyp, node) : [] const chainRes = `(${typeToString(chainToKind)} & { input: ${RESULT_TYPE_NAME}[${JSON.stringify(hashRes)}]['output'] })['return']` - const updateEffNode = effNode.replaceWithText(chainRes) - evalAccumulator(updateEffNode, node) }, _: () => { + console.log(effNode.print()) + console.log('TTTT', typeToString(effTyp)) console.log(`${name} effect is unhandled`) } }) @@ -131,4 +144,5 @@ if (typeRefNode) { } console.log(entryPoint?.print()) +console.log(statement?.print()) -- cgit v1.3.1