本文整理汇总了Python中pypy.translator.interactive.Translation.rtype方法的典型用法代码示例。如果您正苦于以下问题:Python Translation.rtype方法的具体用法?Python Translation.rtype怎么用?Python Translation.rtype使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pypy.translator.interactive.Translation
的用法示例。
在下文中一共展示了Translation.rtype方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_translate_compiled_parser
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_translate_compiled_parser():
r0 = Rule("expression", [["additive", "EOF"]])
r1 = Rule("additive", [["multitive", "+", "additive"], ["multitive"]])
r2 = Rule("multitive", [["primary", "*", "multitive"], ["primary"]])
r3 = Rule("primary", [["(", "additive", ")"], ["decimal"]])
r4 = Rule("decimal", [[symb] for symb in "0123456789"])
p = PackratParser([r0, r1, r2, r3, r4], "expression")
compiler = ParserCompiler(p)
kls = compiler.compile()
p = kls()
tree = p.parse([Token(c, i, SourcePos(i, 0, i))
for i, c in enumerate(list("2*(3+4)") + ["EOF"])])
data = [Token(c, i, SourcePos(i, 0, i))
for i, c in enumerate(list("2*(3+4)") + ["EOF"])]
print tree
p = kls()
def parse(choose):
tree = p.parse(data)
return tree.symbol + " " + "-%-".join([c.symbol for c in tree.children])
t = Translation(parse)
t.annotate([bool])
t.backendopt()
t.rtype()
func = t.compile_c()
res1 = parse(True)
res2 = func(True)
assert res1 == res2
示例2: _makefunc_str_int
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def _makefunc_str_int(cls, f):
def main(argv):
arg0 = argv[1]
arg1 = int(argv[2])
try:
res = f(arg0, arg1)
except MemoryError:
print "MEMORY-ERROR"
else:
print res
return 0
t = Translation(main, standalone=True, gc=cls.gcpolicy,
policy=annpolicy.StrictAnnotatorPolicy(),
taggedpointers=cls.taggedpointers,
gcremovetypeptr=cls.removetypeptr)
t.disable(['backendopt'])
t.set_backend_extra_options(c_debug_defines=True)
t.rtype()
if conftest.option.view:
t.viewcg()
exename = t.compile()
def run(s, i):
data = py.process.cmdexec("%s %s %d" % (exename, s, i))
data = data.strip()
if data == 'MEMORY-ERROR':
raise MemoryError
return data
return run
示例3: test_translate_simple
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_translate_simple(self):
digits = RangeExpression("0", "9")
lower = RangeExpression("a", "z")
upper = RangeExpression("A", "Z")
keywords = StringExpression("if") | StringExpression("else") | StringExpression("def") | StringExpression("class")
underscore = StringExpression("_")
atoms = lower + (upper | lower | digits | underscore).kleene()
vars = underscore | (upper + (upper | lower | underscore | digits).kleene())
integers = StringExpression("0") | (RangeExpression("1", "9") + digits.kleene())
white = StringExpression(" ")
l1 = self.get_lexer([keywords, atoms, vars, integers, white], ["KEYWORD", "ATOM", "VAR", "INT", "WHITE"])
l2 = self.get_lexer([keywords, atoms, vars, integers, white], ["KEYWORD", "ATOM", "VAR", "INT", "WHITE"], ["WHITE"])
def lex(s, ignore=False):
if ignore:
tokens = l2.tokenize(s)
else:
tokens = l1.tokenize(s)
return "-%-".join([t.name for t in tokens])
res = lex("if A a 12341 0 else").split("-%-")
assert res == ("KEYWORD WHITE VAR WHITE ATOM WHITE INT WHITE "
"INT WHITE KEYWORD").split()
res = lex("if A a 12341 0 else", True).split("-%-")
assert res == "KEYWORD VAR ATOM INT INT KEYWORD".split()
t = Translation(lex)
t.annotate([str, bool])
t.rtype()
func = t.compile_c()
res = lex("if A a 12341 0 else", False).split("-%-")
assert res == ("KEYWORD WHITE VAR WHITE ATOM WHITE INT WHITE "
"INT WHITE KEYWORD").split()
res = lex("if A a 12341 0 else", True).split("-%-")
assert res == "KEYWORD VAR ATOM INT INT KEYWORD".split()
示例4: test_annotator_folding
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_annotator_folding():
from pypy.translator.interactive import Translation
gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
gcgroup = OptionDescription('gc', '', [gcoption])
descr = OptionDescription('pypy', '', [gcgroup])
config = Config(descr)
def f(x):
if config.gc.name == 'ref':
return x + 1
else:
return 'foo'
t = Translation(f)
t.rtype([int])
block = t.context.graphs[0].startblock
assert len(block.exits[0].target.operations) == 0
assert len(block.operations) == 1
assert len(block.exits) == 1
assert block.operations[0].opname == 'int_add'
assert config._freeze_()
# does not raise, since it does not change the attribute
config.gc.name = "ref"
py.test.raises(TypeError, 'config.gc.name = "framework"')
示例5: build_adi
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def build_adi(function, types):
t = Translation(function)
t.rtype(types)
if conftest.option.view:
t.view()
adi = AbstractDataFlowInterpreter(t.context)
graph = graphof(t.context, function)
adi.schedule_function(graph)
adi.complete()
return t.context, adi, graph
示例6: test_enforced_args
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_enforced_args():
from pypy.annotation.model import s_None
from pypy.rpython.annlowlevel import MixLevelHelperAnnotator
from pypy.translator.interactive import Translation
def f1():
str2charp("hello")
def f2():
str2charp("world")
t = Translation(f1, [])
t.rtype()
mixann = MixLevelHelperAnnotator(t.context.rtyper)
mixann.getgraph(f2, [], s_None)
mixann.finish()
示例7: test_computed_int_symbolic
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_computed_int_symbolic():
too_early = True
def compute_fn():
assert not too_early
return 7
k = ComputedIntSymbolic(compute_fn)
def f():
return k*6
t = Translation(f)
t.rtype()
if conftest.option.view:
t.view()
too_early = False
fn = t.compile_c()
res = fn()
assert res == 42
示例8: test_parser
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_parser():
def f(x):
if x:
s = "a(X, Y, Z)."
else:
s = "f(a, X, _, _, X, f(X, 2.455))."
term = parsing.parse_file(s)
assert isinstance(term, parsing.Nonterminal)
return term.symbol
assert f(True) == "file"
assert f(True) == "file"
t = Translation(f)
t.annotate([bool])
t.rtype()
t.backendopt()
func = t.compile_c()
assert func(True) == "file"
assert func(False) == "file"
示例9: test_inhibit_tail_call
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_inhibit_tail_call():
from pypy.rpython.lltypesystem import lltype
def foobar_fn(n):
return 42
foobar_fn._dont_inline_ = True
def main(n):
return foobar_fn(n)
#
t = Translation(main, [int], backend="c")
t.rtype()
t.context._graphof(foobar_fn).inhibit_tail_call = True
t.source_c()
lines = t.driver.cbuilder.c_source_filename.readlines()
for i, line in enumerate(lines):
if '= pypy_g_foobar_fn' in line:
break
else:
assert 0, "the call was not found in the C source"
assert 'PYPY_INHIBIT_TAIL_CALL();' in lines[i+1]
示例10: test_simple_rtype_with_type_system
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_simple_rtype_with_type_system():
def f(x,y):
return x+y
t = Translation(f, [int, int])
s = t.annotate()
t.rtype(type_system='lltype')
assert 'rtype_lltype' in t.driver.done
t = Translation(f, [int, int])
s = t.annotate()
t.rtype(type_system='ootype')
assert 'rtype_ootype' in t.driver.done
t = Translation(f, type_system='ootype')
s = t.annotate([int, int])
t.rtype()
assert 'rtype_ootype' in t.driver.done
t = Translation(f)
s = t.annotate([int, int])
t.rtype(backend='cli')
assert 'rtype_ootype' in t.driver.done
t = Translation(f, backend='cli', type_system='ootype')
s = t.annotate([int, int])
t.rtype()
assert 'rtype_ootype' in t.driver.done
t = Translation(f, type_system='lltype')
s = t.annotate([int, int])
py.test.raises(Exception, "t.rtype(backend='cli')")
t = Translation(f, backend='cli')
s = t.annotate([int, int])
py.test.raises(Exception, "t.rtype(type_system='lltype')")
示例11: test_engine
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_engine():
e = get_engine("""
g(a, a).
g(a, b).
g(b, c).
f(X, Z) :- g(X, Y), g(Y, Z).
""")
t1 = parse_query_term("f(a, c).")
t2 = parse_query_term("f(X, c).")
def run():
e.run(t1)
e.run(t2)
v0 = e.heap.getvar(0)
if isinstance(v0, Atom):
return v0.name
return "no!"
assert run() == "a"
t = Translation(run)
t.annotate()
t.rtype()
func = t.compile_c()
assert func() == "a"
示例12: test_translate_pypackrat_regex
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_translate_pypackrat_regex():
from pypy.rlib.parsing.pypackrat import PackratParser
class parser(PackratParser):
"""
num:
`([1-9][0-9]*)|0`;
"""
print parser._code
def parse(s):
p = parser(s)
return p.num()
res = parse("1234")
assert res == '1234'
t = Translation(parse)
t.annotate([str])
t.rtype()
t.backendopt()
if option.view:
t.view()
func = t.compile_c()
res = func("12345")
assert res == '12345'
res = func("0")
assert res == '0'
示例13: test_translate_ast_visitor
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_translate_ast_visitor():
from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
regexs, rules, ToAST = parse_ebnf("""
DECIMAL: "0|[1-9][0-9]*";
IGNORE: " ";
additive: multitive ["+!"] additive | <multitive>;
multitive: primary ["*!"] multitive | <primary>; #nonsense!
primary: "(" <additive> ")" | <DECIMAL>;
""")
parse = make_parse_function(regexs, rules)
def f():
tree = parse("(0 +! 10) *! (999 +! 10) +! 1")
tree = ToAST().visit_additive(tree)
assert len(tree) == 1
tree = tree[0]
return tree.symbol + " " + "-&-".join([c.symbol for c in tree.children])
res1 = f()
t = Translation(f)
t.annotate()
t.rtype()
t.backendopt()
func = t.compile_c()
res2 = func()
assert res1 == res2
示例14: test_translate_pypackrat
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def test_translate_pypackrat():
from pypy.rlib.parsing.pypackrat import PackratParser
class parser(PackratParser):
"""
expr:
additive;
additive:
a = additive
'-'
b = multitive
return {'(%s - %s)' % (a, b)}
| multitive;
multitive:
a = multitive
'*'
b = simple
return {'(%s * %s)' % (a, b)}
| simple;
simple:
('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
"""
print parser._code
def parse(s):
p = parser(s)
return p.expr()
res = parse("5-5-5")
assert res == '((5 - 5) - 5)'
t = Translation(parse)
t.annotate([str])
t.rtype()
t.backendopt()
if option.view:
t.view()
func = t.compile_c()
res = func("5-5-5")
assert res == '((5 - 5) - 5)'
示例15: make_graph
# 需要导入模块: from pypy.translator.interactive import Translation [as 别名]
# 或者: from pypy.translator.interactive.Translation import rtype [as 别名]
def make_graph(self, f, arguments):
t = Translation(f)
t.rtype(arguments)
return t.context.graphs[0]