Mangle special forms

This commit is contained in:
Kodi Arfer 2017-11-15 17:21:01 -08:00
parent ccb3ba6092
commit 0c8c5dc830
3 changed files with 25 additions and 10 deletions

View File

@ -83,6 +83,8 @@ def builds(*types, **kwargs):
def _dec(fn):
for t in types:
if isinstance(t, string_types):
t = ast_str(t)
_compile_table[t] = fn
return fn
return _dec
@ -415,6 +417,8 @@ class HyASTCompiler(object):
return ret.stmts
def compile_atom(self, atom_type, atom):
if isinstance(atom_type, string_types):
atom_type = ast_str(atom_type)
if atom_type in _compile_table:
# _compile_table[atom_type] is a method for compiling this
# type of atom, so call it. If it has an extra parameter,
@ -1530,15 +1534,16 @@ class HyASTCompiler(object):
values=[value.force_expr for value in values])
return ret
def _compile_compare_op_expression(self, expression):
ops = {"=": ast.Eq, "!=": ast.NotEq,
"<": ast.Lt, "<=": ast.LtE,
">": ast.Gt, ">=": ast.GtE,
"is": ast.Is, "is-not": ast.IsNot,
"in": ast.In, "not-in": ast.NotIn}
ops = {"=": ast.Eq, "!=": ast.NotEq,
"<": ast.Lt, "<=": ast.LtE,
">": ast.Gt, ">=": ast.GtE,
"is": ast.Is, "is-not": ast.IsNot,
"in": ast.In, "not-in": ast.NotIn}
ops = {ast_str(k): v for k, v in ops.items()}
inv = expression.pop(0)
ops = [ops[inv]() for _ in range(len(expression) - 1)]
def _compile_compare_op_expression(self, expression):
inv = ast_str(expression.pop(0))
ops = [self.ops[inv]() for _ in range(len(expression) - 1)]
e = expression[0]
exprs, ret, _ = self._compile_collect(expression)
@ -2115,7 +2120,7 @@ class HyASTCompiler(object):
compile_time_ns(self.module_name),
self.module_name)
return (self._compile_branch(expression[1:])
if building == "eval-and-compile"
if building == "eval_and_compile"
else Result())
@builds(HyCons)

View File

@ -7,6 +7,7 @@
[functools [partial]]
[collections [OrderedDict]]
[hy.macros [macroexpand :as mexpand]]
[hy.lex.parser [hy-symbol-mangle]]
[hy.compiler [HyASTCompiler]])
(defn walk [inner outer form]
@ -257,7 +258,7 @@ Arguments without a header are under None.
(= head 'defclass) (self.handle-defclass)
(= head 'quasiquote) (self.+quote)
;; must be checked last!
(in head special-forms) (self.handle-special-form)
(in (hy-symbol-mangle (string head)) special-forms) (self.handle-special-form)
;; Not a special form. Traverse it like a coll
(self.handle-coll)))

View File

@ -124,6 +124,15 @@
(assert (= x "aabb")))
(defn test-special-form []
(setv not-in 1)
; We set the variable to make sure that if this test works, it's
; because we're calling the special form instead of the shadow
; function.
(assert (is (not-in 2 [1 2 3]) False))
(assert (is (not_in 2 [1 2 3]) False)))
(defn test-python-keyword []
(setv if 3)
(assert (= if 3))