Revert "Make HySymbol bytes free!"

This reverts commit 8b144a4f3d.
This commit is contained in:
Paul R. Tagliamonte 2013-04-06 20:02:08 -04:00
parent 262da59c77
commit e0ed7cac40
9 changed files with 64 additions and 82 deletions

View File

@ -137,13 +137,13 @@ class HyASTCompiler(object):
def compile_raw_list(self, entries): def compile_raw_list(self, entries):
return [self.compile(x) for x in entries] return [self.compile(x) for x in entries]
@builds(HySymbol("do")) @builds("do")
@builds(HySymbol("progn")) @builds("progn")
@checkargs(min=1) @checkargs(min=1)
def compile_do_expression(self, expr): def compile_do_expression(self, expr):
return [self.compile(x) for x in expr[1:]] return [self.compile(x) for x in expr[1:]]
@builds(HySymbol("throw")) @builds("throw")
@checkargs(min=1) @checkargs(min=1)
def compile_throw_expression(self, expr): def compile_throw_expression(self, expr):
expr.pop(0) expr.pop(0)
@ -156,7 +156,7 @@ class HyASTCompiler(object):
inst=None, inst=None,
tback=None) tback=None)
@builds(HySymbol("try")) @builds("try")
@checkargs(min=1) @checkargs(min=1)
def compile_try_expression(self, expr): def compile_try_expression(self, expr):
expr.pop(0) # try expr.pop(0) # try
@ -175,7 +175,7 @@ class HyASTCompiler(object):
finalbody=[], finalbody=[],
orelse=[]) orelse=[])
@builds(HySymbol("catch")) @builds("catch")
@checkargs(min=2) @checkargs(min=2)
def compile_catch_expression(self, expr): def compile_catch_expression(self, expr):
expr.pop(0) # catch expr.pop(0) # catch
@ -205,7 +205,7 @@ class HyASTCompiler(object):
return self._mangle_branch(branch) return self._mangle_branch(branch)
return self._mangle_branch([branch]) return self._mangle_branch([branch])
@builds(HySymbol("if")) @builds("if")
@checkargs(min=2, max=3) @checkargs(min=2, max=3)
def compile_if_expression(self, expr): def compile_if_expression(self, expr):
expr.pop(0) # if expr.pop(0) # if
@ -223,7 +223,7 @@ class HyASTCompiler(object):
lineno=expr.start_line, lineno=expr.start_line,
col_offset=expr.start_column) col_offset=expr.start_column)
@builds(HySymbol("print")) @builds("print")
def compile_print_expression(self, expr): def compile_print_expression(self, expr):
call = expr.pop(0) # print call = expr.pop(0) # print
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
@ -243,7 +243,7 @@ class HyASTCompiler(object):
values=[self.compile(x) for x in expr], values=[self.compile(x) for x in expr],
nl=True) nl=True)
@builds(HySymbol("assert")) @builds("assert")
@checkargs(1) @checkargs(1)
def compile_assert_expression(self, expr): def compile_assert_expression(self, expr):
expr.pop(0) # assert expr.pop(0) # assert
@ -253,7 +253,7 @@ class HyASTCompiler(object):
lineno=e.start_line, lineno=e.start_line,
col_offset=e.start_column) col_offset=e.start_column)
@builds(HySymbol("lambda")) @builds("lambda")
@checkargs(min=2) @checkargs(min=2)
def compile_lambda_expression(self, expr): def compile_lambda_expression(self, expr):
expr.pop(0) expr.pop(0)
@ -276,12 +276,12 @@ class HyASTCompiler(object):
kw_defaults=[]), kw_defaults=[]),
body=self.compile(body)) body=self.compile(body))
@builds(HySymbol("pass")) @builds("pass")
@checkargs(0) @checkargs(0)
def compile_pass_expression(self, expr): def compile_pass_expression(self, expr):
return ast.Pass(lineno=expr.start_line, col_offset=expr.start_column) return ast.Pass(lineno=expr.start_line, col_offset=expr.start_column)
@builds(HySymbol("yield")) @builds("yield")
@checkargs(1) @checkargs(1)
def compile_yield_expression(self, expr): def compile_yield_expression(self, expr):
expr.pop(0) expr.pop(0)
@ -290,7 +290,7 @@ class HyASTCompiler(object):
lineno=expr.start_line, lineno=expr.start_line,
col_offset=expr.start_column) col_offset=expr.start_column)
@builds(HySymbol("import")) @builds("import")
def compile_import_expression(self, expr): def compile_import_expression(self, expr):
expr.pop(0) # index expr.pop(0) # index
return ast.Import( return ast.Import(
@ -298,7 +298,7 @@ class HyASTCompiler(object):
col_offset=expr.start_column, col_offset=expr.start_column,
names=[ast.alias(name=str(x), asname=None) for x in expr]) names=[ast.alias(name=str(x), asname=None) for x in expr])
@builds(HySymbol("import-as")) @builds("import_as")
def compile_import_as_expression(self, expr): def compile_import_as_expression(self, expr):
expr.pop(0) # index expr.pop(0) # index
modlist = [expr[i:i + 2] for i in range(0, len(expr), 2)] modlist = [expr[i:i + 2] for i in range(0, len(expr), 2)]
@ -309,7 +309,7 @@ class HyASTCompiler(object):
names=[ast.alias(name=str(x[0]), names=[ast.alias(name=str(x[0]),
asname=str(x[1])) for x in modlist]) asname=str(x[1])) for x in modlist])
@builds(HySymbol("import-from")) @builds("import_from")
@checkargs(min=1) @checkargs(min=1)
def compile_import_from_expression(self, expr): def compile_import_from_expression(self, expr):
expr.pop(0) # index expr.pop(0) # index
@ -320,7 +320,7 @@ class HyASTCompiler(object):
names=[ast.alias(name=str(x), asname=None) for x in expr], names=[ast.alias(name=str(x), asname=None) for x in expr],
level=0) level=0)
@builds(HySymbol("get")) @builds("get")
@checkargs(2) @checkargs(2)
def compile_index_expression(self, expr): def compile_index_expression(self, expr):
expr.pop(0) # index expr.pop(0) # index
@ -334,7 +334,7 @@ class HyASTCompiler(object):
slice=ast.Index(value=sli), slice=ast.Index(value=sli),
ctx=ast.Load()) ctx=ast.Load())
@builds(HySymbol("slice")) @builds("slice")
@checkargs(min=1, max=3) @checkargs(min=1, max=3)
def compile_slice_expression(self, expr): def compile_slice_expression(self, expr):
expr.pop(0) # index expr.pop(0) # index
@ -357,7 +357,7 @@ class HyASTCompiler(object):
step=None), step=None),
ctx=ast.Load()) ctx=ast.Load())
@builds(HySymbol("assoc")) @builds("assoc")
@checkargs(3) @checkargs(3)
def compile_assoc_expression(self, expr): def compile_assoc_expression(self, expr):
expr.pop(0) # assoc expr.pop(0) # assoc
@ -378,7 +378,7 @@ class HyASTCompiler(object):
ctx=ast.Store())], ctx=ast.Store())],
value=self.compile(val)) value=self.compile(val))
@builds(HySymbol("decorate-with")) @builds("decorate_with")
@checkargs(min=1) @checkargs(min=1)
def compile_decorate_expression(self, expr): def compile_decorate_expression(self, expr):
expr.pop(0) # decorate-with expr.pop(0) # decorate-with
@ -388,7 +388,7 @@ class HyASTCompiler(object):
fn.decorator_list = [self.compile(x) for x in expr] fn.decorator_list = [self.compile(x) for x in expr]
return fn return fn
@builds(HySymbol("with-as")) @builds("with_as")
@checkargs(min=2) @checkargs(min=2)
def compile_with_as_expression(self, expr): def compile_with_as_expression(self, expr):
expr.pop(0) # with-as expr.pop(0) # with-as
@ -407,7 +407,7 @@ class HyASTCompiler(object):
return ret return ret
@builds(HySymbol(",")) @builds(",")
def compile_tuple(self, expr): def compile_tuple(self, expr):
expr.pop(0) expr.pop(0)
return ast.Tuple(elts=[self.compile(x) for x in expr], return ast.Tuple(elts=[self.compile(x) for x in expr],
@ -415,7 +415,7 @@ class HyASTCompiler(object):
col_offset=expr.start_column, col_offset=expr.start_column,
ctx=ast.Load()) ctx=ast.Load())
@builds(HySymbol("list-comp")) @builds("list_comp")
@checkargs(min=2, max=3) @checkargs(min=2, max=3)
def compile_list_comprehension(self, expr): def compile_list_comprehension(self, expr):
# (list-comp expr (target iter) cond?) # (list-comp expr (target iter) cond?)
@ -450,7 +450,7 @@ class HyASTCompiler(object):
name.ctx = ast.Store() name.ctx = ast.Store()
return name return name
@builds(HySymbol("kwapply")) @builds("kwapply")
@checkargs(2) @checkargs(2)
def compile_kwapply_expression(self, expr): def compile_kwapply_expression(self, expr):
expr.pop(0) # kwapply expr.pop(0) # kwapply
@ -465,8 +465,8 @@ class HyASTCompiler(object):
return call return call
@builds(HySymbol("not")) @builds("not")
@builds(HySymbol("~")) @builds("~")
@checkargs(1) @checkargs(1)
def compile_unary_operator(self, expression): def compile_unary_operator(self, expression):
ops = {"not": ast.Not, ops = {"not": ast.Not,
@ -478,23 +478,23 @@ class HyASTCompiler(object):
lineno=operator.start_line, lineno=operator.start_line,
col_offset=operator.start_column) col_offset=operator.start_column)
@builds(HySymbol("=")) @builds("=")
@builds(HySymbol("!=")) @builds("!=")
@builds(HySymbol("<")) @builds("<")
@builds(HySymbol("<=")) @builds("<=")
@builds(HySymbol(">")) @builds(">")
@builds(HySymbol(">=")) @builds(">=")
@builds(HySymbol("is")) @builds("is")
@builds(HySymbol("in")) @builds("in")
@builds(HySymbol("is-not")) @builds("is_not")
@builds(HySymbol("not-in")) @builds("not_in")
@checkargs(min=2) @checkargs(min=2)
def compile_compare_op_expression(self, expression): def compile_compare_op_expression(self, expression):
ops = {"=": ast.Eq, "!=": ast.NotEq, ops = {"=": ast.Eq, "!=": ast.NotEq,
"<": ast.Lt, "<=": ast.LtE, "<": ast.Lt, "<=": ast.LtE,
">": ast.Gt, ">=": ast.GtE, ">": ast.Gt, ">=": ast.GtE,
"is": ast.Is, "is-not": ast.IsNot, "is": ast.Is, "is_not": ast.IsNot,
"in": ast.In, "not-in": ast.NotIn} "in": ast.In, "not_in": ast.NotIn}
inv = expression.pop(0) inv = expression.pop(0)
op = ops[inv] op = ops[inv]
@ -507,11 +507,11 @@ class HyASTCompiler(object):
lineno=e.start_line, lineno=e.start_line,
col_offset=e.start_column) col_offset=e.start_column)
@builds(HySymbol("+")) @builds("+")
@builds(HySymbol("%")) @builds("%")
@builds(HySymbol("-")) @builds("-")
@builds(HySymbol("/")) @builds("/")
@builds(HySymbol("*")) @builds("*")
@checkargs(min=2) @checkargs(min=2)
def compile_maths_expression(self, expression): def compile_maths_expression(self, expression):
# operator = Mod | Pow | LShift | RShift | BitOr | # operator = Mod | Pow | LShift | RShift | BitOr |
@ -578,9 +578,9 @@ class HyASTCompiler(object):
lineno=expression.start_line, lineno=expression.start_line,
col_offset=expression.start_column) col_offset=expression.start_column)
@builds(HySymbol("def")) @builds("def")
@builds(HySymbol("setf")) @builds("setf")
@builds(HySymbol("setv")) @builds("setv")
@checkargs(2) @checkargs(2)
def compile_def_expression(self, expression): def compile_def_expression(self, expression):
expression.pop(0) # "def" expression.pop(0) # "def"
@ -602,7 +602,7 @@ class HyASTCompiler(object):
col_offset=expression.start_column, col_offset=expression.start_column,
targets=[name], value=what) targets=[name], value=what)
@builds(HySymbol("foreach")) @builds("foreach")
@checkargs(min=1) @checkargs(min=1)
def compile_for_expression(self, expression): def compile_for_expression(self, expression):
ret_status = self.returnable ret_status = self.returnable
@ -626,7 +626,7 @@ class HyASTCompiler(object):
self.returnable = ret_status self.returnable = ret_status
return ret return ret
@builds(HySymbol("while")) @builds("while")
@checkargs(min=2) @checkargs(min=2)
def compile_while_expression(self, expr): def compile_while_expression(self, expr):
expr.pop(0) # "while" expr.pop(0) # "while"
@ -647,7 +647,7 @@ class HyASTCompiler(object):
lineno=expr.start_line, lineno=expr.start_line,
col_offset=expr.start_column) col_offset=expr.start_column)
@builds(HySymbol("fn")) @builds("fn")
@checkargs(min=2) @checkargs(min=2)
def compile_fn_expression(self, expression): def compile_fn_expression(self, expression):
expression.pop(0) # fn expression.pop(0) # fn

