interpreter.h 20 KB

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