value.h 8.0 KB

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