Skip to content

Commit ceccc62

Browse files
authored
shunting yard algo (#17)
* Added shunting yard algo for cooking op soup * Add missing file * Test and fix unary handling
1 parent 0af4187 commit ceccc62

File tree

10 files changed

+555
-48
lines changed

10 files changed

+555
-48
lines changed

package.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ library:
3030
- exceptions
3131
- filepath
3232
- file-embed
33+
- mtl
3334
- network-uri
3435
- optparse-applicative
3536
- megaparsec
37+
- monad-loops
3638
- parsec
3739
- parsec-numeric
3840
- path

src/Eucalypt/Core/Builtin.hs

+30-30
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Builtin = WhnfEvaluator -> [CoreExpr] -> Interpreter CoreExpr
3333
--
3434
euPanic :: WhnfEvaluator -> [CoreExpr] -> Interpreter CoreExpr
3535
euPanic _ [e@(CorePrim (CoreString s))] = throwEvalError $ Panic s e
36-
euPanic _ args = throwEvalError $ Bug "Bad arguments for panic" (CoreList args)
36+
euPanic _ as = throwEvalError $ Bug "Bad arguments for panic" (CoreList as)
3737

3838
-- | __NULL builtin - evaluates to null primitive. Arity 0.
3939
--
@@ -71,7 +71,7 @@ euEq whnfM [l, r] = do
7171
l' <- forceDataStructures whnfM l
7272
r' <- forceDataStructures whnfM r
7373
return $ (CorePrim . CoreBoolean) (l' == r')
74-
euEq _ args = throwEvalError $ Bug "__EQ called with bad args" (CoreList args)
74+
euEq _ as = throwEvalError $ Bug "__EQ called with bad as" (CoreList as)
7575

7676
-- | Evaluate an expression and ensure it's a boolean value.
7777
evalBoolean :: WhnfEvaluator -> CoreExpr -> Interpreter Bool
@@ -90,14 +90,14 @@ euIf whnfM [c, t, f] = do
9090
if cbool
9191
then t
9292
else f
93-
euIf _ args = throwEvalError $ Bug "__IF called with bad args" (CoreList args)
93+
euIf _ as = throwEvalError $ Bug "__IF called with bad as" (CoreList as)
9494

9595
-- | __NOT(b) - 'b' must be boolean. Arity 1. Strict in 'b'. Runtime
9696
-- error if 'b' is not a boolean.
9797
--
9898
euNot :: WhnfEvaluator -> [CoreExpr] -> Interpreter CoreExpr
9999
euNot whnfM [e] = CorePrim . CoreBoolean . not <$> evalBoolean whnfM e
100-
euNot _ args = throwEvalError $ Bug "__NOT called with bad args" (CoreList args)
100+
euNot _ as = throwEvalError $ Bug "__NOT called with bad as" (CoreList as)
101101

102102
-- | __AND(l,r) - 'l' and 'r' must be boolean. Arity 2. Strict in 'l'
103103
-- and 'r'.
@@ -107,7 +107,7 @@ euAnd whnfM [l, r] = do
107107
lbool <- evalBoolean whnfM l
108108
rbool <- evalBoolean whnfM r
109109
return $ CorePrim (CoreBoolean (lbool && rbool))
110-
euAnd _ args = throwEvalError $ Bug "__AND called with bad args" (CoreList args)
110+
euAnd _ as = throwEvalError $ Bug "__AND called with bad as" (CoreList as)
111111

112112
-- | __OR(l,r) - 'l' and 'r' must be boolean. Arity 2. Strict in 'l'
113113
-- and 'r'.
@@ -117,7 +117,7 @@ euOr whnfM [l, r] = do
117117
lbool <- evalBoolean whnfM l
118118
rbool <- evalBoolean whnfM r
119119
return $ CorePrim (CoreBoolean (lbool || rbool))
120-
euOr _ args = throwEvalError $ Bug "__OR called with bad args" (CoreList args)
120+
euOr _ as = throwEvalError $ Bug "__OR called with bad as" (CoreList as)
121121

122122
-- | General binary arithmetic implementation, takes care of
123123
-- destructuring and type casting
@@ -135,7 +135,7 @@ arith whnfM op [l, r] = do
135135
(CorePrim (CoreFloat i), CorePrim (CoreInt j)) ->
136136
return $ CorePrim (CoreFloat $ i `op` fromInteger j)
137137
(i, j) -> throwEvalError $ NotNumber (CoreList [i, j])
138-
arith _ _ args = throwEvalError $ Bug "Arith op called with bad args" (CoreList args)
138+
arith _ _ as = throwEvalError $ Bug "Arith op called with bad as" (CoreList as)
139139

140140

141141
-- | __ADD(l, r) - 'l' and 'r' must be numbers. Arity 2. String in both.
@@ -169,7 +169,7 @@ euDiv whnfM [l, r] = do
169169
then throwEvalError $ DivideByZero r
170170
else return $ CorePrim (CoreFloat val)
171171

172-
euDiv _ args = throwEvalError $ Bug "Division with bad args" (CoreList args)
172+
euDiv _ as = throwEvalError $ Bug "Division with bad as" (CoreList as)
173173

174174
-- | General binary arithmetic comparison implementation, takes care
175175
-- of destructuring and type casting
@@ -187,7 +187,7 @@ arithComp whnfM op [l, r] = do
187187
(CorePrim (CoreFloat i), CorePrim (CoreInt j)) ->
188188
return $ CorePrim (CoreBoolean $ i `op` fromInteger j)
189189
(i, j) -> throwEvalError $ NotNumber (CoreList [i, j])
190-
arithComp _ _ args = throwEvalError $ Bug "Comparison op called with bad args" (CoreList args)
190+
arithComp _ _ as = throwEvalError $ Bug "Comparison op called with bad as" (CoreList as)
191191

192192
-- | __LT(l, r) - 'l' and 'r' must be numbers. Arity 2. String in both.
193193
euLt :: WhnfEvaluator -> [CoreExpr] -> Interpreter CoreExpr
@@ -213,7 +213,7 @@ euHead whnfM [l] =
213213
(CoreList (h:_)) -> return h
214214
(CoreList []) -> throwEvalError $ EmptyList (CoreList [])
215215
e -> throwEvalError $ NotList e
216-
euHead _ args = throwEvalError $ Bug "__HEAD called with bad args" (CoreList args)
216+
euHead _ as = throwEvalError $ Bug "__HEAD called with bad as" (CoreList as)
217217

218218
-- | __TAIL(l) - 'l' must be list. Arity 1. Strict in 'l'.
219219
--
@@ -223,7 +223,7 @@ euTail whnfM [l] =
223223
(CoreList (_:t)) -> return (CoreList t)
224224
(CoreList []) -> throwEvalError $ EmptyList (CoreList [])
225225
e -> throwEvalError $ NotList e
226-
euTail _ args = throwEvalError $ Bug "__TAIL called with bad args" (CoreList args)
226+
euTail _ as = throwEvalError $ Bug "__TAIL called with bad as" (CoreList as)
227227

228228
-- | __CONS(h, t) - 't' must be list. Arity 2.
229229
--
@@ -235,7 +235,7 @@ euCons whnfM [h, t] =
235235
whnfM t >>= \case
236236
(CoreList t') -> return (CoreList (h : t'))
237237
e -> throwEvalError $ NotList e
238-
euCons _ args = throwEvalError $ Bug "__TAIL called with bad args" (CoreList args)
238+
euCons _ as = throwEvalError $ Bug "__TAIL called with bad as" (CoreList as)
239239

