mes_read.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. * Copyright © 2019 Jeremiah Orians
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "mes.h"
  22. /****************************************************
  23. * Clear out everything between #!..!# and #|..|# *
  24. ****************************************************/
  25. void reader_read_block_comment(FILE* source_file, int match)
  26. {
  27. int last = 0;
  28. int current = fgetc(source_file);
  29. while((match != last) || ('#' != current))
  30. {
  31. last = current;
  32. current = fgetc(source_file);
  33. }
  34. }
  35. /****************************************************
  36. * Deal with #:foo and #;( foo ..) S-Expressions *
  37. ****************************************************/
  38. void reader_s_expression_dump(FILE* source_file)
  39. {
  40. int c =fgetc(source_file);
  41. unsigned depth = 0;
  42. while(TRUE)
  43. {
  44. if((-1 == c) || (4 == c))
  45. {
  46. return;
  47. }
  48. else if((0 == depth) && (('\n'== c) || ('\r' == c) || (' ' == c) || ('\t' == c)))
  49. {
  50. return;
  51. }
  52. else if('(' == c)
  53. {
  54. depth = depth + 1;
  55. }
  56. else if(')' == c)
  57. {
  58. depth = depth - 1;
  59. if(0 == depth) return;
  60. }
  61. c = fgetc(source_file);
  62. }
  63. }
  64. /****************************************************
  65. * Do the heavy lifting of reading an s-expression *
  66. ****************************************************/
  67. unsigned Readline(FILE* source_file, char* temp, unsigned max_string)
  68. {
  69. int c;
  70. unsigned i = 0;
  71. unsigned depth = 0;
  72. int hashed = FALSE;
  73. int escape = FALSE;
  74. for(i = 0; i < max_string; i = i + 1)
  75. {
  76. restart_comment:
  77. c = fgetc(source_file);
  78. restart_paren:
  79. if((-1 == c) || (4 == c))
  80. {
  81. return i;
  82. }
  83. else if('#' == c)
  84. {
  85. hashed = TRUE;
  86. goto restart_comment;
  87. }
  88. else if (hashed && (('!' == c) || ('|' == c)))
  89. {
  90. reader_read_block_comment(source_file, c);
  91. hashed = FALSE;
  92. goto restart_comment;
  93. }
  94. else if(('\'' == c) || ('`' == c))
  95. {
  96. temp[i] = c;
  97. temp[i+1] = ' ';
  98. i = i + 1;
  99. }
  100. else if(',' == c)
  101. {
  102. c = fgetc(source_file);
  103. temp[i] = ',';
  104. i = i + 1;
  105. if('@' == c)
  106. {
  107. temp[i] = '@';
  108. i = i + i;
  109. c = ' ';
  110. }
  111. temp[i] = ' ';
  112. i = i + 1;
  113. goto restart_paren;
  114. }
  115. else if(hashed && (';' == c))
  116. {
  117. reader_s_expression_dump(source_file);
  118. hashed = FALSE;
  119. c = ' ';
  120. goto restart_paren;
  121. }
  122. else if(';' == c)
  123. {
  124. /* drop everything until we hit newline */
  125. while('\n' != c)
  126. {
  127. c = fgetc(source_file);
  128. }
  129. goto restart_comment;
  130. }
  131. else if('"' == c)
  132. { /* Deal with strings */
  133. do
  134. {
  135. if(!escape && '\\' == c ) escape = TRUE;
  136. else escape = FALSE;
  137. temp[i] = c;
  138. i = i + 1;
  139. c = fgetc(source_file);
  140. } while('"' != c || escape);
  141. temp[i] = c;
  142. }
  143. else if((0 == depth) && (('\n' == c) || ('\r' == c) || (' ' == c) || ('\t' == c)))
  144. {
  145. goto Line_complete;
  146. }
  147. else if(('(' == c) || (')' == c))
  148. {
  149. if('(' == c)
  150. {
  151. depth = depth + 1;
  152. }
  153. if(')' == c)
  154. {
  155. depth = depth - 1;
  156. }
  157. if(hashed)
  158. {
  159. temp[i] = '#';
  160. temp[i+1] = c;
  161. temp[i+2] = ' ';
  162. i = i + 2;
  163. hashed = FALSE;
  164. }
  165. else
  166. {
  167. temp[i] = ' ';
  168. temp[i+1] = c;
  169. temp[i+2] = ' ';
  170. i = i + 2;
  171. }
  172. c = ' ';
  173. goto restart_paren;
  174. }
  175. else if(hashed)
  176. {
  177. temp[i] = '#';
  178. temp[i+1] = c;
  179. i = i + 1;
  180. hashed = FALSE;
  181. }
  182. else
  183. {
  184. temp[i] = c;
  185. }
  186. }
  187. Line_complete:
  188. if(0 == i)
  189. {
  190. return Readline(source_file, temp, max_string);
  191. }
  192. return i;
  193. }