aboutsummaryrefslogtreecommitdiff
path: root/src/runtime.ts
blob: 164e66e66654f472ad4e76fc41fee038567769ca (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { Project, ScriptTarget, Type, Node, StringLiteral, TypeFormatFlags, SyntaxKind } from 'ts-morph'
import path from 'path'
import { promises as fs } from 'fs'
import readline from 'readline';
import { stdout } from 'process';
import { v4 as uuid } from 'uuid';

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

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

const project = new Project({
  compilerOptions: {
    target: ScriptTarget.ES3,
  },
})

const typeChecker = project.getTypeChecker()

const sourceFile = project.addSourceFileAtPath(path.resolve("./src/index.ts"))

const entryPoint = sourceFile.getExportedDeclarations().get('main')?.[0]

const typeToString = (ty: Type | undefined): string =>
  ty ? typeChecker.compilerObject.typeToString(ty.compilerType) : ''

const getPropertyType = (n: Node, prop: string): Type | undefined => {
  const tt = typeChecker.getTypeAtLocation(n)
  const propSym = tt.getProperty(prop)
  const ty = propSym && typeChecker.getTypeOfSymbolAtLocation(propSym, n)
  return ty
}

const typeRefNode = entryPoint?.getLastChild()

const RESULT_TYPE_NAME = '__$result'

const [resultTypeNode] = sourceFile.addStatements(`type ${RESULT_TYPE_NAME} = {}`)

const addResult = (name: string, ty: string): Node | undefined => {
  if (resultTypeNode.isKind(SyntaxKind.TypeAliasDeclaration)) {
    const value = resultTypeNode.getChildAtIndex(3)
    if (value.isKind(SyntaxKind.TypeLiteral)) {
      return value.addProperty({
        name: JSON.stringify(name),
        type: `{ output: ${ty} }`,
      })
    }
  }
}

const match = <K extends string, R>(k: K | undefined, pattern: { [key in K | '_']: () => R }) =>
  k && pattern[k] ? pattern[k]() : pattern._()

const accumulateResults = async (effTyp: Type, node: Node): Promise<string[]> => {
  const name = effTyp.getSymbol()?.getName()

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

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

    Debug: async () => {
      const [labelTyp, valueTyp] = effTyp.getTypeArguments()
      const label = JSON.parse(typeToString(labelTyp))
      const value = typeToString(valueTyp)
      console.log(label, value)
      // TODO: Return value
      return []
    },

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

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

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

      const hash = uuid()
      const compNode = addResult(hash,
        `(${typeToString(chainToKind)} & { input: ${RESULT_TYPE_NAME}[${JSON.stringify(resultKey)}]['output'] })['return']`)
      const compTyp = compNode?.getType().getProperty('output')?.getTypeAtLocation(node)

      if (compTyp)
        return accumulateResults(compTyp, node)
      return []
    },

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

    GetArgs: async () => {
      const hash = uuid()
      addResult(hash, `${JSON.stringify(process.argv.slice(2))}`)
      return [hash]
    },

    ReadLine: async () => {
      const line = await readLineFromStdin()
      const hash = uuid()
      addResult(hash, `${JSON.stringify(line)}`)
      return [hash]
    },

    JsExpr: async () => {
      const [exprTyp] = effTyp.getTypeArguments()
      const exprStr = JSON.parse(typeToString(exprTyp))
      const result = eval(`JSON.stringify(${exprStr})`)
      const hash = uuid()
      addResult(hash, `${result}`)
      return [hash]
    },

    Seq: async () => {
      const [effectTyps] = effTyp.getTypeArguments()
      const effectResults: string[] = []
      for (const item of effectTyps?.getTupleElements() ?? []) {
        effectResults.push(...(await accumulateResults(item, node)))
      }

      const hash = uuid()
      addResult(hash, `[
        ${effectResults.map(r => `${RESULT_TYPE_NAME}[${JSON.stringify(r)}]`).join(', ')}
      ]`)
      return [hash]
    },

    _: async () => {
      console.log(`${name} result effect is unhandled`)
      return []
    },
  })
}

// const evalAccumulator = async (effNode: Node, node: Node) => {
//   const effTyp = effNode.getType()
//   await accumulateResults(effTyp, node)
// }

const main = async () => {
  if (typeRefNode) {
    const resultType = entryPoint?.getType()

    if (typeRefNode && entryPoint && resultType?.getSymbol()?.getName() === 'Program') {
      const exitCodeTy = getPropertyType(typeRefNode, 'exitCode')
      const effectTypes = getPropertyType(typeRefNode, 'effects')
      if (effectTypes?.isTuple()) {
        for (const typ of effectTypes.getTupleElements()) {
          await accumulateResults(typ, typeRefNode)
        }
      }

      const exitCode = exitCodeTy?.getLiteralValue() as number

      if (exitCode !== 0) {
        process.exit(exitCode)
      }
    } else {
      const ty = typeChecker.getTypeAtLocation(typeRefNode)
      console.log(typeToString(ty))
    }
  }
}

main()
  .then(() => {
    // console.log(entryPoint?.print())
    // console.log(resultTypeNode?.print())
    process.exit(0)
  })
  .catch(e => (console.error(e), process.exit(1)))