lexical.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "__virtualmath.h"
  2. /**
  3. * 从文件中读取一个字节,并处理is_back
  4. * 每次从文件中读取字符时,则会保存字符到back.p中,调用backChar回退一个字符的时候则不需要传入字符了
  5. * @param file
  6. * @return 返回一个字符,若为EOF则返回-1
  7. */
  8. wint_t readChar(LexFile *file){
  9. if (file->back.is_back)
  10. file->back.is_back = false;
  11. else {
  12. if (file->status == 2) {
  13. file->back.p = file->str[file->seek];
  14. if (file->back.p != NUL)
  15. file->seek ++;
  16. else
  17. file->back.p = WEOF;
  18. } else
  19. file->back.p = fgetwc(file->file);
  20. }
  21. if (file->back.p == L'\n')
  22. file->line++;
  23. return file->back.p;
  24. }
  25. /**
  26. * 设置字符回退
  27. * @param file
  28. */
  29. void backChar(LexFile *file){
  30. file->back.is_back = true;
  31. if (file->back.p == L'\n')
  32. file->line --;
  33. }
  34. void clearLexFile(LexFile *file) {
  35. wint_t ch;
  36. backChar(file);
  37. while ((ch = readChar(file)) != L'\n' && ch != WEOF)
  38. PASS;
  39. }
  40. static LexFile *makeLexCore(){
  41. LexFile *tmp = memCalloc(1, sizeof(LexFile));
  42. tmp->status = 1;
  43. tmp->file = stdin;
  44. tmp->str = NULL;
  45. tmp->seek = 0;
  46. tmp->back.is_back = false;
  47. tmp->back.p = WEOF;
  48. tmp->line = 1;
  49. tmp->filter_data.enter = 0;
  50. return tmp;
  51. }
  52. LexFile *makeLexFile(char *dir){
  53. LexFile *tmp = makeLexCore();
  54. tmp->status = dir == NULL ? 1 : 0;
  55. if (dir != NULL) {
  56. tmp->file = fopen(dir, "r");
  57. tmp->status = 0;
  58. }
  59. tmp->errsyntax = NULL;
  60. return tmp;
  61. }
  62. LexFile *makeLexStr(wchar_t *str){
  63. LexFile *tmp = makeLexCore();
  64. tmp->status = 2;
  65. tmp->file = NULL;
  66. tmp->str = memWidecpy(str);
  67. return tmp;
  68. }
  69. void freeLexFile(LexFile *file) {
  70. FREE_BASE(file, return_);
  71. if (file->status == 0)
  72. fclose(file->file);
  73. memFree(file->str);
  74. memFree(file);
  75. return_:
  76. return;
  77. }
  78. /**
  79. * 初始化mather,代码被复用
  80. * @param mather
  81. */
  82. void setupMather(LexMather *mather){
  83. mather->len = 0;
  84. mather->ascii = 0;
  85. mather->str = NULL;
  86. mather->second_str = NULL;
  87. mather->string_type = L'"';
  88. mather->status = LEXMATHER_START;
  89. }
  90. LexMather *makeMather(){
  91. LexMather *tmp = memCalloc(1, sizeof(LexMather));
  92. setupMather(tmp);
  93. return tmp;
  94. }
  95. void freeMather(LexMather *mather) {
  96. memFree(mather->str);
  97. memFree(mather->second_str);
  98. mather->len = 0;
  99. memFree(mather);
  100. }
  101. LexMathers *makeMathers(int size){
  102. LexMathers *tmp = memCalloc(1, sizeof(LexMathers));
  103. tmp->size = size;
  104. tmp->mathers = (struct LexMather**)memCalloc(size, sizeof(LexMather*));
  105. for(int i=0;i < size; i++)
  106. tmp->mathers[i] = makeMather();
  107. return tmp;
  108. }
  109. void freeMathers(LexMathers *mathers) {
  110. FREE_BASE(mathers, return_);
  111. for(int i=0;i < mathers->size; i++)
  112. freeMather(mathers->mathers[i]);
  113. memFree(mathers->mathers);
  114. mathers->size = 0;
  115. memFree(mathers);
  116. return_:
  117. return;
  118. }
  119. /**
  120. * 初始化mathers,本质是初始化mathers.mathers内所有的mather
  121. * @param mathers
  122. */
  123. void setupMathers(LexMathers *mathers){
  124. for (int i=0;i < mathers->size;i++){
  125. if(mathers->mathers[i]->str != NULL){
  126. memFree(mathers->mathers[i]->str);
  127. memFree(mathers->mathers[i]->second_str);
  128. }
  129. setupMather(mathers->mathers[i]);
  130. }
  131. }
  132. /**
  133. * 检查mathers中mather的匹配情况。
  134. * 情况1:只出现一个匹配器处于END状态,其他均处于MISTAKE或者END_SECOND状态,则视为匹配成功,返回END状态的匹配器
  135. * 情况2:只出现一个匹配器处于END_SECOND状态,其他均处于MISTAKE状态无END状态,则视为匹配成功,返回END_SECOND状态的匹配器
  136. * 情况3:全部都在MISTAKE,返回-2,匹配失败
  137. * 其他情况:匹配还未完成,返回-1
  138. * @param mathers
  139. * @param max
  140. * @return
  141. */
  142. int checkoutMather(LexMathers *mathers, int max) {
  143. bool return_1 = false;
  144. int mistake_count = 0;
  145. int end_count = 0, end_index = -1;
  146. int end_second_count = 0, end_second_index = -1;
  147. for (int i=0;i < mathers->size;i++){
  148. if(mathers->mathers[i]->status == LEXMATHER_END_1){
  149. end_count ++;
  150. end_index = i;
  151. }
  152. else if(mathers->mathers[i]->status == LEXMATHER_END_2){
  153. end_second_count ++;
  154. end_second_index = i;
  155. }
  156. else if(mathers->mathers[i]->status == LEXMATHER_MISTAKE)
  157. mistake_count ++;
  158. else
  159. return_1 = true;
  160. }
  161. if (return_1)
  162. goto return_;
  163. if (mistake_count == max)
  164. return -2;
  165. else if(end_count == 1)
  166. return end_index;
  167. else if(end_second_count == 1)
  168. return end_second_index;
  169. return_:
  170. return -1;
  171. }