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

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