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.

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