Remove support for cons cells

This commit is contained in:
Kodi Arfer 2018-04-08 16:05:23 -07:00
parent 097647bf6f
commit c93a60ede0
6 changed files with 9 additions and 169 deletions

View File

@ -5,7 +5,7 @@ except ImportError:
__version__ = 'unknown' __version__ = 'unknown'
from hy.models import HyExpression, HyInteger, HyKeyword, HyComplex, HyString, HyBytes, HySymbol, HyFloat, HyDict, HyList, HySet, HyCons # NOQA from hy.models import HyExpression, HyInteger, HyKeyword, HyComplex, HyString, HyBytes, HySymbol, HyFloat, HyDict, HyList, HySet # NOQA
import hy.importer # NOQA import hy.importer # NOQA

View File

@ -5,7 +5,7 @@
from hy.models import (HyObject, HyExpression, HyKeyword, HyInteger, HyComplex, from hy.models import (HyObject, HyExpression, HyKeyword, HyInteger, HyComplex,
HyString, HyBytes, HySymbol, HyFloat, HyList, HySet, HyString, HyBytes, HySymbol, HyFloat, HyList, HySet,
HyDict, HyCons, wrap_value) HyDict, wrap_value)
from hy.errors import HyCompileError, HyTypeError from hy.errors import HyCompileError, HyTypeError
from hy.lex.parser import mangle from hy.lex.parser import mangle
@ -89,7 +89,7 @@ def builds(*types, **kwargs):
def spoof_positions(obj): def spoof_positions(obj):
if not isinstance(obj, HyObject) or isinstance(obj, HyCons): if not isinstance(obj, HyObject):
return return
if not hasattr(obj, "start_column"): if not hasattr(obj, "start_column"):
obj.start_column = 0 obj.start_column = 0
@ -719,24 +719,6 @@ class HyASTCompiler(object):
return imports, HyExpression([HySymbol(name), return imports, HyExpression([HySymbol(name),
contents]).replace(form), False contents]).replace(form), False
elif isinstance(form, HyCons):
ret = HyExpression([HySymbol(name)])
nimport, contents, splice = self._render_quoted_form(form.car,
level)
if splice:
raise HyTypeError(form, "Can't splice dotted lists yet")
imports.update(nimport)
ret.append(contents)
nimport, contents, splice = self._render_quoted_form(form.cdr,
level)
if splice:
raise HyTypeError(form, "Can't splice the cdr of a cons")
imports.update(nimport)
ret.append(contents)
return imports, ret.replace(form), False
elif isinstance(form, HySymbol): elif isinstance(form, HySymbol):
return imports, HyExpression([HySymbol(name), return imports, HyExpression([HySymbol(name),
HyString(form)]).replace(form), False HyString(form)]).replace(form), False
@ -2131,10 +2113,6 @@ class HyASTCompiler(object):
if building == "eval_and_compile" if building == "eval_and_compile"
else Result()) else Result())
@builds(HyCons)
def compile_cons(self, cons):
raise HyTypeError(cons, "Can't compile a top-level cons cell")
@builds(HyInteger, HyFloat, HyComplex) @builds(HyInteger, HyFloat, HyComplex)
def compile_numeric_literal(self, x, building): def compile_numeric_literal(self, x, building):
f = {HyInteger: long_type, f = {HyInteger: long_type,

View File

@ -18,9 +18,6 @@
(outer (HyExpression (map inner form)))] (outer (HyExpression (map inner form)))]
[(instance? HyDict form) [(instance? HyDict form)
(HyDict (outer (HyExpression (map inner form))))] (HyDict (outer (HyExpression (map inner form))))]
[(cons? form)
(outer (cons (inner (first form))
(inner (rest form))))]
[(instance? list form) [(instance? list form)
((type form) (outer (HyExpression (map inner form))))] ((type form) (outer (HyExpression (map inner form))))]
[(coll? form) [(coll? form)

View File

@ -18,7 +18,7 @@
(if-python2 (if-python2
(import [collections :as cabc]) (import [collections :as cabc])
(import [collections.abc :as cabc])) (import [collections.abc :as cabc]))
(import [hy.models [HyCons HySymbol HyKeyword]]) (import [hy.models [HySymbol HyKeyword]])
(import [hy.lex [LexException PrematureEndOfInput tokenize]]) (import [hy.lex [LexException PrematureEndOfInput tokenize]])
(import [hy.lex.parser [mangle unmangle]]) (import [hy.lex.parser [mangle unmangle]])
(import [hy.compiler [HyASTCompiler spoof-positions]]) (import [hy.compiler [HyASTCompiler spoof-positions]])
@ -50,14 +50,6 @@
(fn [&rest args &kwargs kwargs] (fn [&rest args &kwargs kwargs]
(not (f #* args #** kwargs)))) (not (f #* args #** kwargs))))
(defn cons [a b]
"Return a fresh cons cell with car = `a` and cdr = `b`."
(HyCons a b))
(defn cons? [c]
"Check whether `c` is a cons cell."
(instance? HyCons c))
(defn constantly [value] (defn constantly [value]
"Create a new function that always returns `value` regardless of its input." "Create a new function that always returns `value` regardless of its input."
(fn [&rest args &kwargs kwargs] (fn [&rest args &kwargs kwargs]
@ -294,7 +286,7 @@ Return series of accumulated sums (or other binary function results)."
(defn juxt [f &rest fs] (defn juxt [f &rest fs]
"Return a function applying each `fs` to args, collecting results in a list." "Return a function applying each `fs` to args, collecting results in a list."
(setv fs (cons f fs)) (setv fs (+ (, f) fs))
(fn [&rest args &kwargs kwargs] (fn [&rest args &kwargs kwargs]
(list-comp (f #* args #** kwargs) [f fs]))) (list-comp (f #* args #** kwargs) [f fs])))
@ -302,12 +294,6 @@ Return series of accumulated sums (or other binary function results)."
"Return last item from `coll`." "Return last item from `coll`."
(get (tuple coll) -1)) (get (tuple coll) -1))
(defn list* [hd &rest tl]
"Return a chain of nested cons cells (dotted list) containing `hd` and `tl`."
(if (not tl)
hd
(cons hd (list* #* tl))))
(defn macroexpand [form] (defn macroexpand [form]
"Return the full macro expansion of `form`." "Return the full macro expansion of `form`."
(import hy.macros) (import hy.macros)
@ -488,11 +474,11 @@ Even objects with the __name__ magic will work."
(setv EXPORTS (setv EXPORTS
'[*map accumulate butlast calling-module-name chain coll? combinations '[*map accumulate butlast calling-module-name chain coll? combinations
comp complement compress cons cons? constantly count cycle dec distinct comp complement compress constantly count cycle dec distinct
disassemble drop drop-last drop-while empty? eval even? every? exec first disassemble drop drop-last drop-while empty? eval even? every? exec first
filter flatten float? fraction gensym group-by identity inc input instance? filter flatten float? fraction gensym group-by identity inc input instance?
integer integer? integer-char? interleave interpose islice iterable? integer integer? integer-char? interleave interpose islice iterable?
iterate iterator? juxt keyword keyword? last list* macroexpand iterate iterator? juxt keyword keyword? last macroexpand
macroexpand-1 mangle map merge-with multicombinations name neg? none? nth macroexpand-1 mangle map merge-with multicombinations name neg? none? nth
numeric? odd? partition permutations pos? product range read read-str numeric? odd? partition permutations pos? product range read read-str
remove repeat repeatedly rest reduce second some string string? symbol? remove repeat repeatedly rest reduce second some string string? symbol?

View File

@ -11,9 +11,8 @@ import string, re, unicodedata
from rply import ParserGenerator from rply import ParserGenerator
from hy._compat import PY3, str_type, isidentifier, UCS4 from hy._compat import PY3, str_type, isidentifier, UCS4
from hy.models import (HyBytes, HyComplex, HyCons, HyDict, HyExpression, from hy.models import (HyBytes, HyComplex, HyDict, HyExpression, HyFloat,
HyFloat, HyInteger, HyKeyword, HyList, HySet, HyString, HyInteger, HyKeyword, HyList, HySet, HyString, HySymbol)
HySymbol)
from .lexer import lexer from .lexer import lexer
from .exceptions import LexException, PrematureEndOfInput from .exceptions import LexException, PrematureEndOfInput
@ -151,28 +150,6 @@ def reject_spurious_dots(*items):
@pg.production("paren : LPAREN list_contents RPAREN") @pg.production("paren : LPAREN list_contents RPAREN")
@set_boundaries @set_boundaries
def paren(p): def paren(p):
cont = p[1]
# Dotted lists are expressions of the form
# (a b c . d)
# that evaluate to nested cons cells of the form
# (a . (b . (c . d)))
if len(cont) >= 3 and isinstance(cont[-2], HySymbol) and cont[-2] == ".":
reject_spurious_dots(cont[:-2], cont[-1:])
if len(cont) == 3:
# Two-item dotted list: return the cons cell directly
return HyCons(cont[0], cont[2])
else:
# Return a nested cons cell
return HyCons(cont[0], paren([p[0], cont[1:], p[2]]))
# Warn preemptively on a malformed dotted list.
# Only check for dots after the first item to allow for a potential
# attribute accessor shorthand
reject_spurious_dots(cont[1:])
return HyExpression(p[1]) return HyExpression(p[1])

View File

@ -334,101 +334,3 @@ class HySet(HyList):
color = staticmethod(colored.red) color = staticmethod(colored.red)
_wrappers[set] = lambda s: HySet(wrap_value(x) for x in s) _wrappers[set] = lambda s: HySet(wrap_value(x) for x in s)
class HyCons(HyObject):
"""
HyCons: a cons object.
Building a HyCons of something and a HyList really builds a HyList
"""
__slots__ = ["car", "cdr"]
def __new__(cls, car, cdr):
if isinstance(cdr, list):
# Keep unquotes in the cdr of conses
if type(cdr) == HyExpression:
if len(cdr) > 0 and type(cdr[0]) == HySymbol:
if cdr[0] in ("unquote", "unquote-splice"):
return super(HyCons, cls).__new__(cls)
return cdr.__class__([wrap_value(car)] + cdr)
elif cdr is None:
return HyExpression([wrap_value(car)])
else:
return super(HyCons, cls).__new__(cls)
def __init__(self, car, cdr):
self.car = wrap_value(car)
self.cdr = wrap_value(cdr)
def __getitem__(self, n):
if n == 0:
return self.car
if n == slice(1, None):
return self.cdr
raise IndexError(
"Can only get the car ([0]) or the cdr ([1:]) of a HyCons")
def __setitem__(self, n, new):
if n == 0:
self.car = new
return
if n == slice(1, None):
self.cdr = new
return
raise IndexError(
"Can only set the car ([0]) or the cdr ([1:]) of a HyCons")
def __iter__(self):
yield self.car
try:
iterator = (i for i in self.cdr)
except TypeError:
if self.cdr is not None:
yield self.cdr
raise TypeError("Iteration on malformed cons")
else:
for i in iterator:
yield i
def replace(self, other):
if self.car is not None:
replace_hy_obj(self.car, other)
if self.cdr is not None:
replace_hy_obj(self.cdr, other)
HyObject.replace(self, other)
def __repr__(self):
if PRETTY:
return str(self)
else:
return "HyCons({}, {})".format(
repr(self.car), repr(self.cdr))
def __str__(self):
with pretty():
c = colored.yellow
lines = ['' + c("<HyCons (")]
while True:
lines.append(" " + repr_indent(self.car))
if not isinstance(self.cdr, HyCons):
break
self = self.cdr
lines.append("{} {}{}".format(
c("."), repr_indent(self.cdr), c(")>")))
return '\n'.join(lines)
def __eq__(self, other):
return (
isinstance(other, self.__class__) and
self.car == other.car and
self.cdr == other.cdr
)