annotate python/tree.py @ 331:a78b41ff6ad2

Added better recipe files
author Windel Bouwman
date Fri, 07 Feb 2014 12:39:59 +0100
parents e9fe6988497c
children
rev   line source
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
1
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
2 class Tree:
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
3 """ Tree node with a name and possibly some child nodes """
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
4 def __init__(self, name, *args):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
5 self.name = name
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 320
diff changeset
6 self.value = None
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
7 self.children = args
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
8
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
9 def __repr__(self):
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
10 if self.children:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
11 ch = ', '.join(str(c) for c in self.children)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
12 return '{}({})'.format(self.name, ch)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
13 else:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
14 return '{}'.format(self.name)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
15
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
16
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
17 class State:
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
18 """ State used to label tree nodes """
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
19 def __init__(self):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
20 self.labels = {}
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
21
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
22 def has_goal(self, goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
23 return goal in self.labels
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
24
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
25 def get_cost(self, goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
26 return self.labels[goal][0]
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
27
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
28 def get_rule(self, goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
29 return self.labels[goal][1]
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
30
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
31 def set_cost(self, goal, cost, rule):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
32 if self.has_goal(goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
33 if self.get_cost(goal) > cost:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
34 self.labels[goal] = (cost, rule)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
35 else:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
36 self.labels[goal] = (cost, rule)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
37
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
38
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
39 class BaseMatcher:
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
40 """ Base class for matcher objects. """
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
41 def kids(self, tree, rule):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
42 return self.kid_functions[rule](tree)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
43
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
44 def nts(self, rule):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
45 return self.nts_map[rule]
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
46
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
47 def burm_label(self, tree):
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
48 """ Label all nodes in the tree bottom up """
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
49 for c in tree.children:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
50 self.burm_label(c)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
51 self.burm_state(tree)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
52
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
53 def apply_rules(self, tree, goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
54 rule = tree.state.get_rule(goal)
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
55 results = [self.apply_rules(kid_tree, kid_goal)
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
56 for kid_tree, kid_goal in zip(self.kids(tree, rule), self.nts(rule))]
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
57 return self.pat_f[rule](tree, *results)