diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-01-14 17:12:48 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-01-14 17:12:48 +0530 |
| commit | cd11925707c42843df195d9b2efb3c77b5de793b (patch) | |
| tree | 0871b0041597096fdbf7c49416f96fa70da00e3c /src/eval-env | |
| parent | d362ef26db894ea52c6abd34489aa9efdbc0c89b (diff) | |
| download | ts-types-lang-cd11925707c42843df195d9b2efb3c77b5de793b.tar.gz ts-types-lang-cd11925707c42843df195d9b2efb3c77b5de793b.zip | |
feat: adds cleanup of result nodes
Diffstat (limited to 'src/eval-env')
| -rw-r--r-- | src/eval-env/builtins.ts | 71 |
1 files changed, 6 insertions, 65 deletions
diff --git a/src/eval-env/builtins.ts b/src/eval-env/builtins.ts index 35f8485..c5b0c82 100644 --- a/src/eval-env/builtins.ts +++ b/src/eval-env/builtins.ts @@ -1,63 +1,6 @@ -import { SyntaxKind, Type } from 'ts-morph' +import { Type } from 'ts-morph' import { Ctx } from '../types' -import { evalList } from '../util' - -const applyFunc = (ctx: Ctx, fn: Type | undefined, val: string): Type => { - const resultType = (() => { - const baseTypes = fn - ?.getBaseTypes() - .flatMap((t) => t.getSymbol()?.getName() ?? []) - - if (!!fn?.getProperty('return') || baseTypes?.includes('Kind1')) { - const [_key, resultNode] = ctx.createResult( - `(${ctx.typeToString(fn)} & { input: ${val} })['return']` - ) - return resultNode - ?.getType() - .getProperty('output') - ?.getTypeAtLocation(resultNode) - } else { - const [_key, resultNode] = ctx.createResult( - `ReturnType<${ctx.typeToString(fn)}>` - ) - - const resValueNode = resultNode - ?.asKind(SyntaxKind.PropertySignature) - ?.getChildAtIndexIfKind(2, SyntaxKind.TypeLiteral) - ?.getProperty('output') - ?.getChildAtIndexIfKind(2, SyntaxKind.TypeReference) - - const functionNode = resValueNode - ?.getChildAtIndexIfKind(1, SyntaxKind.SyntaxList) - ?.getFirstChildIfKind(SyntaxKind.FunctionType) - - if (functionNode) { - const typeParameters = functionNode.getTypeParameters() ?? [] - - if (typeParameters.length > 0) { - const constraint = typeParameters[0]?.getConstraint() - if (constraint) { - constraint?.replaceWithText(val) - } else { - typeParameters[0]?.setConstraint(val) - } - } - - return resValueNode?.getType() - } - } - - return undefined - })() - - // TODO: Cleanup unwanted result node values - - if (!resultType) { - throw new Error('Couldnt get result for function application') - } - - return resultType -} +import { applyFunc, evalList } from '../util' export default (ctx: Ctx, args: Type[]) => ({ SetEvalEnvironment: async () => { @@ -129,14 +72,12 @@ export default (ctx: Ctx, args: Type[]) => ({ Bind: ctx.withScope(async () => { const [inputTyp, chainToKind] = args - const [resultKey] = inputTyp ? await ctx.evaluateType(ctx, inputTyp) : [] - - // TODO: Handle resultKey undefined case + let [resultKey] = inputTyp ? await ctx.evaluateType(ctx, inputTyp) : [] const resultType = applyFunc( ctx, chainToKind, - `(${ctx.getResultExpr(resultKey)})['output']` + resultKey ? `(${ctx.getResultExpr(resultKey)})['output']` : 'never' ) return ctx.evaluateType(ctx, resultType) }), @@ -186,7 +127,7 @@ export default (ctx: Ctx, args: Type[]) => ({ return [resultKey] }, - Seq: async () => { + Seq: ctx.withScope(async () => { const [effectTyps] = args const effectResults = await evalList( ctx, @@ -196,7 +137,7 @@ export default (ctx: Ctx, args: Type[]) => ({ ${effectResults.map(ctx.getResultExpr).join(', ')} ]`) return [resultKey] - }, + }), Do: ctx.withScope(async () => { const [effectTyps] = args |
