value.h 7.8 KB

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