value.h 9.6 KB

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