本文整理汇总了Python中tree.Tree.children方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.children方法的具体用法?Python Tree.children怎么用?Python Tree.children使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.children方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse2ast
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import children [as 别名]
def parse2ast(parse_tree):
"""
Converts a parse tree into an AST.
"""
for node in list(parse_tree.postfix_iter()):
if node.node_type == 'token':
if node.token_type in ('SOF', 'EOF', 'punc'):
# go ahead and filter unncessary punctuation tokens
node.kill()
elif node.token_type in ('str', 'int', 'bool'):
# it's a literal
node.node_type = 'literal'
setattr(node, 'ice9_type', node.token_type)
if node.ice9_type == 'str':
node.value = node.value[1:-1] # remove the quotes
elif node.ice9_type == 'int':
node.value = int(node.value)
elif node.ice9_type == 'bool':
if node.value == 'true':
node.value = True
elif node.value == 'false':
node.value = False
elif node.token_type == 'ident':
node.node_type = 'ident'
elif node.value in ('write', 'writes', 'break', 'exit', 'return', 'read'):
node.parent.node_type = 'operator'
node.parent.value = node.value
assert node.parent.children.pop(0) == node
elif node.node_type == 'rule-expansion':
if (len(node.children) == 0 and node.value != 'proc_call' and
node.value != 'program' and node.value != 'stms'):
# Empty node, let's just kill it and go onto the next
node.kill()
continue
elif len(node.children) >= 2:
if (node.children[0].node_type == 'token' and
node.children[0].value in UNARY_OPS and
node.value == 'high'):
# node.parent node.parent
# | |
# node => op
# / \ |
# op right right
p = node.parent
op = node.children[0].value
node.node_type = 'operator'
node.value = op
node.line = node.children[0].line
node.children.pop(0)
continue
elif (node.children[0].node_type == 'token' and
node.children[0].value in BINARY_OPS and
node.parent.node_type == 'rule-expansion'):
if (node.parent.value != node.value):
# end of the chain
# node.parent
# / \
# a ( node )
# / / | \ \
# op1 b op2 c ...
p = node.parent
index = p.children.index(node)
a = p.children.pop(index - 1)
# node contains an expression a OP b OP c ...
# where OP are all of the same precedence. We
# need to turn it into a left oriented tree.
leftnode = a
children = node.children
while len(children) > 0:
op = children.pop(0)
operand = children.pop(0)
newleft = Tree(value=op.value, node_type='operator', line=op.line)
newleft.children = [leftnode, operand]
leftnode.parent = newleft
operand.parent = leftnode
leftnode = newleft
leftnode.parent = node
node.children = [leftnode]
node.remove_and_promote()
continue
if node.value in transform_rules:
transform_rules[node.value](node)
return parse_tree
示例2: memlookup
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import children [as 别名]
def memlookup(varname, ast):
"""
Returns a tuple (code5, memloc, relreg) meaning after code5,
varname will be in memory[memloc + reg[relreg]].
"""
code5 = []
memloc, relreg = first_definition(variables, varname)
if ast and len(ast.children) > 0:
# array reference. We need to do all our index calculations and such
p = ast.parent
while True:
if hasattr(p, 'vars') and any(x == varname for x, t in p.vars):
break
else:
p = p.parent
arrayindexes = []
vartype = dict(p.vars)[varname]
while type(vartype) is list and vartype[0] == "array":
arrayindexes.append(vartype[2])
vartype = vartype[1]
code5 += comment('%s is an array of size %s' % (varname, arrayindexes))
from tree import Tree
fakeast = Tree(node_type='operator', value='write')
fakeast.children = [Tree(node_type='literal', value='Arrays bounds violation', ice9_type='str')]
failcode = comment('Array out of bounds error code:')
failcode += generate_code(fakeast)
failcode += [('HALT', 0, 0, 0, 'Array out of bounds')]
failcode += comment('End array out of bounds error code')
indexcode = comment('Calculating memory location:')
if relreg == FP:
# we're in a proc, so what we have is a pointer that we'll
# still need to dereference again
indexcode += [('LD', AC4, memloc, relreg, 'Go ahead and dereference %s' % varname)]
memloc, relreg = 0, AC4
else:
# we're just in the main body, so we already know the
# direct location of the array.
indexcode += [('LDA', AC4, memloc, relreg, 'Start array indexing at 0')]
memloc, relreg = 0, AC4
iteration = izip(ast.children, arrayindexes, arrayindexes[1:] + [1])
for indexast, dimension_size, mul_size in iteration:
indexcode += push_register(AC4, "Pushing array address to stack")
indexcode += generate_code(indexast)
indexcode += pop_register(AC4, "Popping array address from stack")
jumpsize = code_length(failcode + indexcode)
indexcode += [('JLT', AC1, - jumpsize - 1, PC, 'Check index >= 0'),
('LDA', AC1, - dimension_size, AC1, 'Prepare for dimension check'),
('JGE', AC1, - jumpsize - 3, PC, 'Check index < size'),
('LDA', AC1, dimension_size, AC1, 'Undo dimension check'),
('LDC', AC2, mul_size, 0, 'Prepare for dimension multiply'),
('MUL', AC2, AC2, AC1, 'Multiply index * arraysize'),
('ADD', AC4, AC4, AC2, 'Add this increment to our offset.')]
code5 += [('JEQ', ZERO, code_length(failcode), PC, 'Skip array out of bounds failure.')]
code5 += failcode
code5 += indexcode
return code5, memloc, relreg