value.h 9.0 KB

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