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 25KB

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