View File

@ -40,7 +40,7 @@ def router(tree, rkwargs=None):
route = HyExpression([HySymbol("kwapply"), route, route = HyExpression([HySymbol("kwapply"), route,
HyDict({HyString("methods"): rkwargs})]) HyDict({HyString("methods"): rkwargs})])
return HyExpression([HySymbol("decorate-with"), route, tree]) return HyExpression([HySymbol("decorate_with"), route, tree])
@macro("route") @macro("route")

View File

@ -80,7 +80,7 @@ def for_macro(tree):
return root return root
@macro("->") @macro("_>")
def threading_macro(tree): def threading_macro(tree):
tree.pop(0) tree.pop(0)
ret = tree.pop(0) ret = tree.pop(0)

View File

@ -41,7 +41,7 @@ class HoistableMangle(hy.mangle.Mangle):
class FunctionMangle(HoistableMangle): class FunctionMangle(HoistableMangle):
hoistable = ["fn"] hoistable = ["fn"]
ignore = ["def", "decorate-with", "setf", "setv"] ignore = ["def", "decorate_with", "setf", "setv"]
def __init__(self): def __init__(self):
self.series = 0 self.series = 0

View File

@ -24,7 +24,6 @@ from hy.core import process
from py_compile import wr_long, MAGIC from py_compile import wr_long, MAGIC
import io
import marshal import marshal
import imp import imp
import sys import sys
@ -44,7 +43,7 @@ def import_buffer_to_hst(fd):
def import_file_to_hst(fpath): def import_file_to_hst(fpath):
return import_buffer_to_hst(io.open(fpath, 'rU', encoding='utf-8')) return import_buffer_to_hst(open(fpath, 'r'))
def import_file_to_ast(fpath): def import_file_to_ast(fpath):