240240

241241
-- | Concatenate two lists or blocks into a list. The default action
@@ -262,14 +262,14 @@ euSym :: WhnfEvaluator -> [CoreExpr] -> Interpreter CoreExpr
262262
euSym whnfM [s] = whnfM s >>= \case
263263
(CorePrim (CoreString v)) -> return (CorePrim (CoreSymbol v))
264264
e -> throwEvalError $ SymbolNamesMustBeStrings e
265-
euSym _ args = throwEvalError $ Bug "__SYM called with bad args" (CoreList args)
265+
euSym _ as = throwEvalError $ Bug "__SYM called with bad as" (CoreList as)
266266

267267

268268
-- | __BLOCK(l) builtin. Arity 1. Non-strict. Wrap up a list of elements as a block.
269269
--
270270
euBlock :: WhnfEvaluator -> [CoreExpr] -> Interpreter CoreExpr
271271
euBlock _ [l] = return $ CoreBlock l
272-
euBlock _ args = throwEvalError $ Bug "__BLOCK called with bad args" (CoreList args)
272+
euBlock _ as = throwEvalError $ Bug "__BLOCK called with bad as" (CoreList as)
273273

274274

275275

@@ -280,7 +280,7 @@ euElements whnfM [e] =
280280
whnfM e >>= \case
281281
CoreBlock l -> return l
282282
_ -> throwEvalError $ ElementsArgumentNotBlock e
283-
euElements _ args = throwEvalError $ Bug "__ELEMENTS called with bad args" (CoreList args)
283+
euElements _ as = throwEvalError $ Bug "__ELEMENTS called with bad as" (CoreList as)
284284

