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
- Slot = namedtuple("Slot", "pull_nid pull_slot")
- Node = namedtuple("Node", "slots defaultArgs")
- Undefined = Ellipsis
- nodeDB = {1: Node(slots=tuple(), defaultArgs=tuple()),
- 2: Node(slots=(Slot(1,0),), defaultArgs=(Undefined,)),
- 3: Node(slots=(Slot(2,1),), defaultArgs=(Undefined,)),
- 4: Node(slots=(Slot(2,0), Slot(3,2)), defaultArgs=(Undefined, Undefined)),
- 5: Node(slots=(Slot(3,0),), defaultArgs=(Undefined,)),
- 6: Node(slots=(Slot(4,0),), defaultArgs=(Undefined,))
- }
- eval_stack = {2: ('a', 'b'), 4: (1, -1)}
- cc_stack = {(4,1): 'cc-x'}
- nodes_eval = (3, 4)
- isSlotinEvalStack = lambda slot: (slot.pull_nid in eval_stack
- and len(eval_stack[slot.pull_nid])-1 >= slot.pull_slot)
- nodes_eval = {nid:
- tuple(
- # read from cc-stack if slot is in cc-stack
- cc_stack[(nid, slot_idx)]
- if (nid, slot_idx) in cc_stack
- # otherwise read obtain a value from eval-stack if slot in eval-stack
- else eval_stack[slot.pull_nid][slot.pull_slot]
- if isSlotinEvalStack(slot)
- # else substitute the default argument
- else nodeDB[nid].defaultArgs[slot_idx]
- # for each slot of the current node
- for slot_idx, slot in enumerate(nodeDB[nid].slots)
- )
- # for all nodes in eval-nodes
- for nid in nodes_eval
- }
- nodes_defr = {nid:
- tuple(
- # read from cc-stack if slot is in cc-stack
- cc_stack[(nid, slot_idx)]
- if (nid, slot_idx) in cc_stack
- # otherwise read obtain a value from eval-stack if slot is undefined and in eval-stack
- else eval_stack[slot.pull_nid][slot.pull_slot]
- if slot_val is Undefined and isSlotinEvalStack(slot)
- # else substitute the currrent slot-value
- else slot_val
- # for each slot of the current node
- for slot_idx, slot_val in enumerate(nodes_deferred[nid])
- )
- # for all nodes in deferred-nodes
- for nid in nodes_deferred
- }
- print(nodes_eval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement