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.

609 lines
25 KiB

  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_register_module_name(state, "debug", mod);
  167. sol_obj_free(mod);
  168. mod = sol_new_map(state);
  169. sol_map_set_name(state, mod, "str", sol_new_cfunc(state, sol_f_iter_str));
  170. sol_map_set_name(state, mod, "list", sol_new_cfunc(state, sol_f_iter_list));
  171. sol_map_set_name(state, mod, "map", sol_new_cfunc(state, sol_f_iter_map));
  172. sol_register_module_name(state, "iter", mod);
  173. sol_obj_free(mod);
  174. mod = sol_new_map(state);
  175. sol_map_set_name(state, mod, "ST_EXPR", sol_new_int(state, ST_EXPR));
  176. sol_map_set_name(state, mod, "ST_IFELSE", sol_new_int(state, ST_IFELSE));
  177. sol_map_set_name(state, mod, "ST_LOOP", sol_new_int(state, ST_LOOP));
  178. sol_map_set_name(state, mod, "ST_ITER", sol_new_int(state, ST_ITER));
  179. sol_map_set_name(state, mod, "ST_LIST", sol_new_int(state, ST_LIST));
  180. sol_map_set_name(state, mod, "ST_RET", sol_new_int(state, ST_RET));
  181. sol_map_set_name(state, mod, "ST_CONT", sol_new_int(state, ST_CONT));
  182. sol_map_set_name(state, mod, "ST_BREAK", sol_new_int(state, ST_BREAK));
  183. sol_map_set_name(state, mod, "EX_LIT", sol_new_int(state, EX_LIT));
  184. sol_map_set_name(state, mod, "EX_LISTGEN", sol_new_int(state, EX_LIT));
  185. sol_map_set_name(state, mod, "EX_MAPGEN", sol_new_int(state, EX_LIT));
  186. sol_map_set_name(state, mod, "EX_BINOP", sol_new_int(state, EX_LIT));
  187. sol_map_set_name(state, mod, "EX_UNOP", sol_new_int(state, EX_LIT));
  188. sol_map_set_name(state, mod, "EX_INDEX", sol_new_int(state, EX_LIT));
  189. sol_map_set_name(state, mod, "EX_SETINDEX", sol_new_int(state, EX_LIT));
  190. sol_map_set_name(state, mod, "EX_ASSIGN", sol_new_int(state, EX_LIT));
  191. sol_map_set_name(state, mod, "EX_REF", sol_new_int(state, EX_LIT));
  192. sol_map_set_name(state, mod, "EX_CALL", sol_new_int(state, EX_LIT));
  193. sol_map_set_name(state, mod, "EX_FUNCDECL", sol_new_int(state, EX_LIT));
  194. sol_map_set_name(state, mod, "OP_ADD", sol_new_int(state, OP_ADD));
  195. sol_map_set_name(state, mod, "OP_SUB", sol_new_int(state, OP_SUB));
  196. sol_map_set_name(state, mod, "OP_MUL", sol_new_int(state, OP_MUL));
  197. sol_map_set_name(state, mod, "OP_DIV", sol_new_int(state, OP_DIV));
  198. sol_map_set_name(state, mod, "OP_MOD", sol_new_int(state, OP_MOD));
  199. sol_map_set_name(state, mod, "OP_POW", sol_new_int(state, OP_POW));
  200. sol_map_set_name(state, mod, "OP_BAND", sol_new_int(state, OP_BAND));
  201. sol_map_set_name(state, mod, "OP_BOR", sol_new_int(state, OP_BOR));
  202. sol_map_set_name(state, mod, "OP_BXOR", sol_new_int(state, OP_BXOR));
  203. sol_map_set_name(state, mod, "OP_LAND", sol_new_int(state, OP_LAND));
  204. sol_map_set_name(state, mod, "OP_LOR", sol_new_int(state, OP_LOR));
  205. sol_map_set_name(state, mod, "OP_EQUAL", sol_new_int(state, OP_EQUAL));
  206. sol_map_set_name(state, mod, "OP_LESS", sol_new_int(state, OP_LESS));
  207. sol_map_set_name(state, mod, "OP_GREATER", sol_new_int(state, OP_GREATER));
  208. sol_map_set_name(state, mod, "OP_LESSEQ", sol_new_int(state, OP_LESSEQ));
  209. sol_map_set_name(state, mod, "OP_GREATEREQ", sol_new_int(state, OP_GREATEREQ));
  210. sol_map_set_name(state, mod, "OP_LSHIFT", sol_new_int(state, OP_LSHIFT));
  211. sol_map_set_name(state, mod, "OP_RSHIFT", sol_new_int(state, OP_RSHIFT));
  212. sol_map_set_name(state, mod, "OP_NEG", sol_new_int(state, OP_NEG));
  213. sol_map_set_name(state, mod, "OP_BNOT", sol_new_int(state, OP_BNOT));
  214. sol_map_set_name(state, mod, "OP_LNOT", sol_new_int(state, OP_LNOT));
  215. sol_map_set_name(state, mod, "OP_LEN", sol_new_int(state, OP_LEN));
  216. sol_map_invert(state, mod);
  217. sol_register_module_name(state, "ast", mod);
  218. btype = sol_new_map(state);
  219. sol_map_set_name(state, btype, "none", sol_new_int(state, BUF_NONE));
  220. sol_map_set_name(state, btype, "uint8", sol_new_int(state, BUF_UINT8));
  221. sol_map_set_name(state, btype, "uint16", sol_new_int(state, BUF_UINT16));
  222. sol_map_set_name(state, btype, "uint32", sol_new_int(state, BUF_UINT32));
  223. sol_map_set_name(state, btype, "uint64", sol_new_int(state, BUF_UINT64));
  224. sol_map_set_name(state, btype, "int8", sol_new_int(state, BUF_INT8));
  225. sol_map_set_name(state, btype, "int16", sol_new_int(state, BUF_INT16));
  226. sol_map_set_name(state, btype, "int32", sol_new_int(state, BUF_INT32));
  227. sol_map_set_name(state, btype, "int64", sol_new_int(state, BUF_INT64));
  228. sol_map_set_name(state, btype, "char", sol_new_int(state, BUF_CHAR));
  229. sol_map_set_name(state, btype, "byte", sol_new_int(state, BUF_BYTE));
  230. sol_map_set_name(state, btype, "int", sol_new_int(state, BUF_INT));
  231. sol_map_set_name(state, btype, "uint", sol_new_int(state, BUF_UINT));
  232. sol_map_set_name(state, btype, "long", sol_new_int(state, BUF_LONG));
  233. sol_map_set_name(state, btype, "ulong", sol_new_int(state, BUF_ULONG));
  234. sol_map_set_name(state, btype, "float", sol_new_int(state, BUF_FLOAT));
  235. sol_map_set_name(state, btype, "double", sol_new_int(state, BUF_DOUBLE));
  236. sol_map_set_name(state, btype, "cstr", sol_new_int(state, BUF_CSTR));
  237. sol_map_set_name(state, btype, "ptr", sol_new_int(state, BUF_PTR));
  238. sol_map_invert(state, btype);
  239. bsize = sol_new_map(state);
  240. sol_map_set_name(state, bsize, "uint8", sol_new_int(state, sizeof(uint8_t)));
  241. sol_map_set_name(state, bsize, "uint16", sol_new_int(state, sizeof(uint16_t)));
  242. sol_map_set_name(state, bsize, "uint32", sol_new_int(state, sizeof(uint32_t)));
  243. sol_map_set_name(state, bsize, "uint64", sol_new_int(state, sizeof(uint64_t)));
  244. sol_map_set_name(state, bsize, "int8", sol_new_int(state, sizeof(int8_t)));
  245. sol_map_set_name(state, bsize, "int16", sol_new_int(state, sizeof(int16_t)));
  246. sol_map_set_name(state, bsize, "int32", sol_new_int(state, sizeof(int32_t)));
  247. sol_map_set_name(state, bsize, "int64", sol_new_int(state, sizeof(int64_t)));
  248. sol_map_set_name(state, bsize, "char", sol_new_int(state, sizeof(char)));
  249. sol_map_set_name(state, bsize, "byte", sol_new_int(state, sizeof(unsigned char)));
  250. sol_map_set_name(state, bsize, "int", sol_new_int(state, sizeof(int)));
  251. sol_map_set_name(state, bsize, "uint", sol_new_int(state, sizeof(unsigned int)));
  252. sol_map_set_name(state, bsize, "long", sol_new_int(state, sizeof(long)));
  253. sol_map_set_name(state, bsize, "ulong", sol_new_int(state, sizeof(unsigned long)));
  254. sol_map_set_name(state, bsize, "float", sol_new_int(state, sizeof(float)));
  255. sol_map_set_name(state, bsize, "double", sol_new_int(state, sizeof(double)));
  256. sol_map_set_name(state, bsize, "cstr", sol_new_int(state, sizeof(char *)));
  257. sol_map_set_name(state, bsize, "ptr", sol_new_int(state, sizeof(void *)));
  258. bobj = sol_new_map(state);
  259. sol_map_set_name(state, bobj, "SOL_SINGLET", sol_new_int(state, SOL_SINGLET));
  260. sol_map_set_name(state, bobj, "SOL_INTEGER", sol_new_int(state, SOL_INTEGER));
  261. sol_map_set_name(state, bobj, "SOL_FLOAT", sol_new_int(state, SOL_FLOAT));
  262. sol_map_set_name(state, bobj, "SOL_STRING", sol_new_int(state, SOL_STRING));
  263. sol_map_set_name(state, bobj, "SOL_LIST", sol_new_int(state, SOL_LIST));
  264. sol_map_set_name(state, bobj, "SOL_MAP", sol_new_int(state, SOL_MAP));
  265. sol_map_set_name(state, bobj, "SOL_MCELL", sol_new_int(state, SOL_MCELL));
  266. sol_map_set_name(state, bobj, "SOL_FUNCTION", sol_new_int(state, SOL_FUNCTION));
  267. sol_map_set_name(state, bobj, "SOL_CFUNCTION", sol_new_int(state, SOL_CFUNCTION));
  268. sol_map_set_name(state, bobj, "SOL_STMT", sol_new_int(state, SOL_STMT));
  269. sol_map_set_name(state, bobj, "SOL_EXPR", sol_new_int(state, SOL_EXPR));
  270. sol_map_set_name(state, bobj, "SOL_BUFFER", sol_new_int(state, SOL_BUFFER));
  271. sol_map_set_name(state, bobj, "SOL_DYLIB", sol_new_int(state, SOL_DYLIB));
  272. sol_map_set_name(state, bobj, "SOL_DYSYM", sol_new_int(state, SOL_DYSYM));
  273. sol_map_set_name(state, bobj, "SOL_STREAM", sol_new_int(state, SOL_STREAM));
  274. sol_map_set_name(state, bobj, "SOL_CDATA", sol_new_int(state, SOL_CDATA));
  275. sol_map_invert(state, bobj);
  276. mod = sol_new_map(state);
  277. sol_map_set_name(state, mod, "new", sol_new_cfunc(state, sol_f_buffer_new));
  278. sol_map_set_name(state, mod, "fromstring", sol_new_cfunc(state, sol_f_buffer_fromstring));
  279. sol_map_set_name(state, mod, "fromobject", sol_new_cfunc(state, sol_f_buffer_fromobject));
  280. sol_map_set_name(state, mod, "fromaddress", sol_new_cfunc(state, sol_f_buffer_fromaddress));
  281. sol_map_set_name(state, mod, "type", btype);
  282. sol_map_set_name(state, mod, "sizeof", bsize);
  283. sol_map_set_name(state, mod, "objtype", bobj);
  284. sol_register_module_name(state, "buffer", mod);
  285. sol_obj_free(mod);
  286. sol_obj_free(bobj);
  287. sol_obj_free(bsize);
  288. sol_obj_free(btype);
  289. mod = sol_new_map(state);
  290. sol_map_set_name(state, mod, "MODE_READ", sol_new_int(state, MODE_READ));
  291. sol_map_set_name(state, mod, "MODE_WRITE", sol_new_int(state, MODE_WRITE));
  292. sol_map_set_name(state, mod, "MODE_APPEND", sol_new_int(state, MODE_APPEND));
  293. sol_map_set_name(state, mod, "MODE_TRUNCATE", sol_new_int(state, MODE_TRUNCATE));
  294. sol_map_set_name(state, mod, "MODE_BINARY", sol_new_int(state, MODE_BINARY));
  295. sol_map_set_name(state, mod, "SEEK_SET", sol_new_int(state, SEEK_SET));
  296. sol_map_set_name(state, mod, "SEEK_CUR", sol_new_int(state, SEEK_CUR));
  297. sol_map_set_name(state, mod, "SEEK_END", sol_new_int(state, SEEK_END));
  298. sol_map_set_name(state, mod, "ALL", sol_new_string(state, "ALL"));
  299. sol_map_set_name(state, mod, "LINE", sol_new_string(state, "LINE"));
  300. sol_map_set_name(state, mod, "stdin", sol_new_stream(state, stdin, MODE_READ));
  301. sol_map_set_name(state, mod, "stdout", sol_new_stream(state, stdout, MODE_WRITE));
  302. sol_map_set_name(state, mod, "stderr", sol_new_stream(state, stderr, MODE_WRITE));
  303. sol_map_set_name(state, mod, "open", sol_new_cfunc(state, sol_f_stream_open));
  304. sol_register_module_name(state, "io", mod);
  305. sol_obj_free(mod);
  306. meths = sol_new_map(state);
  307. sol_map_set_name(state, meths, "get", sol_new_cfunc(state, sol_f_buffer_get));
  308. sol_map_set_name(state, meths, "set", sol_new_cfunc(state, sol_f_buffer_set));
  309. sol_map_set_name(state, meths, "address", sol_new_cfunc(state, sol_f_buffer_address));
  310. sol_map_set_name(state, meths, "size", sol_new_cfunc(state, sol_f_buffer_size));
  311. sol_register_methods_name(state, "buffer", meths);
  312. sol_obj_free(meths);
  313. meths = sol_new_map(state);
  314. sol_map_set_name(state, meths, "copy", sol_new_cfunc(state, sol_f_list_copy));
  315. sol_map_set_name(state, meths, "insert", sol_new_cfunc(state, sol_f_list_insert));
  316. sol_map_set_name(state, meths, "remove", sol_new_cfunc(state, sol_f_list_remove));
  317. sol_map_set_name(state, meths, "truncate", sol_new_cfunc(state, sol_f_list_truncate));
  318. sol_map_set_name(state, meths, "map", sol_new_cfunc(state, sol_f_list_map));
  319. sol_map_set_name(state, meths, "filter", sol_new_cfunc(state, sol_f_list_filter));
  320. sol_register_methods_name(state, "list", meths);
  321. sol_obj_free(meths);
  322. meths = sol_new_map(state);
  323. sol_map_set_name(state, meths, "read", sol_new_cfunc(state, sol_f_stream_read));
  324. sol_map_set_name(state, meths, "write", sol_new_cfunc(state, sol_f_stream_write));
  325. sol_map_set_name(state, meths, "seek", sol_new_cfunc(state, sol_f_stream_seek));
  326. sol_map_set_name(state, meths, "tell", sol_new_cfunc(state, sol_f_stream_tell));
  327. sol_map_set_name(state, meths, "flush", sol_new_cfunc(state, sol_f_stream_flush));
  328. sol_register_methods_name(state, "stream", meths);
  329. sol_obj_free(meths);
  330. if(sol_has_error(state)) goto cleanup;
  331. // We're all set!
  332. return 1;
  333. cleanup:
  334. sol_obj_free(state->None);
  335. sol_obj_free(state->OutOfMemory);
  336. sol_obj_free(state->StopIteration);
  337. return 0;
  338. }
  339. void sol_state_cleanup(sol_state_t *state) {
  340. sol_obj_free(state->scopes);
  341. sol_obj_free(state->error);
  342. sol_obj_free(state->None);
  343. sol_obj_free(state->OutOfMemory);
  344. sol_obj_free(state->StopIteration);
  345. sol_obj_free(state->ret);
  346. sol_obj_free(state->modules);
  347. sol_obj_free(state->methods);
  348. }
  349. sol_object_t *sol_state_resolve(sol_state_t *state, sol_object_t *key) {
  350. sol_object_t *temp, *args;
  351. dsl_seq_iter *iter = dsl_new_seq_iter(state->scopes->seq);
  352. args = sol_new_list(state);
  353. sol_list_insert(state, args, 0, state->None);
  354. sol_list_insert(state, args, 1, key);
  355. while(!dsl_seq_iter_is_invalid(iter)) {
  356. sol_list_set_index(state, args, 0, dsl_seq_iter_at(iter));
  357. temp = ((sol_object_t *) dsl_seq_iter_at(iter))->ops->index(state, args);
  358. if(!sol_is_none(state, temp)) {
  359. dsl_free_seq_iter(iter);
  360. sol_obj_free(args);
  361. return temp;
  362. }
  363. sol_obj_free(temp);
  364. dsl_seq_iter_next(iter);
  365. }
  366. dsl_free_seq_iter(iter);
  367. sol_obj_free(args);
  368. temp = sol_get_module(state, key);
  369. if(!sol_is_none(state, temp)) {
  370. return temp;
  371. }
  372. return sol_incref(state->None);
  373. }
  374. sol_object_t *sol_state_resolve_name(sol_state_t *state, const char *name) {
  375. sol_object_t *key = sol_new_string(state, name), *temp;
  376. if(sol_has_error(state)) return sol_incref(state->None);
  377. temp = sol_state_resolve(state, key);
  378. sol_obj_free(key);
  379. return temp;
  380. }
  381. void sol_state_assign(sol_state_t *state, sol_object_t *key, sol_object_t *val) {
  382. sol_object_t *cur = state->scopes, *next, *active = NULL, *args;
  383. if(!cur) {
  384. sol_set_error_string(state, "Global state does not exist");
  385. return;
  386. }
  387. active = sol_list_get_index(state, state->scopes, sol_list_len(state, state->scopes)-1);
  388. if(!active || sol_is_none(state, cur)) {
  389. sol_set_error_string(state, "No scopes exist");
  390. return;
  391. }
  392. args=sol_new_list(state);
  393. sol_list_insert(state, args, 0, active);
  394. sol_list_insert(state, args, 1, key);
  395. sol_list_insert(state, args, 2, val);
  396. sol_obj_free(active->ops->setindex(state, args));
  397. sol_obj_free(args);
  398. }
  399. void sol_state_assign_name(sol_state_t *state, const char *name, sol_object_t *val) {
  400. sol_object_t *key = sol_new_string(state, name);
  401. if(sol_has_error(state)) return;
  402. sol_state_assign(state, key, val);
  403. sol_obj_free(key);
  404. }
  405. void sol_state_assign_l(sol_state_t *state, sol_object_t *key, sol_object_t *val) {
  406. sol_object_t *cur = state->scopes, *args;
  407. cur = sol_list_get_index(state, state->scopes, 0);
  408. if(!cur || sol_is_none(state, cur)) {
  409. sol_set_error_string(state, "Local state does not exist");
  410. return;
  411. }
  412. args=sol_new_list(state);
  413. sol_list_insert(state, args, 0, cur);
  414. sol_list_insert(state, args, 1, key);
  415. sol_list_insert(state, args, 2, val);
  416. sol_obj_free(cur->ops->setindex(state, args));
  417. sol_obj_free(args);
  418. }
  419. void sol_state_assign_l_name(sol_state_t *state, const char *name, sol_object_t *val) {
  420. sol_object_t *key = sol_new_string(state, name);
  421. if(sol_has_error(state)) return;
  422. sol_state_assign_l(state, key, val);
  423. sol_obj_free(key);
  424. }
  425. void sol_state_push_scope(sol_state_t *state, sol_object_t *scope) {
  426. sol_list_push(state, state->scopes, scope);
  427. }
  428. sol_object_t *sol_state_pop_scope(sol_state_t *state) {
  429. return sol_list_pop(state, state->scopes);
  430. }
  431. sol_object_t *sol_get_error(sol_state_t *state) {
  432. return sol_incref(state->error);
  433. }
  434. sol_object_t *sol_set_error(sol_state_t *state, sol_object_t *err) {
  435. state->error = sol_incref(err);
  436. return sol_incref(state->None);
  437. }
  438. sol_object_t *sol_set_error_string(sol_state_t *state, const char *serr) {
  439. sol_object_t *err = sol_new_string(state, serr), *res;
  440. if(sol_has_error(state)) return sol_incref(state->None);
  441. res = sol_set_error(state, err);
  442. sol_obj_free(err);
  443. return res;
  444. }
  445. void sol_clear_error(sol_state_t *state) {
  446. // This silliness ensures we don't accidentally free None.
  447. sol_object_t *olderr = state->error;
  448. state->error = sol_incref(state->None);
  449. sol_obj_free(olderr);
  450. }
  451. void sol_register_module(sol_state_t *state, sol_object_t *key, sol_object_t *val) {
  452. sol_map_set(state, state->modules, key, val);
  453. }
  454. void sol_register_module_name(sol_state_t *state, char *name, sol_object_t *val) {
  455. sol_map_set_name(state, state->modules, name, val);
  456. }
  457. sol_object_t *sol_get_module(sol_state_t *state, sol_object_t *key) {
  458. return sol_map_get(state, state->modules, key);
  459. }
  460. sol_object_t *sol_get_module_name(sol_state_t *state, char *name) {
  461. return sol_map_get_name(state, state->modules, name);
  462. }
  463. void sol_register_methods(sol_state_t *state, sol_object_t *key, sol_object_t *val) {
  464. sol_map_set(state, state->methods, key, val);
  465. }
  466. void sol_register_methods_name(sol_state_t *state, char *name, sol_object_t *val) {
  467. sol_map_set_name(state, state->methods, name, val);
  468. }
  469. sol_object_t *sol_get_methods(sol_state_t *state, sol_object_t *key) {
  470. return sol_map_get(state, state->methods, key);
  471. }
  472. sol_object_t *sol_get_methods_name(sol_state_t *state, char *name) {
  473. return sol_map_get_name(state, state->methods, name);
  474. }
  475. sol_object_t *sol_get_stdin(sol_state_t *state) {
  476. sol_object_t *io = sol_state_resolve_name(state, "io");
  477. sol_object_t *res = sol_map_get_name(state, io, "stdin");
  478. sol_obj_free(io);
  479. if(sol_is_none(state, res)) {
  480. printf("WARNING: No io.stdin, returning a new ref\n");
  481. return sol_new_stream(state, stdin, MODE_READ);
  482. }
  483. return res;
  484. }
  485. sol_object_t *sol_get_stdout(sol_state_t *state) {
  486. sol_object_t *io = sol_state_resolve_name(state, "io");
  487. sol_object_t *res = sol_map_get_name(state, io, "stdout");
  488. sol_obj_free(io);
  489. if(sol_is_none(state, res)) {
  490. printf("WARNING: No io.stdout, returning a new ref\n");
  491. return sol_new_stream(state, stdout, MODE_WRITE);
  492. }
  493. return res;
  494. }
  495. sol_object_t *sol_get_stderr(sol_state_t *state) {
  496. sol_object_t *io = sol_state_resolve_name(state, "io");
  497. sol_object_t *res = sol_map_get_name(state, io, "stderr");
  498. sol_obj_free(io);
  499. if(sol_is_none(state, res)) {
  500. printf("WARNING: No io.stderr, returning a new ref\n");
  501. return sol_new_stream(state, stderr, MODE_WRITE);
  502. }
  503. return res;
  504. }
  505. void sol_ops_init(sol_ops_t *ops) {
  506. ops->tname = "unknown";
  507. ops->add = sol_f_not_impl;
  508. ops->sub = sol_f_not_impl;
  509. ops->mul = sol_f_not_impl;
  510. ops->div = sol_f_not_impl;
  511. ops->mod = sol_f_not_impl;
  512. ops->band = sol_f_not_impl;
  513. ops->bor = sol_f_not_impl;
  514. ops->bxor = sol_f_not_impl;
  515. ops->blsh = sol_f_not_impl;
  516. ops->brsh = sol_f_not_impl;
  517. ops->bnot = sol_f_not_impl;
  518. ops->cmp = sol_f_default_cmp;
  519. ops->call = sol_f_not_impl;
  520. ops->index = sol_f_not_impl;
  521. ops->setindex = sol_f_not_impl;
  522. ops->len = sol_f_not_impl;
  523. ops->iter = sol_f_not_impl;
  524. ops->toint = sol_f_not_impl;
  525. ops->tofloat = sol_f_not_impl;
  526. ops->tostring = sol_f_default_tostring;
  527. ops->repr = sol_f_default_repr;
  528. ops->init = sol_f_no_op;
  529. ops->free = sol_f_no_op;
  530. }