285285

286286

@@ -315,7 +315,7 @@ euMerge whnfM [l, r] = do
315315
(CoreList ll, CoreList rr) -> CoreBlock . CoreList <$> mergeElements whnfM ll rr
316316
_ -> throwEvalError $ BadBlockMerge (CoreList [l, r])
317317
_ -> throwEvalError $ BadBlockMerge (CoreList [l, r])
318-
euMerge _ args = throwEvalError $ BadBlockMerge (CoreList args)
318+
euMerge _ as = throwEvalError $ BadBlockMerge (CoreList as)
319319

320320

321321

@@ -383,7 +383,7 @@ euLookupOr whnfM [n, d, b] = do
383383
(CorePrim (CoreSymbol s)) -> lookupOr whnfM (return d) b' s
384384
(CorePrim (CoreString s)) -> lookupOr whnfM (return d) b' s
385385
_ -> throwEvalError $ LookupKeyNotStringLike n'
386-
euLookupOr _ args = throwEvalError $ Bug "__LOOKUP called with bad arguments" (CoreList args)
386+
euLookupOr _ as = throwEvalError $ Bug "__LOOKUP called with bad arguments" (CoreList as)
387387

388388
-- | __LOOKUP(n, d, b) - look up name `n` (string or symbol) in
389389
-- block `b`, returning `d` if it isn't there. Strict in `b` and `r`.
@@ -397,7 +397,7 @@ euLookup whnfM [n, b] = do
397397
(CorePrim (CoreSymbol s)) -> lookupName whnfM b' s
398398
(CorePrim (CoreString s)) -> lookupName whnfM b' s
399399
_ -> throwEvalError $ LookupKeyNotStringLike n'
400-
euLookup _ args = throwEvalError $ Bug "__LOOKUP called with bad arguments" (CoreList args)
400+
euLookup _ as = throwEvalError $ Bug "__LOOKUP called with bad arguments" (CoreList as)
401401

402402
-- | Remove item from block with the specified key
403403
removeItem :: WhnfEvaluator -> CoreExpr -> CoreRelativeName -> Interpreter CoreExpr
@@ -425,7 +425,7 @@ euRemove whnfM [n, b] = do
425425
(CorePrim (CoreSymbol s)) -> removeItem whnfM b' s
426426
(CorePrim (CoreString s)) -> removeItem whnfM b' s
427427
_ -> throwEvalError $ LookupKeyNotStringLike n'
428-
euRemove _ args = throwEvalError $ Bug "__LOOKUP called with bad arguments" (CoreList args)
428+
euRemove _ as = throwEvalError $ Bug "__LOOKUP called with bad arguments" (CoreList as)
429429

430430
-- | Lookup in a block, throwing if key absent.
431431
--
@@ -453,7 +453,7 @@ euSplit whnfM [s, re] = do
453453
(CorePrim (CoreString target), CorePrim (CoreString regex)) ->
454454
return $ CoreList $ map (CorePrim . CoreString) $ splitRegex target regex
455455
_ -> throwEvalError $ BadSplitArgs s re
456-
euSplit _ args = throwEvalError $ Bug "__SPLIT called with bad arguments" (CoreList args)
456+
euSplit _ as = throwEvalError $ Bug "__SPLIT called with bad arguments" (CoreList as)
457457

