Skip to content

A peer-to-peer stack for code collaboration

Add a source tag parameter to `read` and `read-many` (#313)

James Haydonc1db3c62d382
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index abe2158..95862e6 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -69,16 +69,19 @@ label for the exception, the second can be any value.
Exit the interpreter immediately with the given exit code.
-``read``
-~~~~~~~~
+``read-annotated``
+~~~~~~~~~~~~~~~~~~
-Parses a string into a radicle value. Does not evaluate the value.
+``(read-anotated label s)`` parses the string ``s`` into a radicle
+value. The resulting value is not evaluated. The ``label`` argument is a
+string which is used to annotate the value with line numbers.
-``read-many``
-~~~~~~~~~~~~~
+``read-many-annotated``
+~~~~~~~~~~~~~~~~~~~~~~~
-Parses a string into a vector of radicle values. Does not evaluate the
-values.
+(read-many-annotated label s) parses a string into a vector of radicle
+values. The resulting values are not evaluated. The ``label`` argument
+is a string which is used to annotate the values with line numbers.
``base-eval``
~~~~~~~~~~~~~
@@ -702,16 +705,21 @@ and stderr are inherited. See ``man exec`` for more information on
Read a single line of input and interpret it as radicle data.
+``(read-file-value! file)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Read a single radicle value from a file.
+
+``(read-file-values! file)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Read many radicle values from a file.
+
``(send-code! chain-id filename)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Send code from a file to a remote chain.
-``(read-code! filename)``
-~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Read code (as data) from a file. Returns a vector of expressions
-
``(shell-with-stdout! command to-write)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/rad/crypto.rad b/rad/crypto.rad
deleted file mode 100644
index 412a224..0000000
--- a/rad/crypto.rad
+++ /dev/null
@@ -1,15 +0,0 @@
-(def sign-from-file!
- "Create a signed message using a key-pair stored in a file. The file should contain output as generated by `gen-key-pair!`."
- (fn [keys-file msg]
- (def ks (read (read-file! keys-file)))
- (def sig (gen-signature! (lookup :private-key ks) msg))
- {:message msg
- :signature sig
- :key (lookup :public-key ks)}))
-
-(def verify-signed-message
- "Verify the signature of a signed message."
- (fn [sig-msg]
- (verify-signature (lookup :key sig-msg)
- (lookup :signature sig-msg)
- (lookup :message sig-msg))))
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index 6486aad..9df3ab8 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -30,7 +30,7 @@
(def nonce (uuid!))
(def e_ (<> e {:chain-id radicle-issues-chain-id
:nonce nonce}))
- (def ks (read (read-file! "my-keys.rad")))
+ (def ks (read-file-value! "my-keys.rad"))
(def sig
(gen-signature! (lookup :private-key ks)
(show e_)))
@@ -104,7 +104,7 @@
x)))
(def note-comments
(fn [i] (over (@ :comments) (fn [cs] (map note-imported cs)) i)))
- (def issues (read (read-file! "githubissues.rad")))
+ (def issues (read-file-value! "githubissues.rad"))
(def imported
(map
(fn [i] (note-imported (note-comments i)))
diff --git a/rad/prelude/basic.rad b/rad/prelude/basic.rad
index 0c3191c..aa6da2f 100644
--- a/rad/prelude/basic.rad
+++ b/rad/prelude/basic.rad
@@ -88,3 +88,19 @@
[ (elem? "f" (list "f" "g" "h")) ==> #t]
[ (elem? "h" ["f" "g" "h"]) ==> #t]
[ (elem? "a" (list "f" "g" "h")) ==> #f])
+
+(def read
+ "Reads a radicle value from a string."
+ (fn [s]
+ (read-annotated "[read]" s)))
+
+(:test "read"
+ [ (read "[:a 42 \"foo\" {:key (1 2 3)}]") ==> [:a 42 "foo" {:key (1 2 3)}] ])
+
+(def read-many
+ "Reads many radicle values from a string."
+ (fn [s]
+ (read-many-annotated "[read-many]" s)))
+
+(:test "read-many"
+ [ (read-many "([:one] {:two 3}) (four \"five\" 6)") ==> [([:one] {:two 3}) (four "five" 6)]])
diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.rad
index c537b46..fd0cdaf 100644
--- a/rad/prelude/chain.rad
+++ b/rad/prelude/chain.rad
@@ -2,6 +2,7 @@
:doc "Functions for simulating remote chains."
:exports '[new-chain load-chain! eval-in-chain update-chain-ref! eval updatable-eval update-chain! eval-fn-app send-prelude!]}
+(import prelude/io '[read-file-values!] :as 'io)
(import prelude/ref '[modify-ref] :unqualified)
(import prelude/lens '[.. ... view over @] :unqualified)
(import prelude/basic :unqualified)
@@ -197,7 +198,7 @@
(def file-to-module
"Make the text of a file into code for a module."
(fn [file]
- (cons 'module (vec-to-list (read-many (read-file! file))))))
+ (cons 'module (vec-to-list (io/read-file-values! file)))))
(def pure-prelude-code
"Code for the pure prelude. Useful as the first inputs to a new chain."
diff --git a/rad/prelude/io.rad b/rad/prelude/io.rad
index 033e022..3e78b85 100644
--- a/rad/prelude/io.rad
+++ b/rad/prelude/io.rad
@@ -1,25 +1,29 @@
{:module 'prelude/io
:doc "Some basic I/O functions."
- :exports '[print! shell! process! read-line! send-code! read-code!
+ :exports '[print! shell! process! read-line! read-file-value! read-file-values! send-code!
shell-with-stdout! shell-no-stdin! process-with-stdout! write-file!
]}
(import prelude/patterns :unqualified)
-;; read-line
(def read-line!
"Read a single line of input and interpret it as radicle data."
- (fn [] (read (get-line!))))
+ (fn [] (read-annotated "[read-line!]" (get-line!))))
-(def read-code!
- "Read code (as data) from a file. Returns a vector of expressions"
- (fn [filename]
- (read-many (read-file! filename))))
+(def read-file-value!
+ "Read a single radicle value from a file."
+ (fn [file]
+ (read-annotated file (read-file! file))))
+
+(def read-file-values!
+ "Read many radicle values from a file."
+ (fn [file]
+ (read-many-annotated file (read-file! file))))
(def send-code!
"Send code from a file to a remote chain."
(fn [chain-id filename]
- (send! chain-id (read-code! filename))))
+ (send! chain-id (read-file-values! filename))))
(def print!
"Print a value to the console or stdout."
diff --git a/rad/repl.rad b/rad/repl.rad
index cd650ac..08c9af9 100644
--- a/rad/repl.rad
+++ b/rad/repl.rad
@@ -6,7 +6,7 @@
(def line (get-line!))
(if (eq? line nil)
(exit! 0)
- (read-many line))))
+ (read-many-annotated "[read-line-or-exit!]" line))))
(def repl
{:name "repl"
diff --git a/reference-doc.yaml b/reference-doc.yaml
index 7dabc64..79aab13 100644
--- a/reference-doc.yaml
+++ b/reference-doc.yaml
@@ -26,8 +26,8 @@ primFns:
- show
- throw
- exit!
-- read
-- read-many
+- read-annotated
+- read-many-annotated
# Eval
- base-eval
# Refs
diff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hs
index 0676b04..4f1293a 100644
--- a/src/Radicle/Internal/PrimFns.hs
+++ b/src/Radicle/Internal/PrimFns.hs
@@ -75,24 +75,30 @@ purePrimFns = fromList $ allDocs $
[] -> pure $ bindingsToRadicle (pureEnv :: Bindings (PrimFns m))
xs -> throwErrorHere $ WrongNumberOfArgs "pure-env" 0 (length xs)
)
- , ("apply"
+ , ( "apply"
, "Calls the first argument (a function) using as arguments the\
\ elements of the the second argument (a list)."
, \case
[fn, List args] -> callFn fn args
[_, v] -> throwErrorHere $ TypeError "apply" 1 TList v
xs -> throwErrorHere $ WrongNumberOfArgs "apply" 2 (length xs))
- , ( "read"
- , "Parses a string into a radicle value. Does not evaluate the value."
- , oneArg "read" $ \case
- String s -> readValue "[read]" s
- v -> throwErrorHere $ TypeError "read" 0 TString v
- )
- , ( "read-many"
- , "Parses a string into a vector of radicle values. Does not evaluate the values."
- , oneArg "read-many" $ \case
- String s -> Vec . Seq.fromList <$> readValues "[read-many]" s
- v -> throwErrorHere $ TypeError "read-many" 0 TString v
+ , ( "read-annotated"
+ , "`(read-anotated label s)` parses the string `s` into a radicle value.\
+ \ The resulting value is not evaluated. The `label` argument is a string\
+ \ which is used to annotate the value with line numbers."
+ , twoArg "read-annotated" $ \case
+ (String label, String s) -> readValue label s
+ (String _, v) -> throwErrorHere $ TypeError "read" 1 TString v
+ (v, _) -> throwErrorHere $ TypeError "read" 0 TString v
+ )
+ , ( "read-many-annotated"
+ , "(read-many-annotated label s) parses a string into a vector of radicle\
+ \ values. The resulting values are not evaluated. The `label` argument \
+ \is a string which is used to annotate the values with line numbers."
+ , twoArg "read-many" $ \case
+ (String label, String s) -> Vec . Seq.fromList <$> readValues label s
+ (String _, v) -> throwErrorHere $ TypeError "read-many" 1 TString v
+ (v, _) -> throwErrorHere $ TypeError "read-many" 0 TString v
)
, ("get-current-env"
, "Returns the current radicle state."
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 1e2af98..9cd6b02 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -469,8 +469,8 @@ test_eval =
runPureCode "(show (dict 'a 1))" @?= Right (String "{a 1}")
runPureCode "(show (fn [x] x))" @?= Right (String "(fn [x] x)")
- , testCase "'read' works" $
- runPureCode "(read \"(:hello 42)\")" @?= Right (List [Keyword [ident|hello|], int 42])
+ , testCase "'read-anotated' works" $
+ runPureCode "(read-annotated \"foo\" \"(:hello 42)\")" @?= Right (List [Keyword [ident|hello|], int 42])
, testCase "'to-json' works" $ do
runPureCode "(to-json (dict \"foo\" #t))" @?= Right (String "{\"foo\":true}")
@@ -649,8 +649,8 @@ test_env =
test_repl_primops :: [TestTree]
test_repl_primops =
- [ testProperty "(read (get-line!)) returns the input line" $ \(v :: Value) ->
- let prog = [i|(eq? (read (get-line!)) (quote #{renderPrettyDef v}))|]
+ [ testProperty "(read-annotated \"get-line!\" (get-line!)) returns the input line" $ \(v :: Value) ->
+ let prog = [i|(eq? (read-annotated \"get-line!\" (get-line!)) (quote #{renderPrettyDef v}))|]
-- We're not actually using IO
res = unsafePerformIO $ run [renderPrettyDef v] $ toS prog
in counterexample prog $ res == Right (Boolean True)