value.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. V_struct=14,
  56. };
  57. struct Int {
  58. vint num;
  59. };
  60. struct Dou {
  61. vdou num;
  62. };
  63. struct String {
  64. wchar_t *str;
  65. };
  66. struct Struct_ {
  67. int8_t *data; // 列表
  68. vint len; // 长度
  69. };
  70. struct Function{
  71. enum {
  72. c_func,
  73. vm_func,
  74. f_func,
  75. } type;
  76. struct Statement *function;
  77. struct Parameter *pt;
  78. OfficialFunction of;
  79. void (*ffunc)(); // ffi导入的func
  80. struct {
  81. enum FunctionPtType {
  82. fp_no_, // 不包含任何隐式传递的参数
  83. fp_func_, // 不包含self参数
  84. fp_func_obj, // self参数不允许class
  85. fp_func_class, // self参数允许一切,但转换为类
  86. fp_func_all, // self参数允许一切
  87. fp_func_cls, // 使用function自带的cls作为参数
  88. fp_obj, // 同object_static_但不包含func参数
  89. fp_class, // 同object_static_但不包含func参数
  90. fp_all, // 允许class或者object
  91. fp_cls, // 使用function自带的cls作为参数
  92. // 组合
  93. fp_func_cls_obj,
  94. fp_func_cls_class,
  95. fp_func_cls_all,
  96. fp_cls_obj,
  97. fp_cls_class,
  98. fp_cls_all,
  99. // obj和class没有同时存在的意义, 可以使用内置函数直接获取obj所对应的class
  100. // 直接设定class的目的是修饰类方法
  101. } pt_type;
  102. LinkValue *cls;
  103. bool run; // 是否为即时调用
  104. bool push; // 是否需要push var
  105. } function_data;
  106. };
  107. struct List {
  108. enum ListType {
  109. L_tuple,
  110. L_list,
  111. } type;
  112. struct LinkValue **list;
  113. vint size;
  114. };
  115. struct Dict {
  116. struct HashTable *dict;
  117. vint size;
  118. };
  119. struct Bool{
  120. bool bool_;
  121. };
  122. struct Lib{
  123. void *handle;
  124. };
  125. struct File{
  126. FILE *file;
  127. char *path; // 文件路径
  128. char *mode; // 模式
  129. bool is_std;
  130. };
  131. struct Pointer {
  132. void *pointer;
  133. };
  134. struct Value{
  135. enum ValueType type;
  136. struct {
  137. struct VarList *var;
  138. struct VarList *out_var;
  139. struct Inherit *inherit;
  140. } object;
  141. union data {
  142. struct Int int_;
  143. struct Dou dou;
  144. struct String str;
  145. struct Function function;
  146. struct List list;
  147. struct Dict dict;
  148. struct Bool bool_;
  149. struct File file;
  150. struct Lib lib;
  151. struct Pointer pointer;
  152. struct Struct_ struct_;
  153. } data;
  154. struct Value *gc_next;
  155. struct Value *gc_last;
  156. struct GCStatus gc_status;
  157. };
  158. struct LinkValue {
  159. enum ValueAuthority aut;
  160. struct Value *value;
  161. struct LinkValue *belong;
  162. struct LinkValue *gc_next;
  163. struct LinkValue *gc_last;
  164. struct GCStatus gc_status;
  165. };
  166. struct Result {
  167. enum ResultType {
  168. R_not = 1, // 无返回值
  169. R_func=2, // 函数返回值
  170. R_opt=3, // 表达式返回值
  171. R_error=4, // 错误
  172. R_break=5,
  173. R_continue=6,
  174. R_rego=7,
  175. R_restart=8,
  176. R_goto=9,
  177. R_yield=10,
  178. } type;
  179. wchar_t *label;
  180. struct LinkValue *value;
  181. struct Error *error;
  182. vint times;
  183. struct Statement *node;
  184. bool is_yield; // 执行的函数是否为生成器
  185. };
  186. struct Error {
  187. wchar_t *type;
  188. wchar_t *messgae;
  189. char *file;
  190. fline line;
  191. struct Error *next;
  192. };
  193. struct Inherit {
  194. struct LinkValue *value;
  195. struct Inherit *next;
  196. };
  197. struct Package {
  198. struct Value *package;
  199. char *name; // split dir的name
  200. char *md5; // md5地址
  201. struct Package *next;
  202. };
  203. enum BaseErrorType{
  204. E_BaseException = 0,
  205. E_Exception,
  206. E_TypeException,
  207. E_ArgumentException,
  208. E_PermissionsException,
  209. E_GotoException,
  210. E_ResultException,
  211. E_NameExceptiom,
  212. E_AssertException,
  213. E_KeyException,
  214. E_IndexException,
  215. E_StrideException,
  216. E_StopIterException,
  217. E_SuperException,
  218. E_ImportException,
  219. E_IncludeException,
  220. E_SystemException,
  221. E_KeyInterrupt,
  222. E_QuitException,
  223. E_ValueException,
  224. };
  225. Value *makeObject(Inter *inter, VarList *object, VarList *out_var, bool set_out_var, Inherit *inherit);
  226. void freeValue(Value **Value);
  227. LinkValue *makeLinkValue(Value *value, LinkValue *belong, enum ValueAuthority aut, Inter *inter);
  228. void freeLinkValue(LinkValue **value);
  229. Value *useNoneValue(Inter *inter, Result *result);
  230. Value *makeBoolValue(bool bool_num, fline line, char *file, FUNC_NT);
  231. Value *makeStructValue(void *data, vint len, fline line, char *file, FUNC_NT);
  232. Value *makePassValue(fline line, char *file, FUNC_NT);
  233. Value *makeIntValue(vint num, fline line, char *file, FUNC_NT);
  234. Value *makeDouValue(vdou num, fline line, char *file, FUNC_NT);
  235. Value *makePointerValue(void *p, fline line, char *file, FUNC_NT);
  236. Value *makeStringValue(wchar_t *str, fline line, char *file, FUNC_NT);
  237. Value *makeFileValue(FILE *file_, char *mode, bool is_std, char *path, fline line, char *file, FUNC_NT);
  238. Value *makeVMFunctionValue(struct Statement *st, struct Parameter *pt, FUNC_NT);
  239. Value *makeCFunctionValue(OfficialFunction of, fline line, char *file, bool set_var, bool push, FUNC_NT);
  240. LinkValue *makeCFunctionFromOf(OfficialFunction of, LinkValue *func, OfficialFunction function_new, LinkValue *belong, VarList *var_list, Inter *inter);
  241. Value *makeFFunctionValue(void (*ffunc)(), fline line, char *file, FUNC_NT);
  242. Value *makeClassValue(VarList *var_list, Inter *inter, Inherit *father);
  243. Value *makeListValue(Argument *arg, fline line, char *file, enum ListType type, FUNC_NT);
  244. Value *makeDictValue(Argument *arg, bool new_hash, fline line, char *file, FUNC_NT);
  245. void setResultCore(Result *ru);
  246. void setResult(Result *ru, Inter *inter);
  247. void setResultBase(Result *ru, Inter *inter);
  248. void setResultErrorSt(BaseErrorType type, wchar_t *error_message, bool new, FUNC);
  249. void setResultFromERR(enum BaseErrorType exc, FUNC_NT);
  250. void setResultError(BaseErrorType type, wchar_t *error_message, fline line, char *file, bool new, FUNC_NT);
  251. void setResultOperationNone(Result *ru, Inter *inter, LinkValue *belong);
  252. void setResultOperation(Result *ru, LinkValue *value);
  253. void setResultOperationBase(Result *ru, LinkValue *value);
  254. void freeResult(Result *ru);
  255. Package *makePackage(Value *value, char *md5, char *name, Package *base);
  256. void freePackage(Package *base);
  257. Value *checkPackage(Package *base, char *md5, char *name);
  258. Error *makeError(wchar_t *type, wchar_t *message, fline line, char *file);
  259. void freeError(Result *base);
  260. Error *connectError(Error *new, Error *base);
  261. void printError(Result *result, Inter *inter, bool free);
  262. void printValue(Value *value, FILE *debug, bool print_father, bool print_in);
  263. void printLinkValue(LinkValue *value, char *first, char *last, FILE *debug);
  264. bool isType(Value *value, enum ValueType type);
  265. Inherit *makeInherit(LinkValue *value);
  266. Inherit *copyInherit(Inherit *value);
  267. Inherit *freeInherit(Inherit *value);
  268. Inherit *connectInherit(Inherit *base, Inherit *back);
  269. Inherit *connectSafeInherit(Inherit *base, Inherit *back);
  270. bool checkAttribution(Value *self, Value *father);
  271. Inherit *getInheritFromValueCore(LinkValue *num_father);
  272. bool callDel(Value *object_value, Result *result, Inter *inter, VarList *var_list);
  273. bool needDel(Value *object_value, Inter *inter);
  274. #endif //VIRTUALMATH_VALUE_H