cc_reader.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "cc.h"
  18. FILE* input;
  19. struct token_list* token;
  20. int line;
  21. char* file;
  22. int in_set(int c, char* s);
  23. int clearWhiteSpace(int c)
  24. {
  25. if((32 == c) || (9 == c)) return clearWhiteSpace(fgetc(input));
  26. else if (10 == c)
  27. {
  28. line = line + 1;
  29. return clearWhiteSpace(fgetc(input));
  30. }
  31. return c;
  32. }
  33. int consume_byte(int c)
  34. {
  35. hold_string[string_index] = c;
  36. string_index = string_index + 1;
  37. return fgetc(input);
  38. }
  39. int consume_word(int c, int frequent)
  40. {
  41. int escape = FALSE;
  42. do
  43. {
  44. if(!escape && '\\' == c ) escape = TRUE;
  45. else escape = FALSE;
  46. c = consume_byte(c);
  47. } while(escape || (c != frequent));
  48. return fgetc(input);
  49. }
  50. void fixup_label()
  51. {
  52. int hold = ':';
  53. int prev;
  54. int i = 0;
  55. do
  56. {
  57. prev = hold;
  58. hold = hold_string[i];
  59. hold_string[i] = prev;
  60. i = i + 1;
  61. } while(0 != hold);
  62. }
  63. int preserve_keyword(int c)
  64. {
  65. while(in_set(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"))
  66. {
  67. c = consume_byte(c);
  68. }
  69. if(':' == c)
  70. {
  71. fixup_label();
  72. return 32;
  73. }
  74. return c;
  75. }
  76. int preserve_symbol(int c)
  77. {
  78. while(in_set(c, "<=>|&!-"))
  79. {
  80. c = consume_byte(c);
  81. }
  82. return c;
  83. }
  84. int purge_macro(int ch)
  85. {
  86. while(10 != ch) ch = fgetc(input);
  87. return ch;
  88. }
  89. void reset_hold_string()
  90. {
  91. int i = string_index + 2;
  92. while(0 != i)
  93. {
  94. hold_string[i] = 0;
  95. i = i - 1;
  96. }
  97. }
  98. int get_token(int c)
  99. {
  100. struct token_list* current = calloc(1, sizeof(struct token_list));
  101. reset:
  102. reset_hold_string();
  103. string_index = 0;
  104. c = clearWhiteSpace(c);
  105. if('#' == c)
  106. {
  107. c = purge_macro(c);
  108. goto reset;
  109. }
  110. else if(in_set(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"))
  111. {
  112. c = preserve_keyword(c);
  113. }
  114. else if(in_set(c, "<=>|&!-"))
  115. {
  116. c = preserve_symbol(c);
  117. }
  118. else if(c == '\'')
  119. { /* 39 == ' */
  120. c = consume_word(c, '\'');
  121. }
  122. else if(c == '"')
  123. {
  124. c = consume_word(c, '"');
  125. }
  126. else if(c == '/')
  127. {
  128. c = consume_byte(c);
  129. if(c == '*')
  130. {
  131. c = fgetc(input);
  132. while(c != '/')
  133. {
  134. while(c != '*')
  135. {
  136. c = fgetc(input);
  137. if(10 == c) line = line + 1;
  138. }
  139. c = fgetc(input);
  140. if(10 == c) line = line + 1;
  141. }
  142. c = fgetc(input);
  143. goto reset;
  144. }
  145. else if(c == '/')
  146. {
  147. c = fgetc(input);
  148. goto reset;
  149. }
  150. }
  151. else if(c == EOF)
  152. {
  153. free(current);
  154. return c;
  155. }
  156. else
  157. {
  158. c = consume_byte(c);
  159. }
  160. /* More efficiently allocate memory for string */
  161. current->s = calloc(string_index + 2, sizeof(char));
  162. copy_string(current->s, hold_string);
  163. current->prev = token;
  164. current->next = token;
  165. current->linenumber = line;
  166. current->filename = file;
  167. token = current;
  168. return c;
  169. }
  170. struct token_list* reverse_list(struct token_list* head)
  171. {
  172. struct token_list* root = NULL;
  173. while(NULL != head)
  174. {
  175. struct token_list* next = head->next;
  176. head->next = root;
  177. root = head;
  178. head = next;
  179. }
  180. return root;
  181. }
  182. struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* filename)
  183. {
  184. input = a;
  185. line = 1;
  186. file = filename;
  187. token = current;
  188. int ch =fgetc(input);
  189. while(EOF != ch) ch = get_token(ch);
  190. return token;
  191. }