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. #define copyLinkValue(val, inter) makeLinkValue((val)->value, (val)->belong, (val)->aut, (inter))
  18. typedef struct Argument Argument;
  19. typedef struct Inter Inter;
  20. typedef struct VarList VarList;
  21. typedef enum ResultType ResultType;
  22. typedef enum BaseErrorType BaseErrorType;
  23. typedef struct Value Value;
  24. typedef struct LinkValue LinkValue;
  25. typedef struct Result Result;
  26. typedef struct Error Error;
  27. typedef struct Inherit Inherit;
  28. typedef struct Package Package;
  29. typedef enum ResultType (*OfficialFunction)(O_FUNC);
  30. typedef void (*Registered)(R_FUNC);
  31. enum ValueAuthority {
  32. auto_aut,
  33. public_aut,
  34. protect_aut,
  35. private_aut
  36. };
  37. enum ValueType {
  38. V_none=0,
  39. V_int=1,
  40. V_dou=2,
  41. V_str=3,
  42. V_func=4,
  43. V_list=5,
  44. V_dict=6,
  45. V_class=7,
  46. V_obj=8,
  47. V_bool=9,
  48. V_ell=10,
  49. V_file=11,
  50. V_lib=12,
  51. V_pointer=13,
  52. };
  53. struct Int {
  54. vint num;
  55. };
  56. struct Dou {
  57. vdou num;
  58. };
  59. struct String {
  60. wchar_t *str;
  61. };
  62. struct Function{
  63. enum {
  64. c_func,
  65. vm_func,
  66. f_func,
  67. } type;
  68. struct Statement *function;
  69. struct Parameter *pt;
  70. OfficialFunction of;
  71. void (*ffunc)(); // ffi导入的func
  72. struct {
  73. enum FunctionPtType {
  74. free_, // 不包含任何隐式传递的参数
  75. static_, // 不包含self参数
  76. object_static_, // self参数不允许class
  77. class_static_, // self参数允许一切,但转换为类
  78. all_static_, // self参数允许一切
  79. cls_static_, // 使用function自带的cls作为参数
  80. object_free_, // 同object_static_但不包含func参数
  81. class_free_, // 同object_static_但不包含func参数
  82. all_free_, // 允许class或者object
  83. cls_free_, // 使用function自带的cls作为参数
  84. } pt_type;
  85. LinkValue *cls;
  86. bool run; // 是否为即时调用
  87. } function_data;
  88. };
  89. struct List {
  90. enum ListType {
  91. L_tuple,
  92. L_list,
  93. } type;
  94. struct LinkValue **list;
  95. vint size;
  96. };
  97. struct Dict {
  98. struct HashTable *dict;
  99. vint size;
  100. };
  101. struct Bool{
  102. bool bool_;
  103. };
  104. struct Lib{
  105. void *handle;
  106. };
  107. struct File{
  108. FILE *file;
  109. char *path; // 文件路径
  110. char *mode; // 模式
  111. bool is_std;
  112. };
  113. struct Pointer {
  114. void *pointer;
  115. };
  116. struct Value{
  117. enum ValueType type;
  118. struct {
  119. struct VarList *var;
  120. struct VarList *out_var;
  121. struct Inherit *inherit;
  122. } object;
  123. union data {
  124. struct Int int_;
  125. struct Dou dou;
  126. struct String str;
  127. struct Function function;
  128. struct List list;
  129. struct Dict dict;
  130. struct Bool bool_;
  131. struct File file;
  132. struct Lib lib;
  133. struct Pointer pointer;
  134. } data;
  135. struct Value *gc_next;
  136. struct Value *gc_last;
  137. struct GCStatus gc_status;
  138. };
  139. struct LinkValue {
  140. enum ValueAuthority aut;
  141. struct Value *value;
  142. struct LinkValue *belong;
  143. struct LinkValue *gc_next;
  144. struct LinkValue *gc_last;
  145. struct GCStatus gc_status;
  146. };
  147. struct Result {
  148. enum ResultType {
  149. R_not = 1, // 无返回值
  150. R_func=2, // 函数返回值
  151. R_opt=3, // 表达式返回值
  152. R_error=4, // 错误
  153. R_break=5,
  154. R_continue=6,
  155. R_rego=7,
  156. R_restart=8,
  157. R_goto=9,
  158. R_yield=10,
  159. } type;
  160. wchar_t *label;
  161. struct LinkValue *value;
  162. struct Error *error;
  163. vint times;
  164. struct Statement *node;
  165. bool is_yield; // 执行的函数是否为生成器
  166. };
  167. struct Error {
  168. wchar_t *type;
  169. wchar_t *messgae;
  170. char *file;
  171. fline line;
  172. struct Error *next;
  173. };
  174. struct Inherit {
  175. struct LinkValue *value;
  176. struct Inherit *next;
  177. };
  178. struct Package {
  179. struct Value *package;
  180. char *name; // split dir的name
  181. char *md5; // md5地址
  182. struct Package *next;
  183. };
  184. enum BaseErrorType{
  185. E_BaseException = 0,
  186. E_Exception,
  187. E_TypeException,
  188. E_ArgumentException,
  189. E_PermissionsException,
  190. E_GotoException,
  191. E_ResultException,
  192. E_NameExceptiom,
  193. E_AssertException,
  194. E_KeyException,
  195. E_IndexException,
  196. E_StrideException,
  197. E_StopIterException,
  198. E_SuperException,
  199. E_ImportException,
  200. E_IncludeException,
  201. E_SystemException,
  202. E_KeyInterrupt,
  203. E_QuitException,
  204. };
  205. Value *makeObject(Inter *inter, VarList *object, VarList *out_var, Inherit *inherit);
  206. void freeValue(Value **Value);
  207. LinkValue *makeLinkValue(Value *value, LinkValue *belong, enum ValueAuthority aut, Inter *inter);
  208. void freeLinkValue(LinkValue **value);
  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