This script moves all the selected nodes into their destination pins if possible. To use, select the nodes to be moved (don't select the destination node) and run this script.
A node can be moved if:
The ouput is connected to exactly one destination pin,
All nodes connected to the input pins are either already inside the input pin, or they will be moved there by this script
You can look at this script for a start, you'll need to go over the nodes recursively in a similar way. The moving out case will be a bit easier since any internal node will definitely be moved out.
The terminal value nodes are easy to recognise because they don't have any input pins.