The Sol Programming Language!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sol.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #ifndef SOL_H
  2. #define SOL_H
  3. #ifndef NULL
  4. #define NULL ((void *) 0)
  5. #endif
  6. // Forward declarations:
  7. struct sol_tag_object_t;
  8. typedef struct sol_tag_object_t sol_object_t;
  9. struct sol_tag_state_t;
  10. typedef struct sol_tag_state_t sol_state_t;
  11. typedef sol_object_t *(*sol_cfunc_t)(sol_state_t *, sol_object_t *);
  12. typedef struct {
  13. sol_cfunc_t add;
  14. sol_cfunc_t sub;
  15. sol_cfunc_t mul;
  16. sol_cfunc_t div;
  17. sol_cfunc_t pow;
  18. sol_cfunc_t band;
  19. sol_cfunc_t bor;
  20. sol_cfunc_t bxor;
  21. sol_cfunc_t blsh;
  22. sol_cfunc_t brsh;
  23. sol_cfunc_t bnot;
  24. sol_cfunc_t cmp;
  25. sol_cfunc_t call;
  26. sol_cfunc_t index;
  27. sol_cfunc_t setindex;
  28. sol_cfunc_t len;
  29. sol_cfunc_t iter;
  30. sol_cfunc_t toint;
  31. sol_cfunc_t tofloat;
  32. sol_cfunc_t tostring;
  33. sol_cfunc_t init;
  34. sol_cfunc_t free;
  35. } sol_ops_t;
  36. typedef enum {
  37. SOL_SINGLET,
  38. SOL_INTEGER,
  39. SOL_FLOAT,
  40. SOL_STRING,
  41. SOL_LIST,
  42. SOL_LCELL,
  43. SOL_MAP,
  44. SOL_MCELL,
  45. SOL_FUNCTION,
  46. SOL_CFUNCTION,
  47. SOL_CDATA
  48. } sol_objtype_t;
  49. typedef enum {
  50. FR_CALL,
  51. FR_STMT
  52. } sol_frametype_t;
  53. typedef struct sol_tag_object_t {
  54. sol_objtype_t type;
  55. int refcnt;
  56. sol_ops_t *ops;
  57. union {
  58. long ival;
  59. double fval;
  60. char *str;
  61. struct {
  62. struct sol_tag_object_t *lvalue;
  63. struct sol_tag_object_t *lnext;
  64. };
  65. struct {
  66. struct sol_tag_object_t *mkey;
  67. struct sol_tag_object_t *mval;
  68. struct sol_tag_object_t *mnext;
  69. };
  70. struct {
  71. void *func; // Actually a stmt_node *
  72. void *args; // Actually an identlist_node *
  73. struct sol_tag_object_t *closure;
  74. char *fname;
  75. };
  76. sol_cfunc_t cfunc;
  77. void *cdata;
  78. };
  79. } sol_object_t;
  80. typedef enum {SF_NORMAL, SF_BREAKING, SF_CONTINUING} sol_state_flag_t;
  81. typedef struct sol_tag_state_t {
  82. sol_object_t *scopes; // A list of scope maps, innermost out, ending at the global scope
  83. sol_object_t *ret; // Return value of this function, for early return
  84. sol_state_flag_t sflag; // Used to implement break/continue
  85. sol_object_t *error; // Some arbitrary error descriptor, None if no error
  86. sol_object_t *None;
  87. sol_object_t *OutOfMemory;
  88. sol_object_t *StopIteration;
  89. sol_ops_t NullOps;
  90. sol_ops_t IntOps;
  91. sol_ops_t FloatOps;
  92. sol_ops_t StringOps;
  93. sol_ops_t ListOps;
  94. sol_ops_t LCellOps;
  95. sol_ops_t MapOps;
  96. sol_ops_t MCellOps;
  97. sol_ops_t FuncOps;
  98. sol_ops_t CFuncOps;
  99. sol_object_t *ListFuncs;
  100. } sol_state_t;
  101. // state.c
  102. int sol_state_init(sol_state_t *);
  103. void sol_state_cleanup(sol_state_t *);
  104. sol_object_t *sol_state_resolve(sol_state_t *, sol_object_t *);
  105. sol_object_t *sol_state_resolve_name(sol_state_t *, const char *);
  106. void sol_state_assign(sol_state_t *, sol_object_t *, sol_object_t *);
  107. void sol_state_assign_name(sol_state_t *, const char *, sol_object_t *);
  108. void sol_state_assign_l(sol_state_t *, sol_object_t *, sol_object_t *);
  109. void sol_state_assign_l_name(sol_state_t *, const char *, sol_object_t *);
  110. void sol_state_push_scope(sol_state_t *, sol_object_t *);
  111. sol_object_t *sol_state_pop_scope(sol_state_t *);
  112. sol_object_t *sol_get_error(sol_state_t *);
  113. sol_object_t *sol_set_error(sol_state_t *, sol_object_t *);
  114. sol_object_t *sol_set_error_string(sol_state_t *, const char *);
  115. void sol_clear_error(sol_state_t *);
  116. void sol_ops_init(sol_ops_t *);
  117. // builtins.c
  118. sol_object_t *sol_f_not_impl(sol_state_t *, sol_object_t *);
  119. sol_object_t *sol_f_no_op(sol_state_t *, sol_object_t *);
  120. sol_object_t *sol_f_default_cmp(sol_state_t *, sol_object_t *);
  121. sol_object_t *sol_f_toint(sol_state_t *, sol_object_t *);
  122. sol_object_t *sol_f_tofloat(sol_state_t *, sol_object_t *);
  123. sol_object_t *sol_f_tostring(sol_state_t *, sol_object_t *);
  124. sol_object_t *sol_f_try(sol_state_t *, sol_object_t *);
  125. sol_object_t *sol_f_type(sol_state_t *, sol_object_t *);
  126. sol_object_t *sol_f_prepr(sol_state_t *, sol_object_t *);
  127. sol_object_t *sol_f_print(sol_state_t *, sol_object_t *);
  128. sol_object_t *sol_f_rawget(sol_state_t *, sol_object_t *);
  129. sol_object_t *sol_f_rawset(sol_state_t *, sol_object_t *);
  130. sol_object_t *sol_f_range(sol_state_t *, sol_object_t *);
  131. sol_object_t *sol_f_debug_getref(sol_state_t *, sol_object_t *);
  132. sol_object_t *sol_f_debug_setref(sol_state_t *, sol_object_t *);
  133. sol_object_t *sol_f_debug_closure(sol_state_t *, sol_object_t *);
  134. sol_object_t *sol_f_debug_globals(sol_state_t *, sol_object_t *);
  135. sol_object_t *sol_f_debug_locals(sol_state_t *, sol_object_t *);
  136. sol_object_t *sol_f_iter_str(sol_state_t *, sol_object_t *);
  137. sol_object_t *sol_f_iter_list(sol_state_t *, sol_object_t *);
  138. sol_object_t *sol_f_iter_map(sol_state_t *, sol_object_t *);
  139. sol_object_t *sol_f_int_add(sol_state_t *, sol_object_t *);
  140. sol_object_t *sol_f_int_sub(sol_state_t *, sol_object_t *);
  141. sol_object_t *sol_f_int_mul(sol_state_t *, sol_object_t *);
  142. sol_object_t *sol_f_int_div(sol_state_t *, sol_object_t *);
  143. sol_object_t *sol_f_int_pow(sol_state_t *, sol_object_t *);
  144. sol_object_t *sol_f_int_band(sol_state_t *, sol_object_t *);
  145. sol_object_t *sol_f_int_bor(sol_state_t *, sol_object_t *);
  146. sol_object_t *sol_f_int_bxor(sol_state_t *, sol_object_t *);
  147. sol_object_t *sol_f_int_blsh(sol_state_t *, sol_object_t *);
  148. sol_object_t *sol_f_int_brsh(sol_state_t *, sol_object_t *);
  149. sol_object_t *sol_f_int_bnot(sol_state_t *, sol_object_t *);
  150. sol_object_t *sol_f_int_cmp(sol_state_t *, sol_object_t *);
  151. sol_object_t *sol_f_int_toint(sol_state_t *, sol_object_t *);
  152. sol_object_t *sol_f_int_tofloat(sol_state_t *, sol_object_t *);
  153. sol_object_t *sol_f_int_tostring(sol_state_t *, sol_object_t *);
  154. sol_object_t *sol_f_float_add(sol_state_t *, sol_object_t *);
  155. sol_object_t *sol_f_float_sub(sol_state_t *, sol_object_t *);
  156. sol_object_t *sol_f_float_mul(sol_state_t *, sol_object_t *);
  157. sol_object_t *sol_f_float_div(sol_state_t *, sol_object_t *);
  158. sol_object_t *sol_f_float_pow(sol_state_t *, sol_object_t *);
  159. sol_object_t *sol_f_float_cmp(sol_state_t *, sol_object_t *);
  160. sol_object_t *sol_f_float_toint(sol_state_t *, sol_object_t *);
  161. sol_object_t *sol_f_float_tofloat(sol_state_t *, sol_object_t *);
  162. sol_object_t *sol_f_float_tostring(sol_state_t *, sol_object_t *);
  163. sol_object_t *sol_f_str_add(sol_state_t *, sol_object_t *);
  164. sol_object_t *sol_f_str_mul(sol_state_t *, sol_object_t *);
  165. sol_object_t *sol_f_str_len(sol_state_t *, sol_object_t *);
  166. sol_object_t *sol_f_str_iter(sol_state_t *, sol_object_t *);
  167. sol_object_t *sol_f_str_cmp(sol_state_t *, sol_object_t *);
  168. sol_object_t *sol_f_str_toint(sol_state_t *, sol_object_t *);
  169. sol_object_t *sol_f_str_tofloat(sol_state_t *, sol_object_t *);
  170. sol_object_t *sol_f_str_tostring(sol_state_t *, sol_object_t *);
  171. sol_object_t *sol_f_list_add(sol_state_t *, sol_object_t *);
  172. sol_object_t *sol_f_list_mul(sol_state_t *, sol_object_t *);
  173. sol_object_t *sol_f_list_index(sol_state_t *, sol_object_t *);
  174. sol_object_t *sol_f_list_setindex(sol_state_t *, sol_object_t *);
  175. sol_object_t *sol_f_list_len(sol_state_t *, sol_object_t *);
  176. sol_object_t *sol_f_list_iter(sol_state_t *, sol_object_t *);
  177. sol_object_t *sol_f_list_tostring(sol_state_t *, sol_object_t *);
  178. sol_object_t *sol_f_list_copy(sol_state_t *, sol_object_t *);
  179. sol_object_t *sol_f_list_insert(sol_state_t *, sol_object_t *);
  180. sol_object_t *sol_f_list_remove(sol_state_t *, sol_object_t *);
  181. sol_object_t *sol_f_list_truncate(sol_state_t *, sol_object_t *);
  182. sol_object_t *sol_f_list_map(sol_state_t *, sol_object_t *);
  183. sol_object_t *sol_f_list_filter(sol_state_t *, sol_object_t *);
  184. sol_object_t *sol_f_map_add(sol_state_t *, sol_object_t *);
  185. sol_object_t *sol_f_map_index(sol_state_t *, sol_object_t *);
  186. sol_object_t *sol_f_map_setindex(sol_state_t *, sol_object_t *);
  187. sol_object_t *sol_f_map_call(sol_state_t *, sol_object_t *);
  188. sol_object_t *sol_f_map_len(sol_state_t *, sol_object_t *);
  189. sol_object_t *sol_f_map_iter(sol_state_t *, sol_object_t *);
  190. sol_object_t *sol_f_map_tostring(sol_state_t *, sol_object_t *);
  191. sol_object_t *sol_f_func_call(sol_state_t *, sol_object_t *); // Defined in ast.c
  192. sol_object_t *sol_f_func_index(sol_state_t *, sol_object_t *);
  193. sol_object_t *sol_f_func_setindex(sol_state_t *, sol_object_t *);
  194. sol_object_t *sol_f_func_tostring(sol_state_t *, sol_object_t *);
  195. sol_object_t *sol_f_cfunc_call(sol_state_t *, sol_object_t *);
  196. sol_object_t *sol_f_cfunc_tostring(sol_state_t *, sol_object_t *);
  197. // object.c
  198. #define sol_incref(obj) (++((obj)->refcnt), obj)
  199. #define sol_decref(obj) (--((obj)->refcnt))
  200. void sol_obj_free(sol_object_t *);
  201. void sol_obj_release(sol_object_t *);
  202. #define sol_is_singlet(obj) ((obj)->type == SOL_SINGLET)
  203. #define sol_is_none(state, obj) ((obj) == state->None)
  204. #define sol_is_oom(state, obj) ((obj) == state->OutOfMemory)
  205. #define sol_is_int(obj) ((obj)-> type == SOL_INTEGER)
  206. #define sol_is_float(obj) ((obj)->type == SOL_FLOAT)
  207. #define sol_is_string(obj) ((obj)->type == SOL_STRING)
  208. #define sol_is_list(obj) ((obj)->type == SOL_LIST || (obj)->type == SOL_LCELL)
  209. #define sol_is_map(obj) ((obj)->type == SOL_MAP || (obj)->type == SOL_MCELL)
  210. #define sol_is_func(obj) ((obj)->type == SOL_FUNCTION)
  211. #define sol_is_cfunc(obj) ((obj)->type == SOL_CFUNCTION)
  212. #define sol_is_cdata(obj) ((obj)->type == SOL_CDATA)
  213. #define sol_has_error(state) (!sol_is_none((state), (state)->error))
  214. sol_object_t *sol_alloc_object(sol_state_t *);
  215. sol_object_t *sol_new_singlet(sol_state_t *);
  216. sol_object_t *sol_new_int(sol_state_t *, long);
  217. sol_object_t *sol_new_float(sol_state_t *, double);
  218. sol_object_t *sol_new_string(sol_state_t *, const char *);
  219. sol_object_t *sol_new_list(sol_state_t *);
  220. int sol_list_len(sol_state_t *, sol_object_t *);
  221. sol_object_t *sol_list_sublist(sol_state_t *, sol_object_t *, int);
  222. sol_object_t *sol_list_get_index(sol_state_t *, sol_object_t *, int);
  223. void sol_list_set_index(sol_state_t *, sol_object_t *, int, sol_object_t *);
  224. void sol_list_insert(sol_state_t *, sol_object_t *, int, sol_object_t *);
  225. sol_object_t *sol_list_remove(sol_state_t *, sol_object_t *, int);
  226. sol_object_t *sol_list_copy(sol_state_t *, sol_object_t *);
  227. sol_object_t *sol_list_truncate(sol_state_t *, sol_object_t *, int);
  228. void sol_list_append(sol_state_t *, sol_object_t *, sol_object_t *);
  229. #define sol_list_push(st, ls, obj) sol_list_insert(st, ls, 0, obj);
  230. #define sol_list_pop(st, ls) sol_list_remove(st, ls, 0);
  231. sol_object_t *sol_new_map(sol_state_t *);
  232. int sol_map_len(sol_state_t *, sol_object_t *);
  233. sol_object_t *sol_map_mcell(sol_state_t *, sol_object_t *, sol_object_t *);
  234. sol_object_t *sol_map_get(sol_state_t *, sol_object_t *, sol_object_t *);
  235. sol_object_t *sol_map_get_name(sol_state_t *, sol_object_t *, char *);
  236. void sol_map_set(sol_state_t *, sol_object_t *, sol_object_t *, sol_object_t *);
  237. void sol_map_set_name(sol_state_t *, sol_object_t *, char *, sol_object_t *);
  238. void sol_map_set_existing(sol_state_t *, sol_object_t *, sol_object_t *, sol_object_t *);
  239. sol_object_t *sol_map_copy(sol_state_t *, sol_object_t *);
  240. void sol_map_merge(sol_state_t *, sol_object_t *, sol_object_t *);
  241. void sol_map_merge_existing(sol_state_t *, sol_object_t *, sol_object_t *);
  242. // Defined in ast.h
  243. // sol_object_t *sol_new_func(sol_state_t *, identlist_node *, stmt_node *, char *);
  244. sol_object_t *sol_new_cfunc(sol_state_t *, sol_cfunc_t);
  245. sol_object_t *sol_new_cdata(sol_state_t *, void *, sol_ops_t *);
  246. sol_object_t *sol_cast_int(sol_state_t *, sol_object_t *);
  247. sol_object_t *sol_cast_float(sol_state_t *, sol_object_t *);
  248. sol_object_t *sol_cast_string(sol_state_t *, sol_object_t *);
  249. sol_object_t *sol_f_str_free(sol_state_t *, sol_object_t *);
  250. sol_object_t *sol_f_list_free(sol_state_t *, sol_object_t *);
  251. sol_object_t *sol_f_lcell_free(sol_state_t *, sol_object_t *);
  252. sol_object_t *sol_f_map_free(sol_state_t *, sol_object_t *);
  253. sol_object_t *sol_f_mcell_free(sol_state_t *, sol_object_t *);
  254. int sol_validate_list(sol_state_t *, sol_object_t *);
  255. int sol_validate_map(sol_state_t *, sol_object_t *);
  256. // util.c
  257. sol_object_t *sol_util_call(sol_state_t *, sol_object_t *, int, ...)
  258. #endif