aboutsummaryrefslogtreecommitdiff
path: root/src/eval.ts
blob: 76eaa21d0324b330359866ade751a80b786cb758 (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
import { Expr } from "./parser";
import { match, matchString } from "./utils/adt";

export type Dependencies = {
  addClass(id: string, classes: string): Promise<void>
  removeClass(id: string, classes: string): Promise<void>
  delay(num: number): Promise<void>
  jsEval(js: string): Promise<any>
  // loadCssx(id: string, url: string): Promise<string>
  // getVarable(name: string, def?: string): Promise<string>
  // updateVariable(id: string, varName: string, value: string): Promise<void>
  // calculate ??
}

export const evalExpr = async (expr: Expr, deps: Dependencies): Promise<string | undefined> =>
  match<Promise<string | undefined>, Expr>(expr, {
    Call: async ({ name, args }) => {
      await matchString(name, {
        'add-class': async () => {
          const id = await evalExpr(args[0], deps)
          const classes = await evalExpr(args[1], deps)
          if (id && classes) {
            await deps.addClass(id, classes)
          }
        },
        'remove-class': async () => {
          const id = await evalExpr(args[0], deps)
          const classes = await evalExpr(args[1], deps)
          if (id && classes) {
            await deps.removeClass(id, classes)
          }
        },
        'delay': async () => {
          const num = await evalExpr(args[0], deps)
          num && await deps.delay(parseInt(num, 10))
        },
        'js-eval': async () => {
          const js = await evalExpr(args[0], deps)
          js && await deps.jsEval(js)
        },
        _: () => Promise.reject(new Error('not supposed to be here')),
      })
      return undefined
    },
    LiteralString: async s => s,
    Identifier: async s => s,
    VarIdentifier: async s => s,
    _: async _ => undefined,
  })