run.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright t lefering, David Given
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * These are the four essential freedoms with GNU GPL software:
  18. * 1: freedom to run the program, for any purpose
  19. * 2: freedom to study how the program works, and change it to make it do what you wish
  20. * 3: freedom to redistribute copies to help your Free Software friends
  21. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  22. * , ,
  23. * / \
  24. * ((__-^^-,-^^-__))
  25. * `-_---' `---_-'
  26. * `--|o` 'o|--'
  27. * \ ` /
  28. * ): :(
  29. * :o_o:
  30. * "-"
  31. */
  32. var fs = require("fs");
  33. var util = require("util");
  34. function print(s)
  35. {
  36. util.print(s);
  37. util.print("\n");
  38. }
  39. function quit()
  40. {
  41. process.exit();
  42. }
  43. /* Node doesn't have an include() function. And eval() has to be called from
  44. * the top level or it doesn't work. WTF? */
  45. function load(filename)
  46. {
  47. return fs.readFileSync(filename, "utf-8");
  48. }
  49. /* Load the Clue crt and libc. */
  50. eval(load("crt.js"));
  51. eval(load("libc.js"));
  52. eval(load("output.js"));
  53. /* Load the Clue compiled program. */
  54. var argv = process.argv;
  55. var argc = 3;
  56. while (argv[argc] != "--")
  57. {
  58. if (!argv[argc])
  59. {
  60. print("Command line did not contain a --\n");
  61. quit();
  62. }
  63. eval(load(argv[argc]));
  64. argc++;
  65. }
  66. /* And run it. */
  67. {
  68. var cargs = [];
  69. for (var i = argc+1; i < argv.length; i++)
  70. {
  71. var v = clue_newstring(argv[i]);
  72. cargs.push(0);
  73. cargs.push(v);
  74. }
  75. clue_run_initializers();
  76. _main(0, [], argv.length - argc, 0, cargs);
  77. quit();
  78. }