Mangle keyword arguments

This commit is contained in:
Kodi Arfer 2017-11-19 08:35:20 -08:00
parent d252bb0e94
commit 0c816f2e83
3 changed files with 27 additions and 9 deletions

View File

@ -503,10 +503,11 @@ class HyASTCompiler(object):
compiled_value = self.compile(value) compiled_value = self.compile(value)
ret += compiled_value ret += compiled_value
# no unicode for py2 in ast names keyword = expr[2:]
keyword = str(expr[2:]) if not keyword:
if "-" in keyword and keyword != "-": raise HyTypeError(expr, "Can't call a function with the "
keyword = keyword.replace("-", "_") "empty keyword")
keyword = ast_str(keyword)
keywords.append(asty.keyword( keywords.append(asty.keyword(
expr, arg=keyword, value=compiled_value.force_expr)) expr, arg=keyword, value=compiled_value.force_expr))

View File

@ -1232,11 +1232,7 @@
(assert (= : :)) (assert (= : :))
(assert (keyword? :)) (assert (keyword? :))
(assert (!= : ":")) (assert (!= : ":"))
(assert (= (name :) "")) (assert (= (name :) "")))
(defn f [&kwargs kwargs]
(list (.items kwargs)))
(assert (= (f : 3) [(, "" 3)])))
(defn test-nested-if [] (defn test-nested-if []

View File

@ -129,6 +129,27 @@
(assert (= hyx_Xplus_signX 3)))) (assert (= hyx_Xplus_signX 3))))
(defn test-keyword-args []
(defn f [a a-b foo? ]
[a a-b foo? ])
(assert (= (f :foo? 3 : 4 :a 1 :a-b 2) [1 2 3 4]))
(if PY3
(assert (= (f :is_foo 3 :hyx_ΔshamrockΔ 4 :a 1 :a_b 2) [1 2 3 4]))
(assert (= (f :is_foo 3 :hyx_XshamrockX 4 :a 1 :a_b 2) [1 2 3 4])))
(defn g [&kwargs x]
x)
(setv sk (.format "hyx_{0}shamrock{0}" (if PY3 "Δ" "X")))
(assert (= (g :foo? 3 : 4 :a 1 :a-b 2)
{"a" 1 "a_b" 2 "is_foo" 3 sk 4}))
(if PY3
(assert (= (g :is_foo 3 :hyx_ΔshamrockΔ 4 :a 1 :a_b 2)
{"a" 1 "a_b" 2 "is_foo" 3 sk 4}))
(assert (= (g :is_foo 3 :hyx_XshamrockX 4 :a 1 :a_b 2)
{"a" 1 "a_b" 2 "is_foo" 3 sk 4}))))
(defn test-late-mangling [] (defn test-late-mangling []
; Mangling should only happen during compilation. ; Mangling should only happen during compilation.
(assert (!= 'foo? 'is_foo)) (assert (!= 'foo? 'is_foo))