value.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #ifndef VIRTUALMATH_VALUE_H
  2. #define VIRTUALMATH_VALUE_H
  3. #include "__macro.h"
  4. // 标准错误信息定义
  5. #define INSTANCE_ERROR(class) L"Instance error when calling func, call non-"#class" "#class" method"
  6. #define VALUE_ERROR(value, acc) L###value" value is not a "#acc" (may be modified by an external program)"
  7. #define ONLY_ACC(var, value) L###var" only accepts "#value
  8. #define ERROR_INIT(class) L###class" get wrong initialization parameters"
  9. #define MANY_ARG L"Too many parameters"
  10. #define FEW_ARG L"Too few parameters"
  11. #define CUL_ERROR(opt) L###opt" operation gets incorrect value"
  12. #define OBJ_NOTSUPPORT(opt) L"Object does not support "#opt" operation"
  13. #define RETURN_ERROR(func, type) L###func" func should return "#type" type data"
  14. #define KEY_INTERRUPT L"KeyInterrupt"
  15. typedef struct Argument Argument;
  16. typedef struct Inter Inter;
  17. typedef struct VarList VarList;
  18. typedef enum ResultType ResultType;
  19. typedef enum BaseErrorType BaseErrorType;
  20. typedef struct Value Value;
  21. typedef struct LinkValue LinkValue;
  22. typedef struct Result Result;
  23. typedef struct Error Error;
  24. typedef struct Inherit Inherit;
  25. typedef struct Package Package;
  26. typedef enum ResultType (*OfficialFunction)(O_FUNC);
  27. typedef void (*Registered)(R_FUNC);
  28. enum ValueAuthority {
  29. auto_aut,
  30. public_aut,
  31. protect_aut,
  32. private_aut
  33. };
  34. enum ValueType {
  35. V_none=0,
  36. V_num=1,
  37. V_str=2,
  38. V_func=3,
  39. V_list=4,
  40. V_dict=5,
  41. V_class=6,
  42. V_obj=7,
  43. V_bool=8,
  44. V_ell=9,
  45. V_file=10,
  46. };
  47. struct Number {
  48. vnum num;
  49. };
  50. struct String {
  51. wchar_t *str;
  52. };
  53. struct Function{
  54. enum {
  55. c_func,
  56. vm_func,
  57. } type;
  58. struct Statement *function;
  59. struct Parameter *pt;
  60. OfficialFunction of;
  61. struct {
  62. enum FunctionPtType {
  63. free_, // 不包含任何隐式传递的参数
  64. static_, // 不包含self参数
  65. object_static_, // self参数不允许class
  66. class_static_, // self参数允许一切,但转换为类
  67. all_static_, // self参数允许一切
  68. cls_static_, // 使用function自带的cls作为参数
  69. object_free_, // 同object_static_但不包含func参数
  70. class_free_, // 同object_static_但不包含func参数
  71. all_free_, // 允许class或者object
  72. cls_free_, // 使用function自带的cls作为参数
  73. } pt_type;
  74. LinkValue *cls;
  75. bool run; // 是否为即时调用
  76. } function_data;
  77. };
  78. struct List {
  79. enum ListType {
  80. L_tuple,
  81. L_list,
  82. } type;
  83. struct LinkValue **list;
  84. vnum size;
  85. };
  86. struct Dict {
  87. struct HashTable *dict;
  88. vnum size;
  89. };
  90. struct Bool{
  91. bool bool_;
  92. };
  93. struct File{
  94. FILE *file;
  95. char *path; // 文件路径
  96. char *mode; // 模式
  97. bool is_std;
  98. };
  99. struct Value{
  100. enum ValueType type;
  101. struct {
  102. struct VarList *var;
  103. struct VarList *out_var;
  104. struct Inherit *inherit;
  105. } object;
  106. union data {
  107. struct Number num;
  108. struct String str;
  109. struct Function function;
  110. struct List list;
  111. struct Dict dict;
  112. struct Bool bool_;
  113. struct File file;
  114. } data;
  115. struct Value *gc_next;
  116. struct Value *gc_last;
  117. struct GCStatus gc_status;
  118. };
  119. struct LinkValue {
  120. enum ValueAuthority aut;
  121. struct Value *value;
  122. struct LinkValue *belong;
  123. struct LinkValue *gc_next;
  124. struct LinkValue *gc_last;
  125. struct GCStatus gc_status;
  126. };
  127. struct Result {
  128. enum ResultType {
  129. R_not = 1, // 无返回值
  130. R_func=2, // 函数返回值
  131. R_opt=3, // 表达式返回值
  132. R_error=4, // 错误
  133. R_break=5,
  134. R_continue=6,
  135. R_rego=7,
  136. R_restart=8,
  137. R_goto=9,
  138. R_yield=10,
  139. } type;
  140. wchar_t *label;
  141. struct LinkValue *value;
  142. struct Error *error;
  143. vnum times;
  144. struct Statement *node;
  145. bool is_yield; // 执行的函数是否为生成器
  146. };
  147. struct Error {
  148. wchar_t *type;
  149. wchar_t *messgae;
  150. char *file;
  151. fline line;
  152. struct Error *next;
  153. };
  154. struct Inherit {
  155. struct LinkValue *value;
  156. struct Inherit *next;
  157. };
  158. struct Package {
  159. struct Value *package;
  160. char *name; // split dir的name
  161. char *md5; // md5地址
  162. struct Package *next;
  163. };
  164. enum BaseErrorType{
  165. E_BaseException,
  166. E_Exception,
  167. E_TypeException,
  168. E_ArgumentException,
  169. E_PermissionsException,
  170. E_GotoException,
  171. E_ResultException,
  172. E_NameExceptiom,
  173. E_AssertException,
  174. E_KeyException,
  175. E_IndexException,
  176. E_StrideException,
  177. E_StopIterException,
  178. E_SuperException,
  179. E_ImportException,
  180. E_IncludeException,
  181. E_SystemException,
  182. E_KeyInterrupt,
  183. E_QuitException,
  184. };
  185. Value *makeObject(Inter *inter, VarList *object, VarList *out_var, Inherit *inherit);
  186. void freeValue(Value **Value);
  187. LinkValue *makeLinkValue(Value *value, LinkValue *belong, Inter *inter);
  188. void freeLinkValue(LinkValue **value);
  189. LinkValue *copyLinkValue(LinkValue *value, Inter *inter);
  190. Value *useNoneValue(Inter *inter, Result *result);
  191. Value *makeBoolValue(bool bool_num, fline line, char *file, FUNC_NT);
  192. Value *makePassValue(fline line, char *file, FUNC_NT);
  193. Value *makeNumberValue(vnum num, fline line, char *file, FUNC_NT);
  194. Value *makeStringValue(wchar_t *str, fline line, char *file, FUNC_NT);
  195. Value *makeVMFunctionValue(struct Statement *st, struct Parameter *pt, FUNC_NT);
  196. Value *makeCFunctionValue(OfficialFunction of, fline line, char *file, FUNC_NT);
  197. LinkValue *makeCFunctionFromOf(OfficialFunction of, LinkValue *func, OfficialFunction function_new, OfficialFunction function_init, LinkValue *belong, VarList *var_list, Inter *inter);
  198. Value *makeClassValue(VarList *var_list, Inter *inter, Inherit *father);
  199. Value *makeListValue(Argument *arg, fline line, char *file, enum ListType type, FUNC_NT);
  200. Value *makeDictValue(Argument *arg, bool new_hash, fline line, char *file, FUNC_NT);
  201. void setResultCore(Result *ru);
  202. void setResult(Result *ru, Inter *inter);
  203. void setResultBase(Result *ru, Inter *inter);
  204. void setResultErrorSt(BaseErrorType type, wchar_t *error_message, bool new, FUNC);
  205. void setResultError(BaseErrorType type, wchar_t *error_message, fline line, char *file, bool new, FUNC_NT);
  206. void setResultOperationNone(Result *ru, Inter *inter, LinkValue *belong);
  207. void setResultOperation(Result *ru, LinkValue *value);
  208. void setResultOperationBase(Result *ru, LinkValue *value);
  209. void freeResult(Result *ru);
  210. Package *makePackage(Value *value, char *md5, char *name, Package *base);
  211. void freePackage(Package *base);
  212. Value *checkPackage(Package *base, char *md5, char *name);
  213. Error *makeError(wchar_t *type, wchar_t *message, fline line, char *file);
  214. void freeError(Result *base);
  215. Error *connectError(Error *new, Error *base);
  216. void printError(Result *result, Inter *inter, bool free);
  217. void printValue(Value *value, FILE *debug, bool print_father, bool print_in);
  218. void printLinkValue(LinkValue *value, char *first, char *last, FILE *debug);
  219. bool isType(Value *value, enum ValueType type);
  220. Inherit *makeInherit(LinkValue *value);
  221. Inherit *copyInherit(Inherit *value);
  222. Inherit *freeInherit(Inherit *value);
  223. Inherit *connectInherit(Inherit *base, Inherit *back);
  224. Inherit *connectSafeInherit(Inherit *base, Inherit *back);
  225. bool checkAttribution(Value *self, Value *father);
  226. Inherit *getInheritFromValueCore(LinkValue *num_father);
  227. bool callDel(Value *object_value, Result *result, Inter *inter, VarList *var_list);
  228. bool needDel(Value *object_value, Inter *inter);
  229. #endif //VIRTUALMATH_VALUE_H