unify-code.nut 904 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (C) 2013 by Domingo Alvarez Duarte <[email protected]>
  3. *
  4. * Licensed under GPLv3, see http://www.gnu.org/licenses/gpl.html.
  5. */
  6. local included = {};
  7. function unify_code(fname){
  8. if(table_rawget(included, fname, false)) return "";
  9. else included[fname] <- true;
  10. local fd = file(fname, "r");
  11. local fc = fd.read(fd.len());
  12. fd.close();
  13. local last_pos = 0, start_pos = 0, end_pos = 0;
  14. local result = fc.gsub("(dofile%(\"([^\"]+)\"[^)]*%);)", function(m1, m2){
  15. print(m1, m2);
  16. return format("dummy();//%s\n{\n%s\n}\n", m1, unify_code(m2));
  17. });
  18. return result;
  19. }
  20. //print(unify_code("ourbiz-fltk.nut"));
  21. if (vargv.len() > 1){
  22. local infile = vargv[1], outfile = "-";
  23. if(vargv.len() > 2) outfile = vargv[2];
  24. local unified = unify_code(infile);
  25. if(outfile == "-") print(unified);
  26. else
  27. {
  28. local fd = file(outfile, "w");
  29. fd.write(unified, unified.len());
  30. fd.close();
  31. }
  32. }