Merge pull request #863 from farhaven/read-str

Add read-str
This commit is contained in:
Morten Linderud 2015-07-31 23:58:16 +02:00
commit b38ec6b92d
3 changed files with 30 additions and 2 deletions

View File

@ -1065,6 +1065,21 @@ if *from-file* ends before a complete expression can be parsed.
hyfriends! hyfriends!
EOF! EOF!
read-str
--------
Usage: ``(read-str "string")``
This is essentially a wrapper around `read` which reads expressions from a
string:
.. code-block:: hy
=> (read-str "(print 1)")
(u'print' 1L)
=> (eval (read-str "(print 1)"))
1
=>
.. _remove-fn: .. _remove-fn:

View File

@ -28,6 +28,9 @@
(import collections) (import collections)
(import [fractions [Fraction :as fraction]]) (import [fractions [Fraction :as fraction]])
(import sys) (import sys)
(if-python2
(import [StringIO [StringIO]])
(import [io [StringIO]]))
(import [hy._compat [long-type]]) ; long for python2, int for python3 (import [hy._compat [long-type]]) ; long for python2, int for python3
(import [hy.models.cons [HyCons]] (import [hy.models.cons [HyCons]]
[hy.models.symbol [HySymbol]] [hy.models.symbol [HySymbol]]
@ -382,6 +385,12 @@
(else (if parsed (break))))) (else (if parsed (break)))))
parsed) parsed)
(defn read-str [input]
"Reads and tokenizes first line of input"
(read :from-file (StringIO input)))
(defun Botsbuildbots () (Botsbuildbots)) (defun Botsbuildbots () (Botsbuildbots))
(defn zipwith [func &rest lists] (defn zipwith [func &rest lists]
@ -423,6 +432,6 @@
identity inc input instance? integer integer? integer-char? identity inc input instance? integer integer? integer-char?
interleave interpose iterable? iterate iterator? keyword interleave interpose iterable? iterate iterator? keyword
keyword? last list* macroexpand macroexpand-1 map merge-with keyword? last list* macroexpand macroexpand-1 map merge-with
name neg? nil? none? nth numeric? odd? pos? range read remove name neg? nil? none? nth numeric? odd? pos? range read read-str
repeat repeatedly rest reduce second some string string? remove repeat repeatedly rest reduce second some string string?
symbol? take take-nth take-while zero? zip zip_longest zipwith]) symbol? take take-nth take-while zero? zip zip_longest zipwith])

View File

@ -1186,6 +1186,10 @@
(catch [e Exception] (catch [e Exception]
(assert (isinstance e EOFError))))) (assert (isinstance e EOFError)))))
(defn test-read-str []
"NATIVE: test read-str"
(assert (= (read-str "(print 1)") '(print 1))))
(defn test-keyword-creation [] (defn test-keyword-creation []
"NATIVE: Test keyword creation" "NATIVE: Test keyword creation"
(assert (= (keyword "foo") :foo)) (assert (= (keyword "foo") :foo))