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

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