interpreter.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. #ifndef _INTERPRETER_H
  2. #define _INTERPRETER_H
  3. #define malloc(size) safe_malloc(size)
  4. #define free(p) p=safe_free(p)
  5. #define realloc(p,size) safe_realloc(p,size)
  6. #define memcpy(p1,p2,size) safe_memcpy(p1,p2,size)
  7. #define false 0
  8. #define true 1
  9. #define bool int
  10. #define read_statement_list(the_statement,the_var) read_statement(the_statement,the_var,NULL)
  11. #define run_func(base_the_var,the_var,name) run_func_core(base_the_var,the_var,name,false)
  12. // the type of data(GWARF_value)
  13. typedef enum{
  14. NUMBER_value = 1, // [只允许系统使用] [1]
  15. INT_value, // INT 类型[只允许系统使用] [2]
  16. BOOL_value, // bool : true or false [只允许系统使用] [3]
  17. STRING_value, // char * [只允许系统使用] [4]
  18. NULL_value, // 无值类型 [5]
  19. FUNC_value, // 函数 [6]
  20. CLASS_value, // 对象 [7]
  21. OBJECT_value, // 实例 [8]
  22. LIST_value, // 列表类型 [只允许系统使用] [9]
  23. } GWARF_value_type;
  24. // all value is GWARF_value
  25. typedef struct GWARF_value{
  26. GWARF_value_type type;
  27. union
  28. {
  29. double double_value; // NUMBER
  30. int int_value;
  31. bool bool_value;
  32. char *string; // STRING
  33. struct func *func_value;
  34. struct class_object *class_value;
  35. struct the_object *object_value;
  36. struct the_list *list_value;
  37. } value;
  38. } GWARF_value;
  39. // ------------------------- parameter for def
  40. typedef struct parameter{
  41. struct
  42. {
  43. char *name; // var name
  44. struct statement *value; // var value
  45. } u;
  46. enum {
  47. only_name, // 形参
  48. only_value, // 实参
  49. name_value, // 形参/实参
  50. put_args,
  51. // put_kwargs,
  52. // get_args,
  53. // get_kwargs,
  54. } type;
  55. struct parameter *next; // for list
  56. } parameter;
  57. // ------------------------- var
  58. typedef struct var{
  59. char *name; // var name
  60. GWARF_value value;
  61. struct var *next; // for list
  62. } var;
  63. // ------------------------- statement
  64. typedef struct statement{
  65. enum statement_type{
  66. start=1, // for base statement
  67. operation, // such as + - * /
  68. base_var, // return var value by name
  69. base_value, // return an GWARF_value
  70. base_list, // return an GWARF_value->LIST_value
  71. while_cycle, // while
  72. for_cycle,
  73. if_branch, // if
  74. break_cycle, // break
  75. broken, // break_cycle and other {}
  76. continue_cycle,
  77. continued,
  78. restart,
  79. restarted,
  80. rego,
  81. rewent,
  82. set_default,
  83. set_global,
  84. set_nonlocal,
  85. code_block,
  86. def, // func
  87. call, // func()
  88. point, // a.b 注:返回变量同时返回the_var链表[func 用于回调]
  89. down, // a[b] 注:返回变量同时返回the_var链表[func 用于回调]
  90. slice,
  91. return_code, // [26]
  92. set_class, // class aaa; b = aaa() is ```call```
  93. try_code, // try to do something except to do something
  94. raise_e, // raise exception
  95. throw_e, // throw the object class func or NULL
  96. import_class, // import file
  97. include_import, // include file
  98. for_in_cycle, // for i in a
  99. } type; // the statement type
  100. union
  101. {
  102. struct{
  103. enum{
  104. ADD_func = 1, // +
  105. SUB_func, // -
  106. DIV_func, // /
  107. MUL_func, // *
  108. ASSIGnMENT_func, // =
  109. EQUAL_func, // ==
  110. MORE_func, // >
  111. LESS_func, // <
  112. MOREEQ_func, // >=
  113. LESSEQ_func, // <=
  114. NOTEQ_func, // <>
  115. POW_func, // <>
  116. LOG_func, // <>
  117. SQRT_func, // <>
  118. NEGATIVE_func, // -a
  119. AND_func, // -a
  120. OR_func, // -a
  121. NOT_func, // -a
  122. MOD_func, // %
  123. INTDIV_func, // //
  124. AADD_func, // +=
  125. ASUB_func, // -=
  126. ADIV_func, // /=
  127. AMUL_func, // *=
  128. AMOD_func, // %=
  129. AINTDIV_func, // //=
  130. FADD_func, // a++
  131. LADD_func, // ++a
  132. FSUB_func, // a--
  133. LSUB_func, // --a
  134. APOW_func, // ^=
  135. BITAND_func,
  136. BITOR_func,
  137. BITNOTOR_func,
  138. BITRIGHT_func,
  139. BITLEFT_func,
  140. BITNOT_func,
  141. } type;
  142. struct statement *right_exp; // the right exp
  143. struct statement *left_exp; // the left exp
  144. } operation;
  145. struct{
  146. struct statement *condition; // when to while
  147. struct statement *done; // while to do
  148. bool first_do; // do_while = true, while = false
  149. } while_cycle;
  150. struct{
  151. struct statement *first; // the first to do
  152. struct statement *condition; // when to while
  153. struct statement *after; // what to do after the done
  154. struct statement *done; // while to do
  155. } for_cycle;
  156. struct{
  157. struct if_list *done; // if_list
  158. } if_branch;
  159. struct{
  160. char *var_name; // return var
  161. struct statement *from; // from where [double->int]
  162. } base_var;
  163. struct{
  164. struct statement *base_var; // a.b --> a
  165. struct statement *child_var; // a.b --> b
  166. } point;
  167. struct{
  168. struct statement *base_var; // a[b] --> a
  169. struct statement *child_var; // a[b] --> b
  170. } down;
  171. struct{
  172. GWARF_value value; // return value
  173. } base_value;
  174. struct{
  175. parameter *value; // [1,2,3,4] -> to_list
  176. } base_list;
  177. struct{
  178. struct statement *base_var; // a[1:2:3] -> a
  179. parameter *value; // a[1:2:3] -> 1 2 3
  180. } slice;
  181. struct{
  182. struct statement *times; // 层数
  183. } break_cycle;
  184. struct{
  185. struct statement *times; // 层数
  186. } broken;
  187. struct{
  188. struct statement *times; // 层数
  189. } continue_cycle;
  190. struct{
  191. struct statement *times; // 层数
  192. } continued;
  193. struct{
  194. struct statement *times; // 层数
  195. } restart;
  196. struct{
  197. struct statement *times; // 层数
  198. } restarted;
  199. struct{
  200. } rego;
  201. struct{
  202. } rewent;
  203. struct{
  204. char *name;
  205. struct statement *times; // 层数
  206. } set_default;
  207. struct{
  208. char *name;
  209. } set_global;
  210. struct{
  211. char *name;
  212. } set_nonlocal;
  213. struct{
  214. struct statement *done; // block to do
  215. } code_block;
  216. struct{
  217. parameter *parameter_list; // def parameter
  218. struct statement *done; // def to do
  219. struct statement *var; // from where
  220. } def;
  221. struct{
  222. parameter *parameter_list; // def parameter
  223. struct statement *func; // get func value
  224. } call;
  225. struct{
  226. struct statement *times; // 层数
  227. struct statement *value; // return value
  228. } return_code;
  229. struct{
  230. struct statement *done; // class to do
  231. parameter *father_list; // 继承
  232. struct statement *var; // from where [double->int]
  233. } set_class;
  234. struct
  235. {
  236. struct statement *try;
  237. struct statement *except;
  238. struct statement *var; // as var
  239. } try_code;
  240. struct
  241. {
  242. struct statement *done; // done to get exception object
  243. struct statement *info; // the info
  244. } raise_e;
  245. struct
  246. {
  247. struct statement *done; // done to get exception object
  248. } throw_e;
  249. struct
  250. {
  251. struct statement *file; // get address for file
  252. struct statement *var; // as name
  253. } import_class;
  254. struct
  255. {
  256. struct statement *file; // get address for file
  257. } include_import;
  258. struct
  259. {
  260. struct statement *var; // for i in a -> i
  261. struct statement *iter; // for i in a -> a
  262. struct statement *done; // for while to do
  263. } for_in_cycle;
  264. } code;
  265. struct statement *next;
  266. } statement;
  267. // ------------------------- result value
  268. typedef struct GWARF_result{
  269. GWARF_value value;
  270. GWARF_value *father; // a.b --> a
  271. enum{
  272. return_def=1,
  273. statement_end,
  274. cycle_break,
  275. code_broken,
  276. cycle_continue,
  277. code_continued,
  278. cycle_restart,
  279. code_restarted,
  280. code_rego,
  281. code_rewent,
  282. error,
  283. } u; // the result type[from where]
  284. int return_times; // return用
  285. char *error_info; // 输出的错误信息
  286. } GWARF_result;
  287. // ------------------------- default_var [记录默认变量[层]] 用于default语句
  288. typedef struct default_var{
  289. char *name;
  290. int from;
  291. struct default_var *next;
  292. } default_var;
  293. // ------------------------- var base list [记录每一层变量base的链表]
  294. typedef struct var_list{
  295. var *var_base;
  296. default_var *default_list;
  297. struct var_list *next;
  298. } var_list;
  299. // ------------------------- inter paser [记录每一层变量code的链表]
  300. typedef struct statement_list{
  301. statement *statement_base;
  302. struct statement_list *next;
  303. } statement_list;
  304. // ------------------------- if list [记录着if...elif...else]
  305. typedef struct if_list{
  306. struct statement *condition; // when to while
  307. struct statement *done; // while to do
  308. struct if_list *next;
  309. } if_list;
  310. // ------------------------- inter
  311. typedef struct{
  312. var *global_var; // global var链表
  313. statement *global_code; // global code链表
  314. } inter;
  315. //------- class/object/func
  316. typedef enum{
  317. customize = 1, // func by user
  318. official, // func by gwarf
  319. } func_type;
  320. typedef enum{
  321. printf_func = 1, // print_func
  322. __init__func = 2,
  323. __value__func = 3,
  324. __add__func = 4,
  325. __sub__func = 5,
  326. __mul__func = 6,
  327. __div__func = 7,
  328. __eq__func = 8,
  329. __more__func = 9,
  330. __less__func = 10,
  331. __eqmore__func = 11,
  332. __eqless__func = 12,
  333. __noteq__func = 13,
  334. __pow__func = 14,
  335. __log__func = 15,
  336. __sqrt__func = 16,
  337. __negative__func = 17,
  338. __powr__func = 18,
  339. __logr__func = 19,
  340. __sqrtr__func = 20,
  341. __subr__func = 21,
  342. __divr__func = 22,
  343. __len__func = 23,
  344. __down__func = 24,
  345. __set__func = 25,
  346. __slice__func = 26,
  347. __iter__func = 27,
  348. __next__func = 28,
  349. __idiv__func = 29,
  350. __idivr__func = 30,
  351. __mod__func = 31,
  352. __modr__func = 32,
  353. __bitand__func = 33,
  354. __bitor__func = 34,
  355. __bitnotor__func = 35,
  356. __bitleft__func = 36,
  357. __bitleftr__func = 37,
  358. __bitright__func = 38,
  359. __bitrightr__func = 39,
  360. __bitnot__func = 40,
  361. __assignment__func = 41,
  362. } official_func_type;
  363. typedef struct func{
  364. func_type type;
  365. official_func_type official_func;
  366. struct GWARF_result (*paser)(struct func *, struct parameter *, struct var_list *the_var, GWARF_result, var_list *);
  367. struct parameter *parameter_list; // def parameter
  368. struct statement *done; // def to do
  369. struct var_list *the_var; // func会记录the_var,因为不同地方调用var如果var链不统一那就会很乱
  370. int is_class;
  371. } func;
  372. typedef struct class_object{
  373. struct var_list *out_var; // 外部the_var list
  374. struct var_list *the_var; // 记录class_object的 -- 相当与cls
  375. } class_object;
  376. typedef struct the_object{
  377. struct var_list *cls; // 记录class_object的 -- 相当与cls
  378. struct var_list *the_var; // 记录class_object的实例 -- 相当与self
  379. } the_object;
  380. typedef struct the_list // 列表类型
  381. {
  382. GWARF_value *list_value; // 列表类型
  383. int index; // the max index
  384. } the_list;
  385. // 函数声明
  386. GWARF_result operation_func(statement *, var_list *, var_list *);
  387. GWARF_result while_func(statement *, var_list *);
  388. GWARF_result if_func(if_list *, var_list *);
  389. GWARF_result for_func(statement *, var_list *);
  390. GWARF_result call_back(statement *, var_list *);
  391. GWARF_result call_back_core(GWARF_result, var_list *, parameter *);
  392. GWARF_result block_func(statement *, var_list *);
  393. GWARF_result try_func(statement *, var_list *);
  394. GWARF_result raise_func(statement *, var_list *, bool);
  395. GWARF_result import_func(statement *, var_list *);
  396. GWARF_result include_func(statement *, var_list *);
  397. GWARF_result forin_func(statement *, var_list *);
  398. GWARF_result add_func(GWARF_result, GWARF_result, var_list *);
  399. GWARF_result sub_func(GWARF_result, GWARF_result, var_list *);
  400. GWARF_result mul_func(GWARF_result, GWARF_result, var_list *);
  401. GWARF_result div_func(GWARF_result, GWARF_result, var_list *);
  402. GWARF_result pow_func(GWARF_result, GWARF_result, var_list *);
  403. GWARF_result log_func(GWARF_result, GWARF_result, var_list *);
  404. GWARF_result sqrt_func(GWARF_result, GWARF_result, var_list *);
  405. GWARF_result assignment_func(char *, GWARF_result, var_list *, int);
  406. GWARF_result equal_func(GWARF_result, GWARF_result, var_list *, int);
  407. GWARF_result negative_func(GWARF_result, var_list *);
  408. GWARF_result assignment_statement(statement *, var_list *, var_list *, GWARF_result);
  409. GWARF_result not_func(GWARF_result, var_list *);
  410. GWARF_result or_func(statement *, statement *, var_list *);
  411. GWARF_result and_func(statement *, statement *, var_list *);
  412. GWARF_result int_div_func(GWARF_result, GWARF_result, var_list *);
  413. GWARF_result mod_func(GWARF_result, GWARF_result, var_list *);
  414. GWARF_result bit_not_func(GWARF_result, var_list *);
  415. GWARF_result bit_right_func(GWARF_result, GWARF_result, var_list *);
  416. GWARF_result bit_left_func(GWARF_result, GWARF_result, var_list *);
  417. GWARF_result bit_notor_func(GWARF_result, GWARF_result, var_list *);
  418. GWARF_result bit_or_func(GWARF_result, GWARF_result, var_list *);
  419. GWARF_result bit_and_func(GWARF_result, GWARF_result, var_list *);
  420. double sqrt_(double, double);
  421. double log_(double, double);
  422. GWARF_value to_int(GWARF_value, var_list *the_var);
  423. GWARF_value to_double(GWARF_value value, var_list *the_var);
  424. GWARF_value to_str(GWARF_value value, var_list *the_var);
  425. GWARF_value to_bool_(GWARF_value value, var_list *the_var);
  426. GWARF_value to_list(GWARF_value value, var_list *the_var);
  427. GWARF_value parameter_to_list(parameter *tmp_s, var_list *the_var);
  428. bool to_bool(GWARF_value);
  429. GWARF_result get__value__(GWARF_value *, var_list *);
  430. GWARF_result get__bool__(GWARF_value *, var_list *);
  431. GWARF_result get__iter__(GWARF_value *, var_list *);
  432. GWARF_result get__next__(GWARF_value *, var_list *);
  433. GWARF_result get__assignment__(GWARF_value *, var_list *);
  434. GWARF_result run_func_core(GWARF_value *, var_list *, char *, bool);
  435. int len_only_double(double num);
  436. int len_double(double num);
  437. int len_int(int num);
  438. int len_intx(unsigned int num);
  439. GWARF_value to_object(GWARF_value, var_list *);
  440. class_object *make_object(var_list *the_var, var_list *father_var_list);
  441. 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 *));
  442. void login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *));
  443. // 内置函数
  444. GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  445. // object内置类
  446. class_object *object_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *));
  447. GWARF_result object_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  448. // gobject内置类
  449. 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);
  450. GWARF_result gobject_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  451. // int内置类
  452. 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);
  453. GWARF_result int_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  454. // double内置类
  455. 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);
  456. GWARF_result double_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  457. // str内置类
  458. 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);
  459. GWARF_result str_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  460. // bool内置类
  461. 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);
  462. GWARF_result bool_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  463. // list内置类
  464. class_object *list_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  465. GWARF_result list_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  466. // 错误内置类
  467. class_object *BaseException_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  468. GWARF_result BaseException_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  469. class_object *Exception_login_official(var_list *the_var, var_list *father_var_list);
  470. class_object *NameException_login_official(var_list *the_var, var_list *father_var_list);
  471. class_object *IterException_login_official(var_list *the_var, var_list *father_var_list);
  472. // 生成错误
  473. GWARF_result to_error(char *error_info, char *error_type, var_list *the_var);
  474. bool is_space(GWARF_result *);
  475. bool is_error(GWARF_result *tmp);
  476. if_list *make_base_if();
  477. if_list *make_if(statement *, statement *);
  478. if_list *append_elif(if_list *, if_list *);
  479. statement *make_statement();
  480. statement *append_statement(statement *, statement*);
  481. statement_list *make_statement_list();
  482. statement_list *make_statement_base(statement *);
  483. statement_list *append_statement_list(statement *, statement_list *);
  484. statement *find_statement_list(int, statement_list *);
  485. statement_list *free_statement_list(statement_list *);
  486. var *make_var();
  487. void append_var(char *name, GWARF_value, var *);
  488. void free_var(var *);
  489. var *get_var(char *, var *);
  490. void del_var(char *, var *);
  491. default_var *make_default_var();
  492. default_var *make_default_var_base();
  493. void append_default_var_base(char * ,int , default_var *);
  494. int get_default(char *, default_var *);
  495. var_list *make_var_list();
  496. var_list *make_var_base(var *);
  497. var_list *append_var_list(var *, var_list *);
  498. var_list *append_by_var_list(var_list *, var_list *);
  499. var_list *free_var_list(var_list *);
  500. int get_var_list_len(var_list *);
  501. var *find_var(var_list *,int , char *);
  502. void add_var(var_list *,int , char *, GWARF_value);
  503. var_list *copy_var_list(var_list *);
  504. parameter *make_parameter_name(char *);
  505. parameter *append_parameter_name(char *, parameter *);
  506. parameter *make_parameter_value(statement *);
  507. parameter *append_parameter_value(statement *, parameter *);
  508. parameter *add_parameter_value(statement *, parameter *);
  509. parameter *pack_value_parameter(GWARF_value);
  510. statement *pack_call_name(char *, statement *);
  511. GWARF_result traverse(statement *, var_list *, bool);
  512. GWARF_result traverse_global(statement *, var_list *);
  513. inter *get_inter();
  514. void login(var_list *the_var);
  515. inter *global_inter;
  516. statement_list *statement_base;
  517. int yyerror(char const *);
  518. FILE *yyin;
  519. char *yytext;
  520. #endif