Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # evaluate nodes with a stack -- functional algorithm
- from collections import namedtuple
- #------------------------------------------------------------------------------80
- PullOp = namedtuple("PullOp", "nid slot_id")
- Node = namedtuple("Node", "slots defaultArgs")
- Undefined = Ellipsis
- nodeDB = {1: Node(slots=tuple(), defaultArgs=tuple()),
- 2: Node(slots=(PullOp(1,0),), defaultArgs=(Undefined,)),
- 3: Node(slots=(PullOp(2,1),), defaultArgs=(Undefined,)),
- 4: Node(slots=(PullOp(2,0), PullOp(3,2)), defaultArgs=(Undefined, Undefined)),
- 5: Node(slots=(PullOp(3,0),), defaultArgs=(Undefined,)),
- 6: Node(slots=(PullOp(4,0),), defaultArgs=(Undefined,))
- }
- eval_stack = {2: ('a', 'b'), 4: (1, -1)}
- cc_stack = {(4,1): 'cc-x'}
- nodes = {3: (Undefined,), 4: (Undefined, Undefined)}
- nodes_eval = (3, 4)
- def fresh_slot_value(nid, slot_id, slot_value):
- ''' (PURE)
- returns a fresh slot value from cc-stack, eval-stack or default
- arguments, if the slot value is Undefined.
- The function needs read-only access to cc-stack, eval-stack, and nodeDB.
- '''
- # if current slot-value is already 'defined' by a prior evaluation,
- # then no need to change
- if slot_value is not Undefined:
- return slot_value
- #ELSE: if current slot-value is 'undefined'
- # 1) obtain value from cc-stack, if slot is in cc-stack
- push_op = (nid, slot_id) # push operation (from cc-stack)
- if push_op in cc_stack:
- return cc_stack[(nid, slot_id)] # do push-operation
- # 2) obtain a value from eval-stack, if slot in eval-stack
- pull_op = nodeDB[nid].slots[slot_id] # pull operation (from eval-stack)
- if (pull_op.nid in eval_stack
- and len(eval_stack[pull_op.nid])-1 >= pull_op.slot_id):
- return eval_stack[pull_op.nid][pull_op.slot_id] # do pull-operation
- # otherwise take the default argument
- return nodeDB[nid].defaultArgs[slot_id]
- #END if
- new_nodes_eval = {nid:
- tuple(fresh_slot_value(nid, slot_id, nodes[nid][slot_id])
- # for each slot of the current node
- for slot_id, _ in enumerate(nodeDB[nid].slots)
- )
- # for all nodes in eval-nodes
- for nid in nodes
- }
- print(new_nodes_eval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement