value.h 7.6 KB

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