interpreter.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. parameter *father_list; // 继承
  173. } set_class;
  174. } code;
  175. struct statement *next;
  176. } statement;
  177. // ------------------------- result value
  178. typedef struct GWARF_result{
  179. GWARF_value value;
  180. GWARF_value *father; // a.b --> a
  181. enum{
  182. return_def=1,
  183. statement_end,
  184. cycle_break,
  185. code_broken,
  186. cycle_continue,
  187. code_continued,
  188. cycle_restart,
  189. code_restarted,
  190. code_rego,
  191. code_rewent,
  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. //------- class/object/func
  225. typedef enum{
  226. customize = 1, // func by user
  227. official, // func by gwarf
  228. } func_type;
  229. typedef enum{
  230. printf_func = 1, // print_func
  231. __init__func = 2,
  232. __value__func = 3,
  233. __add__func = 4,
  234. __sub__func = 5,
  235. __mul__func = 6,
  236. __div__func = 7,
  237. __eq__func = 8,
  238. __more__func = 9,
  239. __less__func = 10,
  240. __eqmore__func = 11,
  241. __eqless__func = 12,
  242. __noteq__func = 13,
  243. __pow__func = 14,
  244. __log__func = 15,
  245. __sqrt__func = 16,
  246. __negative__func = 17,
  247. } official_func_type;
  248. typedef struct func{
  249. func_type type;
  250. official_func_type official_func;
  251. struct GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var, GWARF_result, var_list *);
  252. struct parameter *parameter_list; // def parameter
  253. struct statement *done; // def to do
  254. struct var_list *the_var; // func会记录the_var,因为不同地方调用var如果var链不统一那就会很乱
  255. int is_class;
  256. } func;
  257. typedef struct class_object{
  258. struct var_list *out_var; // 外部the_var list
  259. struct var_list *the_var; // 记录class_object的 -- 相当与cls
  260. } class_object;
  261. typedef struct the_object{
  262. struct var_list *cls; // 记录class_object的 -- 相当与cls
  263. struct var_list *the_var; // 记录class_object的实例 -- 相当与self
  264. } the_object;
  265. // 函数声明
  266. GWARF_result operation_func(statement *, var_list *, var_list *);
  267. GWARF_result while_func(statement *, var_list *);
  268. GWARF_result if_func(if_list *, var_list *);
  269. GWARF_result for_func(statement *, var_list *);
  270. GWARF_result call_back(statement *, var_list *);
  271. GWARF_result call_back_core(GWARF_result, var_list *, parameter *);
  272. GWARF_result block_func(statement *, var_list *);
  273. GWARF_result add_func(GWARF_result, GWARF_result, var_list *);
  274. GWARF_result sub_func(GWARF_result, GWARF_result, var_list *);
  275. GWARF_result mul_func(GWARF_result, GWARF_result, var_list *);
  276. GWARF_result div_func(GWARF_result, GWARF_result, var_list *);
  277. GWARF_result pow_func(GWARF_result, GWARF_result, var_list *);
  278. GWARF_result log_func(GWARF_result, GWARF_result, var_list *);
  279. GWARF_result sqrt_func(GWARF_result, GWARF_result, var_list *);
  280. GWARF_result assigment_func(char *, GWARF_result, var_list *, int);
  281. GWARF_result equal_func(GWARF_result, GWARF_result, var_list *, int);
  282. GWARF_result negative_func(GWARF_result, var_list *);
  283. double sqrt_(double, double);
  284. double log_(double, double);
  285. GWARF_value to_int(GWARF_value, var_list *the_var);
  286. GWARF_value to_double(GWARF_value value, var_list *the_var);
  287. GWARF_value to_str(GWARF_value value, var_list *the_var);
  288. GWARF_value to_bool_(GWARF_value value, var_list *the_var);
  289. bool to_bool(GWARF_value);
  290. GWARF_result get__value__(GWARF_value *, var_list *);
  291. GWARF_result get__bool__(GWARF_value *, var_list *);
  292. GWARF_result run_func(GWARF_value *, var_list *, char *);
  293. int len_only_double(double num);
  294. int len_double(double num);
  295. int len_int(int num);
  296. int len_intx(unsigned int num);
  297. GWARF_value to_object(GWARF_value, var_list *);
  298. 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 *));
  299. void login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *));
  300. // 内置函数
  301. GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  302. // object内置类
  303. class_object *object_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *));
  304. GWARF_result object_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  305. // gobject内置类
  306. class_object *gobject_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  307. GWARF_result gobject_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  308. // int内置类
  309. class_object *int_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  310. GWARF_result int_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  311. // double内置类
  312. class_object *double_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  313. GWARF_result double_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  314. // str内置类
  315. class_object *str_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  316. GWARF_result str_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  317. // bool内置类
  318. class_object *bool_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  319. GWARF_result bool_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  320. if_list *make_base_if();
  321. if_list *make_if(statement *, statement *);
  322. if_list *append_elif(if_list *, if_list *);
  323. statement *make_statement();
  324. statement *append_statement(statement *, statement*);
  325. statement_list *make_statement_list();
  326. statement_list *make_statement_base(statement *);
  327. statement_list *append_statement_list(statement *, statement_list *);
  328. statement *find_statement_list(int, statement_list *);
  329. statement_list *free_statement_list(statement_list *);
  330. var *make_var();
  331. void append_var(char *name, GWARF_value, var *);
  332. void free_var(var *);
  333. var *get_var(char *, var *);
  334. void del_var(char *, var *);
  335. default_var *make_default_var();
  336. default_var *make_default_var_base();
  337. void append_default_var_base(char * ,int , default_var *);
  338. int get_default(char *, default_var *);
  339. var_list *make_var_list();
  340. var_list *make_var_base(var *);
  341. var_list *append_var_list(var *, var_list *);
  342. var_list *append_by_var_list(var_list *, var_list *);
  343. var_list *free_var_list(var_list *);
  344. int get_var_list_len(var_list *);
  345. var *find_var(var_list *,int , char *);
  346. void add_var(var_list *,int , char *, GWARF_value);
  347. var_list *copy_var_list(var_list *);
  348. parameter *make_parameter_name(char *);
  349. void append_parameter_name(char *, parameter *);
  350. parameter *make_parameter_value(statement *);
  351. void append_parameter_value(statement *, parameter *);
  352. parameter *add_parameter_value(statement *, parameter *);
  353. parameter *pack_value_parameter(GWARF_value);
  354. statement *pack_call_name(char *, statement *);
  355. GWARF_result traverse(statement *, var_list *, bool);
  356. GWARF_result traverse_global(statement *, var_list *);
  357. inter *get_inter();
  358. inter *global_inter;
  359. statement_list *statement_base;
  360. int yyerror(char const *);
  361. FILE *yyin;
  362. char *yytext;