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.

state.c 36KB

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