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.

766 lines
30 KiB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include "ast.h"
  4. int sol_state_init(sol_state_t *state) {
  5. sol_object_t *globals, *mod, *meths;
  6. sol_object_t *btype, *bsize, *bobj;
  7. unsigned long i;
  8. sol_mm_initialize(state);
  9. state->None = NULL;
  10. state->OutOfMemory = NULL;
  11. state->scopes = NULL;
  12. state->error = NULL;
  13. state->traceback = NULL;
  14. state->ret = NULL;
  15. state->sflag = SF_NORMAL;
  16. state->lastvalue = NULL;
  17. state->loopvalue = NULL;
  18. #ifdef DEBUG_GC
  19. // This is necessary for DEBUG_GC's early allocation; it gets overwritten,
  20. // unfortunately, during sol_ops_init below, so it's duplicated.
  21. state->SingletOps.tname = "singlet";
  22. #endif
  23. // If any of the following fail, some very weird things are happening.
  24. if(!(state->None = sol_new_singlet(state, "None"))) {
  25. goto cleanup;
  26. }
  27. if(!(state->OutOfMemory = sol_new_singlet(state, "OutOfMemory"))) {
  28. goto cleanup;
  29. }
  30. if(!(state->StopIteration = sol_new_singlet(state, "StopIteration"))) {
  31. goto cleanup;
  32. }
  33. // We can now use the normal error reporting mechanism, now
  34. // that errors are distinguishable. Set that up now.
  35. //Initialize all of the builtin operations
  36. //(these ones are technically already pointed to by None/OOM)
  37. sol_ops_init(&(state->NullOps));
  38. state->SingletOps = state->NullOps;
  39. state->IntOps = state->NullOps;
  40. state->FloatOps = state->NullOps;
  41. state->StringOps = state->NullOps;
  42. state->ListOps = state->NullOps;
  43. state->MapOps = state->NullOps;
  44. state->MCellOps = state->NullOps;
  45. state->FuncOps = state->NullOps;
  46. state->CFuncOps = state->NullOps;
  47. state->ASTNodeOps = state->NullOps;
  48. state->BufferOps = state->NullOps;
  49. state->DyLibOps = state->NullOps;
  50. state->DySymOps = state->NullOps;
  51. state->StreamOps = state->NullOps;
  52. state->SingletOps.tname = "singlet";
  53. state->SingletOps.tostring = sol_f_singlet_tostring;
  54. state->SingletOps.free = sol_f_singlet_free;
  55. state->IntOps.tname = "int";
  56. state->IntOps.add = sol_f_int_add;
  57. state->IntOps.sub = sol_f_int_sub;
  58. state->IntOps.mul = sol_f_int_mul;
  59. state->IntOps.div = sol_f_int_div;
  60. state->IntOps.mod = sol_f_int_mod;
  61. state->IntOps.band = sol_f_int_band;
  62. state->IntOps.bor = sol_f_int_bor;
  63. state->IntOps.bxor = sol_f_int_bxor;
  64. state->IntOps.blsh = sol_f_int_blsh;
  65. state->IntOps.brsh = sol_f_int_brsh;
  66. state->IntOps.bnot = sol_f_int_bnot;
  67. state->IntOps.cmp = sol_f_int_cmp;
  68. state->IntOps.toint = sol_f_int_toint;
  69. state->IntOps.tofloat = sol_f_int_tofloat;
  70. state->IntOps.tostring = sol_f_int_tostring;
  71. state->FloatOps.tname = "float";
  72. state->FloatOps.add = sol_f_float_add;
  73. state->FloatOps.sub = sol_f_float_sub;
  74. state->FloatOps.mul = sol_f_float_mul;
  75. state->FloatOps.div = sol_f_float_div;
  76. state->FloatOps.cmp = sol_f_float_cmp;
  77. state->FloatOps.toint = sol_f_float_toint;
  78. state->FloatOps.tofloat = sol_f_float_tofloat;
  79. state->FloatOps.tostring = sol_f_float_tostring;
  80. state->StringOps.tname = "string";
  81. state->StringOps.add = sol_f_str_add;
  82. state->StringOps.mul = sol_f_str_mul;
  83. state->StringOps.cmp = sol_f_str_cmp;
  84. state->StringOps.index = sol_f_str_index;
  85. state->StringOps.len = sol_f_str_len;
  86. state->StringOps.iter = sol_f_str_iter;
  87. state->StringOps.toint = sol_f_str_toint;
  88. state->StringOps.tofloat = sol_f_str_tofloat;
  89. state->StringOps.tostring = sol_f_str_tostring;
  90. state->StringOps.repr = sol_f_str_repr;
  91. state->StringOps.free = sol_f_str_free;
  92. state->ListOps.tname = "list";
  93. state->ListOps.add = sol_f_list_add;
  94. state->ListOps.mul = sol_f_list_mul;
  95. state->ListOps.call = sol_f_not_impl;
  96. state->ListOps.index = sol_f_list_index;
  97. state->ListOps.setindex = sol_f_list_setindex;
  98. state->ListOps.len = sol_f_list_len;
  99. state->ListOps.iter = sol_f_list_iter;
  100. state->ListOps.tostring = sol_f_list_tostring;
  101. state->ListOps.free = sol_f_list_free;
  102. state->MapOps.tname = "map";
  103. state->MapOps.add = sol_f_map_add;
  104. state->MapOps.call = sol_f_map_call;
  105. state->MapOps.index = sol_f_map_index;
  106. state->MapOps.setindex = sol_f_map_setindex;
  107. state->MapOps.len = sol_f_map_len;
  108. state->MapOps.iter = sol_f_map_iter;
  109. state->MapOps.tostring = sol_f_map_tostring;
  110. state->MapOps.repr = sol_f_map_repr;
  111. state->MapOps.free = sol_f_map_free;
  112. state->MCellOps.tname = "mcell";
  113. state->MCellOps.tostring = sol_f_mcell_tostring;
  114. state->MCellOps.free = sol_f_mcell_free;
  115. state->FuncOps.tname = "function";
  116. state->FuncOps.call = sol_f_func_call;
  117. state->FuncOps.index = sol_f_func_index;
  118. state->FuncOps.setindex = sol_f_func_setindex;
  119. state->FuncOps.tostring = sol_f_func_tostring;
  120. state->FuncOps.free = sol_f_func_free;
  121. state->CFuncOps.tname = "cfunction";
  122. state->CFuncOps.call = sol_f_cfunc_call;
  123. state->CFuncOps.tostring = sol_f_cfunc_tostring;
  124. state->ASTNodeOps.tname = "astnode";
  125. state->ASTNodeOps.call = sol_f_astnode_call;
  126. state->ASTNodeOps.index = sol_f_astnode_index;
  127. state->ASTNodeOps.setindex = sol_f_astnode_setindex;
  128. state->ASTNodeOps.tostring = sol_f_astnode_tostring;
  129. state->ASTNodeOps.free = sol_f_astnode_free;
  130. state->BufferOps.tname = "buffer";
  131. state->BufferOps.index = sol_f_buffer_index;
  132. state->BufferOps.tostring = sol_f_buffer_tostring;
  133. state->BufferOps.free = sol_f_buffer_free;
  134. state->DyLibOps.tname = "dylib";
  135. state->DyLibOps.index = sol_f_dylib_index;
  136. state->DyLibOps.free = sol_f_dylib_free;
  137. state->DyLibOps.tostring = sol_f_dylib_tostring;
  138. state->DySymOps.tname = "dysym";
  139. state->DySymOps.call = sol_f_dysym_call;
  140. state->DySymOps.index = sol_f_dysym_index;
  141. state->DySymOps.setindex = sol_f_dysym_setindex;
  142. state->DySymOps.tostring = sol_f_dysym_tostring;
  143. state->StreamOps.tname = "stream";
  144. state->StreamOps.blsh = sol_f_stream_blsh;
  145. state->StreamOps.brsh = sol_f_stream_brsh;
  146. state->StreamOps.index = sol_f_stream_index;
  147. state->StreamOps.free = sol_f_stream_free;
  148. state->StreamOps.tostring = sol_f_stream_tostring;
  149. #ifdef DEBUG_GC
  150. state->obfuncs.copy = (dsl_copier) _sol_gc_dsl_copier;
  151. state->obfuncs.destr = (dsl_destructor) _sol_gc_dsl_destructor;
  152. #else
  153. state->obfuncs.copy = (dsl_copier) sol_obj_acquire;
  154. state->obfuncs.destr = (dsl_destructor) sol_obj_free;
  155. #endif
  156. #ifdef SOL_ICACHE
  157. state->icache_bypass = 1;
  158. for(i = 0; i <= (SOL_ICACHE_MAX - SOL_ICACHE_MIN); i++) {
  159. state->icache[i] = sol_new_int(state, ((long) i) + SOL_ICACHE_MIN);
  160. }
  161. state->icache_bypass = 0;
  162. #endif
  163. state->calling_type = "(none)";
  164. state->calling_meth = "(none)";
  165. state->lastvalue = sol_incref(state->None);
  166. state->loopvalue = sol_incref(state->None);
  167. state->error = state->None;
  168. state->scopes = sol_new_list(state);
  169. if(sol_has_error(state)) {
  170. goto cleanup;
  171. }
  172. globals = sol_new_map(state);
  173. state->modules = sol_new_map(state);
  174. state->methods = sol_new_map(state);
  175. if(sol_has_error(state)) {
  176. goto cleanup;
  177. }
  178. sol_list_insert(state, state->scopes, 0, globals);
  179. sol_obj_free(globals);
  180. if(sol_has_error(state)) {
  181. goto cleanup;
  182. }
  183. state->stdin = sol_new_stream(state, stdin, MODE_READ);
  184. state->stdout = sol_new_stream(state, stdout, MODE_WRITE);
  185. state->stderr = sol_new_stream(state, stderr, MODE_WRITE);
  186. // I'm going to buffer all of these together because I can.
  187. sol_map_borrow_name(state, globals, "OutOfMemory", state->OutOfMemory);
  188. sol_map_borrow_name(state, globals, "StopIteration", state->StopIteration);
  189. sol_map_borrow_name(state, globals, "toint", sol_new_cfunc(state, sol_f_toint));
  190. sol_map_borrow_name(state, globals, "tofloat", sol_new_cfunc(state, sol_f_tofloat));
  191. sol_map_borrow_name(state, globals, "tostring", sol_new_cfunc(state, sol_f_tostring));
  192. sol_map_borrow_name(state, globals, "try", sol_new_cfunc(state, sol_f_try));
  193. sol_map_borrow_name(state, globals, "error", sol_new_cfunc(state, sol_f_error));
  194. sol_map_borrow_name(state, globals, "type", sol_new_cfunc(state, sol_f_type));
  195. sol_map_borrow_name(state, globals, "prepr", sol_new_cfunc(state, sol_f_prepr));
  196. sol_map_borrow_name(state, globals, "print", sol_new_cfunc(state, sol_f_print));
  197. sol_map_borrow_name(state, globals, "rawget", sol_new_cfunc(state, sol_f_rawget));
  198. sol_map_borrow_name(state, globals, "rawset", sol_new_cfunc(state, sol_f_rawset));
  199. sol_map_borrow_name(state, globals, "range", sol_new_cfunc(state, sol_f_range));
  200. sol_map_borrow_name(state, globals, "exec", sol_new_cfunc(state, sol_f_exec));
  201. sol_map_borrow_name(state, globals, "eval", sol_new_cfunc(state, sol_f_eval));
  202. sol_map_borrow_name(state, globals, "execfile", sol_new_cfunc(state, sol_f_execfile));
  203. sol_map_borrow_name(state, globals, "parse", sol_new_cfunc(state, sol_f_parse));
  204. sol_map_borrow_name(state, globals, "ord", sol_new_cfunc(state, sol_f_ord));
  205. sol_map_borrow_name(state, globals, "chr", sol_new_cfunc(state, sol_f_chr));
  206. mod = sol_new_map(state);
  207. sol_map_borrow_name(state, mod, "getref", sol_new_cfunc(state, sol_f_debug_getref));
  208. sol_map_borrow_name(state, mod, "setref", sol_new_cfunc(state, sol_f_debug_setref));
  209. sol_map_borrow_name(state, mod, "closure", sol_new_cfunc(state, sol_f_debug_closure));
  210. sol_map_borrow_name(state, mod, "globals", sol_new_cfunc(state, sol_f_debug_globals));
  211. sol_map_borrow_name(state, mod, "locals", sol_new_cfunc(state, sol_f_debug_locals));
  212. sol_map_borrow_name(state, mod, "scopes", sol_new_cfunc(state, sol_f_debug_scopes));
  213. sol_map_borrow_name(state, mod, "version", sol_new_string(state, VERSION));
  214. sol_map_borrow_name(state, mod, "hexversion", sol_new_int(state, HEXVER));
  215. #ifdef SOL_ICACHE
  216. sol_map_borrow_name(state, mod, "icache_min", sol_new_int(state, SOL_ICACHE_MIN));
  217. sol_map_borrow_name(state, mod, "icache_max", sol_new_int(state, SOL_ICACHE_MAX));
  218. #endif
  219. sol_register_module_name(state, "debug", mod);
  220. sol_obj_free(mod);
  221. mod = sol_new_map(state);
  222. sol_map_borrow_name(state, mod, "str", sol_new_cfunc(state, sol_f_iter_str));
  223. sol_map_borrow_name(state, mod, "list", sol_new_cfunc(state, sol_f_iter_list));
  224. sol_map_borrow_name(state, mod, "map", sol_new_cfunc(state, sol_f_iter_map));
  225. sol_register_module_name(state, "iter", mod);
  226. sol_obj_free(mod);
  227. mod = sol_new_map(state);
  228. sol_map_borrow_name(state, mod, "readline", sol_new_cfunc(state, sol_f_readline_readline));
  229. sol_map_borrow_name(state, mod, "add_history", sol_new_cfunc(state, sol_f_readline_add_history));
  230. sol_register_module_name(state, "readline", mod);
  231. sol_obj_free(mod);
  232. mod = sol_new_map(state);
  233. sol_map_borrow_name(state, mod, "ST_EXPR", sol_new_int(state, ST_EXPR));
  234. sol_map_borrow_name(state, mod, "ST_LIST", sol_new_int(state, ST_LIST));
  235. sol_map_borrow_name(state, mod, "ST_RET", sol_new_int(state, ST_RET));
  236. sol_map_borrow_name(state, mod, "ST_CONT", sol_new_int(state, ST_CONT));
  237. sol_map_borrow_name(state, mod, "ST_BREAK", sol_new_int(state, ST_BREAK));
  238. sol_map_borrow_name(state, mod, "EX_LIT", sol_new_int(state, EX_LIT));
  239. sol_map_borrow_name(state, mod, "EX_LISTGEN", sol_new_int(state, EX_LIT));
  240. sol_map_borrow_name(state, mod, "EX_MAPGEN", sol_new_int(state, EX_LIT));
  241. sol_map_borrow_name(state, mod, "EX_BINOP", sol_new_int(state, EX_LIT));
  242. sol_map_borrow_name(state, mod, "EX_UNOP", sol_new_int(state, EX_LIT));
  243. sol_map_borrow_name(state, mod, "EX_INDEX", sol_new_int(state, EX_LIT));
  244. sol_map_borrow_name(state, mod, "EX_SETINDEX", sol_new_int(state, EX_LIT));
  245. sol_map_borrow_name(state, mod, "EX_ASSIGN", sol_new_int(state, EX_LIT));
  246. sol_map_borrow_name(state, mod, "EX_REF", sol_new_int(state, EX_LIT));
  247. sol_map_borrow_name(state, mod, "EX_CALL", sol_new_int(state, EX_LIT));
  248. sol_map_borrow_name(state, mod, "EX_FUNCDECL", sol_new_int(state, EX_LIT));
  249. sol_map_borrow_name(state, mod, "EX_IFELSE", sol_new_int(state, EX_IFELSE));
  250. sol_map_borrow_name(state, mod, "EX_LOOP", sol_new_int(state, EX_LOOP));
  251. sol_map_borrow_name(state, mod, "EX_ITER", sol_new_int(state, EX_ITER));
  252. sol_map_borrow_name(state, mod, "OP_ADD", sol_new_int(state, OP_ADD));
  253. sol_map_borrow_name(state, mod, "OP_SUB", sol_new_int(state, OP_SUB));
  254. sol_map_borrow_name(state, mod, "OP_MUL", sol_new_int(state, OP_MUL));
  255. sol_map_borrow_name(state, mod, "OP_DIV", sol_new_int(state, OP_DIV));
  256. sol_map_borrow_name(state, mod, "OP_MOD", sol_new_int(state, OP_MOD));
  257. sol_map_borrow_name(state, mod, "OP_POW", sol_new_int(state, OP_POW));
  258. sol_map_borrow_name(state, mod, "OP_BAND", sol_new_int(state, OP_BAND));
  259. sol_map_borrow_name(state, mod, "OP_BOR", sol_new_int(state, OP_BOR));
  260. sol_map_borrow_name(state, mod, "OP_BXOR", sol_new_int(state, OP_BXOR));
  261. sol_map_borrow_name(state, mod, "OP_LAND", sol_new_int(state, OP_LAND));
  262. sol_map_borrow_name(state, mod, "OP_LOR", sol_new_int(state, OP_LOR));
  263. sol_map_borrow_name(state, mod, "OP_EQUAL", sol_new_int(state, OP_EQUAL));
  264. sol_map_borrow_name(state, mod, "OP_NEQUAL", sol_new_int(state, OP_NEQUAL));
  265. sol_map_borrow_name(state, mod, "OP_LESS", sol_new_int(state, OP_LESS));
  266. sol_map_borrow_name(state, mod, "OP_GREATER", sol_new_int(state, OP_GREATER));
  267. sol_map_borrow_name(state, mod, "OP_LESSEQ", sol_new_int(state, OP_LESSEQ));
  268. sol_map_borrow_name(state, mod, "OP_GREATEREQ", sol_new_int(state, OP_GREATEREQ));
  269. sol_map_borrow_name(state, mod, "OP_LSHIFT", sol_new_int(state, OP_LSHIFT));
  270. sol_map_borrow_name(state, mod, "OP_RSHIFT", sol_new_int(state, OP_RSHIFT));
  271. sol_map_borrow_name(state, mod, "OP_NEG", sol_new_int(state, OP_NEG));
  272. sol_map_borrow_name(state, mod, "OP_BNOT", sol_new_int(state, OP_BNOT));
  273. sol_map_borrow_name(state, mod, "OP_LNOT", sol_new_int(state, OP_LNOT));
  274. sol_map_borrow_name(state, mod, "OP_LEN", sol_new_int(state, OP_LEN));
  275. sol_map_borrow_name(state, mod, "LIT_INT", sol_new_int(state, LIT_INT));
  276. sol_map_borrow_name(state, mod, "LIT_FLOAT", sol_new_int(state, LIT_FLOAT));
  277. sol_map_borrow_name(state, mod, "LIT_STRING", sol_new_int(state, LIT_STRING));
  278. sol_map_borrow_name(state, mod, "LIT_NONE", sol_new_int(state, LIT_NONE));
  279. sol_map_invert(state, mod);
  280. sol_map_borrow_name(state, mod, "print", sol_new_cfunc(state, sol_f_ast_print));
  281. sol_register_module_name(state, "ast", mod);
  282. btype = sol_new_map(state);
  283. sol_map_borrow_name(state, btype, "none", sol_new_int(state, BUF_NONE));
  284. sol_map_borrow_name(state, btype, "uint8", sol_new_int(state, BUF_UINT8));
  285. sol_map_borrow_name(state, btype, "uint16", sol_new_int(state, BUF_UINT16));
  286. sol_map_borrow_name(state, btype, "uint32", sol_new_int(state, BUF_UINT32));
  287. sol_map_borrow_name(state, btype, "uint64", sol_new_int(state, BUF_UINT64));
  288. sol_map_borrow_name(state, btype, "int8", sol_new_int(state, BUF_INT8));
  289. sol_map_borrow_name(state, btype, "int16", sol_new_int(state, BUF_INT16));
  290. sol_map_borrow_name(state, btype, "int32", sol_new_int(state, BUF_INT32));
  291. sol_map_borrow_name(state, btype, "int64", sol_new_int(state, BUF_INT64));
  292. sol_map_borrow_name(state, btype, "char", sol_new_int(state, BUF_CHAR));
  293. sol_map_borrow_name(state, btype, "byte", sol_new_int(state, BUF_BYTE));
  294. sol_map_borrow_name(state, btype, "int", sol_new_int(state, BUF_INT));
  295. sol_map_borrow_name(state, btype, "uint", sol_new_int(state, BUF_UINT));
  296. sol_map_borrow_name(state, btype, "long", sol_new_int(state, BUF_LONG));
  297. sol_map_borrow_name(state, btype, "ulong", sol_new_int(state, BUF_ULONG));
  298. sol_map_borrow_name(state, btype, "float", sol_new_int(state, BUF_FLOAT));
  299. sol_map_borrow_name(state, btype, "double", sol_new_int(state, BUF_DOUBLE));
  300. sol_map_borrow_name(state, btype, "cstr", sol_new_int(state, BUF_CSTR));
  301. sol_map_borrow_name(state, btype, "ptr", sol_new_int(state, BUF_PTR));
  302. sol_map_invert(state, btype);
  303. bsize = sol_new_map(state);
  304. sol_map_borrow_name(state, bsize, "uint8", sol_new_int(state, sizeof(uint8_t)));
  305. sol_map_borrow_name(state, bsize, "uint16", sol_new_int(state, sizeof(uint16_t)));
  306. sol_map_borrow_name(state, bsize, "uint32", sol_new_int(state, sizeof(uint32_t)));
  307. sol_map_borrow_name(state, bsize, "uint64", sol_new_int(state, sizeof(uint64_t)));
  308. sol_map_borrow_name(state, bsize, "int8", sol_new_int(state, sizeof(int8_t)));
  309. sol_map_borrow_name(state, bsize, "int16", sol_new_int(state, sizeof(int16_t)));
  310. sol_map_borrow_name(state, bsize, "int32", sol_new_int(state, sizeof(int32_t)));
  311. sol_map_borrow_name(state, bsize, "int64", sol_new_int(state, sizeof(int64_t)));
  312. sol_map_borrow_name(state, bsize, "char", sol_new_int(state, sizeof(char)));
  313. sol_map_borrow_name(state, bsize, "byte", sol_new_int(state, sizeof(unsigned char)));
  314. sol_map_borrow_name(state, bsize, "int", sol_new_int(state, sizeof(int)));
  315. sol_map_borrow_name(state, bsize, "uint", sol_new_int(state, sizeof(unsigned int)));
  316. sol_map_borrow_name(state, bsize, "long", sol_new_int(state, sizeof(long)));
  317. sol_map_borrow_name(state, bsize, "ulong", sol_new_int(state, sizeof(unsigned long)));
  318. sol_map_borrow_name(state, bsize, "float", sol_new_int(state, sizeof(float)));
  319. sol_map_borrow_name(state, bsize, "double", sol_new_int(state, sizeof(double)));
  320. sol_map_borrow_name(state, bsize, "cstr", sol_new_int(state, sizeof(char *)));
  321. sol_map_borrow_name(state, bsize, "ptr", sol_new_int(state, sizeof(void *)));
  322. bobj = sol_new_map(state);
  323. sol_map_borrow_name(state, bobj, "SOL_SINGLET", sol_new_int(state, SOL_SINGLET));
  324. sol_map_borrow_name(state, bobj, "SOL_INTEGER", sol_new_int(state, SOL_INTEGER));
  325. sol_map_borrow_name(state, bobj, "SOL_FLOAT", sol_new_int(state, SOL_FLOAT));
  326. sol_map_borrow_name(state, bobj, "SOL_STRING", sol_new_int(state, SOL_STRING));
  327. sol_map_borrow_name(state, bobj, "SOL_LIST", sol_new_int(state, SOL_LIST));
  328. sol_map_borrow_name(state, bobj, "SOL_MAP", sol_new_int(state, SOL_MAP));
  329. sol_map_borrow_name(state, bobj, "SOL_MCELL", sol_new_int(state, SOL_MCELL));
  330. sol_map_borrow_name(state, bobj, "SOL_FUNCTION", sol_new_int(state, SOL_FUNCTION));
  331. sol_map_borrow_name(state, bobj, "SOL_CFUNCTION", sol_new_int(state, SOL_CFUNCTION));
  332. sol_map_borrow_name(state, bobj, "SOL_STMT", sol_new_int(state, SOL_STMT));
  333. sol_map_borrow_name(state, bobj, "SOL_EXPR", sol_new_int(state, SOL_EXPR));
  334. sol_map_borrow_name(state, bobj, "SOL_BUFFER", sol_new_int(state, SOL_BUFFER));
  335. sol_map_borrow_name(state, bobj, "SOL_DYLIB", sol_new_int(state, SOL_DYLIB));
  336. sol_map_borrow_name(state, bobj, "SOL_DYSYM", sol_new_int(state, SOL_DYSYM));
  337. sol_map_borrow_name(state, bobj, "SOL_STREAM", sol_new_int(state, SOL_STREAM));
  338. sol_map_borrow_name(state, bobj, "SOL_CDATA", sol_new_int(state, SOL_CDATA));
  339. sol_map_invert(state, bobj);
  340. mod = sol_new_map(state);
  341. sol_map_borrow_name(state, mod, "new", sol_new_cfunc(state, sol_f_buffer_new));
  342. sol_map_borrow_name(state, mod, "fromstring", sol_new_cfunc(state, sol_f_buffer_fromstring));
  343. sol_map_borrow_name(state, mod, "fromobject", sol_new_cfunc(state, sol_f_buffer_fromobject));
  344. sol_map_borrow_name(state, mod, "fromaddress", sol_new_cfunc(state, sol_f_buffer_fromaddress));
  345. sol_map_set_name(state, mod, "type", btype);
  346. sol_map_set_name(state, mod, "sizeof", bsize);
  347. sol_map_set_name(state, mod, "objtype", bobj);
  348. sol_register_module_name(state, "buffer", mod);
  349. sol_obj_free(mod);
  350. sol_obj_free(bobj);
  351. sol_obj_free(bsize);
  352. sol_obj_free(btype);
  353. mod = sol_new_map(state);
  354. sol_map_borrow_name(state, mod, "MODE_READ", sol_new_int(state, MODE_READ));
  355. sol_map_borrow_name(state, mod, "MODE_WRITE", sol_new_int(state, MODE_WRITE));
  356. sol_map_borrow_name(state, mod, "MODE_APPEND", sol_new_int(state, MODE_APPEND));
  357. sol_map_borrow_name(state, mod, "MODE_TRUNCATE", sol_new_int(state, MODE_TRUNCATE));
  358. sol_map_borrow_name(state, mod, "MODE_BINARY", sol_new_int(state, MODE_BINARY));
  359. sol_map_borrow_name(state, mod, "SEEK_SET", sol_new_int(state, SEEK_SET));
  360. sol_map_borrow_name(state, mod, "SEEK_CUR", sol_new_int(state, SEEK_CUR));
  361. sol_map_borrow_name(state, mod, "SEEK_END", sol_new_int(state, SEEK_END));
  362. sol_map_borrow_name(state, mod, "ALL", sol_new_string(state, "ALL"));
  363. sol_map_borrow_name(state, mod, "LINE", sol_new_string(state, "LINE"));
  364. sol_map_borrow_name(state, mod, "open", sol_new_cfunc(state, sol_f_stream_open));
  365. sol_map_borrow_name(state, mod, "__setindex", sol_new_cfunc(state, sol_f_io_setindex));
  366. sol_map_borrow_name(state, mod, "__index", sol_new_cfunc(state, sol_f_io_index));
  367. sol_register_module_name(state, "io", mod);
  368. sol_obj_free(mod);
  369. meths = sol_new_map(state);
  370. sol_map_borrow_name(state, meths, "get", sol_new_cfunc(state, sol_f_buffer_get));
  371. sol_map_borrow_name(state, meths, "set", sol_new_cfunc(state, sol_f_buffer_set));
  372. sol_map_borrow_name(state, meths, "address", sol_new_cfunc(state, sol_f_buffer_address));
  373. sol_map_borrow_name(state, meths, "size", sol_new_cfunc(state, sol_f_buffer_size));
  374. sol_register_methods_name(state, "buffer", meths);
  375. sol_obj_free(meths);
  376. meths = sol_new_map(state);
  377. sol_map_borrow_name(state, meths, "copy", sol_new_cfunc(state, sol_f_list_copy));
  378. sol_map_borrow_name(state, meths, "insert", sol_new_cfunc(state, sol_f_list_insert));
  379. sol_map_borrow_name(state, meths, "remove", sol_new_cfunc(state, sol_f_list_remove));
  380. sol_map_borrow_name(state, meths, "truncate", sol_new_cfunc(state, sol_f_list_truncate));
  381. sol_map_borrow_name(state, meths, "map", sol_new_cfunc(state, sol_f_list_map));
  382. sol_map_borrow_name(state, meths, "filter", sol_new_cfunc(state, sol_f_list_filter));
  383. sol_register_methods_name(state, "list", meths);
  384. sol_obj_free(meths);
  385. meths = sol_new_map(state);
  386. sol_map_borrow_name(state, meths, "read", sol_new_cfunc(state, sol_f_stream_read));
  387. sol_map_borrow_name(state, meths, "write", sol_new_cfunc(state, sol_f_stream_write));
  388. sol_map_borrow_name(state, meths, "seek", sol_new_cfunc(state, sol_f_stream_seek));
  389. sol_map_borrow_name(state, meths, "tell", sol_new_cfunc(state, sol_f_stream_tell));
  390. sol_map_borrow_name(state, meths, "flush", sol_new_cfunc(state, sol_f_stream_flush));
  391. sol_map_borrow_name(state, meths, "eof", sol_new_cfunc(state, sol_f_stream_eof));
  392. sol_register_methods_name(state, "stream", meths);
  393. sol_obj_free(meths);
  394. meths = sol_new_map(state);
  395. sol_map_borrow_name(state, meths, "sub", sol_new_cfunc(state, sol_f_str_sub));
  396. sol_map_borrow_name(state, meths, "split", sol_new_cfunc(state, sol_f_str_split));
  397. sol_map_borrow_name(state, meths, "find", sol_new_cfunc(state, sol_f_str_find));
  398. sol_register_methods_name(state, "string", meths);
  399. sol_obj_free(meths);
  400. if(sol_has_error(state)) {
  401. goto cleanup;
  402. }
  403. // We're all set!
  404. return 1;
  405. cleanup:
  406. sol_obj_free(state->None);
  407. sol_obj_free(state->OutOfMemory);
  408. sol_obj_free(state->StopIteration);
  409. return 0;
  410. }
  411. void sol_state_cleanup(sol_state_t *state) {
  412. long i;
  413. sol_obj_free(state->scopes);
  414. sol_obj_free(state->error);
  415. sol_obj_free(state->None);
  416. sol_obj_free(state->OutOfMemory);
  417. sol_obj_free(state->StopIteration);
  418. sol_obj_free(state->stdin);
  419. sol_obj_free(state->stdout);
  420. sol_obj_free(state->stderr);
  421. if(state->ret) {
  422. sol_obj_free(state->ret);
  423. }
  424. #ifdef SOL_ICACHE
  425. for(i = 0; i <= (SOL_ICACHE_MAX - SOL_ICACHE_MIN); i++) {
  426. sol_obj_free(state->icache[i]);
  427. }
  428. #endif
  429. sol_obj_free(state->modules);
  430. sol_obj_free(state->methods);
  431. sol_mm_finalize(state);
  432. }
  433. sol_object_t *sol_state_resolve(sol_state_t *state, sol_object_t *key) {
  434. sol_object_t *temp, *args;
  435. dsl_seq_iter *iter = dsl_new_seq_iter(state->scopes->seq);
  436. args = sol_new_list(state);
  437. sol_list_insert(state, args, 0, state->None);
  438. sol_list_insert(state, args, 1, key);
  439. while(!dsl_seq_iter_is_invalid(iter)) {
  440. sol_list_set_index(state, args, 0, dsl_seq_iter_at(iter));
  441. temp = CALL_METHOD(state, ((sol_object_t *) dsl_seq_iter_at(iter)), index, args);
  442. if(!sol_is_none(state, temp)) {
  443. dsl_free_seq_iter(iter);
  444. sol_obj_free(args);
  445. return temp;
  446. }
  447. sol_obj_free(temp);
  448. dsl_seq_iter_next(iter);
  449. }
  450. dsl_free_seq_iter(iter);
  451. sol_obj_free(args);
  452. temp = sol_get_module(state, key);
  453. if(!sol_is_none(state, temp)) {
  454. return temp;
  455. }
  456. sol_obj_free(temp);
  457. return sol_incref(state->None);
  458. }
  459. sol_object_t *sol_state_resolve_name(sol_state_t *state, const char *name) {
  460. sol_object_t *key = sol_new_string(state, name), *temp;
  461. if(sol_has_error(state)) {
  462. return sol_incref(state->None);
  463. }
  464. temp = sol_state_resolve(state, key);
  465. sol_obj_free(key);
  466. return temp;
  467. }
  468. void sol_state_assign(sol_state_t *state, sol_object_t *key, sol_object_t *val) {
  469. sol_object_t *cur = state->scopes, *next, *active = NULL, *args;
  470. if(!cur) {
  471. sol_set_error_string(state, "Global state does not exist");
  472. return;
  473. }
  474. active = sol_list_get_index(state, state->scopes, sol_list_len(state, state->scopes) - 1);
  475. if(!active || sol_is_none(state, cur)) {
  476. sol_set_error_string(state, "No scopes exist");
  477. return;
  478. }
  479. args = sol_new_list(state);
  480. sol_list_insert(state, args, 0, active);
  481. sol_list_insert(state, args, 1, key);
  482. sol_list_insert(state, args, 2, val);
  483. sol_obj_free(CALL_METHOD(state, active, setindex, args));
  484. sol_obj_free(args);
  485. }
  486. void sol_state_assign_name(sol_state_t *state, const char *name, sol_object_t *val) {
  487. sol_object_t *key = sol_new_string(state, name);
  488. if(sol_has_error(state)) {
  489. return;
  490. }
  491. sol_state_assign(state, key, val);
  492. sol_obj_free(key);
  493. }
  494. void sol_state_assign_l(sol_state_t *state, sol_object_t *key, sol_object_t *val) {
  495. sol_object_t *cur = state->scopes, *args;
  496. cur = sol_list_get_index(state, state->scopes, 0);
  497. if(!cur || sol_is_none(state, cur)) {
  498. sol_set_error_string(state, "Local state does not exist");
  499. return;
  500. }
  501. args = sol_new_list(state);
  502. sol_list_insert(state, args, 0, cur);
  503. sol_list_insert(state, args, 1, key);
  504. sol_list_insert(state, args, 2, val);
  505. sol_obj_free(CALL_METHOD(state, cur, setindex, args));
  506. sol_obj_free(args);
  507. }
  508. void sol_state_assign_l_name(sol_state_t *state, const char *name, sol_object_t *val) {
  509. sol_object_t *key = sol_new_string(state, name);
  510. if(sol_has_error(state)) {
  511. return;
  512. }
  513. sol_state_assign_l(state, key, val);
  514. sol_obj_free(key);
  515. }
  516. void sol_state_push_scope(sol_state_t *state, sol_object_t *scope) {
  517. sol_list_push(state, state->scopes, scope);
  518. }
  519. sol_object_t *sol_state_pop_scope(sol_state_t *state) {
  520. return sol_list_pop(state, state->scopes);
  521. }
  522. sol_object_t *sol_get_error(sol_state_t *state) {
  523. return sol_incref(state->error);
  524. }
  525. sol_object_t *sol_set_error(sol_state_t *state, sol_object_t *err) {
  526. state->error = sol_incref(err);
  527. sol_init_traceback(state);
  528. return sol_incref(state->None);
  529. }
  530. sol_object_t *sol_set_error_string(sol_state_t *state, const char *serr) {
  531. sol_object_t *err = sol_new_string(state, serr), *res;
  532. if(sol_has_error(state)) {
  533. return sol_incref(state->None);
  534. }
  535. res = sol_set_error(state, err);
  536. sol_obj_free(err);
  537. return res;
  538. }
  539. void sol_clear_error(sol_state_t *state) {
  540. // This silliness ensures we don't accidentally free None.
  541. sol_object_t *olderr = state->error;
  542. state->error = sol_incref(state->None);
  543. sol_obj_free(olderr);
  544. }
  545. void sol_init_traceback(sol_state_t *state) {
  546. if(state->traceback) {
  547. sol_obj_free(state->traceback);
  548. }
  549. state->traceback = sol_new_list(state);
  550. }
  551. void sol_add_traceback(sol_state_t *state, sol_object_t *node) {
  552. sol_object_t *pair = sol_new_list(state), *scope = sol_list_get_index(state, state->scopes, 0);
  553. sol_list_insert(state, pair, 0, node);
  554. sol_list_insert(state, pair, 1, scope);
  555. sol_obj_free(scope);
  556. sol_list_insert(state, state->traceback, 0, pair);
  557. sol_obj_free(pair);
  558. }
  559. sol_object_t *sol_traceback(sol_state_t *state) {
  560. if(state->traceback) {
  561. return sol_incref(state->traceback);
  562. }
  563. return sol_incref(state->None);
  564. }
  565. void sol_register_module(sol_state_t *state, sol_object_t *key, sol_object_t *val) {
  566. sol_map_set(state, state->modules, key, val);
  567. }
  568. void sol_register_module_name(sol_state_t *state, char *name, sol_object_t *val) {
  569. sol_map_set_name(state, state->modules, name, val);
  570. }
  571. sol_object_t *sol_get_module(sol_state_t *state, sol_object_t *key) {
  572. return sol_map_get(state, state->modules, key);
  573. }
  574. sol_object_t *sol_get_module_name(sol_state_t *state, char *name) {
  575. return sol_map_get_name(state, state->modules, name);
  576. }
  577. void sol_register_methods(sol_state_t *state, sol_object_t *key, sol_object_t *val) {
  578. sol_map_set(state, state->methods, key, val);
  579. }
  580. void sol_register_methods_name(sol_state_t *state, char *name, sol_object_t *val) {
  581. sol_map_set_name(state, state->methods, name, val);
  582. }
  583. sol_object_t *sol_get_methods(sol_state_t *state, sol_object_t *key) {
  584. return sol_map_get(state, state->methods, key);
  585. }
  586. sol_object_t *sol_get_methods_name(sol_state_t *state, char *name) {
  587. return sol_map_get_name(state, state->methods, name);
  588. }
  589. sol_object_t *sol_f_io_index(sol_state_t *state, sol_object_t *args) {
  590. sol_object_t *self = sol_list_get_index(state, args, 0), *name = sol_list_get_index(state, args, 1), *namestr = sol_cast_string(state, name), *res;
  591. if(sol_string_eq(state, namestr, "stdin")) {
  592. sol_obj_free(name);
  593. sol_obj_free(namestr);
  594. return sol_incref(state->stdin);
  595. }
  596. if(sol_string_eq(state, namestr, "stdout")) {
  597. sol_obj_free(name);
  598. sol_obj_free(namestr);
  599. return sol_incref(state->stdout);
  600. }
  601. if(sol_string_eq(state, namestr, "stderr")) {
  602. sol_obj_free(name);
  603. sol_obj_free(namestr);
  604. return sol_incref(state->stderr);
  605. }
  606. sol_obj_free(namestr);
  607. res = sol_map_get(state, self, name);
  608. sol_obj_free(self);
  609. sol_obj_free(name);
  610. return res;
  611. }
  612. sol_object_t *sol_f_io_setindex(sol_state_t *state, sol_object_t *args) {
  613. sol_object_t *name = sol_list_get_index(state, args, 1), *value = sol_list_get_index(state, args, 2);
  614. sol_object_t *namestr = sol_cast_string(state, name), *io;
  615. if(sol_string_eq(state, namestr, "stdin")) {
  616. sol_obj_free(state->stdin);
  617. state->stdin = sol_incref(value);
  618. goto done;
  619. }
  620. if(sol_string_eq(state, namestr, "stdout")) {
  621. sol_obj_free(state->stdout);
  622. state->stdout = sol_incref(value);
  623. goto done;
  624. }
  625. if(sol_string_eq(state, namestr, "stderr")) {
  626. sol_obj_free(state->stderr);
  627. state->stderr = sol_incref(value);
  628. goto done;
  629. }
  630. io = sol_list_get_index(state, args, 0);
  631. sol_map_set(state, io, name, value);
  632. sol_obj_free(io);
  633. done:
  634. sol_obj_free(namestr);
  635. sol_obj_free(value);
  636. sol_obj_free(name);
  637. return sol_incref(state->None);
  638. }
  639. sol_object_t *sol_get_stdin(sol_state_t *state) {
  640. return sol_incref(state->stdin);
  641. }
  642. sol_object_t *sol_get_stdout(sol_state_t *state) {
  643. return sol_incref(state->stdout);
  644. }
  645. sol_object_t *sol_get_stderr(sol_state_t *state) {
  646. return sol_incref(state->stderr);
  647. }
  648. void sol_ops_init(sol_ops_t *ops) {
  649. ops->tname = "unknown";
  650. ops->add = sol_f_not_impl;
  651. ops->sub = sol_f_not_impl;
  652. ops->mul = sol_f_not_impl;
  653. ops->div = sol_f_not_impl;
  654. ops->mod = sol_f_not_impl;
  655. ops->band = sol_f_not_impl;
  656. ops->bor = sol_f_not_impl;
  657. ops->bxor = sol_f_not_impl;
  658. ops->blsh = sol_f_not_impl;
  659. ops->brsh = sol_f_not_impl;
  660. ops->bnot = sol_f_not_impl;
  661. ops->cmp = sol_f_default_cmp;
  662. ops->call = sol_f_not_impl;
  663. ops->index = sol_f_not_impl;
  664. ops->setindex = sol_f_not_impl;
  665. ops->len = sol_f_not_impl;
  666. ops->iter = sol_f_not_impl;
  667. ops->toint = sol_f_not_impl;
  668. ops->tofloat = sol_f_not_impl;
  669. ops->tostring = sol_f_default_tostring;
  670. ops->repr = sol_f_default_repr;
  671. ops->init = sol_f_no_op;
  672. ops->free = sol_f_no_op;
  673. }