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.

builtins.c 69KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdint.h>
  6. #include <dlfcn.h>
  7. #include "ast.h"
  8. #include "dsl/dsl.h"
  9. // XXX hardcoded buffer sizes
  10. #define STDIO_CHUNK_SIZE 4096
  11. static char *_itoa(int i) {
  12. int n = 33;
  13. char *s = malloc(n);
  14. snprintf(s, n, "%d", i);
  15. return s;
  16. }
  17. static char *_ftoa(double f) {
  18. int n = 65;
  19. char *s = malloc(n);
  20. snprintf(s, n, "%f", f);
  21. return s;
  22. }
  23. sol_object_t *sol_f_not_impl(sol_state_t *state, sol_object_t *args) {
  24. return sol_set_error_string(state, "Undefined method");
  25. }
  26. sol_object_t *sol_f_default_cmp(sol_state_t *state, sol_object_t *args) {
  27. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  28. sol_object_t *res = sol_new_int(state, a!=b);
  29. sol_obj_free(a);
  30. sol_obj_free(b);
  31. return res;
  32. }
  33. sol_object_t *sol_f_default_tostring(sol_state_t *state, sol_object_t *args) {
  34. sol_object_t *obj = sol_list_get_index(state, args, 0);
  35. char s[64];
  36. snprintf(s, 64, "<%s object at %p>", obj->ops->tname, obj);
  37. sol_obj_free(obj);
  38. return sol_new_string(state, s);
  39. }
  40. sol_object_t *sol_f_default_repr(sol_state_t *state, sol_object_t *args) {
  41. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = obj->ops->tostring(state, args);
  42. sol_obj_free(obj);
  43. return res;
  44. }
  45. sol_object_t *sol_f_no_op(sol_state_t *state, sol_object_t *args) {
  46. if(state) return sol_incref(state->None);
  47. return NULL;
  48. }
  49. sol_object_t *sol_f_toint(sol_state_t *state, sol_object_t *args) {
  50. sol_object_t *obj = sol_list_get_index(state, args, 0);
  51. sol_object_t *res = obj->ops->toint(state, args);
  52. sol_obj_free(obj);
  53. return res;
  54. }
  55. sol_object_t *sol_f_tofloat(sol_state_t *state, sol_object_t *args) {
  56. sol_object_t *obj = sol_list_get_index(state, args, 0);
  57. sol_object_t *res = obj->ops->tofloat(state, args);
  58. sol_obj_free(obj);
  59. return res;
  60. }
  61. sol_object_t *sol_f_tostring(sol_state_t *state, sol_object_t *args) {
  62. sol_object_t *obj = sol_list_get_index(state, args, 0);
  63. sol_object_t *res = obj->ops->tostring(state, args);
  64. sol_obj_free(obj);
  65. return res;
  66. }
  67. sol_object_t *sol_f_try(sol_state_t *state, sol_object_t *args) {
  68. sol_object_t *func = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  69. sol_object_t *ls = sol_new_list(state), *one = sol_new_int(state, 1);
  70. sol_object_t *res;
  71. sol_list_insert(state, fargs, 0, func);
  72. res = func->ops->call(state, fargs);
  73. sol_obj_free(func);
  74. sol_obj_free(fargs);
  75. if(sol_has_error(state)) {
  76. sol_object_t *err = sol_get_error(state);
  77. sol_clear_error(state);
  78. sol_object_t *zero = sol_new_int(state, 0);
  79. sol_obj_free(res);
  80. sol_obj_free(one);
  81. sol_list_insert(state, ls, 0, err);
  82. sol_obj_free(err);
  83. sol_list_insert(state, ls, 0, zero);
  84. sol_obj_free(zero);
  85. return ls;
  86. }
  87. sol_list_insert(state, ls, 0, res);
  88. sol_obj_free(res);
  89. sol_list_insert(state, ls, 0, one);
  90. sol_obj_free(one);
  91. return ls;
  92. }
  93. sol_object_t *sol_f_error(sol_state_t *state, sol_object_t *args) {
  94. sol_object_t *arg = sol_list_get_index(state, args, 0), *res;
  95. res = sol_set_error(state, arg);
  96. sol_obj_free(arg);
  97. return res;
  98. }
  99. sol_object_t *sol_f_type(sol_state_t *state, sol_object_t *args) {
  100. sol_object_t *obj = sol_list_get_index(state, args, 0);
  101. sol_object_t *res = sol_new_string(state, obj->ops->tname);
  102. sol_obj_free(obj);
  103. return res;
  104. }
  105. static dsl_seq *seen=NULL;
  106. int test_seen(sol_object_t *obj) {
  107. dsl_seq_iter *iter;
  108. if(seen) {
  109. iter = dsl_new_seq_iter(seen);
  110. while(!dsl_seq_iter_is_invalid(iter)) {
  111. if(dsl_seq_iter_at(iter) == obj) {
  112. return 1;
  113. }
  114. dsl_seq_iter_next(iter);
  115. }
  116. dsl_free_seq_iter(iter);
  117. dsl_seq_insert(seen, dsl_seq_len(seen), obj);
  118. }
  119. return 0;
  120. }
  121. void ob_print(sol_object_t *obj) {
  122. sol_object_t *cur;
  123. dsl_seq_iter *iter;
  124. int i;
  125. if(test_seen(obj)) {
  126. return;
  127. }
  128. switch(obj->type) {
  129. case SOL_SINGLET:
  130. printf("%s", obj->str);
  131. break;
  132. case SOL_INTEGER:
  133. printf("%ld", obj->ival);
  134. break;
  135. case SOL_FLOAT:
  136. printf("%f", obj->fval);
  137. break;
  138. case SOL_STRING:
  139. printf("\"%s\"", obj->str);
  140. break;
  141. case SOL_LIST:
  142. printf("[");
  143. iter = dsl_new_seq_iter(obj->seq);
  144. while(!dsl_seq_iter_is_invalid(iter)) {
  145. ob_print(dsl_seq_iter_at(iter));
  146. printf(", ");
  147. dsl_seq_iter_next(iter);
  148. }
  149. dsl_free_seq_iter(iter);
  150. printf("]");
  151. break;
  152. case SOL_MCELL:
  153. printf("<<");
  154. ob_print(obj->key);
  155. printf("=");
  156. ob_print(obj->val);
  157. printf(">>");
  158. case SOL_MAP:
  159. printf("{");
  160. iter = dsl_new_seq_iter(obj->seq);
  161. while(!dsl_seq_iter_is_invalid(iter)) {
  162. printf("[");
  163. ob_print(AS_OBJ(dsl_seq_iter_at(iter))->key);
  164. printf("] = ");
  165. ob_print(AS_OBJ(dsl_seq_iter_at(iter))->val);
  166. printf(", ");
  167. dsl_seq_iter_next(iter);
  168. }
  169. dsl_free_seq_iter(iter);
  170. printf("}");
  171. break;
  172. case SOL_FUNCTION:
  173. if(obj->fname) {
  174. printf("<Function %s>", obj->fname);
  175. } else {
  176. printf("<Function>");
  177. }
  178. break;
  179. case SOL_CFUNCTION:
  180. printf("<CFunction>");
  181. break;
  182. case SOL_STMT:
  183. st_print(obj->node);
  184. break;
  185. case SOL_EXPR:
  186. ex_print(obj->node);
  187. break;
  188. case SOL_BUFFER:
  189. if(obj->sz == -1) {
  190. printf("<Buffer @%p>", obj->buffer);
  191. } else {
  192. printf("<Buffer @%p size %ld>", obj->buffer, obj->sz);
  193. }
  194. break;
  195. case SOL_CDATA:
  196. printf("<CData>");
  197. break;
  198. /*default:
  199. cur = sol_cast_string(state, obj);
  200. printf("%s", cur->str);
  201. sol_obj_free(cur);*/
  202. }
  203. }
  204. sol_object_t *sol_f_prepr(sol_state_t *state, sol_object_t *args) {
  205. int i, sz = sol_list_len(state, args);
  206. sol_object_t *obj;
  207. seen = dsl_seq_new_array(NULL, NULL);
  208. for(i=0; i<sz; i++) {
  209. obj = sol_list_get_index(state, args, i);
  210. ob_print(obj);
  211. printf(" ");
  212. sol_obj_free(obj);
  213. }
  214. printf("\n");
  215. dsl_free_seq(seen);
  216. seen = NULL;
  217. return sol_incref(state->None);
  218. }
  219. sol_object_t *sol_f_print(sol_state_t *state, sol_object_t *args) {
  220. int i, sz = sol_list_len(state, args);
  221. sol_object_t *obj, *str;
  222. seen = dsl_seq_new_array(NULL, NULL);
  223. for(i=0; i<sz; i++) {
  224. obj = sol_list_get_index(state, args, i);
  225. str = sol_cast_string(state, obj);
  226. sol_printf(state, "%s", str->str);
  227. sol_printf(state, " ");
  228. sol_obj_free(obj);
  229. sol_obj_free(str);
  230. }
  231. sol_printf(state, "\n");
  232. dsl_free_seq(seen);
  233. seen = NULL;
  234. return sol_incref(state->None);
  235. }
  236. sol_object_t *sol_f_rawget(sol_state_t *state, sol_object_t *args) {
  237. sol_object_t *obj = sol_list_get_index(state, args, 0), *key, *res;
  238. if(!sol_is_map(obj)) return sol_set_error_string(state, "Rawset of non-map");
  239. key = sol_list_get_index(state, args, 1);
  240. res = sol_map_get(state, obj, key);
  241. sol_obj_free(key);
  242. sol_obj_free(obj);
  243. return res;
  244. }
  245. sol_object_t *sol_f_rawset(sol_state_t *state, sol_object_t *args) {
  246. sol_object_t *obj = sol_list_get_index(state, args, 0), *key, *val;
  247. if(!sol_is_map(obj)) return sol_set_error_string(state, "Rawset of non-map");
  248. key = sol_list_get_index(state, args, 1);
  249. val = sol_list_get_index(state, args, 2);
  250. sol_map_set(state, obj, key, val);
  251. sol_obj_free(val);
  252. sol_obj_free(key);
  253. sol_obj_free(obj);
  254. return sol_incref(state->None);
  255. }
  256. sol_object_t *sol_f_range(sol_state_t *state, sol_object_t *args) {
  257. sol_object_t *res = sol_new_list(state), *bound = sol_cast_int(state, sol_list_get_index(state, args, 0));
  258. int i;
  259. for(i=0; i<bound->ival; i++) {
  260. sol_list_insert(state, res, sol_list_len(state, res), sol_new_int(state, i));
  261. }
  262. sol_obj_free(bound);
  263. return res;
  264. }
  265. sol_object_t *sol_f_exec(sol_state_t *state, sol_object_t *args) {
  266. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  267. stmt_node *program;
  268. program = sol_compile(prgstr->str);
  269. if(!program) {
  270. return sol_set_error_string(state, "Compilation failure");
  271. }
  272. // XXX should st_free(program);
  273. sol_exec(state, program);
  274. return sol_incref(state->None);
  275. }
  276. sol_object_t *sol_f_eval(sol_state_t *state, sol_object_t *args) {
  277. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  278. stmt_node *program;
  279. program = sol_compile(prgstr->str);
  280. if(!program) {
  281. return sol_set_error_string(state, "Compilation failure");
  282. }
  283. if(program->type != ST_LIST || program->stmtlist->stmt->type != ST_EXPR) {
  284. return sol_set_error_string(state, "Not an expression");
  285. }
  286. // XXX should st_free(program);
  287. return sol_eval(state, program->stmtlist->stmt->expr);
  288. }
  289. sol_object_t *sol_f_execfile(sol_state_t *state, sol_object_t *args) {
  290. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  291. stmt_node *program;
  292. FILE *f = fopen(prgstr->str, "r");
  293. char *s;
  294. long sz;
  295. if(!f) {
  296. return sol_set_error_string(state, "File open failure");
  297. }
  298. fseek(f, 0, SEEK_END);
  299. sz = ftell(f);
  300. fseek(f, 0, SEEK_SET);
  301. s = malloc(sz+1);
  302. if(!s) {
  303. fclose(f);
  304. return sol_set_error_string(state, "File memory allocation failure");
  305. }
  306. fread(s, 1, sz, f);
  307. s[sz]=0;
  308. fclose(f);
  309. program = sol_compile(s);
  310. free(s);
  311. if(!program) {
  312. return sol_set_error_string(state, "Compilation failure");
  313. }
  314. // XXX should st_free(program);
  315. sol_exec(state, program);
  316. return sol_incref(state->None);
  317. }
  318. sol_object_t *sol_f_parse(sol_state_t *state, sol_object_t *args) {
  319. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  320. stmt_node *program = sol_compile(prgstr->str);
  321. sol_obj_free(prg);
  322. sol_obj_free(prgstr);
  323. if(!program) {
  324. return sol_set_error_string(state, "Compilation failure");
  325. }
  326. return sol_new_stmtnode(state, program);
  327. }
  328. sol_object_t *sol_f_debug_getref(sol_state_t *state, sol_object_t *args) {
  329. sol_object_t *obj = sol_list_get_index(state, args, 0);
  330. sol_object_t *res = sol_new_int(state, obj->refcnt - 2); // NB: We grabbed a reference, and there's one in the arglist, so account for them.
  331. sol_obj_free(obj);
  332. return res;
  333. }
  334. sol_object_t *sol_f_debug_setref(sol_state_t *state, sol_object_t *args) {
  335. sol_object_t *obj = sol_list_get_index(state, args, 0), *cnt = sol_list_get_index(state, args, 1);
  336. obj->refcnt = sol_cast_int(state, cnt)->ival + 2; // NB: As above.
  337. sol_obj_free(cnt);
  338. sol_obj_free(obj);
  339. return sol_incref(state->None);
  340. }
  341. sol_object_t *sol_f_debug_closure(sol_state_t *state, sol_object_t *args) {
  342. sol_object_t *func = sol_list_get_index(state, args, 0);
  343. sol_object_t *res = sol_incref(func->closure);
  344. sol_obj_free(func);
  345. return res;
  346. }
  347. sol_object_t *sol_f_debug_globals(sol_state_t *state, sol_object_t *args) {
  348. return sol_list_get_index(state, state->scopes, sol_list_len(state, state->scopes)-1);
  349. }
  350. sol_object_t *sol_f_debug_locals(sol_state_t *state, sol_object_t *args) {
  351. return sol_list_get_index(state, state->scopes, 0);
  352. }
  353. sol_object_t *sol_f_debug_scopes(sol_state_t *state, sol_object_t *args) {
  354. return sol_incref(state->scopes);
  355. }
  356. sol_object_t *sol_f_iter_str(sol_state_t *state, sol_object_t *args) {
  357. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  358. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  359. char temp[2] = {0, 0};
  360. if(sol_is_none(state, index)) {
  361. sol_obj_free(index);
  362. index = sol_new_int(state, 0);
  363. sol_map_set_name(state, local, "idx", index);
  364. }
  365. if(index->ival >= strlen(obj->str)) {
  366. sol_obj_free(index);
  367. sol_obj_free(obj);
  368. sol_obj_free(local);
  369. return sol_incref(state->StopIteration);
  370. }
  371. temp[0] = obj->str[index->ival];
  372. res = sol_new_string(state, temp);
  373. index->ival++;
  374. sol_obj_free(index);
  375. sol_obj_free(local);
  376. sol_obj_free(obj);
  377. return res;
  378. }
  379. sol_object_t *sol_f_iter_list(sol_state_t *state, sol_object_t *args) {
  380. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  381. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  382. if(sol_is_none(state, index)) {
  383. sol_obj_free(index);
  384. index = sol_new_int(state, 0);
  385. sol_map_set_name(state, local, "idx", index);
  386. sol_obj_free(index);
  387. }
  388. if(index->ival >= sol_list_len(state, obj)) {
  389. sol_obj_free(index);
  390. sol_obj_free(obj);
  391. sol_obj_free(local);
  392. return sol_incref(state->StopIteration);
  393. }
  394. res = sol_list_get_index(state, obj, index->ival);
  395. index->ival++;
  396. sol_obj_free(local);
  397. sol_obj_free(obj);
  398. return res;
  399. }
  400. sol_object_t *sol_f_iter_map(sol_state_t *state, sol_object_t *args) {
  401. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  402. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  403. if(sol_is_none(state, index)) {
  404. sol_obj_free(index);
  405. index = sol_new_int(state, 0);
  406. sol_map_set_name(state, local, "idx", index);
  407. sol_obj_free(index);
  408. }
  409. if(index->ival >= dsl_seq_len(obj->seq)) {
  410. sol_obj_free(index);
  411. sol_obj_free(obj);
  412. sol_obj_free(local);
  413. return sol_incref(state->StopIteration);
  414. }
  415. res = sol_incref(AS_OBJ(dsl_seq_get(obj->seq, index->ival))->key);
  416. index->ival++;
  417. sol_obj_free(local);
  418. sol_obj_free(obj);
  419. return res;
  420. }
  421. sol_object_t *sol_f_singlet_tostring(sol_state_t *state, sol_object_t *args) {
  422. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = sol_new_string(state, obj->str);
  423. sol_obj_free(obj);
  424. return res;
  425. }
  426. sol_object_t *sol_f_int_add(sol_state_t *state, sol_object_t *args) {
  427. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  428. sol_object_t *res = sol_new_int(state, a->ival + b->ival);
  429. sol_obj_free(a);
  430. sol_obj_free(b);
  431. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  432. return res;
  433. }
  434. sol_object_t *sol_f_int_sub(sol_state_t *state, sol_object_t *args) {
  435. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  436. sol_object_t *res = sol_new_int(state, a->ival - b->ival);
  437. sol_obj_free(a);
  438. sol_obj_free(b);
  439. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  440. return res;
  441. }
  442. sol_object_t *sol_f_int_mul(sol_state_t *state, sol_object_t *args) {
  443. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  444. sol_object_t *res = sol_new_int(state, a->ival * b->ival);
  445. sol_obj_free(a);
  446. sol_obj_free(b);
  447. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  448. return res;
  449. }
  450. sol_object_t *sol_f_int_div(sol_state_t *state, sol_object_t *args) {
  451. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  452. sol_object_t *res = sol_new_int(state, a->ival / b->ival);
  453. sol_obj_free(a);
  454. sol_obj_free(b);
  455. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  456. return res;
  457. }
  458. sol_object_t *sol_f_int_mod(sol_state_t *state, sol_object_t *args) {
  459. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  460. sol_object_t *res = sol_new_int(state, a->ival % b->ival);
  461. sol_obj_free(a);
  462. sol_obj_free(b);
  463. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  464. return res;
  465. }
  466. sol_object_t *sol_f_int_pow(sol_state_t *state, sol_object_t *args) {
  467. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  468. sol_object_t *res = sol_new_int(state, (long) pow((double) a->ival, b->ival));
  469. sol_obj_free(a);
  470. sol_obj_free(b);
  471. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  472. return res;
  473. }
  474. sol_object_t *sol_f_int_band(sol_state_t *state, sol_object_t *args) {
  475. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  476. sol_object_t *res = sol_new_int(state, a->ival & b->ival);
  477. sol_obj_free(a);
  478. sol_obj_free(b);
  479. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  480. return res;
  481. }
  482. sol_object_t *sol_f_int_bor(sol_state_t *state, sol_object_t *args) {
  483. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  484. sol_object_t *res = sol_new_int(state, a->ival | b->ival);
  485. sol_obj_free(a);
  486. sol_obj_free(b);
  487. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  488. return res;
  489. }
  490. sol_object_t *sol_f_int_bxor(sol_state_t *state, sol_object_t *args) {
  491. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  492. sol_object_t *res = sol_new_int(state, a->ival ^ b->ival);
  493. sol_obj_free(a);
  494. sol_obj_free(b);
  495. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  496. return res;
  497. }
  498. sol_object_t *sol_f_int_blsh(sol_state_t *state, sol_object_t *args) {
  499. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  500. sol_object_t *res = sol_new_int(state, a->ival << b->ival);
  501. sol_obj_free(a);
  502. sol_obj_free(b);
  503. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  504. return res;
  505. }
  506. sol_object_t *sol_f_int_brsh(sol_state_t *state, sol_object_t *args) {
  507. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  508. sol_object_t *res = sol_new_int(state, a->ival >> b->ival);
  509. sol_obj_free(a);
  510. sol_obj_free(b);
  511. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  512. return res;
  513. }
  514. sol_object_t *sol_f_int_bnot(sol_state_t *state, sol_object_t *args) {
  515. sol_object_t *a = sol_list_get_index(state, args, 0);
  516. sol_object_t *res = sol_new_int(state, ~a->ival);
  517. sol_obj_free(a);
  518. return res;
  519. }
  520. sol_object_t *sol_f_int_cmp(sol_state_t *state, sol_object_t *args) {
  521. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  522. sol_object_t *res = sol_new_int(state, a->ival==b->ival? 0 : (a->ival<b->ival? -1 : 1));
  523. sol_obj_free(a);
  524. sol_obj_free(b);
  525. return res;
  526. }
  527. sol_object_t *sol_f_int_toint(sol_state_t *state, sol_object_t *args) {
  528. return sol_list_get_index(state, args, 0);
  529. }
  530. sol_object_t *sol_f_int_tofloat(sol_state_t *state, sol_object_t *args) {
  531. sol_object_t *a = sol_list_get_index(state, args, 0);
  532. sol_object_t *res = sol_new_float(state, (double) a->ival);
  533. sol_obj_free(a);
  534. return res;
  535. }
  536. sol_object_t *sol_f_int_tostring(sol_state_t *state, sol_object_t *args) {
  537. sol_object_t *a = sol_list_get_index(state, args, 0);
  538. char *s = _itoa(a->ival);
  539. sol_object_t *res = sol_new_string(state, s);
  540. sol_obj_free(a);
  541. free(s);
  542. return res;
  543. }
  544. sol_object_t *sol_f_float_add(sol_state_t *state, sol_object_t *args) {
  545. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  546. sol_object_t *res = sol_new_float(state, a->fval + b->fval);
  547. sol_obj_free(a);
  548. sol_obj_free(b);
  549. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  550. return res;
  551. }
  552. sol_object_t *sol_f_float_sub(sol_state_t *state, sol_object_t *args) {
  553. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  554. sol_object_t *res = sol_new_float(state, a->fval - b->fval);
  555. sol_obj_free(a);
  556. sol_obj_free(b);
  557. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  558. return res;
  559. }
  560. sol_object_t *sol_f_float_mul(sol_state_t *state, sol_object_t *args) {
  561. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  562. sol_object_t *res = sol_new_float(state, a->fval * b->fval);
  563. sol_obj_free(a);
  564. sol_obj_free(b);
  565. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  566. return res;
  567. }
  568. sol_object_t *sol_f_float_div(sol_state_t *state, sol_object_t *args) {
  569. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  570. sol_object_t *res = sol_new_float(state, a->fval / b->fval);
  571. sol_obj_free(a);
  572. sol_obj_free(b);
  573. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  574. return res;
  575. }
  576. sol_object_t *sol_f_float_pow(sol_state_t *state, sol_object_t *args) {
  577. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  578. sol_object_t *res = sol_new_float(state, pow(a->fval, b->fval));
  579. sol_obj_free(a);
  580. sol_obj_free(b);
  581. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  582. return res;
  583. }
  584. sol_object_t *sol_f_float_cmp(sol_state_t *state, sol_object_t *args) {
  585. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  586. sol_object_t *res = sol_new_int(state, a->fval==b->fval? 0 : (a->fval<b->fval? -1 : 1));
  587. sol_obj_free(a);
  588. sol_obj_free(b);
  589. return res;
  590. }
  591. sol_object_t *sol_f_float_toint(sol_state_t *state, sol_object_t *args) {
  592. sol_object_t *a = sol_list_get_index(state, args, 0);
  593. sol_object_t *res = sol_new_int(state, (int) a->fval);
  594. sol_obj_free(a);
  595. return res;
  596. }
  597. sol_object_t *sol_f_float_tofloat(sol_state_t *state, sol_object_t *args) {
  598. return sol_list_get_index(state, args, 0);
  599. }
  600. sol_object_t *sol_f_float_tostring(sol_state_t *state, sol_object_t *args) {
  601. sol_object_t *a = sol_list_get_index(state, args, 0);
  602. char *s = _ftoa(a->fval);
  603. sol_object_t *res = sol_new_string(state, s);
  604. sol_obj_free(a);
  605. free(s);
  606. return res;
  607. }
  608. sol_object_t *sol_f_str_add(sol_state_t *state, sol_object_t *args) {
  609. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_string(state, sol_list_get_index(state, args, 1));
  610. sol_object_t *res = sol_string_concat(state, a, b);
  611. sol_obj_free(a);
  612. sol_obj_free(b);
  613. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  614. return res;
  615. }
  616. sol_object_t *sol_f_str_mul(sol_state_t *state, sol_object_t *args) {
  617. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  618. int n = strlen(a->str) * b->ival + 1;
  619. char *s = malloc(n);
  620. int i;
  621. s[0]='\0';
  622. for(i = 0; i < b->ival; i++) strncat(s, a->str, n);
  623. sol_object_t *res = sol_new_string(state, s);
  624. sol_obj_free(a);
  625. sol_obj_free(b);
  626. free(s);
  627. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  628. return res;
  629. }
  630. sol_object_t *sol_f_str_cmp(sol_state_t *state, sol_object_t *args) {
  631. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_string(state, sol_list_get_index(state, args, 1));
  632. sol_object_t *res = sol_new_int(state, strcmp(a->str, b->str));
  633. sol_obj_free(a);
  634. sol_obj_free(b);
  635. return res;
  636. }
  637. sol_object_t *sol_f_str_len(sol_state_t *state, sol_object_t *args) {
  638. sol_object_t *a = sol_list_get_index(state, args, 0);
  639. sol_object_t *res = sol_new_int(state, strlen(a->str));
  640. sol_obj_free(a);
  641. return res;
  642. }
  643. sol_object_t *sol_f_str_index(sol_state_t *state, sol_object_t *args) {
  644. sol_object_t *str = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *idx, *funcs, *res;
  645. char buf[2]={0, 0};
  646. if(sol_is_string(key)) {
  647. funcs = sol_get_methods_name(state, "string");
  648. res = sol_map_get(state, funcs, key);
  649. sol_obj_free(funcs);
  650. return res;
  651. }
  652. idx = sol_cast_int(state, key);
  653. if(idx->ival>=0 && idx->ival<strlen(str->str)) {
  654. buf[0] = str->str[idx->ival];
  655. }
  656. sol_obj_free(str);
  657. sol_obj_free(key);
  658. sol_obj_free(idx);
  659. return sol_new_string(state, buf);
  660. }
  661. sol_object_t *sol_f_str_iter(sol_state_t *state, sol_object_t *args) {
  662. return sol_new_cfunc(state, sol_f_iter_str);
  663. }
  664. sol_object_t *sol_f_str_toint(sol_state_t *state, sol_object_t *args) {
  665. sol_object_t *a = sol_list_get_index(state, args, 0);
  666. sol_object_t *res = sol_new_int(state, atoi(a->str));
  667. sol_obj_free(a);
  668. return res;
  669. }
  670. sol_object_t *sol_f_str_tofloat(sol_state_t *state, sol_object_t *args) {
  671. sol_object_t *a = sol_list_get_index(state, args, 0);
  672. sol_object_t *res = sol_new_float(state, atof(a->str));
  673. sol_obj_free(a);
  674. return res;
  675. }
  676. sol_object_t *sol_f_str_tostring(sol_state_t *state, sol_object_t *args) {
  677. return sol_list_get_index(state, args, 0);
  678. }
  679. sol_object_t *sol_f_str_repr(sol_state_t *state, sol_object_t *args) {
  680. sol_object_t *obj = sol_list_get_index(state, args, 0), *cur = sol_new_string(state, "\""), *next = sol_string_concat(state, cur, obj);
  681. sol_obj_free(cur);
  682. cur = next;
  683. next = sol_string_concat_cstr(state, cur, "\"");
  684. sol_obj_free(cur);
  685. return next;
  686. }
  687. sol_object_t *sol_f_str_sub(sol_state_t *state, sol_object_t *args) {
  688. sol_object_t *str = sol_list_get_index(state, args, 0), *low = sol_list_get_index(state, args, 1), *high = sol_list_get_index(state, args, 2);
  689. sol_object_t *ilow, *ihigh;
  690. size_t len = strlen(str->str), i;
  691. char *s;
  692. if(sol_is_none(state, low)) {
  693. ilow = sol_new_int(state, 0);
  694. } else {
  695. ilow = sol_cast_int(state, low);
  696. }
  697. if(sol_is_none(state, high)) {
  698. ihigh = sol_new_int(state, len);
  699. } else {
  700. ihigh = sol_cast_int(state, high);
  701. }
  702. sol_obj_free(low);
  703. sol_obj_free(high);
  704. if(ilow->ival<0) {
  705. ilow->ival+=len;
  706. if(ilow->ival<0) ilow->ival=0;
  707. }
  708. if(ilow->ival>len) ilow->ival=len;
  709. if(ihigh->ival<0) {
  710. ihigh->ival+=len;
  711. if(ihigh->ival<0) ihigh->ival=0;
  712. }
  713. if(ihigh->ival>len) ihigh->ival=len;
  714. if(ilow->ival >= ihigh->ival) {
  715. sol_obj_free(ilow);
  716. sol_obj_free(ihigh);
  717. sol_obj_free(str);
  718. return sol_new_string(state, "");
  719. }
  720. s = malloc(ihigh->ival - ilow->ival + 1);
  721. for(i=ilow->ival; i<ihigh->ival; i++) s[i-ilow->ival] = str->str[i];
  722. s[ihigh->ival-ilow->ival]='\0';
  723. sol_obj_free(ihigh);
  724. sol_obj_free(ilow);
  725. sol_obj_free(str);
  726. return sol_new_string(state, s);
  727. }
  728. sol_object_t *sol_f_str_split(sol_state_t *state, sol_object_t *args) {
  729. sol_object_t *str = sol_list_get_index(state, args, 0), *tok = sol_list_get_index(state, args, 1), *stok = sol_cast_string(state, tok);
  730. sol_object_t *res = sol_new_list(state), *opart;
  731. char *s = strdup(str->str);
  732. char *part = strtok(s, stok->str);
  733. sol_obj_free(tok);
  734. sol_obj_free(str);
  735. if(!part) {
  736. sol_obj_free(res);
  737. sol_obj_free(stok);
  738. return sol_incref(state->None);
  739. }
  740. opart = sol_new_string(state, part);
  741. sol_list_insert(state, res, 0, opart);
  742. sol_obj_free(opart);
  743. while(part = strtok(NULL, stok->str)) {
  744. opart = sol_new_string(state, part);
  745. sol_list_insert(state, res, sol_list_len(state, res), opart);
  746. sol_obj_free(opart);
  747. }
  748. sol_obj_free(stok);
  749. return res;
  750. }
  751. sol_object_t *sol_f_list_add(sol_state_t *state, sol_object_t *args) {
  752. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *ls;
  753. if(!sol_is_list(b)) {
  754. sol_obj_free(a);
  755. sol_obj_free(b);
  756. return sol_set_error_string(state, "Adding list to non-list");
  757. }
  758. ls = sol_list_copy(state, a);
  759. sol_list_append(state, ls, b);
  760. sol_obj_free(a);
  761. sol_obj_free(b);
  762. return ls;
  763. }
  764. sol_object_t *sol_f_list_mul(sol_state_t *state, sol_object_t *args) {
  765. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1)), *ls;
  766. int i;
  767. if(sol_has_error(state)) {
  768. sol_obj_free(a);
  769. sol_obj_free(b);
  770. return sol_incref(state->None);
  771. }
  772. ls = sol_new_list(state);
  773. for(i = 0; i < b->ival; i++) {
  774. sol_list_append(state, ls, a);
  775. if(sol_has_error(state)) {
  776. sol_obj_free(a);
  777. sol_obj_free(b);
  778. return sol_incref(state->None);
  779. }
  780. }
  781. return ls;
  782. }
  783. sol_object_t *sol_f_list_index(sol_state_t *state, sol_object_t *args) {
  784. sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *ival;
  785. sol_object_t *res, *funcs;
  786. if(sol_is_string(b)) {
  787. funcs = sol_get_methods_name(state, "list");
  788. res = sol_map_get(state, funcs, b);
  789. sol_obj_free(funcs);
  790. } else {
  791. ival = sol_cast_int(state, b);
  792. res = sol_list_get_index(state, ls, ival->ival);
  793. sol_obj_free(ival);
  794. }
  795. sol_obj_free(ls);
  796. sol_obj_free(b);
  797. return res;
  798. }
  799. sol_object_t *sol_f_list_setindex(sol_state_t *state, sol_object_t *args) {
  800. sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args ,1));
  801. sol_object_t *val = sol_list_get_index(state, args, 2);
  802. sol_list_set_index(state, ls, b->ival, val);
  803. sol_obj_free(ls);
  804. sol_obj_free(b);
  805. sol_obj_free(val);
  806. return sol_incref(state->None);
  807. }
  808. sol_object_t *sol_f_list_len(sol_state_t *state, sol_object_t *args) {
  809. sol_object_t *ls = sol_list_get_index(state, args, 0);
  810. sol_object_t *res = sol_new_int(state, sol_list_len(state, ls));
  811. sol_obj_free(ls);
  812. return res;
  813. }
  814. sol_object_t *sol_f_list_iter(sol_state_t *state, sol_object_t *args) {
  815. return sol_new_cfunc(state, sol_f_iter_list);
  816. }
  817. sol_object_t *sol_f_list_tostring(sol_state_t *state, sol_object_t *args) {
  818. sol_object_t *cur = sol_new_string(state, "["), *next, *str, *obj = sol_list_get_index(state, args, 0), *item;
  819. dsl_seq_iter *iter = dsl_new_seq_iter(obj->seq);
  820. char s[64];
  821. while(!dsl_seq_iter_is_invalid(iter)) {
  822. item = AS_OBJ(dsl_seq_iter_at(iter));
  823. if(test_seen(item)) {
  824. snprintf(s, 64, "... (%p)", item);
  825. next = sol_string_concat_cstr(state, cur, s);
  826. } else {
  827. str = sol_cast_repr(state, item);
  828. next = sol_string_concat(state, cur, str);
  829. sol_obj_free(str);
  830. }
  831. sol_obj_free(cur);
  832. cur = next;
  833. if(!dsl_seq_iter_at_end(iter)) {
  834. next = sol_string_concat_cstr(state, cur, ", ");
  835. sol_obj_free(cur);
  836. cur = next;
  837. }
  838. dsl_seq_iter_next(iter);
  839. }
  840. next = sol_string_concat_cstr(state, cur, "]");
  841. sol_obj_free(cur);
  842. dsl_free_seq_iter(iter);
  843. return next;
  844. }
  845. sol_object_t *sol_f_list_copy(sol_state_t *state, sol_object_t *args) {
  846. sol_object_t *list = sol_list_get_index(state, args, 0);
  847. sol_object_t *res = sol_list_copy(state, list);
  848. sol_obj_free(list);
  849. return res;
  850. }
  851. sol_object_t *sol_f_list_insert(sol_state_t *state, sol_object_t *args) {
  852. sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_cast_int(state, sol_list_get_index(state, args, 1)), *obj = sol_list_get_index(state, args, 2);
  853. sol_list_insert(state, list, idx->ival, obj);
  854. sol_obj_free(list);
  855. return sol_incref(state->None);
  856. }
  857. sol_object_t *sol_f_list_remove(sol_state_t *state, sol_object_t *args) {
  858. sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_cast_int(state, sol_list_get_index(state, args, 1));
  859. sol_list_remove(state, list, idx->ival);
  860. sol_obj_free(list);
  861. return sol_incref(state->None);
  862. }
  863. sol_object_t *sol_f_list_truncate(sol_state_t *state, sol_object_t *args) {
  864. sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1);
  865. sol_object_t *res = sol_list_truncate(state, list, idx->ival);
  866. sol_obj_free(list);
  867. return res;
  868. }
  869. sol_object_t *sol_f_list_map(sol_state_t *state, sol_object_t *args) {
  870. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  871. sol_object_t *fargs = sol_new_list(state), *item;
  872. int idx=0, len = sol_list_len(state, list);
  873. sol_list_insert(state, fargs, 0, func);
  874. while(idx<len) {
  875. item = sol_list_get_index(state, list, idx);
  876. sol_list_insert(state, fargs, 1, item);
  877. sol_obj_free(item);
  878. item = func->ops->call(state, fargs);
  879. if(sol_has_error(state)) return list;
  880. sol_list_remove(state, fargs, 1);
  881. sol_list_set_index(state, list, idx, item);
  882. sol_obj_free(item);
  883. idx++;
  884. }
  885. sol_obj_free(fargs);
  886. sol_obj_free(func);
  887. return list;
  888. }
  889. sol_object_t *sol_f_list_filter(sol_state_t *state, sol_object_t *args) {
  890. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  891. sol_object_t *fargs = sol_new_list(state), *item, *ival;
  892. int idx=0, len = sol_list_len(state, list);
  893. sol_list_insert(state, fargs, 0, func);
  894. while(idx<len) {
  895. item = sol_list_get_index(state, list, idx);
  896. sol_list_insert(state, fargs, 1, item);
  897. sol_obj_free(item);
  898. item = func->ops->call(state, fargs);
  899. if(sol_has_error(state)) return list;
  900. ival = sol_cast_int(state, item);
  901. if(ival->ival) {
  902. idx++;
  903. } else {
  904. sol_list_remove(state, list, idx);
  905. len--;
  906. }
  907. sol_obj_free(item);
  908. sol_obj_free(ival);
  909. }
  910. sol_obj_free(fargs);
  911. sol_obj_free(func);
  912. return list;
  913. }
  914. sol_object_t *sol_f_map_add(sol_state_t *state, sol_object_t *args) {
  915. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *map;
  916. if(!sol_is_map(b)) {
  917. sol_obj_free(a);
  918. sol_obj_free(b);
  919. return sol_set_error_string(state, "Adding map to non-map");
  920. }
  921. map = sol_map_copy(state, a);
  922. sol_map_merge(state, map, b);
  923. sol_obj_free(a);
  924. sol_obj_free(b);
  925. return map;
  926. }
  927. sol_object_t *sol_f_map_index(sol_state_t *state, sol_object_t *args) {
  928. sol_object_t *map = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  929. sol_object_t *indexf = sol_map_get_name(state, map, "__index");
  930. sol_object_t *res = NULL, *newls;
  931. if(!sol_is_none(state, indexf)) {
  932. if(indexf->ops->call && (sol_is_func(indexf) || sol_is_cfunc(indexf)) && indexf->ops->call != sol_f_not_impl) {
  933. newls = sol_new_list(state);
  934. sol_list_insert(state, newls, 0, indexf);
  935. sol_list_append(state, newls, args);
  936. res = indexf->ops->call(state, newls);
  937. sol_obj_free(newls);
  938. } else if(indexf->ops->index && indexf->ops->index != sol_f_not_impl) {
  939. newls = sol_new_list(state);
  940. sol_list_insert(state, newls, 0, indexf);
  941. sol_list_insert(state, newls, 1, b);
  942. res = indexf->ops->index(state, newls);
  943. sol_obj_free(newls);
  944. }
  945. }
  946. if(!res) res = sol_map_get(state, map, b);
  947. sol_obj_free(indexf);
  948. sol_obj_free(map);
  949. sol_obj_free(b);
  950. return res;
  951. }
  952. sol_object_t *sol_f_map_setindex(sol_state_t *state, sol_object_t *args) {
  953. sol_object_t *map = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  954. sol_object_t *val = sol_list_get_index(state, args, 2);
  955. sol_object_t *setindexf = sol_map_get_name(state, map, "__setindex"), *newls;
  956. if(!sol_is_none(state, setindexf)) {
  957. if(setindexf->ops->call && (sol_is_func(setindexf) || sol_is_cfunc(setindexf)) && setindexf->ops->call != sol_f_not_impl) {
  958. newls = sol_new_list(state);
  959. sol_list_insert(state, newls, 0, setindexf);
  960. sol_list_append(state, newls, args);
  961. sol_obj_free(setindexf->ops->call(state, newls));
  962. sol_obj_free(newls);
  963. return sol_incref(state->None);
  964. } else if(setindexf->ops->setindex && setindexf->ops->setindex != sol_f_not_impl) {
  965. newls = sol_new_list(state);
  966. sol_list_insert(state, newls, 0, setindexf);
  967. sol_list_insert(state, newls, 1, b);
  968. sol_list_insert(state, newls, 2, val);
  969. sol_obj_free(setindexf->ops->index(state, newls));
  970. sol_obj_free(newls);
  971. return sol_incref(state->None);
  972. }
  973. }
  974. sol_obj_free(setindexf);
  975. sol_map_set(state, map, b, val);
  976. sol_obj_free(map);
  977. sol_obj_free(b);
  978. sol_obj_free(val);
  979. return sol_incref(state->None);
  980. }
  981. sol_object_t *sol_f_map_call(sol_state_t *state, sol_object_t *args) {
  982. sol_object_t *map = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  983. sol_object_t *callf = sol_map_get_name(state, map, "__call"), *res = NULL;
  984. if(!sol_is_none(state, callf)) {
  985. if(callf->ops->call) {
  986. sol_list_insert(state, fargs, 0, callf);
  987. sol_list_insert(state, fargs, 1, map);
  988. res = callf->ops->call(state, fargs);
  989. }
  990. }
  991. sol_obj_free(map);
  992. sol_obj_free(fargs);
  993. sol_obj_free(callf);
  994. if(res) return res;
  995. return sol_set_error_string(state, "Call map without call method");
  996. }
  997. sol_object_t *sol_f_map_len(sol_state_t *state, sol_object_t *args) {
  998. sol_object_t *map = sol_list_get_index(state, args, 0);
  999. sol_object_t *res = sol_new_int(state, sol_map_len(state, map));
  1000. sol_obj_free(map);
  1001. return res;
  1002. }
  1003. sol_object_t *sol_f_map_iter(sol_state_t *state, sol_object_t *args) {
  1004. return sol_new_cfunc(state, sol_f_iter_map);
  1005. }
  1006. sol_object_t *sol_f_map_tostring(sol_state_t *state, sol_object_t *args) {
  1007. sol_object_t *cur = sol_new_string(state, "{"), *next, *str, *obj = sol_list_get_index(state, args, 0), *item;
  1008. dsl_seq_iter *iter = dsl_new_seq_iter(obj->seq);
  1009. char s[64];
  1010. while(!dsl_seq_iter_is_invalid(iter)) {
  1011. item = AS_OBJ(dsl_seq_iter_at(iter));
  1012. if(test_seen(item)) {
  1013. snprintf(s, 64, "... (%p)", item);
  1014. next = sol_string_concat_cstr(state, cur, s);
  1015. } else {
  1016. str = sol_cast_repr(state, item);
  1017. next = sol_string_concat(state, cur, str);
  1018. sol_obj_free(str);
  1019. }
  1020. sol_obj_free(cur);
  1021. cur = next;
  1022. if(!dsl_seq_iter_at_end(iter)) {
  1023. next = sol_string_concat_cstr(state, cur, ", ");
  1024. sol_obj_free(cur);
  1025. cur = next;
  1026. }
  1027. dsl_seq_iter_next(iter);
  1028. }
  1029. next = sol_string_concat_cstr(state, cur, "}");
  1030. sol_obj_free(cur);
  1031. dsl_free_seq_iter(iter);
  1032. sol_obj_free(obj);
  1033. return next;
  1034. }
  1035. sol_object_t *sol_f_mcell_tostring(sol_state_t *state, sol_object_t *args) {
  1036. sol_object_t *mcell = sol_list_get_index(state, args, 0), *cur = sol_new_string(state, "["), *next, *str;
  1037. char s[64];
  1038. if(test_seen(mcell->key)) {
  1039. snprintf(s, 64, "... (%p)", mcell->key);
  1040. next = sol_string_concat_cstr(state, cur, s);
  1041. } else {
  1042. str = sol_cast_repr(state, mcell->key);
  1043. next = sol_string_concat(state, cur, str);
  1044. sol_obj_free(str);
  1045. }
  1046. sol_obj_free(cur);
  1047. cur = next;
  1048. next = sol_string_concat_cstr(state, cur, "] = ");
  1049. sol_obj_free(cur);
  1050. cur = next;
  1051. if(test_seen(mcell->val)) {
  1052. snprintf(s, 64, "... (%p)", mcell->val);
  1053. next = sol_string_concat_cstr(state, cur, s);
  1054. } else {
  1055. str = sol_cast_repr(state, mcell->val);
  1056. next = sol_string_concat(state, cur, str);
  1057. sol_obj_free(str);
  1058. }
  1059. sol_obj_free(cur);
  1060. sol_obj_free(mcell);
  1061. return next;
  1062. }
  1063. sol_object_t *sol_f_func_index(sol_state_t *state, sol_object_t *args) {
  1064. sol_object_t *func = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *res;
  1065. identlist_node *curi;
  1066. int i=0;
  1067. if(!sol_is_string(key)) {
  1068. res = sol_map_get(state, func->udata, key);
  1069. } else {
  1070. if(sol_string_eq(state, key, "name")) {
  1071. res = sol_new_string(state, func->fname);
  1072. } else if(sol_string_eq(state, key, "closure")) {
  1073. res = sol_incref(func->closure);
  1074. } else if(sol_string_eq(state, key, "udata")) {
  1075. res = sol_incref(func->udata);
  1076. } else if(sol_string_eq(state, key, "stmt")) {
  1077. res = sol_new_stmtnode(state, (stmt_node *) func->func);
  1078. } else if(sol_string_eq(state, key, "args")) {
  1079. res = sol_new_list(state);
  1080. curi = func->args;
  1081. while(curi) {
  1082. sol_list_insert(state, res, i++, sol_new_string(state, curi->ident));
  1083. curi = curi->next;
  1084. }
  1085. } else {
  1086. res = sol_map_get(state, func->udata, key);
  1087. }
  1088. }
  1089. sol_obj_free(func);
  1090. sol_obj_free(key);
  1091. return res;
  1092. }
  1093. sol_object_t *sol_f_func_setindex(sol_state_t *state, sol_object_t *args) {
  1094. sol_object_t *func = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *val = sol_list_get_index(state, args, 2), *temp;
  1095. if(sol_string_eq(state, key, "name") && sol_is_string(val)) {
  1096. free(func->fname);
  1097. func->fname = strdup(val->str);
  1098. } else if(sol_string_eq(state, key, "closure") && sol_is_map(val)) {
  1099. temp = func->closure;
  1100. func->closure = sol_incref(val);
  1101. sol_obj_free(temp);
  1102. } else if(sol_string_eq(state, key, "udata") && sol_is_map(val)) {
  1103. temp = func->udata;
  1104. func->udata = sol_incref(val);
  1105. sol_obj_free(temp);
  1106. } else if(sol_string_eq(state, key, "stmt") && sol_is_aststmt(val)) {
  1107. func->func = val->node;
  1108. } else {
  1109. sol_map_set(state, func->udata, key, val);
  1110. }
  1111. sol_obj_free(func);
  1112. sol_obj_free(key);
  1113. sol_obj_free(val);
  1114. return sol_incref(state->None);
  1115. }
  1116. sol_object_t *sol_f_func_tostring(sol_state_t *state, sol_object_t *args) {
  1117. sol_object_t *func = sol_list_get_index(state, args, 0), *ret;
  1118. char *s = malloc(256 * sizeof(char));
  1119. if(func->fname) {
  1120. snprintf(s, 256, "<Function %s>", func->fname);
  1121. } else {
  1122. snprintf(s, 256, "<Function>");
  1123. }
  1124. ret = sol_new_string(state, s);
  1125. free(s);
  1126. sol_obj_free(func);
  1127. return ret;
  1128. }
  1129. sol_object_t *sol_f_cfunc_call(sol_state_t *state, sol_object_t *args) {
  1130. sol_object_t *func = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  1131. sol_object_t *res = func->cfunc(state, fargs);
  1132. sol_obj_free(func);
  1133. sol_obj_free(fargs);
  1134. return res;
  1135. }
  1136. sol_object_t *sol_f_cfunc_tostring(sol_state_t *state, sol_object_t *args) {
  1137. return sol_new_string(state, "<CFunction>");
  1138. }
  1139. sol_object_t *sol_f_astnode_call(sol_state_t *state, sol_object_t *args) {
  1140. sol_object_t *obj = sol_list_get_index(state, args, 0), *env=NULL, *res;
  1141. stmt_node *stmt = (stmt_node *) obj->node;
  1142. expr_node *expr = (expr_node *) obj->node;
  1143. sol_obj_free(obj);
  1144. if(sol_list_len(state, args)>1) {
  1145. env = sol_list_get_index(state, args, 1);
  1146. sol_state_push_scope(state, env);
  1147. }
  1148. if(sol_is_aststmt(obj)) {
  1149. sol_exec(state, stmt);
  1150. res = sol_incref(state->None);
  1151. } else {
  1152. res = sol_eval(state, expr);
  1153. }
  1154. if(env) {
  1155. sol_state_pop_scope(state);
  1156. sol_obj_free(env);
  1157. }
  1158. return res;
  1159. }
  1160. sol_object_t *sol_f_astnode_index(sol_state_t *state, sol_object_t *args) {
  1161. sol_object_t *obj = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *str = sol_cast_string(state, key), *res=NULL, *pair;
  1162. stmt_node *stmt = (stmt_node *) obj->node;
  1163. stmtlist_node *curs;
  1164. expr_node *expr = (expr_node *) obj->node;
  1165. exprlist_node *cure;
  1166. assoclist_node *cura;
  1167. identlist_node *curi;
  1168. int i=0;
  1169. if(sol_is_aststmt(obj)) {
  1170. if(sol_string_eq(state, str, "type")) {
  1171. res = sol_new_int(state, stmt->type);
  1172. } else {
  1173. switch(stmt->type) {
  1174. case ST_EXPR:
  1175. if(sol_string_eq(state, str, "expr")) {
  1176. res = sol_new_exprnode(state, stmt->expr);
  1177. }
  1178. break;
  1179. case ST_IFELSE:
  1180. if(sol_string_eq(state, str, "cond")) {
  1181. res = sol_new_exprnode(state, stmt->ifelse->cond);
  1182. } else if(sol_string_eq(state, str, "iftrue")) {
  1183. res = sol_new_stmtnode(state, stmt->ifelse->iftrue);
  1184. } else if(sol_string_eq(state, str, "iffalse")) {
  1185. res = sol_new_stmtnode(state, stmt->ifelse->iffalse);
  1186. }
  1187. break;
  1188. case ST_LOOP:
  1189. if(sol_string_eq(state, str, "cond")) {
  1190. res = sol_new_exprnode(state, stmt->loop->cond);
  1191. } else if(sol_string_eq(state, str, "loop")) {
  1192. res = sol_new_stmtnode(state, stmt->loop->loop);
  1193. }
  1194. break;
  1195. case ST_ITER:
  1196. if(sol_string_eq(state, str, "var")) {
  1197. res = sol_new_string(state, stmt->iter->var);
  1198. } else if(sol_string_eq(state, str, "iter")) {
  1199. res = sol_new_exprnode(state, stmt->iter->iter);
  1200. } else if(sol_string_eq(state, str, "loop")) {
  1201. res = sol_new_stmtnode(state, stmt->iter->loop);
  1202. }
  1203. break;
  1204. case ST_LIST:
  1205. if(sol_string_eq(state, str, "stmtlist")) {
  1206. res = sol_new_list(state);
  1207. curs = stmt->stmtlist;
  1208. while(curs) {
  1209. sol_list_insert(state, res, i++, sol_new_stmtnode(state, curs->stmt));
  1210. curs = curs->next;
  1211. }
  1212. }
  1213. break;
  1214. case ST_RET:
  1215. if(sol_string_eq(state, str, "ret")) {
  1216. res = sol_new_exprnode(state, stmt->ret->ret);
  1217. }
  1218. break;
  1219. }
  1220. }
  1221. } else {
  1222. if(sol_string_eq(state, str, "type")) {
  1223. res = sol_new_int(state, expr->type);
  1224. } else {
  1225. switch(expr->type) {
  1226. case EX_LIT:
  1227. if(sol_string_eq(state, str, "littype")) {
  1228. res = sol_new_int(state, expr->lit->type);
  1229. } else if(sol_string_eq(state, str, "ival")) {
  1230. res = sol_new_int(state, expr->lit->ival);
  1231. } else if(sol_string_eq(state, str, "fval")) {
  1232. res = sol_new_float(state, expr->lit->fval);
  1233. } else if(sol_string_eq(state, str, "str")) {
  1234. res = sol_new_string(state, expr->lit->str);
  1235. }
  1236. break;
  1237. case EX_LISTGEN:
  1238. if(sol_string_eq(state, str, "list")) {
  1239. res = sol_new_list(state);
  1240. cure = expr->listgen->list;
  1241. while(cure) {
  1242. sol_list_insert(state, res, i++, sol_new_exprnode(state, cure->expr));
  1243. cure = cure->next;
  1244. }
  1245. }
  1246. break;
  1247. case EX_MAPGEN:
  1248. if(sol_string_eq(state, str, "map")) {
  1249. res = sol_new_list(state);
  1250. cura = expr->mapgen->map;
  1251. while(cura) {
  1252. pair = sol_new_list(state);
  1253. sol_list_insert(state, pair, 0, sol_new_exprnode(state, cura->item->key));
  1254. sol_list_insert(state, pair, 1, sol_new_exprnode(state, cura->item->value));
  1255. sol_list_insert(state, res, i++, pair);
  1256. sol_obj_free(pair);
  1257. }
  1258. }
  1259. break;
  1260. case EX_BINOP:
  1261. if(sol_string_eq(state, str, "binoptype")) {
  1262. res = sol_new_int(state, expr->binop->type);
  1263. } else if(sol_string_eq(state, str, "left")) {
  1264. res = sol_new_exprnode(state, expr->binop->left);
  1265. } else if(sol_string_eq(state, str, "right")) {
  1266. res = sol_new_exprnode(state, expr->binop->right);
  1267. }
  1268. break;
  1269. case EX_UNOP:
  1270. if(sol_string_eq(state, str, "unoptype")) {
  1271. res = sol_new_int(state, expr->unop->type);
  1272. } else if(sol_string_eq(state, str, "expr")) {
  1273. res = sol_new_exprnode(state, expr->unop->expr);
  1274. }
  1275. break;
  1276. case EX_INDEX:
  1277. if(sol_string_eq(state, str, "expr")) {
  1278. res = sol_new_exprnode(state, expr->index->expr);
  1279. } else if(sol_string_eq(state, str, "index")) {
  1280. res = sol_new_exprnode(state, expr->index->index);
  1281. }
  1282. break;
  1283. case EX_SETINDEX:
  1284. if(sol_string_eq(state, str, "expr")) {
  1285. res = sol_new_exprnode(state, expr->setindex->expr);
  1286. } else if(sol_string_eq(state, str, "index")) {
  1287. res = sol_new_exprnode(state, expr->setindex->index);
  1288. } else if(sol_string_eq(state, str, "value")) {
  1289. res = sol_new_exprnode(state, expr->setindex->value);
  1290. }
  1291. break;
  1292. case EX_ASSIGN:
  1293. if(sol_string_eq(state, str, "ident")) {
  1294. res = sol_new_string(state, expr->assign->ident);
  1295. } else if(sol_string_eq(state, str, "value")) {
  1296. res = sol_new_exprnode(state, expr->assign->value);
  1297. }
  1298. break;
  1299. case EX_REF:
  1300. if(sol_string_eq(state, str, "ident")) {
  1301. res = sol_new_string(state, expr->ref->ident);
  1302. }
  1303. break;
  1304. case EX_CALL:
  1305. if(sol_string_eq(state, str, "expr")) {
  1306. res = sol_new_exprnode(state, expr->call->expr);
  1307. } else if(sol_string_eq(state, str, "args")) {
  1308. res = sol_new_list(state);
  1309. cure = expr->call->args;
  1310. while(cure) {
  1311. sol_list_insert(state, res, i++, sol_new_exprnode(state, cure->expr));
  1312. cure = cure->next;
  1313. }
  1314. }
  1315. break;
  1316. case EX_FUNCDECL:
  1317. if(sol_string_eq(state, str, "name")) {
  1318. res = sol_new_string(state, (expr->funcdecl->name?expr->funcdecl->name:""));
  1319. } else if(sol_string_eq(state, str, "args")) {
  1320. res = sol_new_list(state);
  1321. curi = expr->funcdecl->args;
  1322. while(curi) {
  1323. sol_list_insert(state, res, i++, sol_new_string(state, curi->ident));
  1324. curi = curi->next;
  1325. }
  1326. } else if(sol_string_eq(state, str, "body")) {
  1327. res = sol_new_stmtnode(state, expr->funcdecl->body);
  1328. }
  1329. break;
  1330. }
  1331. }
  1332. }
  1333. sol_obj_free(obj);
  1334. sol_obj_free(key);
  1335. sol_obj_free(str);
  1336. if(!res) res = sol_incref(state->None);
  1337. return res;
  1338. }
  1339. sol_object_t *sol_f_astnode_setindex(sol_state_t *state, sol_object_t *args) {
  1340. sol_object_t *obj = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *str = sol_cast_string(state, key), *val = sol_list_get_index(state, args, 2), *pair;
  1341. sol_object_t *ival, *fval, *sval;
  1342. stmt_node *stmt = (stmt_node *) obj->node;
  1343. stmtlist_node *curs, *prevs;
  1344. expr_node *expr = (expr_node *) obj->node;
  1345. exprlist_node *cure, *preve;
  1346. assoclist_node *cura, *preva;
  1347. identlist_node *curi, *previ;
  1348. int i=0, len;
  1349. if(sol_is_aststmt(obj)) {
  1350. if(sol_string_eq(state, str, "type")) {
  1351. ival = sol_cast_int(state, val);
  1352. stmt->type = ival->ival;
  1353. sol_obj_free(ival);
  1354. } else {
  1355. switch(stmt->type) {
  1356. case ST_EXPR:
  1357. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1358. stmt->expr = val->node;
  1359. }
  1360. break;
  1361. case ST_IFELSE:
  1362. if(sol_string_eq(state, str, "cond") && sol_is_astexpr(val)) {
  1363. stmt->ifelse->cond = val->node;
  1364. } else if(sol_string_eq(state, str, "iftrue") && sol_is_aststmt(val)) {
  1365. stmt->ifelse->iftrue = val->node;
  1366. } else if(sol_string_eq(state, str, "iffalse") && sol_is_aststmt(val)) {
  1367. stmt->ifelse->iffalse = val->node;
  1368. }
  1369. break;
  1370. case ST_LOOP:
  1371. if(sol_string_eq(state, str, "cond") && sol_is_astexpr(val)) {
  1372. stmt->loop->cond = val->node;
  1373. } else if(sol_string_eq(state, str, "loop") && sol_is_aststmt(val)) {
  1374. stmt->loop->loop = val->node;
  1375. }
  1376. break;
  1377. case ST_ITER:
  1378. if(sol_string_eq(state, str, "var")) {
  1379. sval = sol_cast_string(state, val);
  1380. stmt->iter->var = strdup(sval->str);
  1381. sol_obj_free(sval);
  1382. } else if(sol_string_eq(state, str, "iter") && sol_is_astexpr(val)) {
  1383. stmt->iter->iter = val->node;
  1384. } else if(sol_string_eq(state, str, "loop") && sol_is_aststmt(val)) {
  1385. stmt->iter->loop = val->node;
  1386. }
  1387. break;
  1388. case ST_LIST:
  1389. if(sol_string_eq(state, str, "stmtlist") && sol_is_list(val)) {
  1390. len = sol_list_len(state, val);
  1391. if(len>0) {
  1392. curs = malloc(sizeof(stmtlist_node));
  1393. stmt->stmtlist = curs;
  1394. for(i=0; i<len; i++) {
  1395. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1396. curs->stmt = sol_list_get_index(state, val, i)->node;
  1397. prevs = curs;
  1398. curs = malloc(sizeof(stmtlist_node));
  1399. prevs->next = curs;
  1400. }
  1401. }
  1402. free(curs);
  1403. prevs->next = NULL;
  1404. } else {
  1405. stmt->stmtlist = NULL;
  1406. }
  1407. }
  1408. break;
  1409. case ST_RET:
  1410. if(sol_string_eq(state, str, "ret") && sol_is_astexpr(val)) {
  1411. stmt->ret->ret = val->node;
  1412. }
  1413. break;
  1414. }
  1415. }
  1416. } else {
  1417. if(sol_string_eq(state, str, "type")) {
  1418. ival = sol_cast_int(state, val);
  1419. expr->type = ival->ival;
  1420. sol_obj_free(ival);
  1421. } else {
  1422. switch(expr->type) {
  1423. case EX_LIT:
  1424. if(sol_string_eq(state, str, "littype")) {
  1425. ival = sol_cast_int(state, val);
  1426. expr->lit->type = ival->ival;
  1427. sol_obj_free(ival);
  1428. } else if(sol_string_eq(state, str, "ival")) {
  1429. ival = sol_cast_int(state, val);
  1430. expr->lit->ival = ival->ival;
  1431. sol_obj_free(ival);
  1432. } else if(sol_string_eq(state, str, "fval")) {
  1433. fval = sol_cast_float(state, val);
  1434. expr->lit->fval = fval->fval;
  1435. sol_obj_free(fval);
  1436. } else if(sol_string_eq(state, str, "str")) {
  1437. sval = sol_cast_string(state, val);
  1438. expr->lit->str = strdup(sval->str);
  1439. sol_obj_free(sval);
  1440. }
  1441. break;
  1442. case EX_LISTGEN:
  1443. if(sol_string_eq(state, str, "list") && sol_is_list(val)) {
  1444. len = sol_list_len(state, val);
  1445. if(len>0) {
  1446. cure = malloc(sizeof(exprlist_node));
  1447. expr->listgen->list = cure;
  1448. for(i=0; i<len; i++) {
  1449. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1450. cure->expr = sol_list_get_index(state, val, i)->node;
  1451. preve = cure;
  1452. cure = malloc(sizeof(exprlist_node));
  1453. preve->next = cure;
  1454. }
  1455. }
  1456. free(cure);
  1457. preve->next = NULL;
  1458. } else {
  1459. expr->listgen->list = NULL;
  1460. }
  1461. }
  1462. break;
  1463. case EX_MAPGEN:
  1464. if(sol_string_eq(state, str, "map") && sol_is_list(val)) {
  1465. len = sol_list_len(state, val);
  1466. if(len>0) {
  1467. cura = malloc(sizeof(assoclist_node));
  1468. expr->mapgen->map = cura;
  1469. for(i=0; i<len; i++) {
  1470. if(sol_is_list(sol_list_get_index(state, val, i))) {
  1471. pair = sol_list_get_index(state, val, i);
  1472. if(sol_list_len(state, pair) >= 2 && sol_is_astexpr(sol_list_get_index(state, pair, 0)) && sol_is_astexpr(sol_list_get_index(state, pair, 1))) {
  1473. cura->item = malloc(sizeof(associtem_node));
  1474. cura->item->key = sol_list_get_index(state, pair, 0)->node;
  1475. cura->item->value = sol_list_get_index(state, pair, 1)->node;
  1476. preva = cura;
  1477. cura = malloc(sizeof(assoclist_node));
  1478. preva->next = cura;
  1479. }
  1480. }
  1481. }
  1482. free(cura);
  1483. preva->next = NULL;
  1484. } else {
  1485. expr->mapgen->map = NULL;
  1486. }
  1487. }
  1488. break;
  1489. case EX_BINOP:
  1490. if(sol_string_eq(state, str, "binoptype")) {
  1491. ival = sol_cast_int(state, val);
  1492. expr->binop->type = ival->ival;
  1493. sol_obj_free(ival);
  1494. } else if(sol_string_eq(state, str, "left") && sol_is_astexpr(val)) {
  1495. expr->binop->left = val->node;
  1496. } else if(sol_string_eq(state, str, "right") && sol_is_astexpr(val)) {
  1497. expr->binop->right = val->node;
  1498. }
  1499. break;
  1500. case EX_UNOP:
  1501. if(sol_string_eq(state, str, "unoptype")) {
  1502. ival = sol_cast_int(state, val);
  1503. expr->unop->type = ival->ival;
  1504. sol_obj_free(ival);
  1505. } else if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1506. expr->unop->expr = val->node;
  1507. }
  1508. break;
  1509. case EX_INDEX:
  1510. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1511. expr->index->expr = val->node;
  1512. } else if(sol_string_eq(state, str, "index") && sol_is_astexpr(val)) {
  1513. expr->index->index = val->node;
  1514. }
  1515. break;
  1516. case EX_SETINDEX:
  1517. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1518. expr->setindex->expr = val->node;
  1519. } else if(sol_string_eq(state, str, "index") && sol_is_astexpr(val)) {
  1520. expr->setindex->index = val->node;
  1521. } else if(sol_string_eq(state, str, "value") && sol_is_astexpr(val)) {
  1522. expr->setindex->value = val->node;
  1523. }
  1524. break;
  1525. case EX_ASSIGN:
  1526. if(sol_string_eq(state, str, "ident")) {
  1527. sval = sol_cast_string(state, val);
  1528. expr->assign->ident = strdup(sval->str);
  1529. sol_obj_free(sval);
  1530. } else if(sol_string_eq(state, str, "value") && sol_is_astexpr(val)) {
  1531. expr->assign->value = val->node;
  1532. }
  1533. break;
  1534. case EX_REF:
  1535. if(sol_string_eq(state, str, "ident")) {
  1536. sval = sol_cast_string(state, val);
  1537. expr->ref->ident = strdup(sval->str);
  1538. sol_obj_free(sval);
  1539. }
  1540. break;
  1541. case EX_CALL:
  1542. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1543. expr->call->expr = val->node;
  1544. } else if(sol_string_eq(state, str, "args") && sol_is_list(val)) {
  1545. len = sol_list_len(state, val);
  1546. if(len>0) {
  1547. cure = malloc(sizeof(exprlist_node));
  1548. expr->call->args= cure;
  1549. for(i=0; i<len; i++) {
  1550. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1551. cure->expr = sol_list_get_index(state, val, i)->node;
  1552. preve = cure;
  1553. cure = malloc(sizeof(exprlist_node));
  1554. preve->next = cure;
  1555. }
  1556. }
  1557. free(cure);
  1558. preve->next = NULL;
  1559. } else {
  1560. expr->call->args = NULL;
  1561. }
  1562. }
  1563. break;
  1564. case EX_FUNCDECL:
  1565. if(sol_string_eq(state, str, "name")) {
  1566. sval = sol_cast_string(state, val);
  1567. expr->funcdecl->name = strdup(sval->str);
  1568. sol_obj_free(sval);
  1569. } else if(sol_string_eq(state, str, "args") && sol_is_list(val)) {
  1570. len = sol_list_len(state, val);
  1571. if(len>0) {
  1572. curi = malloc(sizeof(identlist_node));
  1573. expr->funcdecl->args= curi;
  1574. for(i=0; i<len; i++) {
  1575. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1576. sval = sol_cast_string(state, sol_list_get_index(state, val, i));
  1577. curi->ident = strdup(sval->str);
  1578. sol_obj_free(sval);
  1579. previ = curi;
  1580. curi = malloc(sizeof(identlist_node));
  1581. previ->next = curi;
  1582. }
  1583. }
  1584. free(curi);
  1585. previ->next = NULL;
  1586. } else {
  1587. expr->funcdecl->args = NULL;
  1588. }
  1589. } else if(sol_string_eq(state, str, "body") && sol_is_aststmt(val)) {
  1590. expr->funcdecl->body = val->node;
  1591. }
  1592. break;
  1593. }
  1594. }
  1595. }
  1596. sol_obj_free(obj);
  1597. sol_obj_free(key);
  1598. sol_obj_free(str);
  1599. sol_obj_free(val);
  1600. return sol_incref(state->None);
  1601. }
  1602. static char *sol_StmtNames[]={"EXPR", "IFSELSE", "LOOP", "ITER", "RET", "CONT", "BREAK"};
  1603. static char *sol_ExprNames[]={"LIT", "LISTGEN", "MAPGEN", "BINOP", "UNOP", "INDEX", "SETINDEX", "ASSIGN", "REF", "CALL", "FUNCDECL"};
  1604. sol_object_t *sol_f_astnode_tostring(sol_state_t *state, sol_object_t *args) {
  1605. sol_object_t *obj = sol_list_get_index(state, args, 0), *res;
  1606. char s[64];
  1607. if(sol_is_aststmt(obj)) {
  1608. snprintf(s, 64, "<Stmt[%s]>", sol_StmtNames[((stmt_node *)obj->node)->type]);
  1609. } else {
  1610. snprintf(s, 64, "<Expr[%s]>", sol_ExprNames[((expr_node *)obj->node)->type]);
  1611. }
  1612. sol_obj_free(obj);
  1613. return sol_new_string(state, s);
  1614. }
  1615. sol_object_t *sol_f_buffer_index(sol_state_t *state, sol_object_t *args) {
  1616. sol_object_t *key = sol_list_get_index(state, args, 1), *funcs = sol_get_methods_name(state, "buffer");
  1617. sol_object_t *res = sol_map_get(state, funcs, key);
  1618. sol_obj_free(key);
  1619. sol_obj_free(funcs);
  1620. return res;
  1621. }
  1622. sol_object_t *sol_f_buffer_tostring(sol_state_t *state, sol_object_t *args) {
  1623. sol_object_t *buf = sol_list_get_index(state, args, 0), *res;
  1624. char s[64];
  1625. if(buf->sz == -1) {
  1626. snprintf(s, 64, "<Buffer @%p>", buf->buffer);
  1627. } else {
  1628. snprintf(s, 64, "<Buffer @%p size %ld", buf->buffer, buf->sz);
  1629. }
  1630. res = sol_new_string(state, s);
  1631. sol_obj_free(buf);
  1632. return res;
  1633. }
  1634. sol_object_t *sol_f_buffer_new(sol_state_t *state, sol_object_t *args) {
  1635. sol_object_t *sz = sol_list_get_index(state, args, 0), *isz = sol_cast_int(state, sz);
  1636. size_t bufsz = isz->ival;
  1637. void *buf = malloc(bufsz);
  1638. sol_obj_free(sz);
  1639. sol_obj_free(isz);
  1640. if(buf) {
  1641. return sol_new_buffer(state, buf, bufsz, OWN_FREE, NULL, NULL);
  1642. }
  1643. return sol_incref(state->None);
  1644. }
  1645. sol_object_t *sol_f_buffer_get(sol_state_t *state, sol_object_t *args) {
  1646. sol_object_t *buf = sol_list_get_index(state, args, 0), *tp = sol_list_get_index(state, args, 1), *off = sol_list_get_index(state, args, 2);
  1647. sol_object_t *itp = sol_cast_int(state, tp), *ioff, *res=NULL;
  1648. sol_buftype_t buftp = itp->ival;
  1649. char *data, cbuf[2]={0, 0};
  1650. sol_obj_free(tp);
  1651. sol_obj_free(itp);
  1652. if(!sol_is_none(state, off)) {
  1653. ioff = sol_cast_int(state, off);
  1654. } else {
  1655. ioff = sol_new_int(state, 0);
  1656. }
  1657. sol_obj_free(off);
  1658. if(buf->sz != -1 && (ioff->ival >= buf->sz || ioff->ival < 0)) {
  1659. sol_obj_free(buf);
  1660. sol_obj_free(ioff);
  1661. return sol_incref(state->None);
  1662. }
  1663. data = ((char *) buf->buffer)+ioff->ival;
  1664. sol_obj_free(buf);
  1665. sol_obj_free(ioff);
  1666. switch(buftp) {
  1667. case BUF_UINT8:
  1668. res = sol_new_int(state, *((uint8_t *) data));
  1669. break;
  1670. case BUF_UINT16:
  1671. res = sol_new_int(state, *((uint16_t *) data));
  1672. break;
  1673. case BUF_UINT32:
  1674. res = sol_new_int(state, *((uint32_t *) data));
  1675. break;
  1676. case BUF_UINT64:
  1677. res = sol_new_int(state, *((uint64_t *) data));
  1678. break;
  1679. case BUF_INT8:
  1680. res = sol_new_int(state, *((int8_t *) data));
  1681. break;
  1682. case BUF_INT16:
  1683. res = sol_new_int(state, *((int16_t *) data));
  1684. break;
  1685. case BUF_INT32:
  1686. res = sol_new_int(state, *((int32_t *) data));
  1687. break;
  1688. case BUF_INT64:
  1689. res = sol_new_int(state, *((int64_t *) data));
  1690. break;
  1691. case BUF_CHAR:
  1692. cbuf[0] = *((char *) data);
  1693. res = sol_new_string(state, cbuf);
  1694. break;
  1695. case BUF_BYTE:
  1696. res = sol_new_int(state, *((unsigned char *) data));
  1697. break;
  1698. case BUF_INT:
  1699. res = sol_new_int(state, *((int *) data));
  1700. break;
  1701. case BUF_UINT:
  1702. res = sol_new_int(state, *((unsigned int *) data));
  1703. break;
  1704. case BUF_LONG:
  1705. res = sol_new_int(state, *((long *) data));
  1706. break;
  1707. case BUF_ULONG:
  1708. res = sol_new_int(state, *((unsigned long *) data));
  1709. break;
  1710. case BUF_FLOAT:
  1711. res = sol_new_float(state, *((float *) data));
  1712. break;
  1713. case BUF_DOUBLE:
  1714. res = sol_new_float(state, *((double *) data));
  1715. break;
  1716. case BUF_CSTR:
  1717. res = sol_new_string(state, (char *) data);
  1718. break;
  1719. case BUF_PTR:
  1720. res = sol_new_buffer(state, ((void *) *((unsigned long *) data)), -1, OWN_NONE, NULL, NULL);
  1721. break;
  1722. }
  1723. if(!res) res = sol_incref(state->None);
  1724. return res;
  1725. }
  1726. sol_object_t *sol_f_buffer_set(sol_state_t *state, sol_object_t *args) {
  1727. sol_object_t *buf = sol_list_get_index(state, args, 0), *tp = sol_list_get_index(state, args, 1), *val = sol_list_get_index(state, args, 2), *off = sol_list_get_index(state, args, 3);
  1728. sol_object_t *itp = sol_cast_int(state, tp), *ioff, *ival=NULL, *fval=NULL, *sval=NULL;
  1729. sol_buftype_t buftp = itp->ival;
  1730. char *data;
  1731. sol_obj_free(tp);
  1732. sol_obj_free(itp);
  1733. if(!sol_is_none(state, off)) {
  1734. ioff = sol_cast_int(state, off);
  1735. } else {
  1736. ioff = sol_new_int(state, 0);
  1737. }
  1738. sol_obj_free(off);
  1739. if(buf->sz != -1 && (ioff->ival >= buf->sz || ioff->ival < 0)) {
  1740. sol_obj_free(buf);
  1741. sol_obj_free(ioff);
  1742. return sol_incref(state->None);
  1743. }
  1744. data = ((char *) buf->buffer)+ioff->ival;
  1745. sol_obj_free(buf);
  1746. sol_obj_free(ioff);
  1747. switch(buftp) {
  1748. case BUF_UINT8:
  1749. ival = sol_cast_int(state, val);
  1750. *((uint8_t *) data) = ival->ival;
  1751. break;
  1752. case BUF_UINT16:
  1753. ival = sol_cast_int(state, val);
  1754. *((uint16_t *) data) = ival->ival;
  1755. break;
  1756. case BUF_UINT32:
  1757. ival = sol_cast_int(state, val);
  1758. *((uint32_t *) data) = ival->ival;
  1759. break;
  1760. case BUF_UINT64:
  1761. ival = sol_cast_int(state, val);
  1762. *((uint64_t *) data) = ival->ival;
  1763. break;
  1764. case BUF_INT8:
  1765. ival = sol_cast_int(state, val);
  1766. *((int8_t *) data) = ival->ival;
  1767. break;
  1768. case BUF_INT16:
  1769. ival = sol_cast_int(state, val);
  1770. *((int16_t *) data) = ival->ival;
  1771. break;
  1772. case BUF_INT32:
  1773. ival = sol_cast_int(state, val);
  1774. *((int32_t *) data) = ival->ival;
  1775. break;
  1776. case BUF_INT64:
  1777. ival = sol_cast_int(state, val);
  1778. *((int64_t *) data) = ival->ival;
  1779. break;
  1780. case BUF_CHAR:
  1781. sval = sol_cast_string(state, val);
  1782. *((char *) data) = sval->str[0];
  1783. break;
  1784. case BUF_BYTE:
  1785. ival = sol_cast_int(state, val);
  1786. *((unsigned char *) data) = ival->ival;
  1787. break;
  1788. case BUF_INT:
  1789. ival = sol_cast_int(state, val);
  1790. *((int *) data) = ival->ival;
  1791. break;
  1792. case BUF_UINT:
  1793. ival = sol_cast_int(state, val);
  1794. *((unsigned int *) data) = ival->ival;
  1795. break;
  1796. case BUF_LONG:
  1797. ival = sol_cast_int(state, val);
  1798. *((long *) data) = ival->ival;
  1799. break;
  1800. case BUF_ULONG:
  1801. ival = sol_cast_int(state, val);
  1802. *((unsigned long *) data) = ival->ival;
  1803. break;
  1804. case BUF_FLOAT:
  1805. fval = sol_cast_float(state, val);
  1806. *((float *) data) = fval->fval;
  1807. break;
  1808. case BUF_DOUBLE:
  1809. fval = sol_cast_float(state, val);
  1810. *((double *) data) = fval->fval;
  1811. break;
  1812. case BUF_CSTR:
  1813. sval = sol_cast_string(state, val);
  1814. strcpy((char *) data, sval->str);
  1815. break;
  1816. case BUF_PTR:
  1817. if(sol_is_buffer(val)) {
  1818. *((unsigned long *) data) = ((unsigned long) val->buffer);
  1819. if(val->own == OWN_CALLF) {
  1820. val->movef(val->buffer, val->sz);
  1821. }
  1822. } else {
  1823. ival = sol_cast_int(state, val);
  1824. *((unsigned long *) data) = ival->ival;
  1825. }
  1826. break;
  1827. }
  1828. if(ival) sol_obj_free(ival);
  1829. if(fval) sol_obj_free(fval);
  1830. if(sval) sol_obj_free(sval);
  1831. return sol_incref(state->None);
  1832. }
  1833. sol_object_t *sol_f_buffer_address(sol_state_t *state, sol_object_t *args) {
  1834. sol_object_t *buf = sol_list_get_index(state, args, 0);
  1835. sol_object_t *res = sol_new_int(state, (unsigned long) buf->buffer);
  1836. sol_obj_free(buf);
  1837. return res;
  1838. }
  1839. sol_object_t *sol_f_buffer_size(sol_state_t *state, sol_object_t *args) {
  1840. sol_object_t *buf = sol_list_get_index(state, args, 0);
  1841. sol_object_t *res = sol_new_int(state, (long) buf->sz);
  1842. sol_obj_free(buf);
  1843. return res;
  1844. }
  1845. sol_object_t *sol_f_buffer_fromstring(sol_state_t *state, sol_object_t *args) {
  1846. sol_object_t *val = sol_list_get_index(state, args, 0), *sval = sol_cast_string(state, val);
  1847. size_t sz = strlen(sval->str)+1;
  1848. sol_object_t *buf = sol_new_buffer(state, malloc(sz), sz, OWN_FREE, NULL, NULL);
  1849. strcpy(buf->buffer, sval->str);
  1850. sol_obj_free(val);
  1851. sol_obj_free(sval);
  1852. return buf;
  1853. }
  1854. sol_object_t *sol_f_buffer_fromobject(sol_state_t *state, sol_object_t *args) {
  1855. sol_object_t *obj = sol_list_get_index(state, args, 0);
  1856. sol_object_t *buf = sol_new_buffer(state, obj, sizeof(sol_object_t), OWN_CALLF, (sol_freefunc_t) sol_obj_free, (sol_movefunc_t) sol_obj_acquire);
  1857. //Keep ref to obj so buf remains alive
  1858. return buf;
  1859. }
  1860. sol_object_t *sol_f_buffer_fromaddress(sol_state_t *state, sol_object_t *args) {
  1861. sol_object_t *addr = sol_list_get_index(state, args, 0), *sz = sol_list_get_index(state, args, 1);
  1862. sol_object_t *iaddr = sol_cast_int(state, addr), *isz = sol_cast_int(state, sz);
  1863. sol_object_t *buf = sol_new_buffer(state, (void *) iaddr->ival, (ssize_t) isz->ival, OWN_NONE, NULL, NULL);
  1864. sol_obj_free(addr);
  1865. sol_obj_free(sz);
  1866. sol_obj_free(iaddr);
  1867. sol_obj_free(isz);
  1868. return buf;
  1869. }
  1870. sol_object_t *sol_f_dylib_index(sol_state_t *state, sol_object_t *args) {
  1871. sol_object_t *dylib = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *skey = sol_cast_string(state, key);
  1872. void *handle = dlsym(dylib->dlhandle, skey->str);
  1873. sol_obj_free(dylib);
  1874. sol_obj_free(key);
  1875. sol_obj_free(skey);
  1876. if(handle) {
  1877. return sol_new_dysym(state, handle, NULL, BUF_NONE);
  1878. } else {
  1879. return sol_incref(state->None);
  1880. }
  1881. }
  1882. sol_object_t *sol_f_dylib_tostring(sol_state_t *state, sol_object_t *args) {
  1883. return sol_new_string(state, "<DyLib>");
  1884. }
  1885. sol_object_t *sol_f_dysym_call(sol_state_t *state, sol_object_t *args) {
  1886. //TODO
  1887. return sol_incref(state->None);
  1888. }
  1889. sol_object_t *sol_f_dysym_index(sol_state_t *state, sol_object_t *args) {
  1890. //TODO
  1891. return sol_incref(state->None);
  1892. }
  1893. sol_object_t *sol_f_dysym_setindex(sol_state_t *state, sol_object_t *args) {
  1894. //TODO
  1895. return sol_incref(state->None);
  1896. }
  1897. sol_object_t *sol_f_dysym_tostring(sol_state_t *state, sol_object_t *args) {
  1898. return sol_new_string(state, "<DySym>");
  1899. }
  1900. sol_object_t *sol_f_stream_blsh(sol_state_t *state, sol_object_t *args) {
  1901. return sol_f_stream_write(state, args);
  1902. }
  1903. sol_object_t *sol_f_stream_brsh(sol_state_t *state, sol_object_t *args) {
  1904. return sol_f_stream_read(state, args);
  1905. }
  1906. sol_object_t *sol_f_stream_index(sol_state_t *state, sol_object_t *args) {
  1907. sol_object_t *key = sol_list_get_index(state, args, 1), *funcs = sol_get_methods_name(state, "stream");
  1908. sol_object_t *res = sol_map_get(state, funcs, key);
  1909. sol_obj_free(key);
  1910. sol_obj_free(funcs);
  1911. return res;
  1912. }
  1913. sol_object_t *sol_f_stream_tostring(sol_state_t *state, sol_object_t *args) {
  1914. return sol_new_string(state, "<Stream>");
  1915. }
  1916. sol_object_t *sol_f_stream_write(sol_state_t *state, sol_object_t *args) {
  1917. sol_object_t *stream = sol_list_get_index(state, args, 0), *obj = sol_list_get_index(state, args, 1), *str = sol_cast_string(state, obj);
  1918. size_t sz = sol_stream_printf(state, stream, "%s", str->str);
  1919. sol_obj_free(obj);
  1920. sol_obj_free(str);
  1921. sol_obj_free(stream);
  1922. return sol_new_int(state, sz);
  1923. }
  1924. sol_object_t *sol_f_stream_read(sol_state_t *state, sol_object_t *args) {
  1925. sol_object_t *stream = sol_list_get_index(state, args, 0), *amt = sol_list_get_index(state, args, 1), *iamt, *res;
  1926. char *s=NULL, *p;
  1927. size_t count=0, max=0, pos, end;
  1928. if(sol_is_string(amt)) {
  1929. if(sol_string_eq(state, amt, "ALL")) {
  1930. pos = sol_stream_ftell(state, stream);
  1931. sol_stream_fseek(state, stream, 0, SEEK_END);
  1932. end = sol_stream_ftell(state, stream);
  1933. sol_stream_fseek(state, stream, pos, SEEK_SET);
  1934. //printf("IO: Reading %ld bytes starting at %ld\n", end-pos, pos);
  1935. s = malloc((end-pos+1)*sizeof(char));
  1936. if(sol_stream_fread(state, stream, s, sizeof(char), end-pos)<(end-pos)) {
  1937. free(s);
  1938. sol_obj_free(stream);
  1939. sol_obj_free(amt);
  1940. return sol_set_error_string(state, "IO read error");
  1941. }
  1942. s[end-pos]='\0';
  1943. } else if(sol_string_eq(state, amt, "LINE")) {
  1944. s = malloc(STDIO_CHUNK_SIZE*sizeof(char));
  1945. sol_stream_fgets(state, stream, s, STDIO_CHUNK_SIZE);
  1946. }
  1947. } else {
  1948. iamt = sol_cast_int(state, amt);
  1949. s = malloc((iamt->ival + 1)*sizeof(char));
  1950. count = sol_stream_fread(state, stream, s, sizeof(char), iamt->ival);
  1951. s[iamt->ival]='\0';
  1952. sol_obj_free(iamt);
  1953. }
  1954. if(s) {
  1955. //printf("IO: Read result: %s\n", s);
  1956. res = sol_new_string(state, s);
  1957. free(s);
  1958. } else {
  1959. //printf("IO: No read result!\n");
  1960. res = sol_incref(state->None);
  1961. }
  1962. sol_obj_free(amt);
  1963. sol_obj_free(stream);
  1964. return res;
  1965. }
  1966. sol_object_t *sol_f_stream_seek(sol_state_t *state, sol_object_t *args) {
  1967. sol_object_t *stream = sol_list_get_index(state, args, 0), *offset = sol_list_get_index(state, args, 1), *whence = sol_list_get_index(state, args, 2);
  1968. sol_object_t *ioffset = sol_cast_int(state, offset), *iwhence = sol_cast_int(state, whence);
  1969. sol_object_t *res = sol_new_int(state, sol_stream_fseek(state, stream, ioffset->ival, iwhence->ival));
  1970. sol_obj_free(stream);
  1971. sol_obj_free(offset);
  1972. sol_obj_free(whence);
  1973. sol_obj_free(ioffset);
  1974. sol_obj_free(iwhence);
  1975. return res;
  1976. }
  1977. sol_object_t *sol_f_stream_tell(sol_state_t *state, sol_object_t *args) {
  1978. sol_object_t *stream = sol_list_get_index(state, args, 0), *res = sol_new_int(state, sol_stream_ftell(state, stream));
  1979. sol_obj_free(stream);
  1980. return res;
  1981. }
  1982. sol_object_t *sol_f_stream_flush(sol_state_t *state, sol_object_t *args) {
  1983. sol_object_t *stream = sol_list_get_index(state, args, 0), *res = sol_new_int(state, sol_stream_fflush(state, stream));
  1984. sol_obj_free(stream);
  1985. return res;
  1986. }
  1987. static char *sol_FileModes[]={
  1988. NULL,
  1989. "r",
  1990. "w",
  1991. "r+",
  1992. NULL,
  1993. NULL,
  1994. "a",
  1995. "a+",
  1996. NULL,
  1997. NULL,
  1998. "w",
  1999. "w+",
  2000. NULL,
  2001. NULL,
  2002. NULL,
  2003. NULL,
  2004. NULL,
  2005. "rb",
  2006. "wb",
  2007. "r+b",
  2008. NULL,
  2009. NULL,
  2010. "ab",
  2011. "a+b",
  2012. NULL,
  2013. NULL,
  2014. "wb",
  2015. "w+b",
  2016. NULL,
  2017. NULL,
  2018. NULL,
  2019. NULL
  2020. };
  2021. sol_object_t *sol_f_stream_open(sol_state_t *state, sol_object_t *args) {
  2022. sol_object_t *fn = sol_list_get_index(state, args, 0), *mode = sol_list_get_index(state, args, 1);
  2023. sol_object_t *sfn = sol_cast_string(state, fn), *imode = sol_cast_int(state, mode);
  2024. sol_modes_t m = imode->ival;
  2025. char *smode = sol_FileModes[m];
  2026. FILE *f;
  2027. sol_obj_free(mode);
  2028. sol_obj_free(imode);
  2029. if(!smode) {
  2030. sol_obj_free(fn);
  2031. sol_obj_free(sfn);
  2032. return sol_set_error_string(state, "Bad file open mode");
  2033. }
  2034. f = fopen(sfn->str, smode);
  2035. sol_obj_free(sfn);
  2036. sol_obj_free(fn);
  2037. if(!f) return sol_set_error_string(state, "File open failed");
  2038. return sol_new_stream(state, f, m);
  2039. }