diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-01-13 20:32:20 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-01-13 20:33:01 +0530 |
| commit | 518a6a9ee31f0b03f08cb77bc613b4d708bb640b (patch) | |
| tree | 7f17ce66f337dee573192ce06a24385415048ae2 /src/eval-env | |
| parent | 42f8c401dc9519bc8b2d386ce9eda072144a46d0 (diff) | |
| download | ts-types-lang-518a6a9ee31f0b03f08cb77bc613b4d708bb640b.tar.gz ts-types-lang-518a6a9ee31f0b03f08cb77bc613b4d708bb640b.zip | |
chore: prettier
Diffstat (limited to 'src/eval-env')
| -rw-r--r-- | src/eval-env/builtins.ts | 236 | ||||
| -rw-r--r-- | src/eval-env/test.ts | 2 |
2 files changed, 120 insertions, 118 deletions
diff --git a/src/eval-env/builtins.ts b/src/eval-env/builtins.ts index f9c4ce8..7e00253 100644 --- a/src/eval-env/builtins.ts +++ b/src/eval-env/builtins.ts @@ -1,143 +1,145 @@ import { Type } from 'ts-morph' -import { Ctx } from "../types" +import { Ctx } from '../types' import { evalList } from '../util' export default (ctx: Ctx, args: Type[]) => ({ - SetEvalEnvironment: async () => { - ctx.setEnv(ctx.getTypeValue(args[0])) - return [] - }, + SetEvalEnvironment: async () => { + ctx.setEnv(ctx.getTypeValue(args[0])) + return [] + }, - DefineEffect: async () => { - const [nameTyp, exprTyp] = args - const name = nameTyp?.getLiteralValue() as string - const exprStr = exprTyp?.getLiteralValue() as string + DefineEffect: async () => { + const [nameTyp, exprTyp] = args + const name = nameTyp?.getLiteralValue() as string + const exprStr = exprTyp?.getLiteralValue() as string - ctx.addCustomEffect(name, exprStr) - return [] - }, + ctx.addCustomEffect(name, exprStr) + return [] + }, - CreateRef: async () => { - const val = ctx.typeToString(args[0]) - const refKey = ctx.createRef(val) - const [resultKey, _] = ctx.createResult(JSON.stringify(refKey)) - return [resultKey] - }, + CreateRef: async () => { + const val = ctx.typeToString(args[0]) + const refKey = ctx.createRef(val) + const [resultKey, _] = ctx.createResult(JSON.stringify(refKey)) + return [resultKey] + }, - GetRef: async () => { - const refKey = ctx.getTypeValue(args[0]) - const val = ctx.getRef(refKey) - if (!val) throw new Error('Ref has been deleted') - const [resultKey, _] = ctx.createResult(val) - return [resultKey] - }, + GetRef: async () => { + const refKey = ctx.getTypeValue(args[0]) + const val = ctx.getRef(refKey) + if (!val) throw new Error('Ref has been deleted') + const [resultKey, _] = ctx.createResult(val) + return [resultKey] + }, - SetRef: async () => { - const [ keyTy, valTyp ] = args - ctx.setRef(ctx.getTypeValue(keyTy), ctx.typeToString(valTyp)) - return [] - }, + SetRef: async () => { + const [keyTy, valTyp] = args + ctx.setRef(ctx.getTypeValue(keyTy), ctx.typeToString(valTyp)) + return [] + }, - DeleteRef: async () => { - ctx.deleteRef(ctx.getTypeValue(args[0])) - return [] - }, + DeleteRef: async () => { + ctx.deleteRef(ctx.getTypeValue(args[0])) + return [] + }, - Pure: async () => { - const [valTyp] = args - const [resultKey, _] = ctx.createResult(ctx.typeToString(valTyp)) - return [resultKey] - }, + Pure: async () => { + const [valTyp] = args + const [resultKey, _] = ctx.createResult(ctx.typeToString(valTyp)) + return [resultKey] + }, - Print: async () => { - console.log(...args.map(ctx.typeToString)) - return [] - }, + Print: async () => { + console.log(...args.map(ctx.typeToString)) + return [] + }, - PutString: async () => { - const [strinTyp] = args - const typString = ctx.getTypeValue(strinTyp) ?? ctx.typeToString(strinTyp) - process.stdout.write(typString) - return [] - }, + PutString: async () => { + const [strinTyp] = args + const typString = ctx.getTypeValue(strinTyp) ?? ctx.typeToString(strinTyp) + process.stdout.write(typString) + return [] + }, - Debug: async () => { - const [labelTyp, valueTyp] = args - const label = ctx.getTypeValue(labelTyp) - const value = ctx.typeToString(valueTyp) - console.log(label, value) - const [resultKey, _] = ctx.createResult(JSON.stringify(value)) - return [resultKey] - }, + Debug: async () => { + const [labelTyp, valueTyp] = args + const label = ctx.getTypeValue(labelTyp) + const value = ctx.typeToString(valueTyp) + console.log(label, value) + const [resultKey, _] = ctx.createResult(JSON.stringify(value)) + return [resultKey] + }, - Bind: async () => { - const [inputTyp, chainToKind] = args - const [resultKey] = inputTyp ? await ctx.evaluateType(ctx, inputTyp) : [] + Bind: async () => { + const [inputTyp, chainToKind] = args + const [resultKey] = inputTyp ? await ctx.evaluateType(ctx, inputTyp) : [] - // TODO: Handle resultKey undefined case - const [_, compNode] = ctx.createResult( - `(${ctx.typeToString(chainToKind)} & { input: (${ctx.getResultExpr( - resultKey - )})['output'] })['return']` - ) - // TODO: Avoid using getTypeAtLocation? - const compTyp = compNode - ?.getType() - .getProperty('output') - ?.getTypeAtLocation(ctx.entryPoint) + // TODO: Handle resultKey undefined case + const [_, compNode] = ctx.createResult( + `(${ctx.typeToString(chainToKind)} & { input: (${ctx.getResultExpr( + resultKey + )})['output'] })['return']` + ) + // TODO: Avoid using getTypeAtLocation? + const compTyp = compNode + ?.getType() + .getProperty('output') + ?.getTypeAtLocation(ctx.entryPoint) - return compTyp ? await ctx.evaluateType(ctx, compTyp) : [] - }, + return compTyp ? await ctx.evaluateType(ctx, compTyp) : [] + }, - Try: async () => { - const [effTyp, catchK] = args + Try: async () => { + const [effTyp, catchK] = args - try { - if (!effTyp) throw new Error('wow') - return await ctx.evaluateType(ctx, effTyp) - } catch(e) { - const error = JSON.stringify((e as any)?.message ?? e) - const catchResExpr = `(${ctx.typeToString(catchK)} & { input: ${error} })['return']` - const [resultKey, _] = ctx.createResult(catchResExpr) - return [resultKey] - } - }, + try { + if (!effTyp) throw new Error('wow') + return await ctx.evaluateType(ctx, effTyp) + } catch (e) { + const error = JSON.stringify((e as any)?.message ?? e) + const catchResExpr = `(${ctx.typeToString( + catchK + )} & { input: ${error} })['return']` + const [resultKey, _] = ctx.createResult(catchResExpr) + return [resultKey] + } + }, - Throw: async () => { - throw args[0] && ctx.getTypeValue(args[0]) - }, + Throw: async () => { + throw args[0] && ctx.getTypeValue(args[0]) + }, - JsExpr: async () => { - const [exprTyp] = args - const exprStr = ctx.getTypeValue(exprTyp) - const result = eval(`JSON.stringify(${exprStr})`) - const [resultKey, _] = ctx.createResult(`${result}`) - return [resultKey] - }, + JsExpr: async () => { + const [exprTyp] = args + const exprStr = ctx.getTypeValue(exprTyp) + const result = eval(`JSON.stringify(${exprStr})`) + const [resultKey, _] = ctx.createResult(`${result}`) + return [resultKey] + }, - Seq: async () => { - const [effectTyps] = args - const effectResults = await evalList( - ctx, - effectTyps?.getTupleElements() ?? [] - ) - const [resultKey, _] = ctx.createResult(`[ + Seq: async () => { + const [effectTyps] = args + const effectResults = await evalList( + ctx, + effectTyps?.getTupleElements() ?? [] + ) + const [resultKey, _] = ctx.createResult(`[ ${effectResults.map(ctx.getResultExpr).join(', ')} ]`) - return [resultKey] - }, + return [resultKey] + }, - Do: async () => { - const [effectTyps] = args - const effectResults = await evalList( - ctx, - effectTyps?.getTupleElements() ?? [] - ) - // TODO: Use last type's result instead of last result key - const lastResKey = effectResults[effectResults.length - 1] - const [resultKey, _] = ctx.createResult( - `(${ctx.getResultExpr(lastResKey)})['output']` - ) - return [resultKey] - }, + Do: async () => { + const [effectTyps] = args + const effectResults = await evalList( + ctx, + effectTyps?.getTupleElements() ?? [] + ) + // TODO: Use last type's result instead of last result key + const lastResKey = effectResults[effectResults.length - 1] + const [resultKey, _] = ctx.createResult( + `(${ctx.getResultExpr(lastResKey)})['output']` + ) + return [resultKey] + }, }) diff --git a/src/eval-env/test.ts b/src/eval-env/test.ts index eddf366..84ae138 100644 --- a/src/eval-env/test.ts +++ b/src/eval-env/test.ts @@ -12,7 +12,7 @@ export default (ctx: Ctx, args: Type[]) => ({ } console.log('[✓]') - } catch(e) { + } catch (e) { console.log('[TEST FAILED]') throw e } |
