Skip to content

Commit b4ee010

Browse files
committed
fix: update precedences of cons and similar, to match haskell
1 parent 92777ef commit b4ee010

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/EK/ExprParser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ structExpr :: [FuncItem] -> Parser Token Expr
161161
structExpr funcItems = StructLit <$> (TypeName <$> identifier <* parseTokenType CurlyOpen) <*> (structExprContent funcItems <* parseTokenType CurlyClose)
162162

163163
arrLit :: [Expr] -> Expr
164-
arrLit = foldr (\x acc -> Call ("_ cons _" `precedence` Prec 8 RightAssoc) [x, acc]) (Call "empty" [])
164+
arrLit = foldr (\x acc -> Call ("_ cons _" `precedence` Prec 5 RightAssoc) [x, acc]) (Call "empty" [])
165165

166166
arrExpr :: [FuncItem] -> Parser Token Expr
167167
arrExpr funcItems = arrLit <$> (parseTokenType BracketOpen *> structExprContent funcItems <* parseTokenType BracketClose)

stdlib/list.ek

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct cons {
1616

1717
type list = cons | empty
1818

19-
fn (a) cons (b) precedence 8r = cons { a, b }
19+
fn (a) cons (b) precedence 5r = cons { a, b }
2020

2121
fn (l) drop (n) =
2222
if l is empty || n == 0 then
@@ -69,7 +69,7 @@ fn (l) foldr (f) initial (acc) =
6969
fn (l) reverse =
7070
l foldl (\ acc x = x cons acc) initial empty
7171

72-
fn (l1) <> (l2) precedence 6 =
72+
fn (l1) <> (l2) precedence 5r =
7373
l1 foldr (\ x acc = x cons acc) initial l2
7474

7575
fn (a)[(b)] =

stdlib/std.ek

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ fn panic (str) = eprint str >> exit 1
4545

4646
fn assert (cond) message (str) = if cond then void else panic str
4747

48-
fn clamp (val) min (min) max (max) precedence 5 = if val < min then min else if val > max then max else val
48+
fn clamp (val) min (min) max (max) = if val < min then min else if val > max then max else val
4949

5050
fn (a) min (b) precedence 4 = if a < b then a else b
5151
fn (a) max (b) precedence 4 = if a > b then a else b
5252

53-
fn _ ++ _ precedence 6 = builtin concat
53+
fn _ ++ _ precedence 5r = builtin concat
5454
fn _ toString = builtin toString
5555
fn _ toInt = builtin toInt
5656
fn _ toFloat = builtin toFloat

test/EKParsing.hs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pat :: [FuncPatternItem] -> FuncPattern
4141
pat a = FuncPattern a Nothing Nothing
4242

4343
cons :: FunctionName
44-
cons = "_ cons _" `precedence` Prec 8 RightAssoc
44+
cons = "_ cons _" `precedence` Prec 5 RightAssoc
4545

4646
tests :: Test
4747
tests = test
@@ -92,7 +92,7 @@ tests = test
9292
@?= Right [FuncDef (pat [SymbolPattern "key"]) (IntegerLit 42)]
9393
, "Empty StructLit" ~: do
9494
doc [tkt FnKw, idt "foo", tkt Equal, idt "bar", tkt CurlyOpen, tkt CurlyClose]
95-
@?= Right [FuncDef (pat [SymbolPattern "foo"]) (StructLit (TypeName"bar") [])]
95+
@?= Right [FuncDef (pat [SymbolPattern "foo"]) (StructLit (TypeName "bar") [])]
9696
, "one int in StructLit" ~: do
9797
doc [tkt FnKw, idt "foo", tkt Equal, idt "bar", tkt CurlyOpen, int 42, tkt CurlyClose]
9898
@?= Right [FuncDef (pat [SymbolPattern "foo"]) (StructLit (TypeName "bar") [IntegerLit 42])]
@@ -407,8 +407,8 @@ tests = test
407407
]
408408
@?= Right [ ExternDef (FuncPattern [PlaceholderPattern, SymbolPattern "-", PlaceholderPattern] Nothing (Just (Prec 6 LeftAssoc)))
409409
, FuncDef (pat [SymbolPattern "test"])
410-
(Call ("_ - _" `precedence` (Prec 6 LeftAssoc))
411-
[ Call ("_ - _" `precedence` (Prec 6 LeftAssoc))
410+
(Call ("_ - _" `precedence` Prec 6 LeftAssoc)
411+
[ Call ("_ - _" `precedence` Prec 6 LeftAssoc)
412412
[ IntegerLit 10, IntegerLit 3 ]
413413
, IntegerLit 2
414414
]
@@ -420,8 +420,8 @@ tests = test
420420
]
421421
@?= Right [ ExternDef (FuncPattern [PlaceholderPattern, SymbolPattern "-", PlaceholderPattern] Nothing (Just (Prec 6 LeftAssoc)))
422422
, FuncDef (pat [SymbolPattern "test"])
423-
(Call ("_ - _" `precedence` (Prec 6 LeftAssoc))
424-
[ Call ("_ - _" `precedence` (Prec 6 LeftAssoc))
423+
(Call ("_ - _" `precedence` Prec 6 LeftAssoc)
424+
[ Call ("_ - _" `precedence` Prec 6 LeftAssoc)
425425
[ IntegerLit 10, IntegerLit 3 ]
426426
, IntegerLit 2
427427
]
@@ -433,9 +433,9 @@ tests = test
433433
]
434434
@?= Right [ ExternDef (FuncPattern [PlaceholderPattern, SymbolPattern "^", PlaceholderPattern] Nothing (Just (Prec 8 RightAssoc)))
435435
, FuncDef (pat [SymbolPattern "test"])
436-
(Call ("_ ^ _" `precedence` (Prec 8 RightAssoc))
436+
(Call ("_ ^ _" `precedence` Prec 8 RightAssoc)
437437
[ IntegerLit 2
438-
, Call ("_ ^ _" `precedence` (Prec 8 RightAssoc))
438+
, Call ("_ ^ _" `precedence` Prec 8 RightAssoc)
439439
[ IntegerLit 3, IntegerLit 4 ]
440440
]
441441
)
@@ -446,11 +446,11 @@ tests = test
446446
]
447447
@?= Right [ ExternDef (FuncPattern [PlaceholderPattern, SymbolPattern "^", PlaceholderPattern] Nothing (Just (Prec 8 RightAssoc)))
448448
, FuncDef (pat [SymbolPattern "test"])
449-
(Call ("_ ^ _" `precedence` (Prec 8 RightAssoc))
449+
(Call ("_ ^ _" `precedence` Prec 8 RightAssoc)
450450
[ IntegerLit 2
451-
, Call ("_ ^ _" `precedence` (Prec 8 RightAssoc))
451+
, Call ("_ ^ _" `precedence` Prec 8 RightAssoc)
452452
[ IntegerLit 3
453-
, Call ("_ ^ _" `precedence` (Prec 8 RightAssoc))
453+
, Call ("_ ^ _" `precedence` Prec 8 RightAssoc)
454454
[ IntegerLit 4, IntegerLit 5 ]
455455
]
456456
]
@@ -467,7 +467,7 @@ tests = test
467467
]
468468
@?= Right [ ExternDef (FuncPattern [PlaceholderPattern, SymbolPattern "==", PlaceholderPattern] Nothing (Just (Prec 4 NonAssoc)))
469469
, FuncDef (pat [SymbolPattern "test"])
470-
(Call ("_ == _" `precedence` (Prec 4 NonAssoc))
470+
(Call ("_ == _" `precedence` Prec 4 NonAssoc)
471471
[ IntegerLit 1, IntegerLit 2 ]
472472
)
473473
]
@@ -479,10 +479,10 @@ tests = test
479479
@?= Right [ ExternDef (FuncPattern [PlaceholderPattern, SymbolPattern "=", PlaceholderPattern] Nothing (Just (Prec 1 RightAssoc)))
480480
, ExternDef (FuncPattern [PlaceholderPattern, SymbolPattern "+", PlaceholderPattern] Nothing (Just (Prec 6 LeftAssoc)))
481481
, FuncDef (pat [SymbolPattern "test"])
482-
(Call ("_ = _" `precedence` (Prec 1 RightAssoc))
482+
(Call ("_ = _" `precedence` Prec 1 RightAssoc)
483483
[ IntegerLit 1
484-
, Call ("_ = _" `precedence` (Prec 1 RightAssoc))
485-
[ Call ("_ + _" `precedence` (Prec 6 LeftAssoc))
484+
, Call ("_ = _" `precedence` Prec 1 RightAssoc)
485+
[ Call ("_ + _" `precedence` Prec 6 LeftAssoc)
486486
[ IntegerLit 2, IntegerLit 3 ]
487487
, IntegerLit 4
488488
]

0 commit comments

Comments
 (0)