cc-minimal.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet 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. * M2-Planet 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 M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include<stdlib.h>
  18. #include<stdio.h>
  19. #include<string.h>
  20. #include"cc.h"
  21. /* The core functions */
  22. void initialize_types();
  23. struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* filename);
  24. struct token_list* reverse_list(struct token_list* head);
  25. struct token_list* program();
  26. void recursive_output(struct token_list* i, FILE* out);
  27. int match(char* a, char* b);
  28. void file_print(char* s, FILE* f);
  29. char* parse_string(char* string);
  30. int main()
  31. {
  32. MAX_STRING = 4096;
  33. hold_string = calloc(MAX_STRING, sizeof(char));
  34. FILE* in = fopen("tape_01", "r");
  35. FILE* destination_file = fopen("tape_02", "w");
  36. Architecture = KNIGHT_NATIVE;
  37. global_token = read_all_tokens(in, global_token, "tape_01");
  38. fclose(in);
  39. if(NULL == global_token)
  40. {
  41. file_print("Either no input files were given or they were empty\n", stderr);
  42. exit(EXIT_FAILURE);
  43. }
  44. global_token = reverse_list(global_token);
  45. initialize_types();
  46. reset_hold_string();
  47. output_list = NULL;
  48. program();
  49. /* Output the program we have compiled */
  50. file_print("\n# Core program\n", destination_file);
  51. recursive_output(output_list, destination_file);
  52. file_print("\n\n# Program global variables\n", destination_file);
  53. recursive_output(globals_list, destination_file);
  54. file_print("\n# Program strings\n", destination_file);
  55. recursive_output(strings_list, destination_file);
  56. file_print("\n:STACK\n", destination_file);
  57. fclose(destination_file)
  58. return EXIT_SUCCESS;
  59. }