458458
-- | __JOIN(l, sep) - join (string) items of l with sep.
459459
--
@@ -469,7 +469,7 @@ euJoin whnfM [l, sep] = do
469469
stringItem x = whnfM x >>= extractString
470470
extractString (CorePrim (CoreString x)) = return x
471471
extractString x = throwEvalError $ NotString x
472-
euJoin _ args = throwEvalError $ Bug "__JOIN called with bad arguments" (CoreList args)
472+
euJoin _ as = throwEvalError $ Bug "__JOIN called with bad arguments" (CoreList as)
473473

474474
-- | __MATCH(s, re) - match s with re, returning list of full match t
475475
-- index 0 then groups.
@@ -483,7 +483,7 @@ euMatch whnfM [s, re] = do
483483
return . CoreList . map (CorePrim . CoreString) $
484484
getAllTextSubmatches (target =~ regex :: AllTextSubmatches [] String)
485485
_ -> throwEvalError $ BadMatchArgs s re
486-
euMatch _ args = throwEvalError $ Bug "__MATCH called with bad arguments" (CoreList args)
486+
euMatch _ as = throwEvalError $ Bug "__MATCH called with bad arguments" (CoreList as)
487487

488488
-- | __MATCHES(s, re) - find all matches of @re@ in @s@, returning list of
489489
-- string matches.
@@ -497,15 +497,15 @@ euMatches whnfM [s, re] = do
497497
return . CoreList . map (CorePrim . CoreString) $
498498
getAllTextMatches (target =~ regex :: AllTextMatches [] String)
499499
_ -> throwEvalError $ BadMatchArgs s re
500-
euMatches _ args = throwEvalError $ Bug "__MATCHES called with bad arguments" (CoreList args)
500+
euMatches _ as = throwEvalError $ Bug "__MATCHES called with bad arguments" (CoreList as)
501501

502502
-- | __WITHMETA(m, e) - tag metadata `m` onto expression `e`.
503-
-- Lazy in both args.
503+
-- Lazy in both as.
504504
--
505505
euWithMeta :: WhnfEvaluator -> [CoreExpr] -> Interpreter CoreExpr
506506
euWithMeta _ [m, e] = return $ CoreMeta m e
507-
euWithMeta _ args =
508-
throwEvalError $ Bug "__WITHMETA called with bad arguments" (CoreList args)
507+
euWithMeta _ as =
508+
throwEvalError $ Bug "__WITHMETA called with bad arguments" (CoreList as)
509509

510510
-- | __META(e) - retrieve metadata from value - which must be a
511511
-- metadata annotated value (not the value contained
@@ -516,8 +516,8 @@ euMeta whnfM [e] = do
516516
case e' of
517517
(CoreMeta m _) -> return m
518518
_ -> return $ block []
519-
euMeta _ args =
520-
throwEvalError $ Bug "__WITHMETA called with bad arguments" (CoreList args)
519+
euMeta _ as =
520+
throwEvalError $ Bug "__WITHMETA called with bad arguments" (CoreList as)
521521

522522

523523
-- | __STR(e) - convert to string.
@@ -529,8 +529,8 @@ euStr whnfM [e] = whnfM e >>= skipMeta whnfM >>= \case
529529
(CorePrim (CoreSymbol s)) -> (return . CorePrim . CoreString) s
530530
(CorePrim (CoreBoolean b)) -> (return . CorePrim . CoreString . show) b
531531
x -> (return . CorePrim . CoreString . show) x
532-
euStr _ args =
533-
throwEvalError $ Bug "__STR called with bad arguments" (CoreList args)
532+
euStr _ as =
533+
throwEvalError $ Bug "__STR called with bad arguments" (CoreList as)
534534

535535

536536
-- | The builtins exposed to the language.

0 commit comments

Comments
 (0)