View File

@ -58,7 +58,16 @@ def _resolve_atom(obj):
"null": "None", "null": "None",
} }
return HySymbol(table.get(obj, obj)) if obj in table:
return HySymbol(table[obj])
if obj.startswith("*") and obj.endswith("*") and obj != "*":
obj = obj[1:-1].upper()
if "-" in obj and obj != "-":
obj = obj.replace("-", "_")
return HySymbol(obj)
class State(object): class State(object):

View File

@ -22,14 +22,13 @@ from hy.models.expression import HyExpression
from hy.models.string import HyString from hy.models.string import HyString
from hy.models.dict import HyDict from hy.models.dict import HyDict
from hy.models.list import HyList from hy.models.list import HyList
from hy.models.symbol import HySymbol
_hy_macros = {} _hy_macros = {}
def macro(name): def macro(name):
def _(fn): def _(fn):
_hy_macros[HySymbol(name)] = fn _hy_macros[name] = fn
return fn return fn
return _ return _

View File

@ -1,5 +1,4 @@
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org> # Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
# #
# Permission is hereby granted, free of charge, to any person obtaining a # Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"), # copy of this software and associated documentation files (the "Software"),
@ -19,8 +18,6 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE. # DEALINGS IN THE SOFTWARE.
import base64
from hy.models.string import HyString from hy.models.string import HyString
@ -28,13 +25,6 @@ class HySymbol(HyString):
""" """
Hy Symbol. Basically a String. Hy Symbol. Basically a String.
""" """
def encode_symbol(self):
return ("_hy_symbol_"
+ base64.b64encode(
self.encode(
'unicode-escape')).decode('ascii'))
def __str__(self): def __init__(self, string):
if all([(c.isalnum() or c == "_" or c == ".") for c in self]): self += string
return super(HyString, self).__str__()
return self.encode_symbol()

View File

@ -160,21 +160,6 @@
(assert (= (.join " " ["one" "two"]) "one two"))) (assert (= (.join " " ["one" "two"]) "one two")))
(defn test-symbol-utf-8 []
"NATIVE: test symbol encoded"
(let [[ "love"]
[ "flower"]]
(assert (= (+ ) "flowerlove"))))
(defn test-symbol-dash []
"NATIVE: test symbol encoded"
(let [[- "doublelove"]
[-_- "what?"]]
(assert (= - "doublelove"))
(assert (= -_- "what?"))))
(defn test-exceptions [] (defn test-exceptions []
"NATIVE: test Exceptions" "NATIVE: test Exceptions"
(try (try