Home Reference Source

cables_dev/cables/src/core/cg/preproc.js

  1. export function preproc(str, vars)
  2. {
  3. const lines = str.split("\n");
  4. const outLines = [];
  5. let stack = [];
  6.  
  7. for (let i = 0; i < lines.length; i++)
  8. {
  9. let line = lines[i].trim();
  10. let parts = line.split(" ");
  11.  
  12. if (line.startsWith("#ifdef "))
  13. {
  14. const s = vars[parts[1]];
  15. stack.push({ "state": s });
  16. continue;
  17. }
  18. if (line.startsWith("#ifndef "))
  19. {
  20. const s = vars[parts[1]];
  21. stack.push({ "state": !s });
  22. continue;
  23. }
  24. if (line.startsWith("#endif"))
  25. {
  26. stack.pop();
  27. continue;
  28. }
  29. const state = stack[stack.length - 1];
  30.  
  31. if (line.startsWith("#else"))
  32. {
  33. state.state = !state.state;
  34. continue;
  35. }
  36.  
  37. if (!state || state.state)
  38. {
  39. outLines.push(lines[i]);
  40. }
  41. }
  42.  
  43. return outLines.join("\n");
  44. }