aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-01-11 16:02:26 +0530
committerAkshay Nair <phenax5@gmail.com>2023-01-11 16:02:26 +0530
commit49d6379c3d40b684b28f4a957ff1ffb81a2560ee (patch)
treefac62b2de1670ecfea6496949b028fa8fe0fe444 /src
parenta6ff70d09e1e5f161e401a3665bfe44b2abfc44e (diff)
downloadts-types-lang-49d6379c3d40b684b28f4a957ff1ffb81a2560ee.tar.gz
ts-types-lang-49d6379c3d40b684b28f4a957ff1ffb81a2560ee.zip
refactor: minor refactor all around
Diffstat (limited to 'src')
-rw-r--r--src/context.ts15
-rw-r--r--src/eval.ts23
-rw-r--r--src/types.ts1
3 files changed, 25 insertions, 14 deletions
diff --git a/src/context.ts b/src/context.ts
index 196c85d..14b5115 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -51,13 +51,20 @@ export const createContext = (options: CtxOptions): Ctx => {
return [resultKey, node]
}
- const customEffects: Record<string, (...args: Type[]) => any> = {}
+ const customEffects: Record<string, (args: Type[], ctx: Ctx) => any> = {}
- return {
+ const getTypeValue = (ty: Type | undefined): any => {
+ try {
+ return JSON.parse(typeToString(ty))
+ } catch(_) { return }
+ }
+
+ const ctx: Ctx = {
sourceFile,
typeChecker,
entryPoint,
typeToString,
+ getTypeValue,
createResult,
getResultExpr,
@@ -68,7 +75,7 @@ export const createContext = (options: CtxOptions): Ctx => {
Object.assign(customEffects, { [name]: func })
},
runCustomEffect: async (name, args) => {
- const output = await customEffects[name]?.(...args)
+ const output = await customEffects[name]?.(args, ctx)
if (output) {
const [resultKey, _] = createResult(`${JSON.stringify(output)}`)
return [resultKey]
@@ -77,4 +84,6 @@ export const createContext = (options: CtxOptions): Ctx => {
},
hasCustomEffect: (name) => customEffects[name] !== undefined,
}
+
+ return ctx
}
diff --git a/src/eval.ts b/src/eval.ts
index 54036be..8c8940f 100644
--- a/src/eval.ts
+++ b/src/eval.ts
@@ -49,17 +49,14 @@ export const evaluateType = async (
PutString: async () => {
const [strinTyp] = args
- const typString = ctx.typeToString(strinTyp)
- const string = JSON.parse(
- !typString.startsWith('"') ? `"${typString}"` : typString
- )
- process.stdout.write(string)
+ const typString = ctx.getTypeValue(strinTyp) ?? ctx.typeToString(strinTyp)
+ process.stdout.write(typString)
return []
},
Debug: async () => {
const [labelTyp, valueTyp] = args
- const label = JSON.parse(ctx.typeToString(labelTyp))
+ const label = ctx.getTypeValue(labelTyp)
const value = ctx.typeToString(valueTyp)
console.log(label, value)
const [resultKey, _] = ctx.createResult(JSON.stringify(value))
@@ -68,7 +65,7 @@ export const evaluateType = async (
ReadFile: async () => {
const [pathTyp] = args
- const filePath = JSON.parse(ctx.typeToString(pathTyp))
+ const filePath = ctx.getTypeValue(pathTyp)
const contents = await fs.readFile(filePath, 'utf-8')
const [resultKey, _] = ctx.createResult(JSON.stringify(contents))
return [resultKey]
@@ -76,8 +73,8 @@ export const evaluateType = async (
WriteFile: async () => {
const [pathTyp, contentsTyp] = args
- const filePath = JSON.parse(ctx.typeToString(pathTyp))
- const contents = JSON.parse(ctx.typeToString(contentsTyp))
+ const filePath = ctx.getTypeValue(pathTyp)
+ const contents = ctx.getTypeValue(contentsTyp)
await fs.writeFile(filePath, contents)
return []
},
@@ -86,6 +83,7 @@ export const evaluateType = async (
const [inputTyp, chainToKind] = args
const [resultKey] = inputTyp ? await evaluateType(ctx, inputTyp) : []
+ // TODO: Handle resultKey undefined case
const [_, compNode] = ctx.createResult(
`(${ctx.typeToString(chainToKind)} & { input: (${ctx.getResultExpr(
resultKey
@@ -102,7 +100,7 @@ export const evaluateType = async (
GetEnv: async () => {
const [envTyp] = args
- const envName = JSON.parse(ctx.typeToString(envTyp))
+ const envName = ctx.getTypeValue(envTyp)
const [resultKey, _] = ctx.createResult(
`${JSON.stringify(process.env[envName] ?? '')}`
)
@@ -124,7 +122,7 @@ export const evaluateType = async (
JsExpr: async () => {
const [exprTyp] = args
- const exprStr = JSON.parse(ctx.typeToString(exprTyp))
+ const exprStr = ctx.getTypeValue(exprTyp)
const result = eval(`JSON.stringify(${exprStr})`)
const [resultKey, _] = ctx.createResult(`${result}`)
return [resultKey]
@@ -148,6 +146,7 @@ export const evaluateType = async (
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']`
@@ -161,6 +160,8 @@ export const evaluateType = async (
}
console.log(`${name} effect is not handled`)
+ console.log(ctx.typeToString(effTyp))
+ // TODO: Maybe throw?
return []
},
})
diff --git a/src/types.ts b/src/types.ts
index 3ffd416..e3ea7e9 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -11,6 +11,7 @@ export interface Ctx {
typeChecker: TypeChecker
entryPoint: ExportedDeclarations
typeToString: (ty: Type | undefined) => string
+ getTypeValue: (ty: Type | undefined) => any
createResult: (ty: string) => [string, Node | undefined]
getResultExpr: (key?: string) => string