Skip to content

A peer-to-peer stack for code collaboration

Add keyword values and keyword litterals (#55)

James Haydon2a36a5d3711d
diff --git a/package.yaml b/package.yaml
index d7e1ecc..e0d0cad 100644
--- a/package.yaml
+++ b/package.yaml
@@ -91,3 +91,4 @@ default-extensions:
- TypeFamilies
- TypeOperators
- ScopedTypeVariables
+- LambdaCase
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 12bf8ad..7e10d5a 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -88,6 +88,8 @@ readRef (Reference r) = do
data 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 = do
baseEval :: Monad m => Value -> Lang m Value
baseEval val = case val of
Atom i -> lookupAtom i
+ kw@(Keyword _) -> pure kw
Ref i -> pure $ Ref i
List (f:vs) -> f $$ vs
List xs -> throwError
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index 88d292b..a500b59 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -67,6 +67,12 @@ atomOrPrimP = do
prims <- ask
pure $ 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 :: VParser
applyP = List <$> valueP `sepBy` spaceConsumer
@@ -85,6 +91,7 @@ valueP = do
v <- 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.hs
index 0241fbe..0f6f13a 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -22,6 +22,7 @@ instance Pretty Ident where
instance Pretty Value where
pretty v = case v of
Atom i -> pretty i
+ Keyword i -> ":" <> pretty i
Ref i -> parens $ "ref" <+> pretty i
String t -> "\"" <> pretty (escapeStr t) <> "\""
Number n -> pretty (show n :: Text)
diff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hs
index 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 False
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 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" $ do
let 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" $ do
let 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" $ do
let 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!")