Advertisement
SepandMeenu

evaluate nodes using stack

Nov 8th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. # evaluate nodes with a stack -- functional algorithm
  2.  
  3. from collections import namedtuple
  4. #------------------------------------------------------------------------------80
  5.  
  6. Slot = namedtuple("Slot", "pull_nid pull_slot")
  7. Node = namedtuple("Node", "slots defaultArgs")
  8. Undefined = Ellipsis
  9.  
  10. nodeDB = {1: Node(slots=tuple(), defaultArgs=tuple()),
  11.           2: Node(slots=(Slot(1,0),), defaultArgs=(Undefined,)),
  12.           3: Node(slots=(Slot(2,1),), defaultArgs=(Undefined,)),
  13.           4: Node(slots=(Slot(2,0), Slot(3,2)), defaultArgs=(Undefined, Undefined)),
  14.           5: Node(slots=(Slot(3,0),), defaultArgs=(Undefined,)),
  15.           6: Node(slots=(Slot(4,0),), defaultArgs=(Undefined,))
  16. }
  17.  
  18. eval_stack = {2: ('a', 'b'), 4: (1, -1)}
  19. cc_stack = {(4,1): 'cc-x'}
  20.  
  21. nodes_eval = (3, 4)
  22.  
  23. isSlotinEvalStack = lambda slot: (slot.pull_nid in eval_stack
  24.                             and len(eval_stack[slot.pull_nid])-1 >= slot.pull_slot)
  25.  
  26. nodes_eval = {nid:
  27.               tuple(
  28.                   # read from cc-stack if slot is in cc-stack
  29.                   cc_stack[(nid, slot_idx)]
  30.                   if (nid, slot_idx) in cc_stack
  31.                   # otherwise read obtain a value from eval-stack if slot in eval-stack
  32.                   else eval_stack[slot.pull_nid][slot.pull_slot]
  33.                          if isSlotinEvalStack(slot)
  34.                          # else substitute the default argument
  35.                          else nodeDB[nid].defaultArgs[slot_idx]
  36.  
  37.                     # for each slot of the current node
  38.                     for slot_idx, slot in enumerate(nodeDB[nid].slots)
  39.               )
  40.               # for all nodes in eval-nodes
  41.               for nid in nodes_eval
  42.               }
  43.  
  44. nodes_defr = {nid:
  45.               tuple(
  46.                   # read from cc-stack if slot is in cc-stack
  47.                   cc_stack[(nid, slot_idx)]
  48.                   if (nid, slot_idx) in cc_stack
  49.                   # otherwise read obtain a value from eval-stack if slot is undefined and in eval-stack
  50.                   else eval_stack[slot.pull_nid][slot.pull_slot]
  51.                          if slot_val is Undefined and isSlotinEvalStack(slot)
  52.                          # else substitute the currrent slot-value
  53.                          else slot_val
  54.  
  55.                     # for each slot of the current node
  56.                     for slot_idx, slot_val in enumerate(nodes_deferred[nid])
  57.               )
  58.               # for all nodes in deferred-nodes
  59.               for nid in nodes_deferred
  60.               }
  61.  
  62. print(nodes_eval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement