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

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