示例#1
0
def test_lex_strings():
    """ Make sure that strings are valid expressions"""
    objs = tokenize('"foo"')
    assert objs == [HyString("foo")]
    # Make sure backslash-escaped newlines work (see issue #831)
    objs = tokenize(r"""
"a\
bc"
""")
    assert objs == [HyString("abc")]
示例#2
0
def test_preprocessor_expression():
    """ Test inner macro expantion """
    obj = process(tokenize('(test (test "one" "two"))')[0])

    assert type(obj) == HyList
    assert type(obj[0]) == HyList

    assert obj[0] == HyList([HyString("one"), HyString("two")])

    obj = HyList([HyString("one"), HyString("two")])
    obj = tokenize('(shill ["one" "two"])')[0][1]
    assert obj == process(obj)
示例#3
0
def test_preprocessor_expression():
    """ Test that macro expansion doesn't recurse"""
    obj = macroexpand(tokenize('(test (test "one" "two"))')[0], __name__)

    assert type(obj) == HyList
    assert type(obj[0]) == HyExpression

    assert obj[0] == HyExpression([HySymbol("test"),
                                   HyString("one"),
                                   HyString("two")])

    obj = HyList([HyString("one"), HyString("two")])
    obj = tokenize('(shill ["one" "two"])')[0][1]
    assert obj == macroexpand(obj, '')
示例#4
0
def ideas_macro():
    return HyExpression([
        HySymbol('print'),
        HyString("""

    => (import [sh [figlet]])
    => (figlet "Hi, Hy!")
     _   _ _     _   _       _
    | | | (_)   | | | |_   _| |
    | |_| | |   | |_| | | | | |
    |  _  | |_  |  _  | |_| |_|
    |_| |_|_( ) |_| |_|\__, (_)
            |/         |___/


;;; string things
(.join ", " ["what" "the" "heck"])


;;; this one plays with command line bits
(import [sh [cat grep]])
(-> (cat "/usr/share/dict/words") (grep "-E" "bro$"))


;;; filtering a list w/ a lambda
(filter (lambda [x] (= (% x 2) 0)) (range 0 10))


;;; swaggin' functional bits (Python rulez)
(max (map (lambda [x] (len x)) ["hi" "my" "name" "is" "paul"]))

""")
    ])
示例#5
0
    def _render_quoted_form(self, form):
        name = form.__class__.__name__
        self.imports["hy"].append((name, form))

        if isinstance(form, HyList):
            return HyExpression([
                HySymbol(name),
                HyList([self._render_quoted_form(x) for x in form])
            ]).replace(form)
        elif isinstance(form, HySymbol):
            return HyExpression([HySymbol(name), HyString(form)]).replace(form)
        return HyExpression([HySymbol(name), form]).replace(form)
示例#6
0
def koan_macro():
    return HyExpression([HySymbol('print'),
                         HyString("""
  Ummon asked the head monk, "What sutra are you lecturing on?"
  "The Nirvana Sutra."
  "The Nirvana Sutra has the Four Virtues, hasn't it?"
  "It has."
  Ummon asked, picking up a cup, "How many virtues has this?"
  "None at all," said the monk.
  "But ancient people said it had, didn't they?" said Ummon.
  "What do you think of what they said?"
  Ummon struck the cup and asked, "You understand?"
  "No," said the monk.
  "Then," said Ummon, "You'd better go on with your lectures on the sutra."
""")])
示例#7
0
文件: meth.py 项目: yati-sagade/hy
def router(tree, rkwargs=None):
    tree = HyExpression(tree)
    name = tree.pop(0)
    path = tree.pop(0)
    tree.insert(0, HySymbol("fn"))
    tree = HyExpression([HySymbol("def"), name, tree])

    route = HyExpression([HySymbol(".route"), HySymbol("app"), path])

    if rkwargs:
        route = HyExpression([
            HySymbol("kwapply"), route,
            HyDict({HyString("methods"): rkwargs})
        ])

    return HyExpression([HySymbol("with_decorator"), route, tree])
示例#8
0
文件: test_lex.py 项目: zenhack/hy
def test_lex_strings():
    """ Make sure that strings are valid expressions"""
    objs = tokenize("\"foo\" ")
    assert objs == [HyString("foo")]
示例#9
0
文件: test_lex.py 项目: zenhack/hy
def test_lex_expression_strings():
    """ Test that expressions can produce symbols """
    objs = tokenize("(foo \"bar\")")
    assert objs == [HyExpression([HySymbol("foo"), HyString("bar")])]
示例#10
0
文件: test_lex.py 项目: zenhack/hy
def test_reader_macro():
    """Ensure reader macros are handles properly"""
    entry = tokenize("#^()")
    assert entry[0][0] == HySymbol("dispatch_reader_macro")
    assert entry[0][1] == HyString("^")
    assert len(entry[0]) == 3
示例#11
0
 def exit(self):
     self.result = HyString("".join(self.nodes))
示例#12
0
文件: parser.py 项目: mtmiller/hy
 def uni_hystring(s):
     return HyString(eval('u' + s))
示例#13
0
文件: parser.py 项目: mtmiller/hy
def hash_reader(p):
    st = p[0].getstr()[1]
    str_object = HyString(st)
    expr = p[1]
    return HyExpression([HySymbol("dispatch_reader_macro"), str_object, expr])
示例#14
0
文件: meth.py 项目: yati-sagade/hy
def get_route_macro(*tree):
    return router(tree, rkwargs=HyList([HyString("GET")]))
示例#15
0
文件: meth.py 项目: yati-sagade/hy
def post_route_macro(*tree):
    return router(tree, rkwargs=HyList([HyString("POST")]))
示例#16
0
def test_replace_string_type():
    """Test replacing python string"""
    replaced = replace_hy_obj(str_type("foo"), HyString("bar"))
    assert replaced == HyString("foo")