aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-08-20 11:29:07 +0530
committerAkshay Nair <phenax5@gmail.com>2023-08-20 11:29:07 +0530
commitf61677bbc3ae32cc460014cffe4d9ae9264291c5 (patch)
treeaf7f8935ba15364699f1b1abf6a5987b61011d33
parent2b94b07fdc8f1a82f507b99000add9b7dca2c3d8 (diff)
downloadcss-everything-f61677bbc3ae32cc460014cffe4d9ae9264291c5.tar.gz
css-everything-f61677bbc3ae32cc460014cffe4d9ae9264291c5.zip
feat: function declaration
Diffstat (limited to '')
-rw-r--r--src/eval.ts4
-rw-r--r--src/parser.ts12
-rw-r--r--tests/eval.spec.ts60
3 files changed, 71 insertions, 5 deletions
diff --git a/src/eval.ts b/src/eval.ts
index 26b3df0..e421d7a 100644
--- a/src/eval.ts
+++ b/src/eval.ts
@@ -245,7 +245,9 @@ const getFunctions = (
return EvalValue.Void()
},
- _: () => Promise.reject(new Error('not supposed to be here')),
+ func: async () => EvalValue.Lazy(args),
+
+ _: () => Promise.reject(new Error(`Not implemented: ${name}`)),
})
}
diff --git a/src/parser.ts b/src/parser.ts
index 732e32a..eeb9acf 100644
--- a/src/parser.ts
+++ b/src/parser.ts
@@ -108,6 +108,18 @@ const exprParser: P.Parser<Expr> = P.or([
identifierExprParser,
])
+export const parseExpr = (input: string): Expr => {
+ return match(exprParser(input), {
+ Ok: ({ value, input }) => {
+ if (input) throw new Error(`Aaaaaa. Input left: ${input}`)
+ return value
+ },
+ Err: e => {
+ throw e
+ },
+ })
+}
+
const declarationParser = P.or([callExprParser, selectorExprParser])
const multiDeclarationParser = P.sepBy(declarationParser, whitespace)
diff --git a/tests/eval.spec.ts b/tests/eval.spec.ts
index 660f0b9..d61a470 100644
--- a/tests/eval.spec.ts
+++ b/tests/eval.spec.ts
@@ -1,5 +1,5 @@
-import { EvalActions, evalExpr } from '../src/eval'
-import { Expr } from '../src/parser'
+import { EvalActions, EvalValue, evalExpr } from '../src/eval'
+import { Expr, exprParser, parseExpr } from '../src/parser'
describe('eval', () => {
const deps: EvalActions = {
@@ -20,8 +20,60 @@ describe('eval', () => {
callMethod: jest.fn(),
}
- fit('should do stuff', () => {
- console.log('yo')
+ fdescribe('function/call', () => {
+ it('should declare function correctly', async () => {
+ const evalValue = await evalExpr(
+ parseExpr(`func(if(get-var(--bool), 'false', 'true'))`),
+ deps,
+ )
+ expect(evalValue).toEqual(
+ EvalValue.Lazy([
+ Expr.Call({
+ name: 'if',
+ args: [
+ Expr.Call({
+ name: 'get-var',
+ args: [Expr.VarIdentifier('--bool')],
+ }),
+ Expr.LiteralString('false'),
+ Expr.LiteralString('true'),
+ ],
+ }),
+ ]),
+ )
+ })
+
+ it('should allow multiple expressions in func', async () => {
+ const evalValue = await evalExpr(
+ parseExpr(`func(
+ update(--some-var, 'hello world'),
+ if(get-var(--bool), 'false', 'true')
+ )`),
+ deps,
+ )
+ expect(evalValue).toEqual(
+ EvalValue.Lazy([
+ Expr.Call({
+ name: 'update',
+ args: [
+ Expr.VarIdentifier('--some-var'),
+ Expr.LiteralString('hello world'),
+ ],
+ }),
+ Expr.Call({
+ name: 'if',
+ args: [
+ Expr.Call({
+ name: 'get-var',
+ args: [Expr.VarIdentifier('--bool')],
+ }),
+ Expr.LiteralString('false'),
+ Expr.LiteralString('true'),
+ ],
+ }),
+ ]),
+ )
+ })
})
it('should add classes', async () => {