A peer-to-peer stack for code collaboration
Add vectors with clojure-like syntax. (#122)
James Haydon2c2786bbde39
diff --git a/src/Radicle.hs b/src/Radicle.hsindex acd9a44..6942982 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -29,6 +29,7 @@ module Radicle, pattern Number, pattern Boolean, pattern List+ , pattern Vec, pattern Primop, pattern Dict, pattern Refdiff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 4e87f90..cd69f46 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -21,6 +21,7 @@ import qualified Data.List.NonEmpty as NonEmptyimport qualified Data.Map as Mapimport Data.Scientific (Scientific, floatingOrInteger)import Data.Semigroup ((<>))+import qualified Data.Sequence as Seqimport Generics.Eotimport qualified GHC.Exts as GhcExtsimport qualified Text.Megaparsec.Error as Par@@ -125,6 +126,7 @@ data ValueF r =| NumberF Scientific| BooleanF Bool| ListF [r]+ | VecF (Seq.Seq r)| PrimopF Ident-- | Map from *pure* Values -- annotations shouldn't change lookup semantics.| DictF (Map.Map Value r)@@ -170,6 +172,11 @@ pattern List vs <- (Ann.match -> ListF vs)whereList = Ann.annotate . ListF+pattern Vec :: ValueConC t => Seq.Seq (Annotated t ValueF) -> Annotated t ValueF+pattern Vec vs <- (Ann.match -> VecF vs)+ where+ Vec = Ann.annotate . VecF+pattern Primop :: ValueConC t => Ident -> Annotated t ValueFpattern Primop i <- (Ann.match -> PrimopF i)where@@ -394,6 +401,7 @@ baseEval val = logValPos val $ case val of$ WrongNumberOfArgs ("application: " <> show xs)2(length xs)+ Vec xs -> Vec <$> traverse baseEval xsDict mp -> dolet evalBoth (a,b) = (,) <$> baseEval a <*> baseEval bDict . Map.fromList <$> traverse evalBoth (Map.toList mp)@@ -406,6 +414,8 @@ class FromRad a wheredefault fromRad :: (HasEot a, FromRadG (Eot a)) => Value -> Either Text afromRad = fromRadG+instance FromRad Value where+ fromRad = pureinstance FromRad Scientific wherefromRad x = case x ofNumber n -> pure n@@ -423,6 +433,7 @@ instance FromRad Text whereinstance FromRad a => FromRad [a] wherefromRad x = case x ofList xs -> traverse fromRad xs+ Vec xs -> traverse fromRad (toList xs)_ -> Left "Expecting list"instance FromRad (Env Value) wherefromRad x = case x of@@ -522,8 +533,11 @@ kwLookup key = Map.lookup (Keyword $ Ident key)a ?? n = n `note` ahoistEither :: MonadError e m => Either e a -> m a-hoistEither (Left e) = throwError e-hoistEither (Right x) = pure x+hoistEither = hoistEitherWith identity++hoistEitherWith :: MonadError e' m => (e -> e') -> Either e a -> m a+hoistEitherWith f (Left e) = throwError (f e)+hoistEitherWith _ (Right x) = pure x-- * Generic encoding/decoding of Radicle values.diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex b5ff5cd..6c90e76 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -5,6 +5,7 @@ import Protolude hiding (SrcLoc, try)import Data.Char (isAlphaNum, isLetter)import Data.List.NonEmpty (NonEmpty((:|)))import qualified Data.Map as Map+import qualified Data.Sequence as Seqimport qualified Data.Text as Timport GHC.Exts (IsString(..))import Text.Megaparsec@@ -62,6 +63,9 @@ parensP = inside "(" ")"bracesP :: Parser a -> Parser abracesP = inside "{" "}"+sqBracketsP :: Parser a -> Parser a+sqBracketsP = inside "[" "]"+stringLiteralP :: VParserstringLiteralP = lexeme $tag =<< StringF . toS <$> (char '"' >> manyTill L.charLiteral (char '"'))@@ -98,6 +102,9 @@ keywordP = dolistP :: VParserlistP = parensP (tag =<< (ListF <$> valueP `sepBy` spaceConsumer))+vecP :: VParser+vecP = sqBracketsP (tag =<< (VecF . Seq.fromList <$> valueP `sepBy` spaceConsumer))+dictP :: VParserdictP = bracesP (tag =<< (DictF . Map.fromList <$> evenItems))where@@ -124,6 +131,7 @@ valueP = do, atomOrPrimP <?> "identifier", quoteP <?> "quote", listP <?> "list"+ , vecP <?> "vector", dictP <?> "dict"]spaceConsumerdiff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex ae929b9..2927708 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -5,6 +5,7 @@ import Protolude hiding (TypeError, (<>))import Data.Copointed (Copointed(..))import qualified Data.Map as Map+import Data.Sequence (Seq(..))import qualified Data.Text as Timport Data.Text.Prettyprint.Docimport Data.Text.Prettyprint.Doc.Render.Text@@ -35,6 +36,10 @@ instance (Copointed t, Ann.Annotation t) => Pretty (Ann.Annotated t ValueF) wher[] -> "()"[v'] -> parens $ pretty v'_ -> parens $ hang 1 (sep $ pretty <$> vs)+ Vec vs -> case vs of+ Empty -> "[]"+ v' :<| Empty -> brackets $ pretty v'+ _ -> brackets . sep $ pretty <$> toList vsPrimop i -> pretty iDict mp -> braces . align $sep [ pretty k <+> pretty valdiff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hsindex 812bcfb..4dc923e 100644--- a/src/Radicle/Internal/Primops.hs+++ b/src/Radicle/Internal/Primops.hs@@ -129,9 +129,11 @@ purePrimops = Primops $ fromList $ first Ident <$>_ -> throwErrorHere $ TypeError "tail: expects list argument"), ( "nth", evalArgs $ \case- [Number n, List xs] -> case floatingOrInteger n of+ [Number n, vs] -> case floatingOrInteger n ofLeft (_ :: Double) -> throwErrorHere $ OtherError "nth: first argument was not an integer"- Right i -> case xs `atMay` i of+ Right i -> do+ xs <- hoistEitherWith (const (toLangError . OtherError $ "nth: first argument must be sequential")) $ fromRad vs+ case xs `atMay` i ofJust x -> pure xNothing -> throwErrorHere $ OtherError "nth: index out of bounds"[_,_] -> throwErrorHere $ TypeError "nth: expects a integer and a list"@@ -256,8 +258,9 @@ purePrimops = Primops $ fromList $ first Ident <$>, evalOneArg "seq" $\casex@(List _) -> pure x+ x@(Vec _) -> pure xDict kvs -> pure $ List [List [k, v] | (k,v) <- Map.toList kvs ]- _ -> throwErrorHere $ TypeError "seq: can only create a list from a list or a dict"+ _ -> throwErrorHere $ TypeError "seq: can only be used on a list, vector or dictionary"), ( "to-json", evalOneArg "to-json" $ \v -> String . toS . Aeson.encode <$>diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 89d517b..2500bf9 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -7,6 +7,7 @@ import Data.List (isInfixOf, isSuffixOf)import qualified Data.Map.Strict as Mapimport Data.Maybe (fromJust)import Data.Scientific (Scientific)+import Data.Sequence (Seq(..))import Data.String.Interpolate (i)import Data.String.QQ (s)import qualified Data.Text as T@@ -39,6 +40,14 @@ test_eval =|]prog `succeedsWith` String "Steve Wozniak"+ , testCase "vector syntax creates vectors" $ do+ "[]" `succeedsWith` Vec Empty+ "[1 2]" `succeedsWith` Vec (fromList [Number 1, Number 2])+ "[1 :a [:b 2]]" `succeedsWith` Vec (fromList [Number 1, kw "a", Vec (fromList [kw "b", Number 2])])++ , testCase "vectors evaluate their elements" $+ "[(+ 1 2) (* 3 4)]" `succeedsWith` Vec (fromList [Number 3, Number 12])+, testCase "'dict' creates a Dict with given key/vals" $ dolet prog1 = [s|(dict 'why "not")|]prog1 `succeedsWith` Dict (fromList [(Atom $ toIdent "why", String "not")])@@ -61,6 +70,12 @@ test_eval =let prog = [s|(tail (list #t #f #t))|]prog `succeedsWith` List [Boolean False, Boolean True]+ , testCase "'nth' extracts elements from lists and vectors" $ do+ "(nth 0 [0 1 2])" `succeedsWith` Number 0+ "(nth 1 [0 1 2])" `succeedsWith` Number 1+ "(nth 0 '(0 1 2))" `succeedsWith` Number 0+ "(nth 1 '(0 1 2))" `succeedsWith` Number 1+, testProperty "'eq?' considers equal values equal" $ \(val :: Value) -> dolet prog = [i|(eq? #{renderPrettyDef val} #{renderPrettyDef val})|]res = runTest' $ toS prog