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.

133 lines
2.2 KiB

  1. #include <stdio.h>
  2. #include "ast.h"
  3. #include "parser.tab.h"
  4. extern char _binary_sol_help_txt_start, _binary_sol_help_txt_end;
  5. int main(int argc, char **argv) {
  6. stmt_node *program;
  7. sol_state_t state;
  8. char *c;
  9. int printtree = 0, clean = 1, argidx = 2;
  10. FILE *prgstream = stdin, *compstream = NULL;
  11. int result = 0, compile = 0, compiled = 0;
  12. state.features = 0;
  13. if(argc > 1) {
  14. c = argv[1];
  15. while(*c) {
  16. switch(*c) {
  17. case 'd':
  18. yydebug = 1;
  19. break;
  20. case 'D':
  21. state.features |= SOL_FT_DEBUG;
  22. break;
  23. case 't':
  24. printtree = 1;
  25. break;
  26. case 'r':
  27. if(argc < argidx) {
  28. printf("r option requires file\n");
  29. return 2;
  30. }
  31. prgstream = fopen(argv[argidx++], "r");
  32. break;
  33. case 'i':
  34. state.features |= SOL_FT_NO_USR_INIT;
  35. break;
  36. case 'c':
  37. compile = 1;
  38. if(argc < argidx) {
  39. printf("c option requires file\n");
  40. return 2;
  41. }
  42. compstream = fopen(argv[argidx++], "wb");
  43. break;
  44. case 'C':
  45. compiled = 1;
  46. break;
  47. case 'V':
  48. printf(SOL_BUILD_ID "\n");
  49. case 'h':
  50. fwrite(
  51. &_binary_sol_help_txt_start,
  52. 1,
  53. &_binary_sol_help_txt_end - &_binary_sol_help_txt_start,
  54. stdout
  55. );
  56. return 0;
  57. break;
  58. }
  59. c++;
  60. }
  61. }
  62. if(!prgstream) {
  63. printf("No input program (check filenames)\n");
  64. return 2;
  65. }
  66. if(compiled) {
  67. program = sol_deser_stmt(prgstream);
  68. } else {
  69. program = sol_compile_file(prgstream);
  70. }
  71. if(!program) {
  72. printf("NULL program (probably a syntax error)\n");
  73. return 2;
  74. }
  75. if(prgstream != stdin) {
  76. fclose(prgstream);
  77. }
  78. if(compile) {
  79. sol_ser_stmt(compstream, program);
  80. return 0;
  81. }
  82. if(!sol_state_init(&state)) {
  83. printf("State init error (internal bug)\n");
  84. result = 2;
  85. clean = 0;
  86. goto out_results;
  87. }
  88. if(printtree) {
  89. st_print(&state, program);
  90. }
  91. sol_exec(&state, program);
  92. out_results:
  93. if(sol_has_error(&state)) {
  94. printf("Error: ");
  95. ob_print(state.error);
  96. printf("\n");
  97. result = 1;
  98. }
  99. if(state.ret) {
  100. printf("Toplevel return: ");
  101. ob_print(state.ret);
  102. printf("\n");
  103. if(sol_is_int(state.ret)) {
  104. result = state.ret->ival;
  105. }
  106. }
  107. st_free(program);
  108. if(clean) sol_state_cleanup(&state);
  109. return result;
  110. }