Skip to content

Commit

Permalink
refactoring: Simplify SExp.To instances
Browse files Browse the repository at this point in the history
  • Loading branch information
supki committed Oct 5, 2024
1 parent f05c264 commit ecff98c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
12 changes: 4 additions & 8 deletions src/T/Exp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module T.Exp
, keyE_
) where

import Data.HashMap.Strict qualified as HashMap
import Data.List qualified as List
import Data.List.NonEmpty qualified as NonEmpty
import Text.Regex.PCRE.Light qualified as Pcre
Expand Down Expand Up @@ -172,10 +171,8 @@ instance SExp.To Literal where
sexp = \case
Null ->
SExp.var "null"
Bool False ->
SExp.var "false"
Bool True ->
SExp.var "true"
Bool b ->
sexp b
Int n ->
sexp n
Double n ->
Expand All @@ -185,7 +182,6 @@ instance SExp.To Literal where
Regexp regexp ->
SExp.round ["regexp", sexp regexp]
Array xs ->
SExp.square (map sexp (toList xs))
sexp xs
Record xs ->
SExp.curly
(concatMap (\(k, v) -> [sexp k, sexp v]) (List.sortOn (\(k, _v) -> k) (HashMap.toList xs)))
sexp xs
4 changes: 2 additions & 2 deletions src/T/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ evalExp = \case
r <- enforceRecord exp
case HashMap.lookup key r of
Nothing ->
throwError (MissingProperty exp (sexp (Value.Record r)) (sexp key))
throwError (MissingProperty exp (sexp r) (sexp key))
Just x ->
pure x

Expand Down Expand Up @@ -393,7 +393,7 @@ insertVar Path {var = (ann :+ name), lookups} v = do
[] ->
pure (Value.Record (HashMap.insert (fromString (Name.toString key)) v r))
_ ->
throwError (MissingProperty (ann0 :< Lit Null) (sexp (Value.Record r)) (sexp key))
throwError (MissingProperty (ann0 :< Lit Null) (sexp r) (sexp key))
go v0 (K (ann0 :+ _key) : _path) =
throwError (TypeError (ann0 :< Lit Null) Type.Record (Value.typeOf v0) (sexp v0))
go (Value.Array xs) (I (ann0 :+ idx) : path) =
Expand Down
16 changes: 16 additions & 0 deletions src/T/SExp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module T.SExp
, renderLazyText
) where

import Data.HashMap.Strict qualified as HashMap
import Data.List qualified as List
import Data.Text.Encoding qualified as Text
import Data.Text.Lazy qualified as Lazy
import Data.Text.Lazy.Builder (Builder)
Expand Down Expand Up @@ -66,6 +68,12 @@ class To a where
instance To SExp where
sexp x = x

instance To Bool where
sexp =
Var . \case
False -> "false"
True -> "true"

instance To Int64 where
sexp =
Var . fromString . show
Expand Down Expand Up @@ -98,6 +106,14 @@ instance To a => To (Vector a) where
sexp =
sexp . toList

instance (Ord k, To k, To v) => To (HashMap k v) where
sexp xs =
curly
(concatMap (\(k, v) -> [sexp k, sexp v])
(List.sortOn
(\(k, _v) -> k)
(HashMap.toList xs)))

renderLazyText :: To sexp => sexp -> Lazy.Text
renderLazyText =
Builder.toLazyText . render
Expand Down
13 changes: 4 additions & 9 deletions src/T/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ module T.Value
import Data.Aeson qualified as Aeson
import Data.Aeson.KeyMap qualified as Aeson (fromHashMapText, toHashMapText)
import Data.ByteString.Lazy qualified as Lazy (ByteString)
import Data.HashMap.Strict qualified as HashMap
import Data.List qualified as List
import Data.Scientific qualified as Scientific
import Data.Text.Lazy qualified as Text.Lazy
import Data.Text.Lazy.Encoding qualified as Text.Lazy
Expand Down Expand Up @@ -41,10 +39,8 @@ instance SExp.To Value where
sexp = \case
Null ->
SExp.var "null"
Bool False ->
SExp.var "false"
Bool True ->
SExp.var "true"
Bool b ->
sexp b
Int n ->
sexp n
Double n ->
Expand All @@ -54,10 +50,9 @@ instance SExp.To Value where
Regexp regexp ->
SExp.round ["regexp", sexp regexp]
Array xs ->
SExp.square (map sexp (toList xs))
sexp xs
Record xs ->
SExp.curly
(concatMap (\(k, v) -> [sexp k, sexp v]) (List.sortOn (\(k, _v) -> k) (HashMap.toList xs)))
sexp xs
Lam _ ->
SExp.round ["lambda", SExp.square ["_"], "..."]

Expand Down
18 changes: 13 additions & 5 deletions test/T/SExpSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,36 @@ spec = do
it "bool" $ do
render (sexp (Value.Bool False)) `shouldBe` "false"
render (sexp (Value.Bool True)) `shouldBe` "true"
render (sexp False) `shouldBe` "false"
render (sexp True) `shouldBe` "true"

it "int" $
it "int" $ do
render (sexp (Value.Int 42)) `shouldBe` "42"
render (sexp (42 :: Int64)) `shouldBe` "42"

it "double" $
it "double" $ do
render (sexp (Value.Double 4.2)) `shouldBe` "4.2"
render (sexp (4.2 :: Double)) `shouldBe` "4.2"

it "regexp" $ do
let
Right regexp =
Pcre.compileM "foo" []
render (sexp (Value.Regexp regexp)) `shouldBe` "(regexp \"foo\")"

it "string" $
it "string" $ do
render (sexp (Value.String "foo")) `shouldBe` "\"foo\""
render (sexp ("foo" :: Text)) `shouldBe` "\"foo\""

it "array" $
it "array" $ do
render (sexp (Value.Array [Value.Int 1, Value.Int 2, Value.Int 3])) `shouldBe` "[1 2 3]"
render (sexp ([1, 2, 3] :: [Int64])) `shouldBe` "[1 2 3]"

it "record" $
it "record" $ do
render (sexp (Value.Record [("foo", Value.Int 4), ("bar", Value.Int 7)])) `shouldBe`
"{\"bar\" 7 \"foo\" 4}"
render (sexp ([("foo", 4), ("bar", 7)] :: HashMap Text Int64)) `shouldBe`
"{\"bar\" 7 \"foo\" 4}"

it "lam" $
render (sexp (Value.Lam (\_ -> pure Value.Null))) `shouldBe` "(lambda [_] ...)"
Expand Down

0 comments on commit ecff98c

Please sign in to comment.