value.h 9.3 KB

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