Guest User

code gen

a guest
Aug 6th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CodeGenCtx } from "./CodeGenCtx";
  2. import { NodesSystem } from "./systems/NodesSystem";
  3.  
  4. export function generateCode(params: {
  5.   nodesSystem: NodesSystem,
  6. }): string {
  7.   let nodesSystem = params.nodesSystem;
  8.   let entityIdOutputAtomsMap = new Map<
  9.     string,
  10.     {
  11.       outputAtoms: Map<string,string>,
  12.     }[]
  13.   >();
  14.   let codeGenCtx = new CodeGenCtx();
  15.   let stack1 = [ ...nodesSystem.nodes(), ];
  16.   let stack2: typeof stack1 = [];
  17.   while (true) {
  18.     let at = stack1.pop();
  19.     if (at == undefined) {
  20.       let tmp = stack1;
  21.       stack1 = stack2;
  22.       stack2 = tmp;
  23.       at = stack2.pop();
  24.       if (at == undefined) {
  25.         break;
  26.       }
  27.     }
  28.     let hasStaleChildren = false;
  29.     for (let source of at.node.inputPins?.() ?? []) {
  30.       let source2 = source.source()?.target;
  31.       if (source2 == undefined) {
  32.         continue;
  33.       }
  34.       if (!entityIdOutputAtomsMap.has(source2)) {
  35.         let node = nodesSystem.lookupNodeById(source2);
  36.         if (node != undefined) {
  37.           stack1.push(node);
  38.           hasStaleChildren = true;
  39.         }
  40.       }
  41.     }
  42.     if (hasStaleChildren) {
  43.       stack2.push(at);
  44.     } else {
  45.       // TODO
  46.       let sourceEntityPinInputMap = new Map<string,Map<string,string[]>>();
  47.       for (let source of at.node.inputPins?.() ?? []) {
  48.         let source2 = source.source();
  49.         if (source2 != undefined) {
  50.           let sourceEntity = source2.target;
  51.           let pinInputMap: Map<string,string[]>;
  52.           if (sourceEntityPinInputMap.has(sourceEntity)) {
  53.             pinInputMap = sourceEntityPinInputMap.get(sourceEntity)!;
  54.           } else {
  55.             pinInputMap = new Map<string,string[]>();
  56.             sourceEntityPinInputMap.set(sourceEntity, pinInputMap);
  57.           }
  58.           let outputAtoms = entityIdOutputAtomsMap.get(source2.target) ?? [];
  59.           pinInputMap.set(
  60.             source.name,
  61.             outputAtoms.flatMap((outputAtoms2) => {
  62.               let x = outputAtoms2.outputAtoms.get(source2.pin);
  63.               if (x == undefined) {
  64.                 return [];
  65.               }
  66.               return [x];
  67.             })
  68.           );
  69.         }
  70.       }
  71.     }
  72.   }
  73.   return codeGenCtx.code;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment