Merge branch 'f/hy.models.keyword' of git://github.com/algernon/hy into pr-101
This commit is contained in:
commit
c15d8750e8
@ -29,6 +29,7 @@ from hy.models.string import HyString
|
|||||||
from hy.models.symbol import HySymbol
|
from hy.models.symbol import HySymbol
|
||||||
from hy.models.list import HyList
|
from hy.models.list import HyList
|
||||||
from hy.models.dict import HyDict
|
from hy.models.dict import HyDict
|
||||||
|
from hy.models.keyword import HyKeyword
|
||||||
|
|
||||||
from hy.util import flatten_literal_list
|
from hy.util import flatten_literal_list
|
||||||
|
|
||||||
@ -768,6 +769,11 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
if expression[0].startswith("."):
|
if expression[0].startswith("."):
|
||||||
return self.compile_dotted_expression(expression)
|
return self.compile_dotted_expression(expression)
|
||||||
|
if isinstance(fn, HyKeyword):
|
||||||
|
new_expr = HyExpression(["get", expression[1], fn])
|
||||||
|
new_expr.start_line = expression.start_line
|
||||||
|
new_expr.start_column = expression.start_column
|
||||||
|
return self.compile_index_expression(new_expr)
|
||||||
|
|
||||||
return ast.Call(func=self.compile(fn),
|
return ast.Call(func=self.compile(fn),
|
||||||
args=[self.compile(x) for x in expression[1:]],
|
args=[self.compile(x) for x in expression[1:]],
|
||||||
@ -927,6 +933,14 @@ class HyASTCompiler(object):
|
|||||||
return ast.Str(s=ast_str(string), lineno=string.start_line,
|
return ast.Str(s=ast_str(string), lineno=string.start_line,
|
||||||
col_offset=string.start_column)
|
col_offset=string.start_column)
|
||||||
|
|
||||||
|
@builds(HyKeyword)
|
||||||
|
def compile_keyword(self, keyword):
|
||||||
|
_str_type = str
|
||||||
|
if sys.version_info[0] < 3:
|
||||||
|
_str_type = unicode
|
||||||
|
return ast.Str(s=_str_type(keyword), lineno=keyword.start_line,
|
||||||
|
col_offset=keyword.start_column)
|
||||||
|
|
||||||
@builds(HyDict)
|
@builds(HyDict)
|
||||||
def compile_dict(self, m):
|
def compile_dict(self, m):
|
||||||
keys = []
|
keys = []
|
||||||
|
@ -22,6 +22,7 @@ from hy.models.expression import HyExpression
|
|||||||
from hy.models.integer import HyInteger
|
from hy.models.integer import HyInteger
|
||||||
from hy.models.symbol import HySymbol
|
from hy.models.symbol import HySymbol
|
||||||
from hy.models.string import HyString
|
from hy.models.string import HyString
|
||||||
|
from hy.models.keyword import HyKeyword
|
||||||
from hy.models.dict import HyDict
|
from hy.models.dict import HyDict
|
||||||
from hy.models.list import HyList
|
from hy.models.list import HyList
|
||||||
|
|
||||||
@ -61,6 +62,9 @@ def _resolve_atom(obj):
|
|||||||
if obj in table:
|
if obj in table:
|
||||||
return HySymbol(table[obj])
|
return HySymbol(table[obj])
|
||||||
|
|
||||||
|
if obj.startswith(":"):
|
||||||
|
return HyKeyword(obj)
|
||||||
|
|
||||||
if obj.startswith("*") and obj.endswith("*") and obj != "*":
|
if obj.startswith("*") and obj.endswith("*") and obj != "*":
|
||||||
obj = obj[1:-1].upper()
|
obj = obj[1:-1].upper()
|
||||||
|
|
||||||
|
39
hy/models/keyword.py
Normal file
39
hy/models/keyword.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
# Copyright (c) 2013 Gergely Nagy <algernon@madhouse-project.org>
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
# copy of this software and associated documentation files (the "Software"),
|
||||||
|
# to deal in the Software without restriction, including without limitation
|
||||||
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
# and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
# Software is furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
# DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
from hy.models import HyObject
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
if sys.version_info[0] >= 3:
|
||||||
|
_str_type = str
|
||||||
|
else:
|
||||||
|
_str_type = unicode
|
||||||
|
|
||||||
|
|
||||||
|
class HyKeyword(HyObject, _str_type):
|
||||||
|
"""Generic Hy Keyword object. It's either a ``str`` or a ``unicode``,
|
||||||
|
depending on the Python version.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __new__(cls, value):
|
||||||
|
obj = _str_type.__new__(cls, "\uFDD0" + value)
|
||||||
|
return obj
|
@ -510,6 +510,18 @@
|
|||||||
(pass)
|
(pass)
|
||||||
((fn [] 1))))))
|
((fn [] 1))))))
|
||||||
|
|
||||||
|
(defn test-keyword []
|
||||||
|
"NATIVE: test if keywords are recognised"
|
||||||
|
|
||||||
|
(assert (= :foo :foo))
|
||||||
|
(assert (= (get {:foo "bar"} :foo) "bar"))
|
||||||
|
(assert (= (get {:bar "quux"} (get {:foo :bar} :foo)) "quux")))
|
||||||
|
|
||||||
|
(defn test-keyword-clash []
|
||||||
|
"NATIVE: test that keywords do not clash with normal strings"
|
||||||
|
|
||||||
|
(assert (= (get {:foo "bar" ":foo" "quux"} :foo) "bar"))
|
||||||
|
(assert (= (get {:foo "bar" ":foo" "quux"} ":foo") "quux")))
|
||||||
|
|
||||||
(defn test-nested-if []
|
(defn test-nested-if []
|
||||||
"NATIVE: test nested if"
|
"NATIVE: test nested if"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user