Added read and tests
This commit is contained in:
parent
2628653380
commit
f7675c829e
@ -862,6 +862,32 @@ Return an iterator of `x`, `fn(x)`, `fn(fn(x))`.
|
||||
[5, 25, 625, 390625, 152587890625]
|
||||
|
||||
|
||||
.. _read-fn:
|
||||
|
||||
read
|
||||
----
|
||||
|
||||
Usage: ``(read [stdin eof])``
|
||||
|
||||
Reads the given form and parses it to hy. Takes optional argument
|
||||
for a different stdin object or eof byte. Throws `EOFError` when
|
||||
stdin is empty.
|
||||
|
||||
.. code-block:: hy
|
||||
=> (read)
|
||||
(+ 2 2)
|
||||
('+' 2 2)
|
||||
=> (eval (read))
|
||||
(+ 2 2)
|
||||
4
|
||||
=> (import io)
|
||||
=> (def buffer (io.StringIO "(+ 2 2)\n(- 2 1)"))
|
||||
=> (eval (apply read [] {"stdin" buffer}))
|
||||
4
|
||||
=> (eval (apply read [] {"stdin" buffer}))
|
||||
1
|
||||
|
||||
|
||||
.. _remove-fn:
|
||||
|
||||
remove
|
||||
|
@ -26,8 +26,10 @@
|
||||
(import itertools)
|
||||
(import functools)
|
||||
(import collections)
|
||||
(import sys)
|
||||
(import [hy._compat [long-type]]) ; long for python2, int for python3
|
||||
(import [hy.models.cons [HyCons]])
|
||||
(import [hy.lex [LexException PrematureEndOfInput tokenize]])
|
||||
|
||||
|
||||
(defn _numeric-check [x]
|
||||
@ -326,12 +328,29 @@
|
||||
(_numeric_check n)
|
||||
(= n 0))
|
||||
|
||||
(defn read [&optional [stdin sys.stdin]
|
||||
[eof ""]]
|
||||
"Read from input and returns a tokenized string.
|
||||
Can take a given input buffer to read from"
|
||||
(def buff "")
|
||||
(while true
|
||||
(def inn (str (.read stdin 1)))
|
||||
(if (= inn eof)
|
||||
(throw (EOFError "Reached end of file" )))
|
||||
(setv buff (+ buff inn))
|
||||
(try
|
||||
(def parsed (first (tokenize buff)))
|
||||
(except [e [LexException PrematureEndOfInput IndexError]])
|
||||
(else (if parsed (break)))))
|
||||
parsed)
|
||||
|
||||
|
||||
(def *exports* '[butlast calling-module-name coll? cons cons? cycle
|
||||
dec distinct disassemble drop drop-while empty? even?
|
||||
every? first filter filterfalse flatten float? gensym identity
|
||||
inc input instance? integer integer? integer-char?
|
||||
iterable? iterate iterator? keyword? list*
|
||||
macroexpand macroexpand-1 map neg? nil? none? nth
|
||||
numeric? odd? pos? range remove repeat repeatedly
|
||||
numeric? odd? pos? range read remove repeat repeatedly
|
||||
rest reduce second some string string? take take-nth
|
||||
take-while zero? zip zip_longest zipwith])
|
||||
|
@ -1028,3 +1028,30 @@
|
||||
(foo [&rest spam] 1)
|
||||
(catch [NameError] True)
|
||||
(else (raise AssertionError))))
|
||||
|
||||
(defn test-read []
|
||||
"NATIVE: test that read takes something for stdin and reads"
|
||||
(if-python2
|
||||
(import [StringIO [StringIO]])
|
||||
(import [io [StringIO]]))
|
||||
(import [hy.models.expression [HyExpression]])
|
||||
|
||||
(def stdin-buffer (StringIO "(+ 2 2)\n(- 2 2)"))
|
||||
(assert (= (eval (apply read [] {"stdin" stdin-buffer})) 4))
|
||||
(assert (isinstance (apply read [] {"stdin" stdin-buffer}) HyExpression))
|
||||
|
||||
"Multiline test"
|
||||
(def stdin-buffer (StringIO "(\n+\n41\n1\n)\n(-\n2\n1\n)"))
|
||||
(assert (= (eval (apply read [] {"stdin" stdin-buffer})) 42))
|
||||
(assert (= (eval (apply read [] {"stdin" stdin-buffer})) 1))
|
||||
|
||||
"EOF test"
|
||||
(def stdin-buffer (StringIO "(+ 2 2)"))
|
||||
(apply read [] {"stdin" stdin-buffer})
|
||||
(try
|
||||
(apply read [] {"stdin" stdin-buffer})
|
||||
(catch [e Exception]
|
||||
(print e)
|
||||
(assert (isinstance e EOFError)))))
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user