Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions unison/lexer.u
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ Lexer.lexer str =
loop tup =
(str, tokens) = tup
match str with
"" -> tokens
lex -> processText str tokens |> loop
"" -> tokens
lex -> processText str tokens |> loop
loop (str, [])

Lexer.processText : Text -> [Token] -> (Text, [Token])
Lexer.processText str tokens =
(ch, txt) = Text.uncons str |> Optional.getOrElse (?\NUL, "")
match ch with
x
| isWhitespace x -> (txt, tokens)
| x == ?= -> identEquals txt tokens
| x == ?! -> identNotEqual txt tokens
| isLetter(x) ->
(var, rem) = identChar txt
val = fromCharList([ch]) ++ var
(rem, tokens :+ keyWords(val) )
| isDigit(x) ->
(var, rem) = identNum txt
num = fromCharList([ch]) ++ var
(rem, tokens :+ Int num)
_ ->(txt, tokens :+ nextToken(ch))
match ch with
_
| isWhitespace ch -> (txt, tokens)
| ch == ?= -> identEquals txt tokens
| ch == ?! -> identNotEqual txt tokens
| isLetter(ch) ->
(var, rem) = identChar txt
val = fromCharList([ch]) ++ var
(rem, tokens :+ keyWords(val) )
| isDigit(ch) ->
(var, rem) = identNum txt
num = fromCharList([ch]) ++ var
(rem, tokens :+ Int num)
| otherwise ->(txt, tokens :+ nextToken(ch))

Lexer.identEquals : Text -> [Token] -> (Text, [Token])
Lexer.identEquals str tokens =
Expand Down Expand Up @@ -79,3 +79,9 @@ Lexer.identNum str =
Lexer.isLetter : Char -> Boolean
Lexer.isLetter ch =
((?a <= ch) && (ch <= ?z)) || ((?A <= ch) && (ch <= ?Z)) || (ch == ?_)

Test.testFile : '{IO, Exception} [Token]
Test.testFile = do
path = FilePath "test.txt"
tokens = lexer << getText <| open path Read
tokens :+ Eof
7 changes: 1 addition & 6 deletions unison/main.u
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

---
Test.test : '{IO, Exception} Boolean
Test.test = do
result = lexer """let five = 5;
Expand Down Expand Up @@ -45,8 +45,3 @@ Test.test = do
result === expect


Test.testFile : '{IO, Exception} [Token]
Test.testFile = do
path = FilePath "test.txt"
tokens = lexer << getText <| open path Read
tokens :+ Eof
4 changes: 3 additions & 1 deletion unison/parTest.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
foobar;
a + add(b * c) + d;
add(a, b, 1, 2 * 3, 4 + 5, add(6, 7 * 8));
add(a + b + c * d / f + g);
Loading