cc_reader.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * Copyright (C) 2021 Andrius Štikonas <andrius@stikonas.eu>
  3. * This file is part of M2-Planet.
  4. *
  5. * M2-Planet is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * M2-Planet is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "cc.h"
  19. int strtoint(char *a);
  20. /* Globals */
  21. FILE* input;
  22. struct token_list* token;
  23. int line;
  24. char* file;
  25. int grab_byte(void)
  26. {
  27. int c = fgetc(input);
  28. if(10 == c) line = line + 1;
  29. return c;
  30. }
  31. int clearWhiteSpace(int c)
  32. {
  33. if((32 == c) || (9 == c)) return clearWhiteSpace(grab_byte());
  34. return c;
  35. }
  36. int consume_byte(int c)
  37. {
  38. hold_string[string_index] = c;
  39. string_index = string_index + 1;
  40. require(MAX_STRING > string_index, "Token exceeded MAX_STRING char limit\nuse --max-string number to increase\n");
  41. return grab_byte();
  42. }
  43. int preserve_string(int c)
  44. {
  45. int frequent = c;
  46. int escape = FALSE;
  47. do
  48. {
  49. if(!escape && '\\' == c ) escape = TRUE;
  50. else escape = FALSE;
  51. c = consume_byte(c);
  52. require(EOF != c, "Unterminated string\n");
  53. } while(escape || (c != frequent));
  54. return grab_byte();
  55. }
  56. void copy_string(char* target, char* source, int max)
  57. {
  58. int i = 0;
  59. while(0 != source[i])
  60. {
  61. target[i] = source[i];
  62. i = i + 1;
  63. if(i == max) break;
  64. }
  65. }
  66. void fixup_label(void)
  67. {
  68. int hold = ':';
  69. int prev;
  70. int i = 0;
  71. do
  72. {
  73. prev = hold;
  74. hold = hold_string[i];
  75. hold_string[i] = prev;
  76. i = i + 1;
  77. } while(0 != hold);
  78. }
  79. int preserve_keyword(int c, char* S)
  80. {
  81. while(in_set(c, S))
  82. {
  83. c = consume_byte(c);
  84. }
  85. return c;
  86. }
  87. void reset_hold_string(void)
  88. {
  89. int i = MAX_STRING;
  90. while(0 <= i)
  91. {
  92. hold_string[i] = 0;
  93. i = i - 1;
  94. }
  95. string_index = 0;
  96. }
  97. /* note if this is the first token in the list, head needs fixing up */
  98. struct token_list* eat_token(struct token_list* token)
  99. {
  100. if(NULL != token->prev)
  101. {
  102. token->prev->next = token->next;
  103. }
  104. /* update backlinks */
  105. if(NULL != token->next)
  106. {
  107. token->next->prev = token->prev;
  108. }
  109. return token->next;
  110. }
  111. struct token_list* eat_until_newline(struct token_list* head)
  112. {
  113. while (NULL != head)
  114. {
  115. if('\n' == head->s[0])
  116. {
  117. return head;
  118. }
  119. else
  120. {
  121. head = eat_token(head);
  122. }
  123. }
  124. return NULL;
  125. }
  126. struct token_list* remove_line_comments(struct token_list* head)
  127. {
  128. struct token_list* first = NULL;
  129. while (NULL != head)
  130. {
  131. if(match("//", head->s))
  132. {
  133. head = eat_until_newline(head);
  134. }
  135. else
  136. {
  137. if(NULL == first)
  138. {
  139. first = head;
  140. }
  141. head = head->next;
  142. }
  143. }
  144. return first;
  145. }
  146. struct token_list* remove_line_comment_tokens(struct token_list* head)
  147. {
  148. struct token_list* first = NULL;
  149. while (NULL != head)
  150. {
  151. if(match("//", head->s))
  152. {
  153. head = eat_token(head);
  154. }
  155. else
  156. {
  157. if(NULL == first)
  158. {
  159. first = head;
  160. }
  161. head = head->next;
  162. }
  163. }
  164. return first;
  165. }
  166. struct token_list* remove_preprocessor_directives(struct token_list* head)
  167. {
  168. struct token_list* first = NULL;
  169. while (NULL != head)
  170. {
  171. if('#' == head->s[0])
  172. {
  173. head = eat_until_newline(head);
  174. }
  175. else
  176. {
  177. if(NULL == first)
  178. {
  179. first = head;
  180. }
  181. head = head->next;
  182. }
  183. }
  184. return first;
  185. }
  186. void new_token(char* s, int size)
  187. {
  188. struct token_list* current = calloc(1, sizeof(struct token_list));
  189. require(NULL != current, "Exhausted memory while getting token\n");
  190. /* More efficiently allocate memory for string */
  191. current->s = calloc(size, sizeof(char));
  192. require(NULL != current->s, "Exhausted memory while trying to copy a token\n");
  193. copy_string(current->s, s, MAX_STRING);
  194. current->prev = token;
  195. current->next = token;
  196. current->linenumber = line;
  197. current->filename = file;
  198. token = current;
  199. }
  200. int get_token(int c)
  201. {
  202. struct token_list* current = calloc(1, sizeof(struct token_list));
  203. require(NULL != current, "Exhausted memory while getting token\n");
  204. reset:
  205. reset_hold_string();
  206. string_index = 0;
  207. c = clearWhiteSpace(c);
  208. if(c == EOF)
  209. {
  210. free(current);
  211. return c;
  212. }
  213. else if('#' == c)
  214. {
  215. c = consume_byte(c);
  216. c = preserve_keyword(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
  217. }
  218. else if(in_set(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"))
  219. {
  220. c = preserve_keyword(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
  221. if(':' == c)
  222. {
  223. fixup_label();
  224. c = ' ';
  225. }
  226. }
  227. else if(in_set(c, "<=>|&!^%"))
  228. {
  229. c = preserve_keyword(c, "<=>|&!^%");
  230. }
  231. else if(in_set(c, "'\""))
  232. {
  233. c = preserve_string(c);
  234. }
  235. else if(c == '/')
  236. {
  237. c = consume_byte(c);
  238. if(c == '*')
  239. {
  240. c = grab_byte();
  241. while(c != '/')
  242. {
  243. while(c != '*')
  244. {
  245. c = grab_byte();
  246. require(EOF != c, "Hit EOF inside of block comment\n");
  247. }
  248. c = grab_byte();
  249. require(EOF != c, "Hit EOF inside of block comment\n");
  250. }
  251. c = grab_byte();
  252. goto reset;
  253. }
  254. else if(c == '/')
  255. {
  256. c = consume_byte(c);
  257. }
  258. else if(c == '=')
  259. {
  260. c = consume_byte(c);
  261. }
  262. }
  263. else if (c == '\n')
  264. {
  265. c = consume_byte(c);
  266. }
  267. else if(c == '*')
  268. {
  269. c = consume_byte(c);
  270. if(c == '=')
  271. {
  272. c = consume_byte(c);
  273. }
  274. }
  275. else if(c == '+')
  276. {
  277. c = consume_byte(c);
  278. if(c == '=')
  279. {
  280. c = consume_byte(c);
  281. }
  282. if(c == '+')
  283. {
  284. c = consume_byte(c);
  285. }
  286. }
  287. else if(c == '-')
  288. {
  289. c = consume_byte(c);
  290. if(c == '=')
  291. {
  292. c = consume_byte(c);
  293. }
  294. if(c == '>')
  295. {
  296. c = consume_byte(c);
  297. }
  298. if(c == '-')
  299. {
  300. c = consume_byte(c);
  301. }
  302. }
  303. else
  304. {
  305. c = consume_byte(c);
  306. }
  307. new_token(hold_string, string_index + 2);
  308. return c;
  309. }
  310. int consume_filename(int c)
  311. {
  312. reset_hold_string();
  313. int done = FALSE;
  314. while(!done)
  315. {
  316. if(c == EOF)
  317. {
  318. fputs("we don't support EOF as a filename in #FILENAME statements\n", stderr);
  319. exit(EXIT_FAILURE);
  320. }
  321. else if((32 == c) || (9 == c) || (c == '\n'))
  322. {
  323. c = grab_byte();
  324. }
  325. else
  326. {
  327. do
  328. {
  329. c = consume_byte(c);
  330. require(EOF != c, "Unterminated filename in #FILENAME\n");
  331. } while((32 != c) && (9 != c) && ('\n' != c));
  332. done = TRUE;
  333. }
  334. }
  335. /* with just a little extra to put in the matching at the end */
  336. new_token(hold_string, string_index + 3);
  337. return c;
  338. }
  339. int change_filename(int ch)
  340. {
  341. require(EOF != ch, "#FILENAME failed to receive filename\n");
  342. /* Remove the #FILENAME */
  343. token = token->next;
  344. /* Get new filename */
  345. ch = consume_filename(ch);
  346. file = token->s;
  347. /* Remove it from the processing list */
  348. token = token->next;
  349. require(EOF != ch, "#FILENAME failed to receive filename\n");
  350. /* Get new line number */
  351. ch = get_token(ch);
  352. line = strtoint(token->s);
  353. if(0 == line)
  354. {
  355. if('0' != token->s[0])
  356. {
  357. fputs("non-line number: ", stderr);
  358. fputs(token->s, stderr);
  359. fputs(" provided to #FILENAME\n", stderr);
  360. exit(EXIT_FAILURE);
  361. }
  362. }
  363. /* Remove it from the processing list */
  364. token = token->next;
  365. return ch;
  366. }
  367. struct token_list* reverse_list(struct token_list* head)
  368. {
  369. struct token_list* root = NULL;
  370. struct token_list* next;
  371. while(NULL != head)
  372. {
  373. next = head->next;
  374. head->next = root;
  375. root = head;
  376. head = next;
  377. }
  378. return root;
  379. }
  380. struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* filename)
  381. {
  382. input = a;
  383. line = 1;
  384. file = filename;
  385. token = current;
  386. int ch = grab_byte();
  387. while(EOF != ch)
  388. {
  389. ch = get_token(ch);
  390. require(NULL != token, "Empty files don't need to be compiled\n");
  391. if(match("#FILENAME", token->s)) ch = change_filename(ch);
  392. }
  393. return token;
  394. }