A peer-to-peer stack for code collaboration
Parse datums (#91)
James Haydonfc3c024d76c5
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex d70184a..ae4089c 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -114,6 +114,11 @@ data Value =instance Serialise Value+-- Should just be a prism+isAtom :: Value -> Maybe Ident+isAtom (Atom i) = pure i+isAtom _ = Nothing+-- | The data portion of values.data DataValue =DAtom Identdiff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex 3a9706f..72ba9a3 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -3,14 +3,13 @@ module Radicle.Internal.Parse whereimport Protolude hiding (some, try)import Data.Char (isAlphaNum, isLetter)-import Data.List.NonEmpty (NonEmpty((:|)), fromList)+import Data.List.NonEmpty (NonEmpty((:|)))import qualified Data.Map as Mapimport qualified Data.Text as Timport GHC.Exts (IsString(..))import Text.Megaparsec (ParsecT, State(..), between, choice,defaultTabWidth, eof, initialPos, manyTill,- runParserT, runParserT', sepBy, some, try,- (<?>))+ runParserT, runParserT', sepBy, try, (<?>))import qualified Text.Megaparsec as Mimport Text.Megaparsec.Char (char, satisfy, space1)import qualified Text.Megaparsec.Char.Lexer as L@@ -36,8 +35,14 @@ symbol = L.symbol spaceConsumerlexeme :: Parser a -> Parser alexeme = L.lexeme spaceConsumer+inside :: Text -> Text -> Parser a -> Parser a+inside b e = between (symbol b >> spaceConsumer) (spaceConsumer >> symbol e)+parensP :: Parser a -> Parser a-parensP = between (symbol "(" >> spaceConsumer) (spaceConsumer >> symbol ")")+parensP = inside "(" ")"++bracesP :: Parser a -> Parser a+bracesP = inside "{" "}"stringLiteralP :: VParserstringLiteralP = lexeme $@@ -72,19 +77,22 @@ keywordP = dokw <- many (satisfy isValidIdentRest)pure . Keyword . Ident . fromString $ kw-applyP :: VParser-applyP = List <$> valueP `sepBy` spaceConsumer+listP :: VParser+listP = parensP (List <$> valueP `sepBy` spaceConsumer)++dictP :: VParser+dictP = bracesP (Dict . Map.fromList <$> evenItems)+ where+ evenItems = twoItems `sepBy` spaceConsumer+ twoItems = do+ x <- valueP+ spaceConsumer+ y <- valueP+ pure (x,y)quoteP :: VParserquoteP = List . ((Primop $ toIdent "quote") :) . pure <$> (char '\'' >> valueP)-lambdaP :: VParser-lambdaP = do- void $ symbol "lambda"- vars <- parensP $ identP `sepBy` spaceConsumer- body <- fromList <$> some valueP- pure $ Lambda vars body Nothing-valueP :: VParservalueP = dov <- choice@@ -94,15 +102,11 @@ valueP = do, try numLiteralP <?> "number", atomOrPrimP <?> "identifier", quoteP <?> "quote"- , parensP appLike <?> "application"+ , listP <?> "list"+ , dictP <?> "dict"]spaceConsumerpure v- where- appLike = choice- [ lambdaP <?> "lambda"- , applyP <?> "application"- ]-- * Utilitiesdiff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 3cd4837..ba9db1b 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -33,9 +33,9 @@ instance Pretty Value where[v'] -> parens $ pretty v'_ -> parens $ hang 1 (sep $ pretty <$> vs)Primop i -> pretty i- Dict mp -> parens . hang 1 $- "dict" <%> sep [ pretty k <+> pretty val- | (k, val) <- Map.toList mp ]+ Dict mp -> braces . align $+ sep [ pretty k <+> pretty val+ | (k, val) <- Map.toList mp ]Lambda ids vals _ -> parens $"lambda" <+> align (sep[ parens . sep $ pretty <$> ids@@ -43,7 +43,6 @@ instance Pretty Value where])whereescapeStr = T.replace "\"" "\\\"" . T.replace "\\" "\\\\"- a <%> b = a <> softline <> binstance Pretty r => Pretty (LangError r) wherepretty v = case v of@@ -64,7 +63,7 @@ instance Pretty r => Pretty (LangError r) where-- Examples:---- >>> renderCompactPretty (List [String "hi", String "there"])--- "(\"hi\"\n\"there\")"+-- "(\"hi\" \"there\")"renderCompactPretty :: Pretty v => v -> TextrenderCompactPretty = renderStrict . layoutCompact . pretty@@ -76,7 +75,7 @@ renderCompactPretty = renderStrict . layoutCompact . pretty-- "(\"hi\" \"there\")"---- >>> renderPretty (AvailablePerLine 6 0.5) (List [String "hi", String "there"])--- "(\"hi\"\n \"there\")"+-- "(\"hi\" \"there\")"renderPretty :: Pretty v => PageWidth -> v -> TextrenderPretty pg = renderStrict . layoutSmart (LayoutOptions pg) . prettydiff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hsindex 3938ddb..c629b79 100644--- a/src/Radicle/Internal/Primops.hs+++ b/src/Radicle/Internal/Primops.hs@@ -34,7 +34,15 @@ pureEnv = Bindings e purePrimops r 1-- not shadowable via 'define'.purePrimops :: forall m. (Monad m) => Primops mpurePrimops = fromList $ first Ident <$>- [ ("base-eval", evalOneArg "base-eval" baseEval)+ [ ( "lambda"+ , \case+ List atoms_ : b : bs -> do+ atoms <- traverse isAtom atoms_ ?? "lambda: expecting a list of symbols"+ e <- gets bindingsEnv+ pure (Lambda atoms (b :| bs) (Just e))+ xs -> throwError $ WrongNumberOfArgs "lambda" 2 (length xs) -- TODO: technically "at least 2"+ )+ , ("base-eval", evalOneArg "base-eval" baseEval), ( "pure-env", \case[] -> pure $ unmakeBindings (pureEnv :: Bindings m)diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 73b3d7a..cb7f8cf 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -396,7 +396,7 @@ test_eval =runTest' "(show #f)" @?= Right (String "#f")runTest' "(show (list 'a 1 \"foo\" (list 'b ''x 2 \"bar\")))" @?= Right (String "(a 1.0 \"foo\" (b (quote x) 2.0 \"bar\"))")runTest' "eval" @?= Right (Primop (toIdent "base-eval"))- runTest' "(show (dict 'a 1))" @?= Right (String "(dict a 1.0)")+ runTest' "(show (dict 'a 1))" @?= Right (String "{a 1.0}")runTest' "(show (lambda (x) x))" @?= Right (String "(lambda (x) x)"), testCase "'read' works" $@@ -437,7 +437,7 @@ test_parser ="(++)" ~~> List [Atom (toIdent "++")]"(++ \"merge\" \"d\")" ~~> List [Atom (toIdent "++"), String "merge", String "d"]- , testCase "parses number" $ do+ , testCase "parses numbers" $ do"0.15" ~~> Number 0.15"2000" ~~> Number 2000@@ -447,6 +447,12 @@ test_parser =, testCase "parses identifiers that have a primop as prefix" $ do"evaluate" ~~> Atom (toIdent "evaluate")++ , testCase "parses dicts" $ do+ "{:foo 3}" ~~> Dict (Map.singleton (Keyword (toIdent "foo")) (Number 3))++ , testCase "a dict litteral with an odd number of forms is a syntax error" $+ assertBool "succesfully parsed a dict with 3 forms" (isLeft $ parseTest "{:foo 3 4}")]wherex ~~> y = parseTest x @?= Right y@@ -486,13 +492,13 @@ test_pretty =let r = renderPretty (apl 80) (Dict $ Map.fromList [ (kw "k1", Number 1), (kw "k2", Number 2)])- r @?= "(dict :k1 1.0 :k2 2.0)"+ r @?= "{:k1 1.0 :k2 2.0}", testCase "dicts split with key-val pairs" $ dolet r = renderPretty (apl 5) (Dict $ Map.fromList [ (kw "k1", Number 1), (kw "k2", Number 2)])- r @?= "(dict\n :k1 1.0\n :k2 2.0)"+ r @?= "{:k1 1.0\n :k2 2.0}"]whereapl cols = AvailablePerLine cols 1