aboutsummaryrefslogtreecommitdiff
path: root/src/eval.ts
blob: 98291940919c8b556a0c21a61b4c881584b31cb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Type } from 'ts-morph'
import { promises as fs } from 'fs'
import readline from 'readline'

import { match } from './util'
import { Ctx } from './types'

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
})

const readLineFromStdin = (): Promise<string> =>
  new Promise((res) => rl.on('line', res))

export const evaluateType = async (
  ctx: Ctx,
  effTyp: Type
): Promise<string[]> => {
  const name = effTyp.getSymbol()?.getName()

  return match(name, {
    DefineEffect: async () => {
      const [nameTyp, exprTyp] = effTyp.getTypeArguments()
      const name = nameTyp?.getLiteralValue() as string
      const exprStr = exprTyp?.getLiteralValue() as string

      ctx.addCustomEffect(name, exprStr)
      return []
    },

    Print: async () => {
      console.log(...effTyp.getTypeArguments().map(ctx.typeToString))
      return []
    },

    PutString: async () => {
      const [strinTyp] = effTyp.getTypeArguments()
      const typString = ctx.typeToString(strinTyp)
      const string = JSON.parse(
        !typString.startsWith('"') ? `"${typString}"` : typString
      )
      process.stdout.write(string)
      return []
    },

    Debug: async () => {
      const [labelTyp, valueTyp] = effTyp.getTypeArguments()
      const label = JSON.parse(ctx.typeToString(labelTyp))
      const value = ctx.typeToString(valueTyp)
      console.log(label, value)
      const [resultKey, _] = ctx.createResult(JSON.stringify(value))
      return [resultKey]
    },

    ReadFile: async () => {
      const [pathTyp] = effTyp.getTypeArguments()
      const filePath = JSON.parse(ctx.typeToString(pathTyp))
      const contents = await fs.readFile(filePath, 'utf-8')
      const [resultKey, _] = ctx.createResult(JSON.stringify(contents))
      return [resultKey]
    },

    WriteFile: async () => {
      const [pathTyp, contentsTyp] = effTyp.getTypeArguments()
      const filePath = JSON.parse(ctx.typeToString(pathTyp))
      const contents = JSON.parse(ctx.typeToString(contentsTyp))
      await fs.writeFile(filePath, contents)
      return []
    },

    Bind: async () => {
      const [inputTyp, chainToKind] = effTyp.getTypeArguments()
      const [resultKey] = inputTyp ? await evaluateType(ctx, inputTyp) : []

      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 evaluateType(ctx, compTyp) : []
    },

    GetEnv: async () => {
      const [envTyp] = effTyp.getTypeArguments()
      const envName = JSON.parse(ctx.typeToString(envTyp))
      const [resultKey, _] = ctx.createResult(
        `${JSON.stringify(process.env[envName] ?? '')}`
      )
      return [resultKey]
    },

    GetArgs: async () => {
      const [resultKey, _] = ctx.createResult(
        `${JSON.stringify(process.argv.slice(2))}`
      )
      return [resultKey]
    },

    ReadLine: async () => {
      const line = await readLineFromStdin()
      const [resultKey, _] = ctx.createResult(`${JSON.stringify(line)}`)
      return [resultKey]
    },

    JsExpr: async () => {
      const [exprTyp] = effTyp.getTypeArguments()
      const exprStr = JSON.parse(ctx.typeToString(exprTyp))
      const result = eval(`JSON.stringify(${exprStr})`)
      const [resultKey, _] = ctx.createResult(`${result}`)
      return [resultKey]
    },

    Seq: async () => {
      const [effectTyps] = effTyp.getTypeArguments()
      const effectResults = await evalList(
        ctx,
        effectTyps?.getTupleElements() ?? []
      )
      const [resultKey, _] = ctx.createResult(`[
        ${effectResults.map(ctx.getResultExpr).join(', ')}
      ]`)
      return [resultKey]
    },

    Do: async () => {
      const [effectTyps] = effTyp.getTypeArguments()
      const effectResults = await evalList(
        ctx,
        effectTyps?.getTupleElements() ?? []
      )
      const lastResKey = effectResults[effectResults.length - 1]
      const [resultKey, _] = ctx.createResult(
        `${ctx.getResultExpr(lastResKey)}['output']`
      )
      return [resultKey]
    },

    _: async () => {
      if (name && ctx.hasCustomEffect(name)) {
        return ctx.runCustomEffect(name, effTyp.getTypeArguments())
      }

      console.log(`${name} result effect is unhandled`)
      return []
    },
  })
}

export const evalList = async (ctx: Ctx, effectTyps: Type[]) => {
  const effectResults: string[] = []
  for (const item of effectTyps ?? []) {
    effectResults.push(...(await evaluateType(ctx, item)))
  }
  return effectResults
}