The Sol Programming Language!
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

state.c 31KB

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