interprete.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include <stdio.h>
  2. #define false 0
  3. #define true 1
  4. #define bool int
  5. #define read_statement_list(the_statement, the_var) read_statement(the_statement, the_var, NULL)
  6. typedef enum{
  7. customize = 1, // func by user
  8. official, // func by gwarf
  9. } func_type;
  10. typedef enum{
  11. printf_func = 1, // print_func
  12. } official_func_type;
  13. typedef struct func{
  14. func_type type;
  15. official_func_type official_func;
  16. struct parameter *parameter_list; // def parameter
  17. struct statement *done; // def to do
  18. struct var_list *the_var; // func会记录the_var,因为不同地方调用var如果var链不统一那就会很乱
  19. int is_class;
  20. } func;
  21. typedef struct class_object{
  22. struct var_list *out_var; // 外部the_var list
  23. struct var_list *the_var; // 记录class_object的 -- 相当与cls
  24. } class_object;
  25. typedef struct the_object{
  26. struct var_list *cls; // 记录class_object的 -- 相当与cls
  27. struct var_list *the_var; // 记录class_object的实例 -- 相当与self
  28. } the_object;
  29. // the type of data(GWARF_value)
  30. typedef enum{
  31. NUMBER_value = 1,
  32. INT_value, // INT 类型
  33. BOOL_value, // bool : true or false
  34. STRING_value, // char *
  35. NULL_value,
  36. FUNC_value,
  37. CLASS_value,
  38. OBJECT_value,
  39. } GWARF_value_type;
  40. // all value is GWARF_value
  41. typedef struct GWARF_value{
  42. GWARF_value_type type;
  43. union
  44. {
  45. double double_value; // NUMBER
  46. int int_value;
  47. bool bool_value;
  48. char *string; // STRING
  49. func *func_value;
  50. class_object *class_value;
  51. the_object *object_value;
  52. } value;
  53. } GWARF_value;
  54. // ------------------------- parameter for def
  55. typedef struct parameter{
  56. union
  57. {
  58. char *name; // var name
  59. struct statement *value; // or value
  60. } u;
  61. struct parameter *next; // for list
  62. } parameter;
  63. // ------------------------- var
  64. typedef struct var{
  65. char *name; // var name
  66. GWARF_value value;
  67. struct var *next; // for list
  68. } var;
  69. // ------------------------- statement
  70. typedef struct statement{
  71. enum statement_type{
  72. start=1, // for base statement
  73. operation, // such as + - * /
  74. base_var, // return var value by name
  75. base_value, // return an number or number
  76. while_cycle, // while
  77. for_cycle,
  78. if_branch, // if
  79. break_cycle, // break
  80. broken, // break_cycle and other {}
  81. continue_cycle,
  82. continued,
  83. restart,
  84. restarted,
  85. rego,
  86. rewent,
  87. set_default,
  88. set_global,
  89. set_nonlocal,
  90. code_block,
  91. def, // func
  92. call, // func()
  93. point, // a.b 注:返回变量同时返回the_var链表[func 用于回调]
  94. return_code,
  95. set_class, // class aaa; b = aaa() is ```call```
  96. } type; // the statement type
  97. union
  98. {
  99. struct{
  100. enum{
  101. ADD_func = 1, // +
  102. SUB_func, // -
  103. DIV_func, // /
  104. MUL_func, // *
  105. ASSIGMENT_func, // =
  106. EQUAL_func, // ==
  107. MORE_func, // >
  108. LESS_func, // <
  109. MOREEQ_func, // >=
  110. LESSEQ_func, // <=
  111. NOTEQ_func, // <>
  112. POW_func, // <>
  113. LOG_func, // <>
  114. SQRT_func, // <>
  115. NEGATIVE_func, // -a
  116. } type;
  117. struct statement *right_exp; // the right exp
  118. struct statement *left_exp; // the left exp
  119. } operation;
  120. struct{
  121. struct statement *condition; // when to while
  122. struct statement *done; // while to do
  123. } while_cycle;
  124. struct{
  125. struct statement *first; // the first to do
  126. struct statement *condition; // when to while
  127. struct statement *after; // what to do after the done
  128. struct statement *done; // while to do
  129. } for_cycle;
  130. struct{
  131. struct if_list *done; // if_list
  132. } if_branch;
  133. struct{
  134. char *var_name; // return var
  135. struct statement *from; // from where [double->int]
  136. } base_var;
  137. struct{
  138. struct statement *base_var; // a.b --> a
  139. struct statement *child_var; // a.b --> b
  140. } point;
  141. struct{
  142. GWARF_value value; // return value
  143. } base_value;
  144. struct{
  145. struct statement *times; // 层数
  146. } break_cycle;
  147. struct{
  148. struct statement *times; // 层数
  149. } broken;
  150. struct{
  151. struct statement *times; // 层数
  152. } continue_cycle;
  153. struct{
  154. struct statement *times; // 层数
  155. } continued;
  156. struct{
  157. struct statement *times; // 层数
  158. } restart;
  159. struct{
  160. struct statement *times; // 层数
  161. } restarted;
  162. struct{
  163. } rego;
  164. struct{
  165. } rewent;
  166. struct{
  167. char *name;
  168. struct statement *times; // 层数
  169. } set_default;
  170. struct{
  171. char *name;
  172. } set_global;
  173. struct{
  174. char *name;
  175. } set_nonlocal;
  176. struct{
  177. struct statement *done; // block to do
  178. } code_block;
  179. struct{
  180. char *name;
  181. parameter *parameter_list; // def parameter
  182. struct statement *done; // def to do
  183. } def;
  184. struct{
  185. parameter *parameter_list; // def parameter
  186. struct statement *func; // get func value
  187. } call;
  188. struct{
  189. struct statement *times; // 层数
  190. struct statement *value; // return value
  191. } return_code;
  192. struct{
  193. char *name; // class name
  194. struct statement *done; // class to do
  195. } set_class;
  196. } code;
  197. struct statement *next;
  198. } statement;
  199. // ------------------------- result value
  200. typedef struct GWARF_result{
  201. GWARF_value value;
  202. GWARF_value *father; // a.b --> a
  203. enum{
  204. return_def=1,
  205. statement_end,
  206. cycle_break,
  207. code_broken,
  208. cycle_continue,
  209. code_continued,
  210. cycle_restart,
  211. code_restarted,
  212. code_rego,
  213. code_rewent,
  214. code_return,
  215. name_no_found,
  216. } u; // the result type[from where]
  217. int return_times; // return用
  218. } GWARF_result;
  219. // ------------------------- default_var [记录默认变量[层]] 用于default语句
  220. typedef struct default_var{
  221. char *name;
  222. int from;
  223. struct default_var *next;
  224. } default_var;
  225. // ------------------------- var base list [记录每一层变量base的链表]
  226. typedef struct var_list{
  227. var *var_base;
  228. default_var *default_list;
  229. struct var_list *next;
  230. } var_list;
  231. // ------------------------- inter paser [记录每一层变量code的链表]
  232. typedef struct statement_list{
  233. statement *statement_base;
  234. struct statement_list *next;
  235. } statement_list;
  236. // ------------------------- if list [记录着if...elif...else]
  237. typedef struct if_list{
  238. struct statement *condition; // when to while
  239. struct statement *done; // while to do
  240. struct if_list *next;
  241. } if_list;
  242. // ------------------------- inter
  243. typedef struct{
  244. var *global_var; // global var链表
  245. statement *global_code; // global code链表
  246. } inter;
  247. //------- var func
  248. var *make_var();
  249. void append_var(char *, GWARF_value , var *);
  250. void free_var(var *);
  251. var *get_var(char *, var *);
  252. void del_var(char *, var *);
  253. default_var *make_default_var();
  254. default_var *make_default_var_base();
  255. void append_default_var_base(char * ,int , default_var *);
  256. int get_default(char *, default_var *);
  257. //------- statement func
  258. statement *make_statement();
  259. statement *append_statement(statement *, statement*);
  260. statement_list *make_statement_list();
  261. statement_list *make_statement_base(statement *);
  262. statement_list *append_statement_list(statement *, statement_list *);
  263. statement *find_statement_list(int, statement_list *);
  264. statement_list *free_statement_list(statement_list *);
  265. //------- if func
  266. if_list *make_base_if();
  267. if_list *make_if(statement *, statement *);
  268. if_list *append_elif(if_list *, if_list *);
  269. //------- run func
  270. GWARF_result traverse(statement *, var_list *, bool);
  271. GWARF_result traverse_global(statement *, var_list *);
  272. //------- inter func
  273. inter *get_inter();
  274. // //------ paser func
  275. int yyerror(char const *);
  276. FILE *yyin;
  277. char *yytext;
  278. // ---- parameter func[形参]
  279. parameter *make_parameter_name(char *);
  280. void append_parameter_name(char *, parameter *);
  281. // ---- parameter func[实参]
  282. parameter *make_parameter_value(statement *);
  283. void append_parameter_value(statement *, parameter *);
  284. parameter *add_parameter_value(statement *, parameter *);
  285. // main
  286. inter *global_inter;
  287. statement_list *statement_base;
  288. void login_official_func(int, int, var_list *, char *);
  289. void login_official(var_list *);