本文整理汇总了Python中src.parser.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_calling_atom_raises_exception
def test_calling_atom_raises_exception():
"""A function call to a non-function should result in an error."""
with assert_raises_regexp(LispError, "not a function"):
evaluate(parse("(#t 'foo 'bar)"), Environment())
with assert_raises_regexp(LispError, "not a function"):
evaluate(parse("(42)"), Environment())
开发者ID:florianletsch,项目名称:lambda,代码行数:7,代码来源:test_5_adding_functions_to_the_mix.py
示例2: test_calling_with_wrong_number_of_arguments
def test_calling_with_wrong_number_of_arguments():
"""Functions should raise exceptions when called with wrong number of arguments."""
env = Environment()
evaluate(parse("(define fn (lambda (p1 p2) 'whatever))"), env)
error_msg = "wrong number of arguments, expected 2 got 3"
with assert_raises_regexp(LispError, error_msg):
evaluate(parse("(fn 1 2 3)"), env)
开发者ID:florianletsch,项目名称:lambda,代码行数:8,代码来源:test_5_adding_functions_to_the_mix.py
示例3: test_parse_exception_extra_paren
def test_parse_exception_extra_paren():
"""Another exception is raised if the expression is too large.
The parse function expects to receive only one single expression. Anything
more than this, should result in the proper exception."""
with assert_raises_regexp(LispError, 'Expected EOF'):
parse('(foo (bar x y)))')
开发者ID:florianletsch,项目名称:lambda,代码行数:8,代码来源:test_1_parsing.py
示例4: test_parse_boolean
def test_parse_boolean():
"""Parsing single booleans.
Booleans are the special symbols #t and #f. In the ASTs they are represented
by Pythons True and False, respectively. """
assert_equals(True, parse('#t'))
assert_equals(False, parse('#f'))
开发者ID:florianletsch,项目名称:lambda,代码行数:8,代码来源:test_1_parsing.py
示例5: test_lambda_arguments_are_lists
def test_lambda_arguments_are_lists():
"""The parameters of a `lambda` should be a list."""
closure = evaluate(parse("(lambda (x y) (+ x y))"), Environment())
assert_true(is_list(closure.params))
with assert_raises(LispError):
evaluate(parse("(lambda not-a-list (body of fn))"), Environment())
开发者ID:florianletsch,项目名称:lambda,代码行数:8,代码来源:test_5_adding_functions_to_the_mix.py
示例6: test_checking_whether_list_is_empty
def test_checking_whether_list_is_empty():
"""The `empty` form checks whether or not a list is empty."""
assert_equals(False, evaluate(parse("(empty '(1 2 3))"), Environment()))
assert_equals(False, evaluate(parse("(empty '(1))"), Environment()))
assert_equals(True, evaluate(parse("(empty '())"), Environment()))
assert_equals(True, evaluate(parse("(empty (tail '(1)))"), Environment()))
开发者ID:florianletsch,项目名称:lambda,代码行数:8,代码来源:test_6_working_with_lists.py
示例7: test_creating_longer_lists_with_only_cons
def test_creating_longer_lists_with_only_cons():
"""`cons` needs to evaluate it's arguments.
Like all the other special forms and functions in our language, `cons` is
call-by-value. This means that the arguments must be evaluated before we
create the list with their values."""
result = evaluate(parse("(cons 3 (cons (- 4 2) (cons 1 '())))"), Environment())
assert_equals(parse("(3 2 1)"), result)
开发者ID:florianletsch,项目名称:lambda,代码行数:9,代码来源:test_6_working_with_lists.py
示例8: test_parse_integer
def test_parse_integer():
"""Parsing single integer.
Integers are represented in the ASTs as Python ints.
Tip: String objects have a handy .isdigit() method.
"""
assert_equals(42, parse('42'))
assert_equals(1337, parse('1337'))
开发者ID:florianletsch,项目名称:lambda,代码行数:10,代码来源:test_1_parsing.py
示例9: test_call_to_function_should_evaluate_arguments
def test_call_to_function_should_evaluate_arguments():
"""Call to function should evaluate all arguments.
When a function is applied, the arguments should be evaluated before being bound
to the parameter names."""
env = Environment()
closure = evaluate(parse("(lambda (a) (+ a 5))"), env)
ast = [closure, parse("(if #f 0 (+ 10 10))")]
assert_equals(25, evaluate(ast, env))
开发者ID:florianletsch,项目名称:lambda,代码行数:11,代码来源:test_5_adding_functions_to_the_mix.py
示例10: test_math_operators_only_work_on_numbers
def test_math_operators_only_work_on_numbers():
"""The math functions should only allow numbers as arguments."""
with assert_raises(LispError):
evaluate(parse("(+ 1 'foo)"), Environment())
with assert_raises(LispError):
evaluate(parse("(- 1 'foo)"), Environment())
with assert_raises(LispError):
evaluate(parse("(/ 1 'foo)"), Environment())
with assert_raises(LispError):
evaluate(parse("(mod 1 'foo)"), Environment())
开发者ID:florianletsch,项目名称:lambda,代码行数:11,代码来源:test_2_evaluating_simple_expressions.py
示例11: test_creating_lists_by_quoting
def test_creating_lists_by_quoting():
"""One way to create lists is by quoting.
We have already implemented `quote` so this test should already be
passing.
The reason we need to use `quote` here is that otherwise the expression would
be seen as a call to the first element -- `1` in this case, which obviously isn't
even a function."""
assert_equals(parse("(1 2 3 #t)"),
evaluate(parse("'(1 2 3 #t)"), Environment()))
开发者ID:florianletsch,项目名称:lambda,代码行数:12,代码来源:test_6_working_with_lists.py
示例12: test_parse_list_of_symbols
def test_parse_list_of_symbols():
"""Parsing list of only symbols.
A list is represented by a number of elements surrounded by parens. Python lists
are used to represent lists as ASTs.
Tip: The useful helper function `find_matching_paren` is already provided in
`parse.py`.
"""
assert_equals(['foo', 'bar', 'baz'], parse('(foo bar baz)'))
assert_equals([], parse('()'))
开发者ID:florianletsch,项目名称:lambda,代码行数:12,代码来源:test_1_parsing.py
示例13: test_calling_very_simple_function_in_environment
def test_calling_very_simple_function_in_environment():
"""A call to a symbol corresponds to a call to its value in the environment.
When a symbol is the first element of the AST list, it is resolved to its value in
the environment (which should be a function closure). An AST with the variables
replaced with its value should then be evaluated instead."""
env = Environment()
evaluate(parse("(define add (lambda (x y) (+ x y)))"), env)
assert_is_instance(env.lookup("add"), Closure)
result = evaluate(parse("(add 1 2)"), env)
assert_equals(3, result)
开发者ID:florianletsch,项目名称:lambda,代码行数:13,代码来源:test_5_adding_functions_to_the_mix.py
示例14: test_basic_if_statement
def test_basic_if_statement():
"""If statements are the basic control structures.
The `if` should first evaluate its first argument. If this evaluates to true, then
the second argument is evaluated and returned. Otherwise the third and last argument
is evaluated and returned instead."""
if_expression = parse("(if #t 42 1000)")
assert_equals(42, evaluate(if_expression, Environment()))
if_expression = parse("(if #f 42 1000)")
assert_equals(1000, evaluate(if_expression, Environment()))
if_expression = parse("(if #t #t #f)")
assert_equals(True, evaluate(if_expression, Environment()))
开发者ID:florianletsch,项目名称:lambda,代码行数:13,代码来源:test_3_evaluating_complex_expressions.py
示例15: test_evaluating_eq_function
def test_evaluating_eq_function():
"""The `eq` form is used to check whether two expressions are the same atom."""
assert_equals(True, evaluate(["eq", 1, 1], Environment()))
assert_equals(False, evaluate(["eq", 1, 2], Environment()))
# From this point, the ASTs might sometimes be too long or cummbersome to
# write down explicitly, and we'll use `parse` to make them for us.
# Remember, if you need to have a look at exactly what is passed to `evaluate`,
# just add a print statement in the test (or in `evaluate`).
assert_equals(True, evaluate(parse("(eq 'foo 'foo)"), Environment()))
assert_equals(False, evaluate(parse("(eq 'foo 'bar)"), Environment()))
# Lists are never equal, because lists are not atoms
assert_equals(False, evaluate(parse("(eq '(1 2 3) '(1 2 3))"), Environment()))
开发者ID:florianletsch,项目名称:lambda,代码行数:16,代码来源:test_2_evaluating_simple_expressions.py
示例16: test_lambda_closure_holds_function
def test_lambda_closure_holds_function():
"""The closure contains the parameter list and function body too."""
closure = evaluate(parse("(lambda (x y) (+ x y))"), Environment())
assert_equals(["x", "y"], closure.params)
assert_equals(["+", "x", "y"], closure.body)
开发者ID:florianletsch,项目名称:lambda,代码行数:7,代码来源:test_5_adding_functions_to_the_mix.py
示例17: test_calling_function_recursively
def test_calling_function_recursively():
"""Tests that a named function is included in the environment
where it is evaluated."""
env = Environment()
evaluate(parse("""
(define my-fn
;; A meaningless, but recursive, function
(lambda (x)
(if (eq x 0)
42
(my-fn (- x 1)))))
"""), env)
assert_equals(42, evaluate(parse("(my-fn 0)"), env))
assert_equals(42, evaluate(parse("(my-fn 10)"), env))
开发者ID:florianletsch,项目名称:lambda,代码行数:16,代码来源:test_5_adding_functions_to_the_mix.py
示例18: main
def main():
problems = ['2-1', '2-2', '2-3', '2-4', '2-5', '2-6', '2-7', '2-8',
'3-7', '3-8',
'4-7', '4-8']
for x in problems:
file_path = "reps/%s" % x
if os.path.exists(file_path):
image_tree = parse(file_path)
d = meta(image_tree)
print "============================================================"
print "The most likely answer based on both solvers is: \t%s" % d['ans']
print ""
print "Motivation: " + d['motivation']
print ""
if show_both:
print "--------------------------------------------------------"
print "Visual solution to problem %s is:" % (x)
pprint(d['vis'][verbosity])
print ""
print "--------------------------------------------------------"
print "Solution to problem %s is:" % (x)
pprint(d['rep'][verbosity])
else:
print "The used (%s) solver gave this answer/solution:" % d['used']
pprint(d[d['used']][verbosity])
print "============================================================"
print "\n\n\n"
else:
print "Didn't find representation for file %s" % x
开发者ID:Tarrasch,项目名称:ravens-test,代码行数:30,代码来源:main.py
示例19: test_parse_list_of_mixed_types
def test_parse_list_of_mixed_types():
"""Parsing a list containing different types.
When parsing lists, make sure each of the sub-expressions are also parsed
properly."""
assert_equals(['foo', True, 123], parse('(foo #t 123)'))
开发者ID:florianletsch,项目名称:lambda,代码行数:7,代码来源:test_1_parsing.py
示例20: test_parse_single_symbol
def test_parse_single_symbol():
"""Parsing a single symbol.
Symbols are represented by text strings. Parsing a single atom should result
in an AST consisting of only that symbol."""
assert_equals('foo', parse('foo'))
开发者ID:florianletsch,项目名称:lambda,代码行数:7,代码来源:test_1_parsing.py
注:本文中的src.parser.parse函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论