interprete.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #define false 0
  2. #define true 1
  3. #define bool int
  4. // the type of data(GWARF_value)
  5. typedef enum{
  6. NUMBER_value = 1,
  7. STRING_value,
  8. } GWARF_value_type;
  9. // all value is GWARF_value
  10. typedef struct GWARF_value{
  11. GWARF_value_type type;
  12. union
  13. {
  14. double double_value; // NUMBER
  15. char *string; // STRING
  16. } value;
  17. } GWARF_value;
  18. // ------------------------- var
  19. typedef struct var{
  20. char *name; // var name
  21. GWARF_value value;
  22. struct var *next; // for list
  23. } var;
  24. // ------------------------- statement
  25. typedef struct statement{
  26. enum statement_type{
  27. start=1, // for base statement
  28. operation, // such as + - * /
  29. base_var, // return var address
  30. base_value, // return an number or number
  31. while_cycle, // while
  32. if_branch, // if
  33. break_cycle, // break
  34. broken, // break_cycle and other {}
  35. continue_cycle,
  36. continued,
  37. restart,
  38. restarted,
  39. rego,
  40. rewent,
  41. } type; // the statement type
  42. union
  43. {
  44. struct{
  45. enum{
  46. ADD_func = 1, // +
  47. SUB_func, // -
  48. DIV_func, // /
  49. MUL_func, // *
  50. ASSIGMENT_func, // =
  51. EQUAL_func, // ==
  52. MORE_func, // >
  53. LESS_func, // <
  54. MOREEQ_func, // >=
  55. LESSEQ_func, // <=
  56. NOTEQ_func, // <>
  57. } type;
  58. struct statement *right_exp; // the right exp
  59. struct statement *left_exp; // the left exp
  60. } operation;
  61. struct{
  62. struct statement *condition; // when to while
  63. struct statement *done; // while to do
  64. } while_cycle;
  65. struct{
  66. struct if_list *done; // if_list
  67. } if_branch;
  68. struct{
  69. char *var_name; // return var
  70. struct statement *from; // from where [double->int]
  71. } base_var;
  72. struct{
  73. GWARF_value value; // return value
  74. } base_value;
  75. struct{
  76. struct statement *times; // 层数
  77. } break_cycle;
  78. struct{
  79. struct statement *times; // 层数
  80. } broken;
  81. struct{
  82. struct statement *times; // 层数
  83. } continue_cycle;
  84. struct{
  85. struct statement *times; // 层数
  86. } continued;
  87. struct{
  88. struct statement *times; // 层数
  89. } restart;
  90. struct{
  91. struct statement *times; // 层数
  92. } restarted;
  93. struct{
  94. } rego;
  95. struct{
  96. } rewent;
  97. } code;
  98. struct statement *next;
  99. } statement;
  100. // ------------------------- result value
  101. typedef struct GWARF_result{
  102. GWARF_value value;
  103. enum{
  104. return_def=1,
  105. statement_end,
  106. cycle_break,
  107. code_broken,
  108. cycle_continue,
  109. code_continued,
  110. cycle_restart,
  111. code_restarted,
  112. code_rego,
  113. code_rewent,
  114. name_no_found,
  115. } u; // the result type[from where]
  116. } GWARF_result;
  117. // ------------------------- var base list [记录每一层变量base的链表]
  118. typedef struct var_list{
  119. var *var_base;
  120. struct var_list *next;
  121. } var_list;
  122. // ------------------------- inter paser [记录每一层变量code的链表]
  123. typedef struct statement_list{
  124. statement *statement_base;
  125. struct statement_list *next;
  126. } statement_list;
  127. // ------------------------- if list [记录着if...elif...else]
  128. typedef struct if_list{
  129. struct statement *condition; // when to while
  130. struct statement *done; // while to do
  131. struct if_list *next;
  132. } if_list;
  133. // ------------------------- inter
  134. typedef struct{
  135. var *global_var; // global var链表
  136. statement *global_code; // global code链表
  137. } inter;
  138. //------- var func
  139. var *make_var();
  140. void append_var(char *, GWARF_value , var *);
  141. void free_var(var *);
  142. var *get_var(char *, var *);
  143. void del_var(char *, var *);
  144. //------- statement func
  145. statement *make_statement();
  146. statement *append_statement(statement *, statement*);
  147. statement_list *make_statement_list();
  148. statement_list *make_statement_base(statement *);
  149. statement_list *append_statement_list(statement *, statement_list *);
  150. statement *find_statement_list(int, statement_list *);
  151. statement_list *free_statement_list(statement_list *);
  152. //------- if func
  153. if_list *make_base_if();
  154. if_list *make_if(statement *, statement *);
  155. if_list *append_elif(if_list *, if_list *);
  156. //------- run func
  157. GWARF_result traverse(statement *, var_list *, bool);
  158. //------- inter func
  159. inter *get_inter();
  160. // //------ paser func
  161. int yyerror(char const *);
  162. FILE *yyin;
  163. char *yytext;
  164. // main
  165. inter *global_inter;
  166. statement_list *statement_base;