cc_reader.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. /* Globals */
  19. FILE* input;
  20. struct token_list* token;
  21. int line;
  22. char* file;
  23. int clearWhiteSpace(int c)
  24. {
  25. if((32 == c) || (9 == c)) return clearWhiteSpace(fgetc(input));
  26. return c;
  27. }
  28. int consume_byte(int c)
  29. {
  30. hold_string[string_index] = c;
  31. string_index = string_index + 1;
  32. require(MAX_STRING > string_index, "Token exceeded MAX_STRING char limit\nuse --max-string number to increase\n");
  33. return fgetc(input);
  34. }
  35. int preserve_string(int c)
  36. {
  37. int frequent = c;
  38. int escape = FALSE;
  39. do
  40. {
  41. if(!escape && '\\' == c ) escape = TRUE;
  42. else escape = FALSE;
  43. c = consume_byte(c);
  44. require(EOF != c, "Unterminated string\n");
  45. } while(escape || (c != frequent));
  46. return fgetc(input);
  47. }
  48. void fixup_label()
  49. {
  50. int hold = ':';
  51. int prev;
  52. int i = 0;
  53. do
  54. {
  55. prev = hold;
  56. hold = hold_string[i];
  57. hold_string[i] = prev;
  58. i = i + 1;
  59. } while(0 != hold);
  60. }
  61. int preserve_keyword(int c, char* S)
  62. {
  63. while(in_set(c, S))
  64. {
  65. c = consume_byte(c);
  66. }
  67. return c;
  68. }
  69. void reset_hold_string()
  70. {
  71. int i = string_index + 2;
  72. while(0 != i)
  73. {
  74. hold_string[i] = 0;
  75. i = i - 1;
  76. }
  77. }
  78. /* note if this is the first token in the list, head needs fixing up */
  79. struct token_list* eat_token(struct token_list* token)
  80. {
  81. if(NULL != token->prev)
  82. {
  83. token->prev->next = token->next;
  84. }
  85. /* update backlinks */
  86. if(NULL != token->next)
  87. {
  88. token->next->prev = token->prev;
  89. }
  90. return token->next;
  91. }
  92. struct token_list* eat_until_newline(struct token_list* head)
  93. {
  94. while (NULL != head)
  95. {
  96. if('\n' == head->s[0])
  97. {
  98. return head;
  99. }
  100. else
  101. {
  102. head = eat_token(head);
  103. }
  104. }
  105. return NULL;
  106. }
  107. struct token_list* remove_line_comments(struct token_list* head)
  108. {
  109. struct token_list* first = NULL;
  110. while (NULL != head)
  111. {
  112. if(match("//", head->s))
  113. {
  114. head = eat_until_newline(head);
  115. }
  116. else
  117. {
  118. if(NULL == first)
  119. {
  120. first = head;
  121. }
  122. head = head->next;
  123. }
  124. }
  125. return first;
  126. }
  127. struct token_list* remove_line_comment_tokens(struct token_list* head)
  128. {
  129. struct token_list* first = NULL;
  130. while (NULL != head)
  131. {
  132. if(match("//", head->s))
  133. {
  134. head = eat_token(head);
  135. }
  136. else
  137. {
  138. if(NULL == first)
  139. {
  140. first = head;
  141. }
  142. head = head->next;
  143. }
  144. }
  145. return first;
  146. }
  147. struct token_list* remove_preprocessor_directives(struct token_list* head)
  148. {
  149. struct token_list* first = NULL;
  150. while (NULL != head)
  151. {
  152. if('#' == head->s[0])
  153. {
  154. head = eat_until_newline(head);
  155. }
  156. else
  157. {
  158. if(NULL == first)
  159. {
  160. first = head;
  161. }
  162. head = head->next;
  163. }
  164. }
  165. return first;
  166. }
  167. int get_token(int c)
  168. {
  169. struct token_list* current = calloc(1, sizeof(struct token_list));
  170. require(NULL != current, "Exhausted memory while getting token\n");
  171. reset:
  172. reset_hold_string();
  173. string_index = 0;
  174. c = clearWhiteSpace(c);
  175. if(c == EOF)
  176. {
  177. free(current);
  178. return c;
  179. }
  180. else if('#' == c)
  181. {
  182. c = consume_byte(c);
  183. c = preserve_keyword(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
  184. }
  185. else if(in_set(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"))
  186. {
  187. c = preserve_keyword(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
  188. if(':' == c)
  189. {
  190. fixup_label();
  191. c = ' ';
  192. }
  193. }
  194. else if(in_set(c, "<=>|&!-"))
  195. {
  196. c = preserve_keyword(c, "<=>|&!-");
  197. }
  198. else if(in_set(c, "'\""))
  199. {
  200. c = preserve_string(c);
  201. }
  202. else if(c == '/')
  203. {
  204. c = consume_byte(c);
  205. if(c == '*')
  206. {
  207. c = fgetc(input);
  208. while(c != '/')
  209. {
  210. while(c != '*')
  211. {
  212. c = fgetc(input);
  213. require(EOF != c, "Hit EOF inside of block comment\n");
  214. if('\n' == c) line = line + 1;
  215. }
  216. c = fgetc(input);
  217. require(EOF != c, "Hit EOF inside of block comment\n");
  218. if('\n' == c) line = line + 1;
  219. }
  220. c = fgetc(input);
  221. goto reset;
  222. }
  223. else if(c == '/')
  224. {
  225. c = consume_byte(c);
  226. }
  227. }
  228. else if (c == '\n')
  229. {
  230. line = line + 1;
  231. c = consume_byte(c);
  232. }
  233. else
  234. {
  235. c = consume_byte(c);
  236. }
  237. /* More efficiently allocate memory for string */
  238. current->s = calloc(string_index + 2, sizeof(char));
  239. require(NULL != current->s, "Exhusted memory while trying to copy a token\n");
  240. copy_string(current->s, hold_string, MAX_STRING);
  241. current->prev = token;
  242. current->next = token;
  243. current->linenumber = line;
  244. current->filename = file;
  245. token = current;
  246. return c;
  247. }
  248. struct token_list* reverse_list(struct token_list* head)
  249. {
  250. struct token_list* root = NULL;
  251. struct token_list* next;
  252. while(NULL != head)
  253. {
  254. next = head->next;
  255. head->next = root;
  256. root = head;
  257. head = next;
  258. }
  259. return root;
  260. }
  261. struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* filename)
  262. {
  263. input = a;
  264. line = 1;
  265. file = filename;
  266. token = current;
  267. int ch =fgetc(input);
  268. while(EOF != ch) ch = get_token(ch);
  269. return token;
  270. }