value.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. V_lib=11,
  47. };
  48. struct Number {
  49. vnum num;
  50. };
  51. struct String {
  52. wchar_t *str;
  53. };
  54. struct Function{
  55. enum {
  56. c_func,
  57. vm_func,
  58. } type;
  59. struct Statement *function;
  60. struct Parameter *pt;
  61. OfficialFunction of;
  62. struct {
  63. enum FunctionPtType {
  64. free_, // 不包含任何隐式传递的参数
  65. static_, // 不包含self参数
  66. object_static_, // self参数不允许class
  67. class_static_, // self参数允许一切,但转换为类
  68. all_static_, // self参数允许一切
  69. cls_static_, // 使用function自带的cls作为参数
  70. object_free_, // 同object_static_但不包含func参数
  71. class_free_, // 同object_static_但不包含func参数
  72. all_free_, // 允许class或者object
  73. cls_free_, // 使用function自带的cls作为参数
  74. } pt_type;
  75. LinkValue *cls;
  76. bool run; // 是否为即时调用
  77. } function_data;
  78. };
  79. struct List {
  80. enum ListType {
  81. L_tuple,
  82. L_list,
  83. } type;
  84. struct LinkValue **list;
  85. vnum size;
  86. };
  87. struct Dict {
  88. struct HashTable *dict;
  89. vnum size;
  90. };
  91. struct Bool{
  92. bool bool_;
  93. };
  94. struct Lib{
  95. void *handle;
  96. };
  97. struct File{
  98. FILE *file;
  99. char *path; // 文件路径
  100. char *mode; // 模式
  101. bool is_std;
  102. };
  103. struct Value{
  104. enum ValueType type;
  105. struct {
  106. struct VarList *var;
  107. struct VarList *out_var;
  108. struct Inherit *inherit;
  109. } object;
  110. union data {
  111. struct Number num;
  112. struct String str;
  113. struct Function function;
  114. struct List list;
  115. struct Dict dict;
  116. struct Bool bool_;
  117. struct File file;
  118. struct Lib lib;
  119. } data;
  120. struct Value *gc_next;
  121. struct Value *gc_last;
  122. struct GCStatus gc_status;
  123. };
  124. struct LinkValue {
  125. enum ValueAuthority aut;
  126. struct Value *value;
  127. struct LinkValue *belong;
  128. struct LinkValue *gc_next;
  129. struct LinkValue *gc_last;
  130. struct GCStatus gc_status;
  131. };
  132. struct Result {
  133. enum ResultType {
  134. R_not = 1, // 无返回值
  135. R_func=2, // 函数返回值
  136. R_opt=3, // 表达式返回值
  137. R_error=4, // 错误
  138. R_break=5,
  139. R_continue=6,
  140. R_rego=7,
  141. R_restart=8,
  142. R_goto=9,
  143. R_yield=10,
  144. } type;
  145. wchar_t *label;
  146. struct LinkValue *value;
  147. struct Error *error;
  148. vnum times;
  149. struct Statement *node;
  150. bool is_yield; // 执行的函数是否为生成器
  151. };
  152. struct Error {
  153. wchar_t *type;
  154. wchar_t *messgae;
  155. char *file;
  156. fline line;
  157. struct Error *next;
  158. };
  159. struct Inherit {
  160. struct LinkValue *value;
  161. struct Inherit *next;
  162. };
  163. struct Package {
  164. struct Value *package;
  165. char *name; // split dir的name
  166. char *md5; // md5地址
  167. struct Package *next;
  168. };
  169. enum BaseErrorType{
  170. E_BaseException,
  171. E_Exception,
  172. E_TypeException,
  173. E_ArgumentException,
  174. E_PermissionsException,
  175. E_GotoException,
  176. E_ResultException,
  177. E_NameExceptiom,
  178. E_AssertException,
  179. E_KeyException,
  180. E_IndexException,
  181. E_StrideException,
  182. E_StopIterException,
  183. E_SuperException,
  184. E_ImportException,
  185. E_IncludeException,
  186. E_SystemException,
  187. E_KeyInterrupt,
  188. E_QuitException,
  189. };
  190. Value *makeObject(Inter *inter, VarList *object, VarList *out_var, Inherit *inherit);
  191. void freeValue(Value **Value);
  192. LinkValue *makeLinkValue(Value *value, LinkValue *belong, Inter *inter);
  193. void freeLinkValue(LinkValue **value);
  194. LinkValue *copyLinkValue(LinkValue *value, Inter *inter);
  195. Value *useNoneValue(Inter *inter, Result *result);
  196. Value *makeBoolValue(bool bool_num, fline line, char *file, FUNC_NT);
  197. Value *makePassValue(fline line, char *file, FUNC_NT);
  198. Value *makeNumberValue(vnum num, fline line, char *file, FUNC_NT);
  199. Value *makeStringValue(wchar_t *str, fline line, char *file, FUNC_NT);
  200. Value *makeVMFunctionValue(struct Statement *st, struct Parameter *pt, FUNC_NT);
  201. Value *makeCFunctionValue(OfficialFunction of, fline line, char *file, FUNC_NT);
  202. LinkValue *makeCFunctionFromOf(OfficialFunction of, LinkValue *func, OfficialFunction function_new, OfficialFunction function_init, LinkValue *belong, VarList *var_list, Inter *inter);
  203. Value *makeClassValue(VarList *var_list, Inter *inter, Inherit *father);
  204. Value *makeListValue(Argument *arg, fline line, char *file, enum ListType type, FUNC_NT);
  205. Value *makeDictValue(Argument *arg, bool new_hash, fline line, char *file, FUNC_NT);
  206. void setResultCore(Result *ru);
  207. void setResult(Result *ru, Inter *inter);
  208. void setResultBase(Result *ru, Inter *inter);
  209. void setResultErrorSt(BaseErrorType type, wchar_t *error_message, bool new, FUNC);
  210. void setResultError(BaseErrorType type, wchar_t *error_message, fline line, char *file, bool new, FUNC_NT);
  211. void setResultOperationNone(Result *ru, Inter *inter, LinkValue *belong);
  212. void setResultOperation(Result *ru, LinkValue *value);
  213. void setResultOperationBase(Result *ru, LinkValue *value);
  214. void freeResult(Result *ru);
  215. Package *makePackage(Value *value, char *md5, char *name, Package *base);
  216. void freePackage(Package *base);
  217. Value *checkPackage(Package *base, char *md5, char *name);
  218. Error *makeError(wchar_t *type, wchar_t *message, fline line, char *file);
  219. void freeError(Result *base);
  220. Error *connectError(Error *new, Error *base);
  221. void printError(Result *result, Inter *inter, bool free);
  222. void printValue(Value *value, FILE *debug, bool print_father, bool print_in);
  223. void printLinkValue(LinkValue *value, char *first, char *last, FILE *debug);
  224. bool isType(Value *value, enum ValueType type);
  225. Inherit *makeInherit(LinkValue *value);
  226. Inherit *copyInherit(Inherit *value);
  227. Inherit *freeInherit(Inherit *value);
  228. Inherit *connectInherit(Inherit *base, Inherit *back);
  229. Inherit *connectSafeInherit(Inherit *base, Inherit *back);
  230. bool checkAttribution(Value *self, Value *father);
  231. Inherit *getInheritFromValueCore(LinkValue *num_father);
  232. bool callDel(Value *object_value, Result *result, Inter *inter, VarList *var_list);
  233. bool needDel(Value *object_value, Inter *inter);
  234. #endif //VIRTUALMATH_VALUE_H