lexer change for a comment that extends to end of file

This commit is contained in:
Paul Chiusano 2018-10-22 15:19:03 -04:00
parent c7ffba7252
commit b72a082238
2 changed files with 83 additions and 0 deletions

View File

@ -180,6 +180,7 @@ lexer0 scope rem =
-- skip whitespace and comments
goWhitespace :: Layout -> Pos -> [Char] -> [Token Lexeme]
goWhitespace l pos rem = span' isSpace rem $ \case
(_spaces, '-':'-':'-':_rem) -> popLayout0 l pos []
(spaces, '-':'-':rem) -> spanThru' (/= '\n') rem $ \(ignored, rem) ->
goWhitespace l (incBy ('-':'-':ignored) . incBy spaces $ pos) rem
(spaces, rem) -> popLayout l (incBy spaces pos) rem

View File

@ -0,0 +1,82 @@
--The expression in red needs the {𝛆} ability, but this location only has access to the {𝛆} ability,
--
-- 8 | odd n = if n == 1 then true else even2 (n `drop` 1)
even : Nat -> Boolean
even n = if n == 0 then true else odd (n `drop` 1)
odd : Nat -> Boolean
odd n = if n == 1 then true else even2 (n `drop` 1)
even2 = even
increment : Nat -> Nat -- signature is optional
increment n = n + 1
-- Lines starting with `>` are evaluated and printed on every file save.
> increment 99
replicate : Nat -> a -> [a]
replicate n a = to-sequence (take n (constant a))
-- this is nice for quick testing!
> replicate 3 "bye"
-- can ask Unison for the type of any expression just by adding `?` to the end of it
-- > (replicate 4)?
-- here's a more interesting example, mergesort -
-- First we define the merge function, it merges two sorted lists
merge : (a -> a -> Boolean) -> [a] -> [a] -> [a]
merge lte a b =
use Sequence ++
use Optional None Some
go acc a b = case at 0 a of
None -> acc ++ b
Some hd1 -> case at 0 b of
None -> acc ++ a
Some hd2 ->
if hd1 `lte` hd2 then go (acc `snoc` hd1) (drop 1 a) b
else go (acc `snoc` hd2) a (drop 1 b)
go [] a b
-- let's make sure it works
> merge (<) [1,3,4,99,504,799] [0,19,22,23]
-- looks good, now let's write mergesort
sort : (a -> a -> Boolean) -> [a] -> [a]
sort lte a =
if Sequence.size a < 2 then a
else
l = sort lte (take (size a / 2) a)
r = sort lte (drop (size a / 2) a)
merge lte l r
-- let's make sure it works
> sort (<) [3,2,1,1,2,3,9182,1,2,34,1,80]
> sort (<) ["Dave", "Carol", "Eve", "Alice", "Bob", "Francis", "Hal", "Illy", "Joanna", "Greg", "Karen"]
-- SHIP IT!! 🚢
-- If you make a mistake, we try to have nice friendly error messages, not:
-- 🤖 ERROR DETECTED ⚡️ BEEP BOOP ⚡️ PLS RESUBMIT PROGRAM TO MAINFRAME
-- a few examples of failing programs -
--err1 =
-- a = "3"
-- sort (<) [1,2,a]
-- err1a = sort (<) "not a list"
--err2 : x -> y -> x
--err2 thing1 thing2 =
-- if true then thing1
-- else thing2