Пример #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
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
def test_lex_strings():
    """ Make sure that strings are valid expressions"""
    objs = tokenize("\"foo\" ")
    assert objs == [HyString("foo")]
Пример #9
0
def test_lex_expression_strings():
    """ Test that expressions can produce symbols """
    objs = tokenize("(foo \"bar\")")
    assert objs == [HyExpression([HySymbol("foo"), HyString("bar")])]
Пример #10
0
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
 def uni_hystring(s):
     return HyString(eval('u' + s))
Пример #13
0
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
def get_route_macro(*tree):
    return router(tree, rkwargs=HyList([HyString("GET")]))
Пример #15
0
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")