From f27378e57a378ee745fbe9729c00351bc1c667f9 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Mon, 8 Apr 2013 20:18:15 -0400 Subject: [PATCH 1/9] Adding in some prototype bits for quoted forms. --- hy/compiler.py | 16 ++++++++++++++++ hy/models/__init__.py | 2 ++ hy/models/list.py | 1 + 3 files changed, 19 insertions(+) diff --git a/hy/compiler.py b/hy/compiler.py index 76b266a..249a0cc 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -160,6 +160,22 @@ class HyASTCompiler(object): def compile_raw_list(self, entries): return [self.compile(x) for x in entries] + def _render_quoted_form(self, form): + name = form.__class__.__name__ + if isinstance(form, HyList): + return HyExpression( + [HySymbol(name), + HyList([self._render_quoted_form(x) for x in form])] + ).replace(form) + elif isinstance(form, HySymbol): + return HyExpression([HySymbol(name), HyString(form)]).replace(form) + return HyExpression([HySymbol(name), form]).replace(form) + + @builds("quote") + @checkargs(min=1, max=2) + def compile_quote(self, entries): + return self.compile(self._render_quoted_form(entries[1])) + @builds("do") @builds("progn") def compile_do_expression(self, expr): diff --git a/hy/models/__init__.py b/hy/models/__init__.py index 408c917..775862a 100644 --- a/hy/models/__init__.py +++ b/hy/models/__init__.py @@ -34,3 +34,5 @@ class HyObject(object): setattr(self, attr, getattr(other, attr)) else: raise TypeError("Can't replace a non Hy object with a Hy object") + + return self diff --git a/hy/models/list.py b/hy/models/list.py index f92bff9..183ec42 100644 --- a/hy/models/list.py +++ b/hy/models/list.py @@ -31,6 +31,7 @@ class HyList(HyObject, list): x.replace(other) HyObject.replace(self, other) + return self def __repr__(self): return "[%s]" % (" ".join([str(x) for x in self])) From 93e4bc17ebba15a00cfc0dd2dabf2babb3e45577 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Mon, 8 Apr 2013 20:23:51 -0400 Subject: [PATCH 2/9] Adding some basic testing in; more to come. --- tests/compilers/native/__init__.hy | 0 tests/compilers/native/quoting.hy | 9 +++++++++ tests/compilers/test_quoting.py | 1 + 3 files changed, 10 insertions(+) create mode 100644 tests/compilers/native/__init__.hy create mode 100644 tests/compilers/native/quoting.hy create mode 100644 tests/compilers/test_quoting.py diff --git a/tests/compilers/native/__init__.hy b/tests/compilers/native/__init__.hy new file mode 100644 index 0000000..e69de29 diff --git a/tests/compilers/native/quoting.hy b/tests/compilers/native/quoting.hy new file mode 100644 index 0000000..3c20bd2 --- /dev/null +++ b/tests/compilers/native/quoting.hy @@ -0,0 +1,9 @@ +;;; +;;; + +(import-from hy.models.expression HyExpression) +(import-from hy.models.symbol HySymbol) + + +(defn test-basic-quoting [] + (assert (= (type (quote (foo bar))) HyExpression))) diff --git a/tests/compilers/test_quoting.py b/tests/compilers/test_quoting.py new file mode 100644 index 0000000..fb83d8d --- /dev/null +++ b/tests/compilers/test_quoting.py @@ -0,0 +1 @@ +from .native.quoting import * # NOQA From eeab56c2b75a5b19527c157877b648dee8719fbc Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Mon, 8 Apr 2013 20:24:47 -0400 Subject: [PATCH 3/9] Adding a symbol checker. --- tests/compilers/native/quoting.hy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/compilers/native/quoting.hy b/tests/compilers/native/quoting.hy index 3c20bd2..63c8f96 100644 --- a/tests/compilers/native/quoting.hy +++ b/tests/compilers/native/quoting.hy @@ -6,4 +6,5 @@ (defn test-basic-quoting [] - (assert (= (type (quote (foo bar))) HyExpression))) + (assert (= (type (quote (foo bar))) HyExpression)) + (assert (= (type (quote foo)) HySymbol))) From 997b32e18cb2f4d453c467da5698af647c4b187a Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Mon, 8 Apr 2013 22:31:26 -0400 Subject: [PATCH 4/9] Testing the behavior of (quote "foo") --- hy/__init__.py | 7 +++++++ tests/compilers/native/quoting.hy | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/hy/__init__.py b/hy/__init__.py index f25dd2f..56ffb1d 100644 --- a/hy/__init__.py +++ b/hy/__init__.py @@ -25,3 +25,10 @@ __version__ = "0.9.5" import hy.importer # NOQA # we import for side-effects. + +from hy.models.expression import HyExpression # NOQA +from hy.models.integer import HyInteger # NOQA +from hy.models.string import HyString # NOQA +from hy.models.symbol import HySymbol # NOQA +from hy.models.dict import HyDict # NOQA +from hy.models.list import HyList # NOQA diff --git a/tests/compilers/native/quoting.hy b/tests/compilers/native/quoting.hy index 63c8f96..6a1b522 100644 --- a/tests/compilers/native/quoting.hy +++ b/tests/compilers/native/quoting.hy @@ -1,10 +1,10 @@ ;;; ;;; -(import-from hy.models.expression HyExpression) -(import-from hy.models.symbol HySymbol) +(import-from hy HyExpression HySymbol HyString) (defn test-basic-quoting [] (assert (= (type (quote (foo bar))) HyExpression)) - (assert (= (type (quote foo)) HySymbol))) + (assert (= (type (quote foo)) HySymbol)) + (assert (= (type (quote "string")) HyString))) From 8212ed57941efe6f4c063fcd32789a38103ae2fb Mon Sep 17 00:00:00 2001 From: Konrad Hinsen Date: Tue, 9 Apr 2013 19:50:49 +0200 Subject: [PATCH 5/9] Do not quit hy if hy.core.process raises an exception --- bin/hy | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bin/hy b/bin/hy index 4167e68..2e27f56 100755 --- a/bin/hy +++ b/bin/hy @@ -48,7 +48,12 @@ class HyREPL(code.InteractiveConsole): _machine = Machine(Idle, 1, 0) return True - tokens = process(_machine.nodes) + try: + tokens = process(_machine.nodes) + except Exception: + _machine = Machine(Idle, 1, 0) + self.showtraceback() + return False _machine = Machine(Idle, 1, 0) try: From e55e63c759b38a63d4afcae1ab49ba2c1ea750a1 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Tue, 9 Apr 2013 19:55:19 -0400 Subject: [PATCH 6/9] Add @khinsen to AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 4fae5f8..ffdeee9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,3 +7,4 @@ * Julien Danjou * Nicolas Dandrimont * Gergely Nagy +* Konrad Hinsen From b71d5c597e3d0dc151eda30ed602b14fec86b4c2 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Tue, 9 Apr 2013 20:34:46 -0400 Subject: [PATCH 7/9] Fixing a bug @jd noticed. --- hy/compiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hy/compiler.py b/hy/compiler.py index c4952cf..9a5be4f 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -189,7 +189,7 @@ class HyASTCompiler(object): return HyExpression([HySymbol(name), form]).replace(form) @builds("quote") - @checkargs(min=1, max=2) + @checkargs(exact=1) def compile_quote(self, entries): return self.compile(self._render_quoted_form(entries[1])) From c0b34181bc3b163ff1790877372cc1b4e7b5e110 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Tue, 9 Apr 2013 20:44:05 -0400 Subject: [PATCH 8/9] futzing with alignment --- hy/compiler.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 9a5be4f..1db4a80 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -83,8 +83,7 @@ def builds(_type): def _raise_wrong_args_number(expression, error): - err = TypeError(error % (expression.pop(0), - len(expression))) + err = TypeError(error % (expression.pop(0), len(expression))) err.start_line = expression.start_line err.start_column = expression.start_column raise err @@ -94,18 +93,15 @@ def checkargs(exact=None, min=None, max=None): def _dec(fn): def checker(self, expression): if exact is not None and (len(expression) - 1) != exact: - _raise_wrong_args_number(expression, - "`%%s' needs %d arguments, got %%d" % - exact) + _raise_wrong_args_number( + expression, "`%%s' needs %d arguments, got %%d" % exact) if min is not None and (len(expression) - 1) < min: - _raise_wrong_args_number( - expression, + _raise_wrong_args_number(expression, "`%%s' needs at least %d arguments, got %%d" % (min)) if max is not None and (len(expression) - 1) > max: - _raise_wrong_args_number( - expression, + _raise_wrong_args_number(expression, "`%%s' needs at most %d arguments, got %%d" % (max)) return fn(self, expression) @@ -140,8 +136,7 @@ class HyASTCompiler(object): def _mangle_branch(self, tree, start_line, start_column): # If tree is empty, just return a pass statement if tree == []: - return [ast.Pass(lineno=start_line, - col_offset=start_column)] + return [ast.Pass(lineno=start_line, col_offset=start_column)] ret = [] tree = list(flatten_literal_list(tree)) @@ -157,7 +152,7 @@ class HyASTCompiler(object): if isinstance(el, ast.FunctionDef): ret.append(ast.Return( value=ast.Name( - arg=el.name, id=el.name, ctx=ast.Load(), + arg=el.name,id=el.name, ctx=ast.Load(), lineno=el.lineno, col_offset=el.col_offset), lineno=el.lineno, col_offset=el.col_offset)) @@ -166,9 +161,10 @@ class HyASTCompiler(object): ret.append(el) continue - ret.append(ast.Expr(value=el, - lineno=el.lineno, - col_offset=el.col_offset)) + ret.append(ast.Expr( + value=el, + lineno=el.lineno, + col_offset=el.col_offset)) ret.reverse() return ret From 3f5ce6440759bc76a7b2396973ac4b733cab55bb Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Tue, 9 Apr 2013 20:44:52 -0400 Subject: [PATCH 9/9] I'm an idiot --- hy/compiler.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 1db4a80..0157142 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -97,11 +97,13 @@ def checkargs(exact=None, min=None, max=None): expression, "`%%s' needs %d arguments, got %%d" % exact) if min is not None and (len(expression) - 1) < min: - _raise_wrong_args_number(expression, + _raise_wrong_args_number( + expression, "`%%s' needs at least %d arguments, got %%d" % (min)) if max is not None and (len(expression) - 1) > max: - _raise_wrong_args_number(expression, + _raise_wrong_args_number( + expression, "`%%s' needs at most %d arguments, got %%d" % (max)) return fn(self, expression) @@ -152,7 +154,7 @@ class HyASTCompiler(object): if isinstance(el, ast.FunctionDef): ret.append(ast.Return( value=ast.Name( - arg=el.name,id=el.name, ctx=ast.Load(), + arg=el.name, id=el.name, ctx=ast.Load(), lineno=el.lineno, col_offset=el.col_offset), lineno=el.lineno, col_offset=el.col_offset))