value.h 8.3 KB

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