interprete.h 11 KB

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