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.

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