A peer-to-peer stack for code collaboration
Add keyword values and keyword litterals (#55)
James Haydon2a36a5d3711d
diff --git a/package.yaml b/package.yamlindex d7e1ecc..e0d0cad 100644--- a/package.yaml+++ b/package.yaml@@ -91,3 +91,4 @@ default-extensions:- TypeFamilies- TypeOperators- ScopedTypeVariables+- LambdaCasediff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 12bf8ad..7e10d5a 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -88,6 +88,8 @@ readRef (Reference r) = dodata Value =-- | A regular (hyperstatic) variable.Atom Ident+ -- | Symbolic identifiers that evaluate to themselves.+ | Keyword Ident| String Text| Number Scientific| Boolean Bool@@ -212,6 +214,7 @@ eval val = dobaseEval :: Monad m => Value -> Lang m ValuebaseEval val = case val ofAtom i -> lookupAtom i+ kw@(Keyword _) -> pure kwRef i -> pure $ Ref iList (f:vs) -> f $$ vsList xs -> throwErrordiff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex 88d292b..a500b59 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -67,6 +67,12 @@ atomOrPrimP = doprims <- askpure $ if i `elem` prims then Primop i else Atom i+keywordP :: VParser+keywordP = do+ _ <- char ':'+ kw <- many (satisfy isValidIdentRest)+ pure . Keyword . Ident . fromString $ kw+applyP :: VParserapplyP = List <$> valueP `sepBy` spaceConsumer@@ -85,6 +91,7 @@ valueP = dov <- choice[ stringLiteralP <?> "string", boolLiteralP <?> "boolean"+ , keywordP <?> "keyword", try numLiteralP <?> "number", atomOrPrimP <?> "identifier", quoteP <?> "quote"@@ -194,7 +201,7 @@ parseTest t = parse "(test)" t (Map.keys $ bindingsPrimops e)-- | A predicate which returns true if the character is valid as the first-- character of an identifier.isValidIdentFirst :: Char -> Bool-isValidIdentFirst x = isLetter x || x `elem` extendedChar+isValidIdentFirst x = x /= ':' && (isLetter x || x `elem` extendedChar)-- | A predicate which returns true if the character is valid as the second or-- later character of an identifier.diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 0241fbe..0f6f13a 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -22,6 +22,7 @@ instance Pretty Ident whereinstance Pretty Value wherepretty v = case v ofAtom i -> pretty i+ Keyword i -> ":" <> pretty iRef i -> parens $ "ref" <+> pretty iString t -> "\"" <> pretty (escapeStr t) <> "\""Number n -> pretty (show n :: Text)diff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hsindex d27b7df..32f8ab7 100644--- a/src/Radicle/Internal/Primops.hs+++ b/src/Radicle/Internal/Primops.hs@@ -129,6 +129,10 @@ purePrimops = fromList $ first Ident <$>[fn, List ls] -> List <$> traverse (fn $$) (pure <$> ls)[_, _] -> throwError $ TypeError "map: second argument should be a list"xs -> throwError $ WrongNumberOfArgs "map" 3 (length xs))+ , ("keyword?", evalArgs $ \case+ [Keyword _] -> pure $ Boolean True+ [_] -> pure $ Boolean False+ xs -> throwError $ WrongNumberOfArgs "keyword?" 1 (length xs)), ("string?", evalArgs $ \args -> case args of[String _] -> pure $ Boolean True[_] -> pure $ Boolean Falsediff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 9892d26..d25cad4 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -25,6 +25,9 @@ test_eval =[ testCase "Fails for undefined atoms" $[s|blah|] `failsWith` UnknownIdentifier (toIdent "blah")+ , testCase "Keywords eval to themselves" $+ [s|:blah|] `succeedsWith` Keyword (toIdent "blah")+, testCase "Succeeds for defined atoms" $ dolet prog = [s|(define rocky-clark "Steve Wozniak")@@ -186,6 +189,12 @@ test_eval =let prog = [s|(if #t "a" (#t "non-sense"))|]prog `succeedsWith` String "a"+ , testCase "'keyword?' is true for keywords" $ do+ "(keyword? :foo)" `succeedsWith` Boolean True++ , testCase "'keyword?' is false for non keywords" $ do+ "(keyword? #t)" `succeedsWith` Boolean False+, testCase "'string?' is true for strings" $ dolet prog = [s|(string? "hi")|]prog `succeedsWith` Boolean True@@ -194,10 +203,6 @@ test_eval =let prog = [s|(string? #f)|]prog `succeedsWith` Boolean False- , testCase "'string?' is false for nonStrings" $ do- let prog = [s|(string? #f)|]- prog `succeedsWith` Boolean False-, testCase "'boolean?' is true for booleans" $ dolet prog = [s|(boolean? #t)|]prog `succeedsWith` Boolean True@@ -360,6 +365,14 @@ test_parser ="boolean?" ~~> Primop (toIdent "boolean?")"base-eval" ~~> Primop (toIdent "base-eval")+ , testCase "parses keywords" $ do+ let kw = Keyword . toIdent+ ":foo" ~~> kw "foo"+ ":what?crazy!" ~~> kw "what?crazy!"+ ":::" ~~> kw "::"+ "::foo" ~~> kw ":foo"+ ":456" ~~> kw "456"+, testCase "parses identifiers" $ do"++" ~~> Atom (toIdent "++")"what?crazy!" ~~> Atom (toIdent "what?crazy!")