value.h 10 KB

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