R7RS.hs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. module Data.SCargot.Scheme.R7RS where
  2. -- | A Scheme value type. This is strictly larger than just
  3. -- 'atoms', as they may include things like vectors or
  4. -- labeled data, which must be able to refer to yet other
  5. -- s-expressions. Thus, the SchemeAtom type must be able
  6. -- to itself refer to the carrier type in which it is
  7. -- contained.
  8. data SchemeAtom carrier
  9. = ScmIdent Text
  10. | ScmBool Bool
  11. | ScmString Text
  12. | ScmNum Integer
  13. | ScmChar Char
  14. | ScmVec [carrier (SchemeAtom carrier)]
  15. | ScmByteVec [Word8]
  16. | ScmLabeledDatum Int (carrier (SchemeAtom carrier))
  17. | ScmLabelReference Int
  18. deriving (Eq, Show)
  19. withQuasiQuote :: SExprSpec (SchemeAtom SExpr) (SExpr (SchemeAtom Sexpr))
  20. -> SExprSpec (SchemeAtom SExpr) (SExpr (SchemeAtom SExpr))
  21. withQuasiQuote spec = addReader '`' (fmap (go "quasiquote"))
  22. $ addReader ',' unquote
  23. $ spec
  24. where go name s = name ::: s ::: Nil
  25. unquote p = char '@' *> fmap (go "unquote-splicing")
  26. <|> fmap (go "unquote")
  27. octoReader :: Reader (SExpr (SchemeAtom SExpr))
  28. octoReader pSexpr =
  29. string "true" *> pure (ScmBool True)
  30. <|> string "false" *> pure (ScmBool False)
  31. <|> char 't' *> pure (ScmBool True)
  32. <|> char 'f' *> pure (ScmBool False)
  33. <|> char '\\' *> characterConstant
  34. <|> char '(' *> fmap ScmVec (vector pSexpr)
  35. <|> string "u8(" *> bytevec
  36. <|> do n <- read <$> many1 digit
  37. (char '#' *> ScmLabelReference n <|>
  38. char '=' *> fmap (ScmLabeledDatum n) pSexpr)
  39. vector :: Parser (SExpr (SchemeAtom SExpr)) -> Parser [SExpr (SchemeAtom SExpr)]
  40. vector pSexpr =
  41. (char ')' *> pure []) <|> ((:) <$> pSExpr <*> vector pSexpr)