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.

854 lines
33 KiB

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