From e01cb693bc5737792e2b37abfd98d2d8f81bac4d Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 10 Aug 2023 22:45:00 +0530 Subject: feat: adds simple evaluator --- src/eval.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/eval.ts (limited to 'src/eval.ts') diff --git a/src/eval.ts b/src/eval.ts new file mode 100644 index 0000000..1e18457 --- /dev/null +++ b/src/eval.ts @@ -0,0 +1,40 @@ +import { Expr } from "./parser"; +import { match, matchString } from "./utils/adt"; + +export type Dependencies = { + addClass(id: string, classes: string): Promise + removeClass(id: string, classes: string): Promise + // requestGetCss(url: string): Promise + // getVarable(name: string, def?: string): Promise + // updateVariable(id: string, varName: string, value: string): Promise + // calculate ?? +} + +export const evalExpr = async (expr: Expr, deps: Dependencies): Promise => + match, 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) + } + }, + _: () => 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, + }) + -- cgit v1.3.1