The Sol Programming Language!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

solrun.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <stdio.h>
  2. #include "ast.h"
  3. #include "parser.tab.h"
  4. #ifndef NO_HELP
  5. extern char _binary_sol_help_txt_start, _binary_sol_help_txt_end;
  6. #endif
  7. int main(int argc, char **argv) {
  8. stmt_node *program;
  9. sol_state_t state;
  10. char *c;
  11. int printtree = 0, clean = 1, argidx = 2;
  12. FILE *prgstream = stdin, *compstream = NULL;
  13. int result = 0, compile = 0, compiled = 0, html = 0;
  14. unsigned i;
  15. state.features = 0;
  16. if(argc > 1) {
  17. c = argv[1];
  18. while(*c) {
  19. switch(*c) {
  20. case 'd':
  21. yydebug = 1;
  22. break;
  23. case 'D':
  24. state.features |= SOL_FT_DEBUG;
  25. break;
  26. case 't':
  27. printtree = 1;
  28. break;
  29. case 'r':
  30. if(argc < argidx) {
  31. printf("r option requires file\n");
  32. return 2;
  33. }
  34. prgstream = fopen(argv[argidx++], "r");
  35. break;
  36. case 'i':
  37. state.features |= SOL_FT_NO_USR_INIT;
  38. break;
  39. case 'c':
  40. compile = 1;
  41. if(argc < argidx) {
  42. printf("c option requires file\n");
  43. return 2;
  44. }
  45. compstream = fopen(argv[argidx++], "wb");
  46. break;
  47. case 'C':
  48. compiled = 1;
  49. break;
  50. case 'V':
  51. printf(SOL_BUILD_ID "\n");
  52. case 'h':
  53. #ifndef NO_HELP
  54. fwrite(
  55. &_binary_sol_help_txt_start,
  56. 1,
  57. &_binary_sol_help_txt_end - &_binary_sol_help_txt_start,
  58. stdout
  59. );
  60. #else
  61. printf("Sorry, no help in this version (-DNO_HELP)!\n");
  62. #endif
  63. printf("\nThis Sol is configured to use the following initialization files (it is never an\nerror if these files cannot be read, but errors propagate normally):\n");
  64. for(i = 0; i < sizeof(sol_AbsInitPaths) / sizeof(sol_AbsInitPaths[0]); i++)
  65. if(sol_AbsInitPaths[i])
  66. printf("- %s\n", sol_AbsInitPaths[i]);
  67. for(i = 0; i < sizeof(sol_HomeInitPaths) / sizeof(sol_HomeInitPaths[0]); i++)
  68. if(sol_HomeInitPaths[i])
  69. printf("- $HOME%s\n", sol_HomeInitPaths[i]);
  70. return 0;
  71. break;
  72. case 'H':
  73. html = 1;
  74. break;
  75. }
  76. c++;
  77. }
  78. }
  79. if(!prgstream) {
  80. printf("No input program (check filenames)\n");
  81. return 2;
  82. }
  83. if(compiled) {
  84. program = sol_deser_stmt(prgstream);
  85. } else {
  86. if(html) {
  87. sol_write_html(prgstream);
  88. return 0;
  89. }
  90. program = sol_compile_file(prgstream);
  91. }
  92. if(!program) {
  93. printf("NULL program (probably a syntax error)\n");
  94. return 2;
  95. }
  96. if(prgstream != stdin) {
  97. fclose(prgstream);
  98. }
  99. if(compile) {
  100. sol_ser_stmt(compstream, program);
  101. return 0;
  102. }
  103. if(!sol_state_init(&state)) {
  104. printf("State init error (internal bug)\n");
  105. result = 2;
  106. clean = 0;
  107. goto out_results;
  108. }
  109. if(printtree) {
  110. st_print(&state, program);
  111. }
  112. sol_exec(&state, program);
  113. out_results:
  114. if(sol_has_error(&state)) {
  115. printf("Error: ");
  116. ob_print(state.error);
  117. printf("\n");
  118. result = 1;
  119. }
  120. if(state.ret) {
  121. printf("Toplevel return: ");
  122. ob_print(state.ret);
  123. printf("\n");
  124. if(sol_is_int(state.ret)) {
  125. result = state.ret->ival;
  126. }
  127. }
  128. st_free(program);
  129. if(clean) sol_state_cleanup(&state);
  130. return result;
  131. }