123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- #include <stdio.h>
- #define false 0
- #define true 1
- #define bool int
- #define read_statement_list(the_statement, the_var) read_statement(the_statement, the_var, NULL)
- typedef enum{
- customize = 1, // func by user
- official, // func by gwarf
- } func_type;
- typedef enum{
- printf_func = 1, // print_func
- } official_func_type;
- typedef struct func{
- func_type type;
- official_func_type official_func;
- struct parameter *parameter_list; // def parameter
- struct statement *done; // def to do
- struct var_list *the_var; // func会记录the_var,因为不同地方调用var如果var链不统一那就会很乱
- int is_class;
- } func;
- typedef struct class_object{
- struct var_list *out_var; // 外部the_var list
- struct var_list *the_var; // 记录class_object的 -- 相当与cls
- } class_object;
- typedef struct the_object{
- struct var_list *cls; // 记录class_object的 -- 相当与cls
- struct var_list *the_var; // 记录class_object的实例 -- 相当与self
- } the_object;
- // the type of data(GWARF_value)
- typedef enum{
- NUMBER_value = 1,
- INT_value, // INT 类型
- BOOL_value, // bool : true or false
- STRING_value, // char *
- NULL_value,
- FUNC_value,
- CLASS_value,
- OBJECT_value,
- } GWARF_value_type;
- // all value is GWARF_value
- typedef struct GWARF_value{
- GWARF_value_type type;
- union
- {
- double double_value; // NUMBER
- int int_value;
- bool bool_value;
- char *string; // STRING
- func *func_value;
- class_object *class_value;
- the_object *object_value;
- } value;
- } GWARF_value;
- // ------------------------- parameter for def
- typedef struct parameter{
- union
- {
- char *name; // var name
- struct statement *value; // or value
- } u;
- struct parameter *next; // for list
- } parameter;
- // ------------------------- var
- typedef struct var{
- char *name; // var name
- GWARF_value value;
- struct var *next; // for list
- } var;
- // ------------------------- statement
- typedef struct statement{
- enum statement_type{
- start=1, // for base statement
- operation, // such as + - * /
- base_var, // return var value by name
- base_value, // return an number or number
- while_cycle, // while
- for_cycle,
- if_branch, // if
- break_cycle, // break
- broken, // break_cycle and other {}
- continue_cycle,
- continued,
- restart,
- restarted,
- rego,
- rewent,
- set_default,
- set_global,
- set_nonlocal,
- code_block,
- def, // func
- call, // func()
- point, // a.b 注:返回变量同时返回the_var链表[func 用于回调]
- return_code,
- set_class, // class aaa; b = aaa() is ```call```
- } type; // the statement type
- union
- {
- struct{
- enum{
- ADD_func = 1, // +
- SUB_func, // -
- DIV_func, // /
- MUL_func, // *
- ASSIGMENT_func, // =
- EQUAL_func, // ==
- MORE_func, // >
- LESS_func, // <
- MOREEQ_func, // >=
- LESSEQ_func, // <=
- NOTEQ_func, // <>
- POW_func, // <>
- LOG_func, // <>
- SQRT_func, // <>
- NEGATIVE_func, // -a
- } type;
- struct statement *right_exp; // the right exp
- struct statement *left_exp; // the left exp
- } operation;
- struct{
- struct statement *condition; // when to while
- struct statement *done; // while to do
- } while_cycle;
- struct{
- struct statement *first; // the first to do
- struct statement *condition; // when to while
- struct statement *after; // what to do after the done
- struct statement *done; // while to do
- } for_cycle;
-
- struct{
- struct if_list *done; // if_list
- } if_branch;
- struct{
- char *var_name; // return var
- struct statement *from; // from where [double->int]
- } base_var;
- struct{
- struct statement *base_var; // a.b --> a
- struct statement *child_var; // a.b --> b
- } point;
- struct{
- GWARF_value value; // return value
- } base_value;
- struct{
- struct statement *times; // 层数
- } break_cycle;
- struct{
- struct statement *times; // 层数
- } broken;
- struct{
- struct statement *times; // 层数
- } continue_cycle;
- struct{
- struct statement *times; // 层数
- } continued;
- struct{
- struct statement *times; // 层数
- } restart;
- struct{
- struct statement *times; // 层数
- } restarted;
- struct{
- } rego;
- struct{
- } rewent;
- struct{
- char *name;
- struct statement *times; // 层数
- } set_default;
- struct{
- char *name;
- } set_global;
- struct{
- char *name;
- } set_nonlocal;
- struct{
- struct statement *done; // block to do
- } code_block;
- struct{
- char *name;
- parameter *parameter_list; // def parameter
- struct statement *done; // def to do
- } def;
- struct{
- parameter *parameter_list; // def parameter
- struct statement *func; // get func value
- } call;
- struct{
- struct statement *times; // 层数
- struct statement *value; // return value
- } return_code;
- struct{
- char *name; // class name
- struct statement *done; // class to do
- } set_class;
- } code;
- struct statement *next;
- } statement;
- // ------------------------- result value
- typedef struct GWARF_result{
- GWARF_value value;
- GWARF_value *father; // a.b --> a
- enum{
- return_def=1,
- statement_end,
- cycle_break,
- code_broken,
- cycle_continue,
- code_continued,
- cycle_restart,
- code_restarted,
- code_rego,
- code_rewent,
- code_return,
- name_no_found,
- } u; // the result type[from where]
- int return_times; // return用
- } GWARF_result;
- // ------------------------- default_var [记录默认变量[层]] 用于default语句
- typedef struct default_var{
- char *name;
- int from;
- struct default_var *next;
- } default_var;
- // ------------------------- var base list [记录每一层变量base的链表]
- typedef struct var_list{
- var *var_base;
- default_var *default_list;
- struct var_list *next;
- } var_list;
- // ------------------------- inter paser [记录每一层变量code的链表]
- typedef struct statement_list{
- statement *statement_base;
- struct statement_list *next;
- } statement_list;
- // ------------------------- if list [记录着if...elif...else]
- typedef struct if_list{
- struct statement *condition; // when to while
- struct statement *done; // while to do
- struct if_list *next;
- } if_list;
- // ------------------------- inter
- typedef struct{
- var *global_var; // global var链表
- statement *global_code; // global code链表
- } inter;
- //------- var func
- var *make_var();
- void append_var(char *, GWARF_value , var *);
- void free_var(var *);
- var *get_var(char *, var *);
- void del_var(char *, var *);
- default_var *make_default_var();
- default_var *make_default_var_base();
- void append_default_var_base(char * ,int , default_var *);
- int get_default(char *, default_var *);
- //------- statement func
- statement *make_statement();
- statement *append_statement(statement *, statement*);
- statement_list *make_statement_list();
- statement_list *make_statement_base(statement *);
- statement_list *append_statement_list(statement *, statement_list *);
- statement *find_statement_list(int, statement_list *);
- statement_list *free_statement_list(statement_list *);
- //------- if func
- if_list *make_base_if();
- if_list *make_if(statement *, statement *);
- if_list *append_elif(if_list *, if_list *);
- //------- run func
- GWARF_result traverse(statement *, var_list *, bool);
- GWARF_result traverse_global(statement *, var_list *);
- //------- inter func
- inter *get_inter();
- // //------ paser func
- int yyerror(char const *);
- FILE *yyin;
- char *yytext;
- // ---- parameter func[形参]
- parameter *make_parameter_name(char *);
- void append_parameter_name(char *, parameter *);
- // ---- parameter func[实参]
- parameter *make_parameter_value(statement *);
- void append_parameter_value(statement *, parameter *);
- parameter *add_parameter_value(statement *, parameter *);
- // main
- inter *global_inter;
- statement_list *statement_base;
- void login_official_func(int, int, var_list *, char *);
- void login_official(var_list *);
|