Merge branch 'master' into pr/197

This commit is contained in:
Paul Tagliamonte 2013-06-29 11:53:28 -04:00
commit ceea41ac93
9 changed files with 111 additions and 21 deletions

View File

@ -13,3 +13,5 @@
* rogererens <roger.erens@e-s-c.biz> * rogererens <roger.erens@e-s-c.biz>
* Thomas Ballinger <thomasballinger@gmail.com> * Thomas Ballinger <thomasballinger@gmail.com>
* Morten Linderud <mcfoxax@gmail.com> * Morten Linderud <mcfoxax@gmail.com>
* Guillermo Vayá <guivaya@gmail.com>

21
bin/hy
View File

@ -4,26 +4,5 @@ import sys
from hy.cmdline import cmdline_handler from hy.cmdline import cmdline_handler
class HyQuitter(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return "Use (%s) or Ctrl-D (i.e. EOF) to exit" % (self.name)
__str__ = __repr__
def __call__(self, code=None):
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)
__builtins__.quit = HyQuitter('quit')
__builtins__.exit = HyQuitter('exit')
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(cmdline_handler("hy", sys.argv)) sys.exit(cmdline_handler("hy", sys.argv))

22
eg/lxml/parse-tumblr.hy Normal file
View File

@ -0,0 +1,22 @@
;;; Hy tumblr printer.
;;; Copyright (c) Paul R. Tagliamonte, 2013, MIT/Expat license.
(import [urllib2 [urlopen]]
[lxml [etree]]
[sys [argv]])
(defn get-rss-feed-name [tumblr]
(kwapply (.format "http://{tumblr}.tumblr.com/rss") {"tumblr" tumblr}))
(defn get-rss-feed [tumblr]
(.parse etree (urlopen (get-rss-feed-name tumblr))))
(defn print-posts [tumblr]
(for [post (.xpath (get-rss-feed tumblr) "//item/title")]
(print post.text)))
(if (slice argv 2)
(print-posts (get argv 2))
(print-posts "this-plt-life"))

45
eg/twisted/get-page.hy Normal file
View File

@ -0,0 +1,45 @@
;; To run this example, do the following:
;; $ hy get-page.hy http://docs.hylang.org/en/latest/
;;
;; At which point, you should see output like this:
;; 2013-06-24 23:03:57-0700 [-] Log opened.
;; 2013-06-24 23:03:57-0700 [-] Starting factory <HTTPClientFactory: http://docs.hylang.org/en/latest/>
;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Byte count for the content of the HTTP page passed: 11835
;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Preparing to stop reactor ...
;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Stopping factory <HTTPClientFactory: http://docs.hylang.org/en/latest/>
;; 2013-06-24 23:03:57-0700 [-] Main loop terminated.
(import sys)
(import [twisted.web.client [getPage]]
[twisted.internet [reactor]]
[twisted.python [log]])
(defun get-page-size (result)
(print
(+ "Byte count for the content of the HTTP page passed: "
(str (len result)))))
(defun log-error (err)
(log.msg err))
(defun finish (ignore)
(log.msg "Preparing to stop reactor ...")
(reactor.stop))
(defun get-page (url)
(let ((d (getPage url)))
(d.addCallback get-page-size)
(d.addErrback log-error)
(d.addCallback finish)))
(defun caddr (list)
; This is how you'd do this in CL:
;(car (cdr (cdr list))))
; However, I think this is more efficient in hy:
(get list 2))
(if (= __name__ "__main__")
(do
(log.startLogging sys.stdout)
(get-page (caddr sys.argv))
(reactor.run)))

View File

@ -49,6 +49,31 @@ from hy.models.symbol import HySymbol
_machine = Machine(Idle, 1, 0) _machine = Machine(Idle, 1, 0)
try:
import __builtin__ as builtins
except ImportError:
import builtins
class HyQuitter(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return "Use (%s) or Ctrl-D (i.e. EOF) to exit" % (self.name)
__str__ = __repr__
def __call__(self, code=None):
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)
builtins.quit = HyQuitter('quit')
builtins.exit = HyQuitter('exit')
class HyREPL(code.InteractiveConsole): class HyREPL(code.InteractiveConsole):
def runsource(self, source, filename='<input>', symbol='single'): def runsource(self, source, filename='<input>', symbol='single'):

View File

@ -1421,6 +1421,8 @@ class HyASTCompiler(object):
@builds(HyExpression) @builds(HyExpression)
def compile_expression(self, expression): def compile_expression(self, expression):
if expression == []:
return self.compile_list(expression)
fn = expression[0] fn = expression[0]
func = None func = None
if isinstance(fn, HyKeyword): if isinstance(fn, HyKeyword):

View File

@ -70,6 +70,9 @@ _wrappers = {
def process(tree, module_name): def process(tree, module_name):
if isinstance(tree, HyExpression): if isinstance(tree, HyExpression):
if tree == []:
return tree
fn = tree[0] fn = tree[0]
if fn in ("quote", "quasiquote"): if fn in ("quote", "quasiquote"):
return tree return tree

View File

@ -755,3 +755,8 @@
(continue)) (continue))
(.append y x)) (.append y x))
(assert (= y [5]))) (assert (= y [5])))
(defn test-empty-list []
"Evaluate an empty list to a []"
(assert (= () [])))

View File

@ -91,3 +91,10 @@ def test_hy2py():
assert len(ret[1]) > 1, f assert len(ret[1]) > 1, f
assert len(ret[2]) == 0, f assert len(ret[2]) == 0, f
assert i assert i
def test_bin_hy_builtins():
import hy.cmdline # NOQA
assert str(exit) == "Use (exit) or Ctrl-D (i.e. EOF) to exit"
assert str(quit) == "Use (quit) or Ctrl-D (i.e. EOF) to exit"