Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.
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
20 changes: 16 additions & 4 deletions Data/AttoLisp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ instance NFData Lisp where

-- | Returns 'True' if the expression is @nil@ or the empty list.
isNull :: Lisp -> Bool
isNull (List []) = True
isNull (Symbol "nil") = True
isNull (List []) = True
isNull (Symbol n) = T.toLower n == "nil"
isNull _ = False

-- | The empty list.
Expand Down Expand Up @@ -655,9 +655,21 @@ atom = number <|> symbol
number :: A.Parser Lisp
number = do
sym <- takeWhile1 (not . terminatingChar)
unless (isNumlike sym) $ fail "Not a number"
case A.parseOnly AC.number sym of
Left _ -> fail "Not a number"
Right n -> return (Number n)
where
isNumlike x = B.all (`elem` numchars) x
&& length exps < 2
&& lpos `notElem` exps
where
exps = B.findIndices (`elem` expchars) x
lpos = B.length x - 1 -- last char
-- map ord "+-.0123456789" ++ expchars
numchars = [43,45,46,48,49,50,51,52,53,54,55,56,57] ++ expchars
-- map ord "esfdlESFDL"
expchars = [101,115,102,100,108,69,83,70,68,76]

symbol :: A.Parser Lisp
symbol = Symbol <$> sym
Expand Down Expand Up @@ -725,8 +737,8 @@ basicPart = do
let !lst = B.last rest
!pref = escapee `B.append` rest
if lst == backslash
then B.append pref <$> chunk
else pure pref
then B.append pref <$> chunk
else pure pref
--
decodeSym = T.decodeUtf8

Expand Down
43 changes: 36 additions & 7 deletions test/test-attolisp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ instance FromLisp Msg where
parseLisp e = struct "msg" Msg e


test_sexp1 =
show (List [Number 42.2, Symbol "foo", "blah"]) == "(42.2 foo \"blah\")"

test_msg1 = toLisp (Msg "foo" 42)
test_msg2 = List [Symbol "msg"]
test_msg3 = List [Symbol "msg", "bar", "baz"]

data T = T { tin :: B.ByteString
, tout :: Maybe Lisp
}
Expand All @@ -40,6 +33,8 @@ main :: IO ()
main = defaultMain
[ testSimple
, testTokens
, testParseLisp
, testShow
]

tcase :: T -> Test.Framework.Test
Expand All @@ -56,6 +51,10 @@ assertParse desc (Just v) (Right v2) = assertEqual desc v v2
testSimple = testGroup "simple" $ map tcase
[ T "()" (Just $ List [])
, T "42" (Just $ Number 42)
, T "42blop" (Just $ Symbol "42blop")
, T "3e3" (Just $ Number 3000)
, T "3e3e" (Just $ Symbol "3e3e")
, T "3e" (Just $ Symbol "3e")
, T ";;foo\n42" (Just $ Number 42)
, T ";;foo\n;;bar\n42" (Just $ Number 42)
, T "(4 5 6)" (Just $ List [Number 4, Number 5, Number 6])
Expand Down Expand Up @@ -134,3 +133,33 @@ addPkg p t = t { tin = BC.concat [ p, ":", tin t ]
tweak Nothing = Nothing
tweak (Just (Symbol x)) = Just . Symbol $ T.concat [T.decodeUtf8 p, ":", x]
tweak (Just x) = Nothing

-- ----------------------------------------------------------------------
-- From Lisp to Haskell
-- ----------------------------------------------------------------------

testParseLisp = testGroup "parseLisp"
[ tc "Maybe Just" (Just (Just 3 :: Maybe Int)) "3"
, tc "Maybe case 1" (Just (Nothing :: Maybe Int)) "nil"
, tc "Maybe case 2" (Just (Nothing :: Maybe Int)) "NIL"
, tc "Msg 1" (Nothing :: Maybe Msg) "(msg)"
, tc "Msg 2" (Nothing :: Maybe Msg) "(msg bar baz)"
, tc "Msg 3" (Just (Msg "foo" 42)) "(msg \"foo\" 42)"
]
where
tc descr res inp =
testCase msg $ assertParse "" res (parse inp)
where
msg = BC.unpack $ BC.concat [descr, " (", inp, ")" ]
parse i = A.parseOnly (lisp <* A.endOfInput) i >>= parseEither parseLisp

-- ----------------------------------------------------------------------
-- Displaying Lisp
-- ----------------------------------------------------------------------

testShow = testGroup "show"
[ tc "(42.2 foo \"blah\")" (List [Number 42.2, Symbol "foo", "blah"])
, tc "(msg \"foo\" 42)" (toLisp (Msg "foo" 42))
]
where
tc res inp = testCase res $ assertEqual "" res (show inp)