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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222
  1. #define _GNU_SOURCE
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <stdint.h>
  7. #include <dlfcn.h>
  8. #ifndef NO_READLINE
  9. #include <readline/readline.h>
  10. #include <readline/history.h>
  11. #endif
  12. #include <termios.h>
  13. #include <sys/ioctl.h>
  14. #include "ast.h"
  15. #include "dsl/dsl.h"
  16. // XXX hardcoded buffer sizes
  17. #define STDIO_CHUNK_SIZE 4096
  18. static char *_itoa(int i) {
  19. int n = 33;
  20. char *s = malloc(n);
  21. snprintf(s, n, "%d", i);
  22. return s;
  23. }
  24. static char *_ftoa(double f) {
  25. int n = 65;
  26. char *s = malloc(n);
  27. snprintf(s, n, "%f", f);
  28. return s;
  29. }
  30. sol_object_t *sol_f_not_impl(sol_state_t *state, sol_object_t *args) {
  31. char buffer[64];
  32. snprintf(buffer, 64, "Undefined method (%s on %s)", state->calling_meth, state->calling_type);
  33. return sol_set_error_string(state, buffer);
  34. }
  35. sol_object_t *sol_f_default_cmp(sol_state_t *state, sol_object_t *args) {
  36. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  37. sol_object_t *res = sol_new_int(state, a != b);
  38. sol_obj_free(a);
  39. sol_obj_free(b);
  40. return res;
  41. }
  42. sol_object_t *sol_f_default_tostring(sol_state_t *state, sol_object_t *args) {
  43. sol_object_t *obj = sol_list_get_index(state, args, 0);
  44. char s[64];
  45. snprintf(s, 64, "<%s object at %p>", obj->ops->tname, obj);
  46. sol_obj_free(obj);
  47. return sol_new_string(state, s);
  48. }
  49. sol_object_t *sol_f_default_repr(sol_state_t *state, sol_object_t *args) {
  50. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = CALL_METHOD(state, obj, tostring, args);
  51. sol_obj_free(obj);
  52. return res;
  53. }
  54. sol_object_t *sol_f_default_tobuffer(sol_state_t *state, sol_object_t *args) {
  55. sol_object_t *obj = sol_list_get_index(state, args, 0), *str = CALL_METHOD(state, obj, tostring, args);
  56. sol_object_t *res = sol_new_buffer(state, strdup(str->str), strlen(str->str), OWN_FREE, NULL, NULL);
  57. sol_obj_free(obj);
  58. sol_obj_free(str);
  59. return res;
  60. }
  61. sol_object_t *sol_f_tbang(sol_state_t *state, sol_object_t *args) {
  62. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  63. int refa = a->refcnt, refb = b->refcnt;
  64. sol_object_t c = *b;
  65. *b = *a;
  66. *a = c;
  67. b->refcnt = refa;
  68. a->refcnt = refb;
  69. sol_obj_free(a);
  70. sol_obj_free(b);
  71. return sol_incref(state->None);
  72. }
  73. sol_object_t *sol_f_no_op(sol_state_t *state, sol_object_t *args) {
  74. if(state) {
  75. return sol_incref(state->None);
  76. }
  77. return NULL;
  78. }
  79. sol_object_t *sol_f_toint(sol_state_t *state, sol_object_t *args) {
  80. sol_object_t *obj = sol_list_get_index(state, args, 0);
  81. sol_object_t *res = CALL_METHOD(state, obj, toint, args);
  82. sol_obj_free(obj);
  83. return res;
  84. }
  85. sol_object_t *sol_f_tofloat(sol_state_t *state, sol_object_t *args) {
  86. sol_object_t *obj = sol_list_get_index(state, args, 0);
  87. sol_object_t *res = CALL_METHOD(state, obj, tofloat, args);
  88. sol_obj_free(obj);
  89. return res;
  90. }
  91. sol_object_t *sol_f_tostring(sol_state_t *state, sol_object_t *args) {
  92. sol_object_t *obj = sol_list_get_index(state, args, 0);
  93. sol_object_t *res = CALL_METHOD(state, obj, tostring, args);
  94. sol_obj_free(obj);
  95. return res;
  96. }
  97. sol_object_t *sol_f_tobuffer(sol_state_t *state, sol_object_t *args) {
  98. sol_object_t *obj = sol_list_get_index(state, args, 0);
  99. sol_object_t *res = CALL_METHOD(state, obj, tobuffer, args);
  100. sol_obj_free(obj);
  101. return res;
  102. }
  103. sol_object_t *sol_f_try(sol_state_t *state, sol_object_t *args) {
  104. sol_object_t *func = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  105. sol_object_t *ls = sol_new_list(state), *one = sol_new_int(state, 1);
  106. sol_object_t *res;
  107. sol_list_insert(state, fargs, 0, func);
  108. res = CALL_METHOD(state, func, call, fargs);
  109. sol_obj_free(func);
  110. sol_obj_free(fargs);
  111. if(sol_has_error(state)) {
  112. sol_object_t *err = sol_get_error(state);
  113. sol_clear_error(state);
  114. sol_object_t *zero = sol_new_int(state, 0);
  115. sol_obj_free(res);
  116. sol_obj_free(one);
  117. sol_list_insert(state, ls, 0, err);
  118. sol_obj_free(err);
  119. sol_list_insert(state, ls, 0, zero);
  120. sol_obj_free(zero);
  121. sol_list_insert(state, ls, 2, state->traceback);
  122. sol_obj_free(state->traceback);
  123. state->traceback = NULL;
  124. return ls;
  125. }
  126. sol_list_insert(state, ls, 0, res);
  127. sol_obj_free(res);
  128. sol_list_insert(state, ls, 0, one);
  129. sol_obj_free(one);
  130. return ls;
  131. }
  132. sol_object_t *sol_f_apply(sol_state_t *state, sol_object_t *args) {
  133. sol_object_t *func = sol_list_get_index(state, args, 0), *arglist = sol_list_get_index(state, args, 1), *rest = sol_list_sublist(state, args, 2);
  134. if(!sol_is_list(arglist)) {
  135. sol_obj_free(func);
  136. sol_obj_free(arglist);
  137. sol_obj_free(rest);
  138. return sol_set_error_string(state, "apply with non-list");
  139. }
  140. sol_list_append(state, rest, arglist);
  141. sol_obj_free(arglist);
  142. sol_list_insert(state, rest, 0, func);
  143. sol_object_t *res = CALL_METHOD(state, func, call, rest);
  144. sol_obj_free(rest);
  145. sol_obj_free(func);
  146. return res;
  147. }
  148. sol_object_t *sol_f_error(sol_state_t *state, sol_object_t *args) {
  149. sol_object_t *arg = sol_list_get_index(state, args, 0), *res;
  150. res = sol_set_error(state, arg);
  151. sol_obj_free(arg);
  152. return res;
  153. }
  154. sol_object_t *sol_f_type(sol_state_t *state, sol_object_t *args) {
  155. sol_object_t *obj = sol_list_get_index(state, args, 0);
  156. sol_object_t *res = sol_new_buffer(state, strdup(obj->ops->tname), strlen(obj->ops->tname), OWN_FREE, NULL, NULL);
  157. sol_obj_free(obj);
  158. return res;
  159. }
  160. static dsl_seq *seen = NULL;
  161. int test_seen(sol_object_t *obj) {
  162. dsl_seq_iter *iter;
  163. if(seen) {
  164. iter = dsl_new_seq_iter(seen);
  165. while(!dsl_seq_iter_is_invalid(iter)) {
  166. if(dsl_seq_iter_at(iter) == obj) {
  167. return 1;
  168. }
  169. dsl_seq_iter_next(iter);
  170. }
  171. dsl_free_seq_iter(iter);
  172. dsl_seq_insert(seen, dsl_seq_len(seen), obj);
  173. }
  174. return 0;
  175. }
  176. void ob_print(sol_object_t *obj) {
  177. sol_object_t *cur;
  178. dsl_seq_iter *iter;
  179. int i;
  180. if(test_seen(obj)) {
  181. return;
  182. }
  183. switch(obj->type) {
  184. case SOL_SINGLET:
  185. printf("%s", obj->str);
  186. break;
  187. case SOL_INTEGER:
  188. printf("%ld", obj->ival);
  189. break;
  190. case SOL_FLOAT:
  191. printf("%f", obj->fval);
  192. break;
  193. case SOL_STRING:
  194. printf("\"%s\"", obj->str);
  195. break;
  196. case SOL_LIST:
  197. printf("[");
  198. iter = dsl_new_seq_iter(obj->seq);
  199. while(!dsl_seq_iter_is_invalid(iter)) {
  200. ob_print(dsl_seq_iter_at(iter));
  201. printf(", ");
  202. dsl_seq_iter_next(iter);
  203. }
  204. dsl_free_seq_iter(iter);
  205. printf("]");
  206. break;
  207. case SOL_MCELL:
  208. printf("<<");
  209. ob_print(obj->key);
  210. printf("=");
  211. ob_print(obj->val);
  212. printf(">>");
  213. case SOL_MAP:
  214. printf("{");
  215. iter = dsl_new_seq_iter(obj->seq);
  216. while(!dsl_seq_iter_is_invalid(iter)) {
  217. printf("[");
  218. ob_print(AS_OBJ(dsl_seq_iter_at(iter))->key);
  219. printf("] = ");
  220. ob_print(AS_OBJ(dsl_seq_iter_at(iter))->val);
  221. printf(", ");
  222. dsl_seq_iter_next(iter);
  223. }
  224. dsl_free_seq_iter(iter);
  225. printf("}");
  226. break;
  227. case SOL_FUNCTION:
  228. if(obj->fname) {
  229. printf("<Function %s>", obj->fname);
  230. } else {
  231. printf("<Function>");
  232. }
  233. break;
  234. case SOL_CFUNCTION:
  235. printf("<CFunction>");
  236. break;
  237. case SOL_STMT:
  238. st_print(NULL, obj->node); //TODO: FIXME
  239. break;
  240. case SOL_EXPR:
  241. ex_print(NULL, obj->node); //TODO: FIXME
  242. break;
  243. case SOL_BUFFER:
  244. /*
  245. if(obj->sz == -1) {
  246. printf("<Buffer @%p>", obj->buffer);
  247. } else {
  248. printf("<Buffer @%p size %ld>", obj->buffer, obj->sz);
  249. }
  250. */
  251. fwrite(obj->buffer, sizeof(char), obj->sz, stdout);
  252. break;
  253. case SOL_CDATA:
  254. printf("<CData>");
  255. break;
  256. /*default:
  257. cur = sol_cast_string(state, obj);
  258. printf("%s", cur->str);
  259. sol_obj_free(cur);*/
  260. }
  261. }
  262. sol_object_t *sol_f_prepr(sol_state_t *state, sol_object_t *args) {
  263. int i, sz = sol_list_len(state, args);
  264. sol_object_t *obj, *str;
  265. seen = dsl_seq_new_array(NULL, NULL);
  266. for(i = 0; i < sz; i++) {
  267. obj = sol_list_get_index(state, args, i);
  268. str = sol_cast_repr(state, obj);
  269. sol_printf(state, "%s", str->str);
  270. sol_printf(state, " ");
  271. sol_obj_free(obj);
  272. sol_obj_free(str);
  273. }
  274. sol_printf(state, "\n");
  275. printf("\n");
  276. dsl_free_seq(seen);
  277. seen = NULL;
  278. return sol_incref(state->None);
  279. }
  280. sol_object_t *sol_f_print(sol_state_t *state, sol_object_t *args) {
  281. int i, sz = sol_list_len(state, args);
  282. sol_object_t *obj, *str;
  283. seen = dsl_seq_new_array(NULL, NULL);
  284. for(i = 0; i < sz; i++) {
  285. obj = sol_list_get_index(state, args, i);
  286. str = sol_cast_string(state, obj);
  287. sol_printf(state, "%s", str->str);
  288. sol_printf(state, " ");
  289. sol_obj_free(obj);
  290. sol_obj_free(str);
  291. }
  292. sol_printf(state, "\n");
  293. dsl_free_seq(seen);
  294. seen = NULL;
  295. return sol_incref(state->None);
  296. }
  297. sol_object_t *sol_f_rawget(sol_state_t *state, sol_object_t *args) {
  298. sol_object_t *obj = sol_list_get_index(state, args, 0), *key, *res;
  299. if(!sol_is_map(obj)) {
  300. return sol_set_error_string(state, "Rawset of non-map");
  301. }
  302. key = sol_list_get_index(state, args, 1);
  303. res = sol_map_get(state, obj, key);
  304. sol_obj_free(key);
  305. sol_obj_free(obj);
  306. return res;
  307. }
  308. sol_object_t *sol_f_rawset(sol_state_t *state, sol_object_t *args) {
  309. sol_object_t *obj = sol_list_get_index(state, args, 0), *key, *val;
  310. if(!sol_is_map(obj)) {
  311. return sol_set_error_string(state, "Rawset of non-map");
  312. }
  313. key = sol_list_get_index(state, args, 1);
  314. val = sol_list_get_index(state, args, 2);
  315. sol_map_set(state, obj, key, val);
  316. sol_obj_free(val);
  317. sol_obj_free(key);
  318. sol_obj_free(obj);
  319. return sol_incref(state->None);
  320. }
  321. sol_object_t *sol_f_range(sol_state_t *state, sol_object_t *args) {
  322. sol_object_t *res = sol_new_list(state), *bound = sol_list_get_index(state, args, 0);
  323. sol_object_t *boundi = sol_cast_int(state, bound);
  324. int i;
  325. sol_obj_free(bound);
  326. if(sol_has_error(state)) {
  327. sol_obj_free(boundi);
  328. sol_obj_free(res);
  329. return sol_incref(state->None);
  330. }
  331. for(i = 0; i < boundi->ival; i++) {
  332. sol_list_insert(state, res, sol_list_len(state, res), sol_new_int(state, i));
  333. }
  334. sol_obj_free(boundi);
  335. return res;
  336. }
  337. /*
  338. sol_object_t *sol_f_exec(sol_state_t *state, sol_object_t *args) {
  339. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  340. stmt_node *program;
  341. program = sol_compile(prgstr->str);
  342. if(!program) {
  343. return sol_set_error_string(state, "Compilation failure");
  344. }
  345. // XXX should st_free(program);
  346. sol_exec(state, program);
  347. return sol_incref(state->None);
  348. }
  349. sol_object_t *sol_f_eval(sol_state_t *state, sol_object_t *args) {
  350. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  351. stmt_node *program;
  352. program = sol_compile(prgstr->str);
  353. if(!program) {
  354. return sol_set_error_string(state, "Compilation failure");
  355. }
  356. if(program->type != ST_LIST || program->stmtlist->stmt->type != ST_EXPR) {
  357. return sol_set_error_string(state, "Not an expression");
  358. }
  359. // XXX should st_free(program);
  360. return sol_eval(state, program->stmtlist->stmt->expr);
  361. }
  362. */
  363. sol_object_t *sol_f_execfile(sol_state_t *state, sol_object_t *args) {
  364. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  365. stmt_node *program;
  366. FILE *f = fopen(prgstr->str, "r");
  367. char *s;
  368. long sz;
  369. if(!f) {
  370. return sol_set_error_string(state, "File open failure");
  371. }
  372. fseek(f, 0, SEEK_END);
  373. sz = ftell(f);
  374. fseek(f, 0, SEEK_SET);
  375. s = malloc(sz + 1);
  376. if(!s) {
  377. fclose(f);
  378. return sol_set_error_string(state, "File memory allocation failure");
  379. }
  380. fread(s, 1, sz, f);
  381. s[sz] = 0;
  382. fclose(f);
  383. program = sol_compile(s);
  384. free(s);
  385. if(!program) {
  386. return sol_set_error_string(state, "Compilation failure");
  387. }
  388. sol_exec(state, program);
  389. st_free(program);
  390. return sol_incref(state->None);
  391. }
  392. sol_object_t *sol_f_parse(sol_state_t *state, sol_object_t *args) {
  393. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr;
  394. stmt_node *program;
  395. if(sol_is_buffer(prg)) {
  396. if(prg->sz >= 0) {
  397. program = sol_compile_buffer(prg->buffer, prg->sz);
  398. } else {
  399. sol_obj_free(prg);
  400. return sol_set_error_string(state, "parse unsized buffer");
  401. }
  402. } else {
  403. prgstr = sol_cast_string(state, prg);
  404. program = sol_compile(prgstr->str);
  405. sol_obj_free(prgstr);
  406. }
  407. sol_obj_free(prg);
  408. if(!program) {
  409. return sol_set_error_string(state, "Compilation failure");
  410. }
  411. return sol_new_stmtnode(state, program);
  412. }
  413. sol_object_t *sol_f_ord(sol_state_t *state, sol_object_t *args) {
  414. sol_object_t *arg = sol_list_get_index(state, args, 0), *str = sol_cast_string(state, arg);
  415. sol_object_t *arg2, *iarg, *res;
  416. long idx = 0;
  417. size_t len = strlen(str->str);
  418. sol_obj_free(arg);
  419. if(sol_list_len(state, args) > 1) {
  420. arg2 = sol_list_get_index(state, args, 1);
  421. iarg = sol_cast_int(state, arg2);
  422. sol_obj_free(arg2);
  423. idx = iarg->ival;
  424. sol_obj_free(iarg);
  425. }
  426. if(idx < 0) {
  427. idx += len;
  428. }
  429. if(idx < 0 || idx >= len) {
  430. sol_obj_free(str);
  431. return sol_set_error_string(state, "Compute ord of out-of-bounds index");
  432. }
  433. res = sol_new_int(state, str->str[idx]);
  434. sol_obj_free(str);
  435. return res;
  436. }
  437. sol_object_t *sol_f_chr(sol_state_t *state, sol_object_t *args) {
  438. sol_object_t *arg = sol_list_get_index(state, args, 0), *iarg = sol_cast_int(state, arg);
  439. char cbuf[2] = {iarg->ival, 0};
  440. sol_object_t *res = sol_new_buffer(state, strdup(cbuf), 1, OWN_FREE, NULL, NULL);
  441. sol_obj_free(arg);
  442. sol_obj_free(iarg);
  443. return res;
  444. }
  445. sol_object_t *sol_f_debug_getref(sol_state_t *state, sol_object_t *args) {
  446. sol_object_t *obj = sol_list_get_index(state, args, 0);
  447. 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.
  448. sol_obj_free(obj);
  449. return res;
  450. }
  451. sol_object_t *sol_f_debug_setref(sol_state_t *state, sol_object_t *args) {
  452. sol_object_t *obj = sol_list_get_index(state, args, 0), *cnt = sol_list_get_index(state, args, 1);
  453. obj->refcnt = sol_cast_int(state, cnt)->ival + 2; // NB: As above.
  454. sol_obj_free(cnt);
  455. sol_obj_free(obj);
  456. return sol_incref(state->None);
  457. }
  458. sol_object_t *sol_f_debug_closure(sol_state_t *state, sol_object_t *args) {
  459. sol_object_t *func = sol_list_get_index(state, args, 0);
  460. sol_object_t *res = sol_incref(func->closure);
  461. sol_obj_free(func);
  462. return res;
  463. }
  464. sol_object_t *sol_f_debug_globals(sol_state_t *state, sol_object_t *args) {
  465. return sol_list_get_index(state, state->scopes, sol_list_len(state, state->scopes) - 1);
  466. }
  467. sol_object_t *sol_f_debug_locals(sol_state_t *state, sol_object_t *args) {
  468. return sol_list_get_index(state, state->scopes, 0);
  469. }
  470. sol_object_t *sol_f_debug_scopes(sol_state_t *state, sol_object_t *args) {
  471. return sol_incref(state->scopes);
  472. }
  473. sol_object_t *sol_f_debug_getops(sol_state_t *state, sol_object_t *args) {
  474. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = sol_new_map(state);
  475. sol_map_borrow_name(state, res, "name", sol_new_buffer(state, obj->ops->tname, strlen(obj->ops->tname), OWN_NONE, NULL, NULL));
  476. sol_map_borrow_name(state, res, "flags", sol_new_int(state, obj->ops->tflags));
  477. sol_map_borrow_name(state, res, "add", sol_new_cfunc(state, obj->ops->add, "add"));
  478. sol_map_borrow_name(state, res, "sub", sol_new_cfunc(state, obj->ops->sub, "sub"));
  479. sol_map_borrow_name(state, res, "mul", sol_new_cfunc(state, obj->ops->mul, "mul"));
  480. sol_map_borrow_name(state, res, "div", sol_new_cfunc(state, obj->ops->div, "div"));
  481. sol_map_borrow_name(state, res, "mod", sol_new_cfunc(state, obj->ops->mod, "mod"));
  482. sol_map_borrow_name(state, res, "pow", sol_new_cfunc(state, obj->ops->pow, "pow"));
  483. sol_map_borrow_name(state, res, "tbang", sol_new_cfunc(state, obj->ops->tbang, "tbang"));
  484. sol_map_borrow_name(state, res, "band", sol_new_cfunc(state, obj->ops->band, "band"));
  485. sol_map_borrow_name(state, res, "bor", sol_new_cfunc(state, obj->ops->bor, "bor"));
  486. sol_map_borrow_name(state, res, "bxor", sol_new_cfunc(state, obj->ops->bxor, "bxor"));
  487. sol_map_borrow_name(state, res, "blsh", sol_new_cfunc(state, obj->ops->blsh, "blsh"));
  488. sol_map_borrow_name(state, res, "brsh", sol_new_cfunc(state, obj->ops->brsh, "brsh"));
  489. sol_map_borrow_name(state, res, "bnot", sol_new_cfunc(state, obj->ops->bnot, "bnot"));
  490. sol_map_borrow_name(state, res, "cmp", sol_new_cfunc(state, obj->ops->cmp, "cmp"));
  491. sol_map_borrow_name(state, res, "call", sol_new_cfunc(state, obj->ops->call, "call"));
  492. sol_map_borrow_name(state, res, "index", sol_new_cfunc(state, obj->ops->index, "index"));
  493. sol_map_borrow_name(state, res, "setindex", sol_new_cfunc(state, obj->ops->setindex, "setindex"));
  494. sol_map_borrow_name(state, res, "len", sol_new_cfunc(state, obj->ops->len, "len"));
  495. sol_map_borrow_name(state, res, "iter", sol_new_cfunc(state, obj->ops->iter, "iter"));
  496. sol_map_borrow_name(state, res, "toint", sol_new_cfunc(state, obj->ops->toint, "toint"));
  497. sol_map_borrow_name(state, res, "tofloat", sol_new_cfunc(state, obj->ops->tofloat, "tofloat"));
  498. sol_map_borrow_name(state, res, "tostring", sol_new_cfunc(state, obj->ops->tostring, "tostring"));
  499. sol_map_borrow_name(state, res, "tobuffer", sol_new_cfunc(state, obj->ops->tobuffer, "tobuffer"));
  500. sol_map_borrow_name(state, res, "repr", sol_new_cfunc(state, obj->ops->repr, "repr"));
  501. sol_map_borrow_name(state, res, "init", sol_new_cfunc(state, obj->ops->init, "init"));
  502. sol_map_borrow_name(state, res, "free", sol_new_cfunc(state, obj->ops->free, "free"));
  503. sol_obj_free(obj);
  504. return res;
  505. }
  506. #ifndef NO_READLINE
  507. sol_object_t *sol_f_readline_readline(sol_state_t *state, sol_object_t *args) {
  508. sol_object_t *obj, *objstr, *res;
  509. char *line;
  510. if(sol_list_len(state, args) > 0) {
  511. obj = sol_list_get_index(state, args, 0);
  512. objstr = sol_cast_string(state, obj);
  513. line = readline(objstr->str);
  514. sol_obj_free(obj);
  515. sol_obj_free(objstr);
  516. } else {
  517. line = readline("");
  518. }
  519. if(line) {
  520. res = sol_new_buffer(state, strdup(line), strlen(line), OWN_FREE, NULL, NULL);
  521. free(line);
  522. } else {
  523. res = sol_new_buffer(state, strdup(""), 0, OWN_FREE, NULL, NULL);
  524. }
  525. return res;
  526. }
  527. sol_object_t *sol_f_readline_add_history(sol_state_t *state, sol_object_t *args) {
  528. sol_object_t *line = sol_list_get_index(state, args, 0), *linestr = sol_cast_string(state, line);
  529. add_history(linestr->str);
  530. sol_obj_free(linestr);
  531. sol_obj_free(line);
  532. return sol_incref(state->None);
  533. }
  534. #endif
  535. void _sol_freef_seq_iter(void *iter, size_t sz) {
  536. dsl_free_seq_iter((dsl_seq_iter *) iter);
  537. }
  538. sol_object_t *sol_f_iter_str(sol_state_t *state, sol_object_t *args) {
  539. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  540. sol_object_t *index = sol_map_get_name(state, local, "idx"), *max = sol_map_get_name(state, local, "sz"), *res;
  541. char temp[2] = {0, 0};
  542. if(sol_is_none(state, index) || sol_is_none(state, max)) {
  543. sol_obj_free(index);
  544. index = sol_new_buffer(state, (void *) 0, sizeof(void *), OWN_NONE, NULL, NULL);
  545. sol_map_set_name(state, local, "idx", index);
  546. sol_obj_free(max);
  547. max = sol_new_int(state, strlen(obj->str));
  548. sol_map_set_name(state, local, "sz", max);
  549. }
  550. if(((size_t) index->buffer) >= max->ival) {
  551. sol_obj_free(index);
  552. sol_obj_free(obj);
  553. sol_obj_free(local);
  554. return sol_incref(state->None);
  555. }
  556. temp[0] = obj->str[((size_t) index->buffer)];
  557. res = sol_new_string(state, temp);
  558. index->buffer = (void *) ((size_t) index->buffer + 1);
  559. sol_obj_free(index);
  560. sol_obj_free(local);
  561. sol_obj_free(obj);
  562. return res;
  563. }
  564. sol_object_t *sol_f_iter_buffer(sol_state_t *state, sol_object_t *args) {
  565. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  566. sol_object_t *idx = sol_map_get_name(state, local, "idx"), *sz = sol_map_get_name(state,local, "sz"), *res;
  567. if(sol_is_none(state, idx) || sol_is_none(state, sz)) {
  568. sol_obj_free(idx);
  569. sol_obj_free(sz);
  570. idx = sol_new_buffer(state, (void *) 0, sizeof(void *), OWN_NONE, NULL, NULL);
  571. sz = sol_new_int(state, obj->sz);
  572. sol_map_set_name(state, local, "idx", idx);
  573. sol_map_set_name(state, local, "sz", sz);
  574. }
  575. if(((size_t) idx->buffer) >= sz->ival) {
  576. sol_obj_free(idx);
  577. sol_obj_free(sz);
  578. sol_obj_free(local);
  579. return sol_incref(state->None);
  580. }
  581. res = sol_new_buffer(state, ((char *) obj->buffer) + ((size_t) idx->buffer), 1, OWN_NONE, NULL, NULL);
  582. idx->buffer = (void *) ((size_t) idx->buffer + 1);
  583. sol_obj_free(idx);
  584. sol_obj_free(sz);
  585. sol_obj_free(local);
  586. return res;
  587. }
  588. sol_object_t *sol_f_iter_list(sol_state_t *state, sol_object_t *args) {
  589. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  590. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  591. if(sol_is_none(state, index)) {
  592. sol_obj_free(index);
  593. index = sol_new_buffer(state, dsl_new_seq_iter(obj->seq), sizeof(dsl_seq_iter), OWN_CALLF, _sol_freef_seq_iter, NULL);
  594. sol_map_set_name(state, local, "idx", index);
  595. sol_obj_free(index);
  596. }
  597. if(dsl_seq_iter_is_invalid(index->buffer)) {
  598. sol_obj_free(index);
  599. sol_obj_free(obj);
  600. sol_obj_free(local);
  601. return sol_incref(state->None);
  602. }
  603. res = sol_incref(AS_OBJ(dsl_seq_iter_at(index->buffer)));
  604. dsl_seq_iter_next(index->buffer);
  605. sol_obj_free(local);
  606. sol_obj_free(obj);
  607. return res;
  608. }
  609. sol_object_t *sol_f_iter_map(sol_state_t *state, sol_object_t *args) {
  610. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  611. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  612. if(sol_is_none(state, index)) {
  613. sol_obj_free(index);
  614. index = sol_new_buffer(state, dsl_new_seq_iter(obj->seq), sizeof(dsl_seq_iter), OWN_CALLF, _sol_freef_seq_iter, NULL);
  615. sol_map_set_name(state, local, "idx", index);
  616. sol_obj_free(index);
  617. }
  618. if(dsl_seq_iter_is_invalid(index->buffer)) {
  619. sol_obj_free(index);
  620. sol_obj_free(obj);
  621. sol_obj_free(local);
  622. return sol_incref(state->None);
  623. }
  624. res = sol_incref(AS_OBJ(dsl_seq_iter_at(index->buffer))->key);
  625. dsl_seq_iter_next(index->buffer);
  626. sol_obj_free(local);
  627. sol_obj_free(obj);
  628. return res;
  629. }
  630. sol_object_t *sol_f_ast_print(sol_state_t *state, sol_object_t *args) {
  631. sol_object_t *obj = sol_list_get_index(state, args, 0);
  632. if(sol_is_aststmt(obj)) {
  633. st_print(state, obj->node);
  634. } else {
  635. ex_print(state, obj->node);
  636. }
  637. sol_obj_free(obj);
  638. return sol_incref(state->None);
  639. }
  640. sol_object_t *sol_f_singlet_tostring(sol_state_t *state, sol_object_t *args) {
  641. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = sol_new_string(state, obj->str);
  642. sol_obj_free(obj);
  643. return res;
  644. }
  645. sol_object_t *sol_f_singlet_tobuffer(sol_state_t *state, sol_object_t *args) {
  646. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = sol_new_buffer(state, strdup(obj->str), strlen(obj->str), OWN_FREE, NULL, NULL);
  647. sol_obj_free(obj);
  648. return res;
  649. }
  650. sol_object_t *sol_f_int_add(sol_state_t *state, sol_object_t *args) {
  651. 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);
  652. sol_object_t *res = sol_new_int(state, a->ival + bint->ival);
  653. sol_obj_free(a);
  654. sol_obj_free(b);
  655. sol_obj_free(bint);
  656. if(sol_has_error(state)) {
  657. sol_obj_free(res);
  658. return sol_incref(state->None);
  659. }
  660. return res;
  661. }
  662. sol_object_t *sol_f_int_sub(sol_state_t *state, sol_object_t *args) {
  663. 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);
  664. sol_object_t *res = sol_new_int(state, a->ival - bint->ival);
  665. sol_obj_free(a);
  666. sol_obj_free(b);
  667. sol_obj_free(bint);
  668. if(sol_has_error(state)) {
  669. sol_obj_free(res);
  670. return sol_incref(state->None);
  671. }
  672. return res;
  673. }
  674. sol_object_t *sol_f_int_mul(sol_state_t *state, sol_object_t *args) {
  675. 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);
  676. sol_object_t *res = sol_new_int(state, a->ival * bint->ival);
  677. sol_obj_free(a);
  678. sol_obj_free(b);
  679. sol_obj_free(bint);
  680. if(sol_has_error(state)) {
  681. sol_obj_free(res);
  682. return sol_incref(state->None);
  683. }
  684. return res;
  685. }
  686. sol_object_t *sol_f_int_div(sol_state_t *state, sol_object_t *args) {
  687. 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);
  688. sol_object_t *res;
  689. sol_obj_free(b);
  690. if(bint->ival == 0) {
  691. sol_obj_free(a);
  692. sol_obj_free(bint);
  693. return sol_set_error_string(state, "integer divide by zero");
  694. }
  695. res = sol_new_int(state, a->ival / bint->ival);
  696. sol_obj_free(a);
  697. sol_obj_free(bint);
  698. if(sol_has_error(state)) {
  699. sol_obj_free(res);
  700. return sol_incref(state->None);
  701. }
  702. return res;
  703. }
  704. sol_object_t *sol_f_int_mod(sol_state_t *state, sol_object_t *args) {
  705. 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);
  706. sol_object_t *res;
  707. sol_obj_free(b);
  708. if(bint->ival == 0) {
  709. sol_obj_free(a);
  710. sol_obj_free(bint);
  711. return sol_set_error_string(state, "integer modulus by zero");
  712. }
  713. res = sol_new_int(state, a->ival % bint->ival);
  714. sol_obj_free(a);
  715. sol_obj_free(bint);
  716. if(sol_has_error(state)) {
  717. sol_obj_free(res);
  718. return sol_incref(state->None);
  719. }
  720. return res;
  721. }
  722. sol_object_t *sol_f_int_pow(sol_state_t *state, sol_object_t *args) {
  723. 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);
  724. sol_object_t *res = sol_new_int(state, (long) pow((double) a->ival, bint->ival));
  725. sol_obj_free(a);
  726. sol_obj_free(b);
  727. sol_obj_free(bint);
  728. if(sol_has_error(state)) {
  729. sol_obj_free(res);
  730. return sol_incref(state->None);
  731. }
  732. return res;
  733. }
  734. sol_object_t *sol_f_int_band(sol_state_t *state, sol_object_t *args) {
  735. 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);
  736. sol_object_t *res = sol_new_int(state, a->ival & bint->ival);
  737. sol_obj_free(a);
  738. sol_obj_free(b);
  739. sol_obj_free(bint);
  740. if(sol_has_error(state)) {
  741. sol_obj_free(res);
  742. return sol_incref(state->None);
  743. }
  744. return res;
  745. }
  746. sol_object_t *sol_f_int_bor(sol_state_t *state, sol_object_t *args) {
  747. 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);
  748. sol_object_t *res = sol_new_int(state, a->ival | bint->ival);
  749. sol_obj_free(a);
  750. sol_obj_free(b);
  751. sol_obj_free(bint);
  752. if(sol_has_error(state)) {
  753. sol_obj_free(res);
  754. return sol_incref(state->None);
  755. }
  756. return res;
  757. }
  758. sol_object_t *sol_f_int_bxor(sol_state_t *state, sol_object_t *args) {
  759. 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);
  760. sol_object_t *res = sol_new_int(state, a->ival ^ bint->ival);
  761. sol_obj_free(a);
  762. sol_obj_free(b);
  763. sol_obj_free(bint);
  764. if(sol_has_error(state)) {
  765. sol_obj_free(res);
  766. return sol_incref(state->None);
  767. }
  768. return res;
  769. }
  770. sol_object_t *sol_f_int_blsh(sol_state_t *state, sol_object_t *args) {
  771. 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);
  772. sol_object_t *res = sol_new_int(state, a->ival << bint->ival);
  773. sol_obj_free(a);
  774. sol_obj_free(b);
  775. sol_obj_free(bint);
  776. if(sol_has_error(state)) {
  777. sol_obj_free(res);
  778. return sol_incref(state->None);
  779. }
  780. return res;
  781. }
  782. sol_object_t *sol_f_int_brsh(sol_state_t *state, sol_object_t *args) {
  783. 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);
  784. sol_object_t *res = sol_new_int(state, a->ival >> bint->ival);
  785. sol_obj_free(a);
  786. sol_obj_free(b);
  787. sol_obj_free(bint);
  788. if(sol_has_error(state)) {
  789. sol_obj_free(res);
  790. return sol_incref(state->None);
  791. }
  792. return res;
  793. }
  794. sol_object_t *sol_f_int_bnot(sol_state_t *state, sol_object_t *args) {
  795. sol_object_t *a = sol_list_get_index(state, args, 0);
  796. sol_object_t *res = sol_new_int(state, ~a->ival);
  797. sol_obj_free(a);
  798. return res;
  799. }
  800. sol_object_t *sol_f_int_cmp(sol_state_t *state, sol_object_t *args) {
  801. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  802. sol_object_t *res;
  803. if(sol_is_int(b)) {
  804. res = sol_new_int(state, a->ival == b->ival ? 0 : (a->ival < b->ival ? -1 : 1));
  805. } else {
  806. res = sol_new_int(state, 1);
  807. }
  808. sol_obj_free(a);
  809. sol_obj_free(b);
  810. return res;
  811. }
  812. sol_object_t *sol_f_int_toint(sol_state_t *state, sol_object_t *args) {
  813. return sol_list_get_index(state, args, 0);
  814. }
  815. sol_object_t *sol_f_int_tofloat(sol_state_t *state, sol_object_t *args) {
  816. sol_object_t *a = sol_list_get_index(state, args, 0);
  817. sol_object_t *res = sol_new_float(state, (double) a->ival);
  818. sol_obj_free(a);
  819. return res;
  820. }
  821. sol_object_t *sol_f_int_tostring(sol_state_t *state, sol_object_t *args) {
  822. sol_object_t *a = sol_list_get_index(state, args, 0);
  823. char *s = _itoa(a->ival);
  824. sol_object_t *res = sol_new_string(state, s);
  825. sol_obj_free(a);
  826. free(s);
  827. return res;
  828. }
  829. sol_object_t *sol_f_float_add(sol_state_t *state, sol_object_t *args) {
  830. 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);
  831. sol_object_t *res = sol_new_float(state, a->fval + bflt->fval);
  832. sol_obj_free(a);
  833. sol_obj_free(b);
  834. sol_obj_free(bflt);
  835. if(sol_has_error(state)) {
  836. sol_obj_free(res);
  837. return sol_incref(state->None);
  838. }
  839. return res;
  840. }
  841. sol_object_t *sol_f_float_sub(sol_state_t *state, sol_object_t *args) {
  842. 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);
  843. sol_object_t *res = sol_new_float(state, a->fval - bflt->fval);
  844. sol_obj_free(a);
  845. sol_obj_free(b);
  846. sol_obj_free(bflt);
  847. if(sol_has_error(state)) {
  848. sol_obj_free(res);
  849. return sol_incref(state->None);
  850. }
  851. return res;
  852. }
  853. sol_object_t *sol_f_float_mul(sol_state_t *state, sol_object_t *args) {
  854. 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);
  855. sol_object_t *res = sol_new_float(state, a->fval * bflt->fval);
  856. sol_obj_free(a);
  857. sol_obj_free(b);
  858. sol_obj_free(bflt);
  859. if(sol_has_error(state)) {
  860. sol_obj_free(res);
  861. return sol_incref(state->None);
  862. }
  863. return res;
  864. }
  865. sol_object_t *sol_f_float_div(sol_state_t *state, sol_object_t *args) {
  866. 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);
  867. sol_object_t *res;
  868. sol_obj_free(b);
  869. if(bflt->fval == 0.0) {
  870. sol_obj_free(a);
  871. sol_obj_free(bflt);
  872. return sol_set_error_string(state, "floating division by zero");
  873. }
  874. res = sol_new_float(state, a->fval / bflt->fval);
  875. sol_obj_free(a);
  876. sol_obj_free(bflt);
  877. if(sol_has_error(state)) {
  878. sol_obj_free(res);
  879. return sol_incref(state->None);
  880. }
  881. return res;
  882. }
  883. sol_object_t *sol_f_float_pow(sol_state_t *state, sol_object_t *args) {
  884. 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);
  885. sol_object_t *res = sol_new_float(state, pow(a->fval, bflt->fval));
  886. sol_obj_free(b);
  887. sol_obj_free(a);
  888. sol_obj_free(bflt);
  889. if(sol_has_error(state)) {
  890. sol_obj_free(res);
  891. return sol_incref(state->None);
  892. }
  893. return res;
  894. }
  895. sol_object_t *sol_f_float_cmp(sol_state_t *state, sol_object_t *args) {
  896. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  897. sol_object_t *res;
  898. if(sol_is_float(b)) {
  899. res = sol_new_int(state, a->fval==b->fval? 0 : (a->fval<b->fval? -1 : 1));
  900. } else {
  901. res = sol_new_int(state, 1);
  902. }
  903. sol_obj_free(a);
  904. sol_obj_free(b);
  905. return res;
  906. }
  907. sol_object_t *sol_f_float_toint(sol_state_t *state, sol_object_t *args) {
  908. sol_object_t *a = sol_list_get_index(state, args, 0);
  909. sol_object_t *res = sol_new_int(state, (int) a->fval);
  910. sol_obj_free(a);
  911. return res;
  912. }
  913. sol_object_t *sol_f_float_tofloat(sol_state_t *state, sol_object_t *args) {
  914. return sol_list_get_index(state, args, 0);
  915. }
  916. sol_object_t *sol_f_float_tostring(sol_state_t *state, sol_object_t *args) {
  917. sol_object_t *a = sol_list_get_index(state, args, 0);
  918. char *s = _ftoa(a->fval);
  919. sol_object_t *res = sol_new_string(state, s);
  920. sol_obj_free(a);
  921. free(s);
  922. return res;
  923. }
  924. sol_object_t *sol_f_str_add(sol_state_t *state, sol_object_t *args) {
  925. 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);
  926. sol_object_t *res = sol_string_concat(state, a, bstr);
  927. sol_obj_free(a);
  928. sol_obj_free(b);
  929. sol_obj_free(bstr);
  930. if(sol_has_error(state)) {
  931. sol_obj_free(res);
  932. return sol_incref(state->None);
  933. }
  934. return res;
  935. }
  936. sol_object_t *sol_f_str_mul(sol_state_t *state, sol_object_t *args) {
  937. 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);
  938. int n = strlen(a->str) * bint->ival + 1;
  939. char *s = malloc(n);
  940. int i;
  941. s[0] = '\0';
  942. for(i = 0; i < b->ival; i++) {
  943. strncat(s, a->str, n);
  944. }
  945. sol_object_t *res = sol_new_string(state, s);
  946. sol_obj_free(a);
  947. sol_obj_free(b);
  948. sol_obj_free(bint);
  949. free(s);
  950. if(sol_has_error(state)) {
  951. sol_obj_free(res);
  952. return sol_incref(state->None);
  953. }
  954. return res;
  955. }
  956. sol_object_t *sol_f_str_cmp(sol_state_t *state, sol_object_t *args) {
  957. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  958. sol_object_t *sb, *res;
  959. if(sol_is_buffer(b)) {
  960. sb = sol_cast_string(state, b);
  961. sol_obj_free(b);
  962. b = sb;
  963. }
  964. if(sol_is_string(b)) {
  965. res = sol_new_int(state, strcmp(a->str, b->str));
  966. } else {
  967. res = sol_new_int(state, 1);
  968. }
  969. sol_obj_free(a);
  970. sol_obj_free(b);
  971. return res;
  972. }
  973. sol_object_t *sol_f_str_len(sol_state_t *state, sol_object_t *args) {
  974. sol_object_t *a = sol_list_get_index(state, args, 0);
  975. sol_object_t *res = sol_new_int(state, strlen(a->str));
  976. sol_obj_free(a);
  977. return res;
  978. }
  979. sol_object_t *sol_f_str_index(sol_state_t *state, sol_object_t *args) {
  980. sol_object_t *str = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *idx, *funcs, *res;
  981. char buf[2] = {0, 0};
  982. if(sol_is_string(key)) {
  983. funcs = sol_get_methods_name(state, "string");
  984. res = sol_map_get(state, funcs, key);
  985. sol_obj_free(funcs);
  986. return res;
  987. }
  988. idx = sol_cast_int(state, key);
  989. if(idx->ival >= 0 && idx->ival < strlen(str->str)) {
  990. buf[0] = str->str[idx->ival];
  991. }
  992. sol_obj_free(str);
  993. sol_obj_free(key);
  994. sol_obj_free(idx);
  995. return sol_new_string(state, buf);
  996. }
  997. sol_object_t *sol_f_str_iter(sol_state_t *state, sol_object_t *args) {
  998. return sol_new_cfunc(state, sol_f_iter_str, "iter.str");
  999. }
  1000. sol_object_t *sol_f_str_toint(sol_state_t *state, sol_object_t *args) {
  1001. sol_object_t *a = sol_list_get_index(state, args, 0);
  1002. sol_object_t *res = sol_new_int(state, atoi(a->str));
  1003. sol_obj_free(a);
  1004. return res;
  1005. }
  1006. sol_object_t *sol_f_str_tofloat(sol_state_t *state, sol_object_t *args) {
  1007. sol_object_t *a = sol_list_get_index(state, args, 0);
  1008. sol_object_t *res = sol_new_float(state, atof(a->str));
  1009. sol_obj_free(a);
  1010. return res;
  1011. }
  1012. sol_object_t *sol_f_str_tostring(sol_state_t *state, sol_object_t *args) {
  1013. return sol_list_get_index(state, args, 0);
  1014. }
  1015. sol_object_t *sol_f_str_tobuffer(sol_state_t *state, sol_object_t *args) {
  1016. sol_object_t *str = sol_list_get_index(state, args, 0);
  1017. sol_object_t *res = sol_new_buffer(state, strdup(str->str), strlen(str->str), OWN_FREE, NULL, NULL);
  1018. sol_obj_free(str);
  1019. return res;
  1020. }
  1021. sol_object_t *sol_f_str_repr(sol_state_t *state, sol_object_t *args) {
  1022. sol_object_t *obj = sol_list_get_index(state, args, 0), *cur = sol_new_string(state, "\""), *next = sol_string_concat(state, cur, obj);
  1023. sol_obj_free(cur);
  1024. cur = next;
  1025. next = sol_string_concat_cstr(state, cur, "\"");
  1026. sol_obj_free(cur);
  1027. return next;
  1028. }
  1029. sol_object_t *sol_f_str_sub(sol_state_t *state, sol_object_t *args) {
  1030. 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);
  1031. sol_object_t *ilow, *ihigh;
  1032. long l, h;
  1033. size_t len = strlen(str->str), i;
  1034. char *s;
  1035. if(sol_is_none(state, low)) {
  1036. ilow = sol_new_int(state, 0);
  1037. } else {
  1038. ilow = sol_cast_int(state, low);
  1039. }
  1040. if(sol_is_none(state, high)) {
  1041. ihigh = sol_new_int(state, len);
  1042. } else {
  1043. ihigh = sol_cast_int(state, high);
  1044. }
  1045. sol_obj_free(low);
  1046. sol_obj_free(high);
  1047. l = ilow->ival;
  1048. h = ihigh->ival;
  1049. sol_obj_free(ihigh);
  1050. sol_obj_free(ilow);
  1051. if(l < 0) {
  1052. l += len;
  1053. if(l < 0) {
  1054. l = 0;
  1055. }
  1056. }
  1057. if(l > len) {
  1058. l = len;
  1059. }
  1060. if(h < 0) {
  1061. h += len;
  1062. if(h < 0) {
  1063. h = 0;
  1064. }
  1065. }
  1066. if(h > len) {
  1067. h = len;
  1068. }
  1069. if(l >= h) {
  1070. sol_obj_free(str);
  1071. return sol_new_string(state, "");
  1072. }
  1073. s = malloc(h - l + 1);
  1074. for(i = l; i < h; i++) {
  1075. s[i - l] = str->str[i];
  1076. }
  1077. s[h - l] = '\0';
  1078. sol_obj_free(str);
  1079. return sol_new_string(state, s);
  1080. }
  1081. sol_object_t *sol_f_str_split(sol_state_t *state, sol_object_t *args) {
  1082. 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);
  1083. sol_object_t *res = sol_new_list(state), *opart;
  1084. char *s = strdup(str->str);
  1085. char *part = strtok(s, stok->str);
  1086. sol_obj_free(tok);
  1087. sol_obj_free(str);
  1088. if(!part) {
  1089. sol_obj_free(res);
  1090. sol_obj_free(stok);
  1091. return sol_incref(state->None);
  1092. }
  1093. opart = sol_new_string(state, part);
  1094. sol_list_insert(state, res, 0, opart);
  1095. sol_obj_free(opart);
  1096. while(part = strtok(NULL, stok->str)) {
  1097. opart = sol_new_string(state, part);
  1098. sol_list_insert(state, res, sol_list_len(state, res), opart);
  1099. sol_obj_free(opart);
  1100. }
  1101. sol_obj_free(stok);
  1102. return res;
  1103. }
  1104. sol_object_t *sol_f_str_find(sol_state_t *state, sol_object_t *args) {
  1105. 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);
  1106. char *ptr = strstr(str->str, ssubstr->str);
  1107. sol_object_t *res = sol_new_int(state, ptr ? ptr - str->str : -1);
  1108. sol_obj_free(str);
  1109. sol_obj_free(substr);
  1110. sol_obj_free(ssubstr);
  1111. return res;
  1112. }
  1113. sol_object_t *sol_f_list_add(sol_state_t *state, sol_object_t *args) {
  1114. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *ls;
  1115. if(!sol_is_list(b)) {
  1116. sol_obj_free(a);
  1117. sol_obj_free(b);
  1118. return sol_set_error_string(state, "Adding list to non-list");
  1119. }
  1120. ls = sol_list_copy(state, a);
  1121. sol_list_append(state, ls, b);
  1122. sol_obj_free(a);
  1123. sol_obj_free(b);
  1124. return ls;
  1125. }
  1126. sol_object_t *sol_f_list_mul(sol_state_t *state, sol_object_t *args) {
  1127. 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;
  1128. int i;
  1129. if(sol_has_error(state)) {
  1130. sol_obj_free(a);
  1131. sol_obj_free(b);
  1132. sol_obj_free(bint);
  1133. return sol_incref(state->None);
  1134. }
  1135. ls = sol_new_list(state);
  1136. for(i = 0; i < b->ival; i++) {
  1137. sol_list_append(state, ls, a);
  1138. if(sol_has_error(state)) {
  1139. sol_obj_free(a);
  1140. sol_obj_free(b);
  1141. sol_obj_free(bint);
  1142. return sol_incref(state->None);
  1143. }
  1144. }
  1145. return ls;
  1146. }
  1147. sol_object_t *sol_f_list_cmp(sol_state_t *state, sol_object_t *args) {
  1148. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *item, *ls, *tmp;
  1149. int i, alen, blen;
  1150. if(!sol_is_list(b)) {
  1151. sol_obj_free(b);
  1152. sol_obj_free(a);
  1153. return sol_new_int(state, 1); // XXX lists always greater than non-lists?
  1154. }
  1155. alen = sol_list_len(state, a);
  1156. blen = sol_list_len(state, b);
  1157. if(alen != blen) {
  1158. sol_obj_free(b);
  1159. sol_obj_free(a);
  1160. return sol_new_int(state, alen > blen ? 1 : -1);
  1161. }
  1162. ls = sol_new_list(state);
  1163. sol_list_insert(state, ls, 0, state->None);
  1164. sol_list_insert(state, ls, 1, state->None);
  1165. for(i = 0; i < alen; i++) {
  1166. tmp = sol_list_get_index(state, a, i);
  1167. sol_list_set_index(state, ls, 0, tmp);
  1168. item = sol_list_get_index(state, b, i);
  1169. sol_list_set_index(state, ls, 1, item);
  1170. sol_obj_free(item);
  1171. item = CALL_METHOD(state, tmp, cmp, ls);
  1172. sol_obj_free(tmp);
  1173. if(item->ival != 0) {
  1174. sol_obj_free(ls);
  1175. sol_obj_free(b);
  1176. sol_obj_free(a);
  1177. return item;
  1178. }
  1179. sol_obj_free(item);
  1180. }
  1181. sol_obj_free(ls);
  1182. sol_obj_free(b);
  1183. sol_obj_free(a);
  1184. return sol_new_int(state, 0);
  1185. }
  1186. sol_object_t *sol_f_list_index(sol_state_t *state, sol_object_t *args) {
  1187. sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *ival;
  1188. sol_object_t *res, *funcs;
  1189. if(sol_is_name(b)) {
  1190. funcs = sol_get_methods_name(state, "list");
  1191. res = sol_map_get(state, funcs, b);
  1192. sol_obj_free(funcs);
  1193. } else {
  1194. ival = sol_cast_int(state, b);
  1195. res = sol_list_get_index(state, ls, ival->ival);
  1196. sol_obj_free(ival);
  1197. }
  1198. sol_obj_free(ls);
  1199. sol_obj_free(b);
  1200. return res;
  1201. }
  1202. sol_object_t *sol_f_list_setindex(sol_state_t *state, sol_object_t *args) {
  1203. 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);
  1204. sol_object_t *val = sol_list_get_index(state, args, 2);
  1205. sol_list_set_index(state, ls, bint->ival, val);
  1206. sol_obj_free(ls);
  1207. sol_obj_free(b);
  1208. sol_obj_free(bint);
  1209. sol_obj_free(val);
  1210. return sol_incref(state->None);
  1211. }
  1212. sol_object_t *sol_f_list_len(sol_state_t *state, sol_object_t *args) {
  1213. sol_object_t *ls = sol_list_get_index(state, args, 0);
  1214. sol_object_t *res = sol_new_int(state, sol_list_len(state, ls));
  1215. sol_obj_free(ls);
  1216. return res;
  1217. }
  1218. sol_object_t *sol_f_list_iter(sol_state_t *state, sol_object_t *args) {
  1219. return sol_new_cfunc(state, sol_f_iter_list, "iter.list");
  1220. }
  1221. sol_object_t *sol_f_list_tostring(sol_state_t *state, sol_object_t *args) {
  1222. sol_object_t *cur = sol_new_string(state, "["), *next, *str, *obj = sol_list_get_index(state, args, 0), *item;
  1223. dsl_seq_iter *iter = dsl_new_seq_iter(obj->seq);
  1224. char s[64];
  1225. while(!dsl_seq_iter_is_invalid(iter)) {
  1226. item = AS_OBJ(dsl_seq_iter_at(iter));
  1227. if(test_seen(item)) {
  1228. snprintf(s, 64, "... (%p)", item);
  1229. next = sol_string_concat_cstr(state, cur, s);
  1230. } else {
  1231. str = sol_cast_repr(state, item);
  1232. next = sol_string_concat(state, cur, str);
  1233. sol_obj_free(str);
  1234. }
  1235. sol_obj_free(cur);
  1236. cur = next;
  1237. if(!dsl_seq_iter_at_end(iter)) {
  1238. next = sol_string_concat_cstr(state, cur, ", ");
  1239. sol_obj_free(cur);
  1240. cur = next;
  1241. }
  1242. dsl_seq_iter_next(iter);
  1243. }
  1244. next = sol_string_concat_cstr(state, cur, "]");
  1245. sol_obj_free(cur);
  1246. dsl_free_seq_iter(iter);
  1247. return next;
  1248. }
  1249. sol_object_t *sol_f_list_copy(sol_state_t *state, sol_object_t *args) {
  1250. sol_object_t *list = sol_list_get_index(state, args, 0);
  1251. sol_object_t *res = sol_list_copy(state, list);
  1252. sol_obj_free(list);
  1253. return res;
  1254. }
  1255. sol_object_t *sol_f_list_insert(sol_state_t *state, sol_object_t *args) {
  1256. 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);
  1257. sol_list_insert(state, list, iidx->ival, obj);
  1258. sol_obj_free(list);
  1259. sol_obj_free(idx);
  1260. sol_obj_free(iidx);
  1261. sol_obj_free(obj);
  1262. return sol_incref(state->None);
  1263. }
  1264. sol_object_t *sol_f_list_remove(sol_state_t *state, sol_object_t *args) {
  1265. 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);
  1266. sol_object_t *res = sol_list_remove(state, list, iidx->ival);
  1267. sol_obj_free(list);
  1268. sol_obj_free(idx);
  1269. sol_obj_free(iidx);
  1270. return res;
  1271. }
  1272. sol_object_t *sol_f_list_truncate(sol_state_t *state, sol_object_t *args) {
  1273. 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);
  1274. sol_object_t *res = sol_list_truncate(state, list, iidx->ival);
  1275. sol_obj_free(list);
  1276. sol_obj_free(idx);
  1277. sol_obj_free(iidx);
  1278. return res;
  1279. }
  1280. sol_object_t *sol_f_list_map(sol_state_t *state, sol_object_t *args) {
  1281. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  1282. sol_object_t *fargs = sol_new_list(state), *item;
  1283. int idx = 0, len = sol_list_len(state, list);
  1284. sol_list_insert(state, fargs, 0, func);
  1285. while(idx < len) {
  1286. item = sol_list_get_index(state, list, idx);
  1287. sol_list_insert(state, fargs, 1, item);
  1288. sol_obj_free(item);
  1289. item = CALL_METHOD(state, func, call, fargs);
  1290. if(sol_has_error(state)) {
  1291. return list;
  1292. }
  1293. sol_list_remove(state, fargs, 1);
  1294. sol_list_set_index(state, list, idx, item);
  1295. sol_obj_free(item);
  1296. idx++;
  1297. }
  1298. sol_obj_free(fargs);
  1299. sol_obj_free(func);
  1300. return list;
  1301. }
  1302. sol_object_t *sol_f_list_filter(sol_state_t *state, sol_object_t *args) {
  1303. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  1304. sol_object_t *fargs = sol_new_list(state), *item, *ival;
  1305. int idx = 0, len = sol_list_len(state, list);
  1306. sol_list_insert(state, fargs, 0, func);
  1307. while(idx < len) {
  1308. item = sol_list_get_index(state, list, idx);
  1309. sol_list_insert(state, fargs, 1, item);
  1310. sol_obj_free(item);
  1311. item = CALL_METHOD(state, func, call, fargs);
  1312. if(sol_has_error(state)) {
  1313. return list;
  1314. }
  1315. ival = sol_cast_int(state, item);
  1316. if(ival->ival) {
  1317. idx++;
  1318. } else {
  1319. sol_list_remove(state, list, idx);
  1320. len--;
  1321. }
  1322. sol_obj_free(item);
  1323. sol_obj_free(ival);
  1324. }
  1325. sol_obj_free(fargs);
  1326. sol_obj_free(func);
  1327. return list;
  1328. }
  1329. sol_object_t *sol_f_list_reduce(sol_state_t *state, sol_object_t *args) {
  1330. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  1331. sol_object_t *val = sol_list_get_index(state, args, 2), *newval;
  1332. sol_object_t *fargs = sol_new_list(state), *item;
  1333. int idx = 0, len = sol_list_len(state, list);
  1334. sol_list_insert(state, fargs, 0, func);
  1335. sol_list_insert(state, fargs, 1, state->None);
  1336. sol_list_insert(state, fargs, 2, state->None);
  1337. while(idx < len) {
  1338. item = sol_list_get_index(state, list, idx);
  1339. sol_list_set_index(state, fargs, 1, val);
  1340. sol_list_set_index(state, fargs, 2, item);
  1341. sol_obj_free(item);
  1342. newval = CALL_METHOD(state, func, call, fargs);
  1343. sol_obj_free(val);
  1344. val = newval;
  1345. idx++;
  1346. }
  1347. sol_obj_free(list);
  1348. sol_obj_free(func);
  1349. sol_obj_free(fargs);
  1350. return val;
  1351. }
  1352. sol_object_t *sol_f_map_add(sol_state_t *state, sol_object_t *args) {
  1353. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *map;
  1354. if(!sol_is_map(b)) {
  1355. sol_obj_free(a);
  1356. sol_obj_free(b);
  1357. return sol_set_error_string(state, "Adding map to non-map");
  1358. }
  1359. map = sol_map_copy(state, a);
  1360. sol_map_merge(state, map, b);
  1361. sol_obj_free(a);
  1362. sol_obj_free(b);
  1363. return map;
  1364. }
  1365. sol_object_t *sol_f_map_index(sol_state_t *state, sol_object_t *args) {
  1366. sol_object_t *map = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  1367. sol_object_t *indexf = sol_map_get_name(state, map, "__index");
  1368. sol_object_t *res = NULL, *newls;
  1369. res = sol_map_get(state, map, b);
  1370. if(sol_is_none(state, res)) {
  1371. if(!sol_is_none(state, indexf)) {
  1372. sol_obj_free(res);
  1373. if(indexf->ops->call && (sol_is_func(indexf) || sol_is_cfunc(indexf)) && indexf->ops->call != sol_f_not_impl) {
  1374. newls = sol_new_list(state);
  1375. sol_list_insert(state, newls, 0, indexf);
  1376. sol_list_append(state, newls, args);
  1377. res = CALL_METHOD(state, indexf, call, newls);
  1378. sol_obj_free(newls);
  1379. } else if(indexf->ops->index && indexf->ops->index != sol_f_not_impl) {
  1380. newls = sol_new_list(state);
  1381. sol_list_insert(state, newls, 0, indexf);
  1382. sol_list_insert(state, newls, 1, b);
  1383. res = CALL_METHOD(state, indexf, index, newls);
  1384. sol_obj_free(newls);
  1385. }
  1386. }
  1387. }
  1388. sol_obj_free(indexf);
  1389. sol_obj_free(map);
  1390. sol_obj_free(b);
  1391. return res;
  1392. }
  1393. sol_object_t *sol_f_map_setindex(sol_state_t *state, sol_object_t *args) {
  1394. sol_object_t *map = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  1395. sol_object_t *val = sol_list_get_index(state, args, 2);
  1396. sol_object_t *setindexf = sol_map_get_name(state, map, "__setindex"), *newls;
  1397. if(!sol_is_none(state, setindexf)) {
  1398. if(setindexf->ops->call && (sol_is_func(setindexf) || sol_is_cfunc(setindexf)) && setindexf->ops->call != sol_f_not_impl) {
  1399. newls = sol_new_list(state);
  1400. sol_list_insert(state, newls, 0, setindexf);
  1401. sol_list_append(state, newls, args);
  1402. sol_obj_free(CALL_METHOD(state, setindexf, call, newls));
  1403. sol_obj_free(newls);
  1404. return sol_incref(state->None);
  1405. } else if(setindexf->ops->setindex && setindexf->ops->setindex != sol_f_not_impl) {
  1406. newls = sol_new_list(state);
  1407. sol_list_insert(state, newls, 0, setindexf);
  1408. sol_list_insert(state, newls, 1, b);
  1409. sol_list_insert(state, newls, 2, val);
  1410. sol_obj_free(CALL_METHOD(state, setindexf, index, newls));
  1411. sol_obj_free(newls);
  1412. return sol_incref(state->None);
  1413. }
  1414. }
  1415. sol_obj_free(setindexf);
  1416. sol_map_set(state, map, b, val);
  1417. sol_obj_free(map);
  1418. sol_obj_free(b);
  1419. sol_obj_free(val);
  1420. return sol_incref(state->None);
  1421. }
  1422. sol_object_t *sol_f_map_call(sol_state_t *state, sol_object_t *args) {
  1423. sol_object_t *map = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  1424. sol_object_t *callf = sol_map_get_name(state, map, "__call"), *res = NULL;
  1425. if(!sol_is_none(state, callf)) {
  1426. if(callf->ops->call) {
  1427. sol_list_insert(state, fargs, 0, callf);
  1428. sol_list_insert(state, fargs, 1, map);
  1429. res = CALL_METHOD(state, callf, call, fargs);
  1430. }
  1431. }
  1432. sol_obj_free(map);
  1433. sol_obj_free(fargs);
  1434. sol_obj_free(callf);
  1435. if(res) {
  1436. return res;
  1437. }
  1438. return sol_set_error_string(state, "Call map without call method");
  1439. }
  1440. sol_object_t *sol_f_map_len(sol_state_t *state, sol_object_t *args) {
  1441. sol_object_t *map = sol_list_get_index(state, args, 0);
  1442. sol_object_t *res = sol_new_int(state, sol_map_len(state, map));
  1443. sol_obj_free(map);
  1444. return res;
  1445. }
  1446. sol_object_t *sol_f_map_iter(sol_state_t *state, sol_object_t *args) {
  1447. return sol_new_cfunc(state, sol_f_iter_map, "iter.map");
  1448. }
  1449. sol_object_t *sol_f_map_tostring(sol_state_t *state, sol_object_t *args) {
  1450. sol_object_t *map = sol_list_get_index(state, args, 0), *res;
  1451. sol_object_t *tostrf = sol_map_get_name(state, map, "__tostring"), *fargs;
  1452. if(!sol_is_none(state, tostrf) && tostrf->ops->call) {
  1453. fargs = sol_new_list(state);
  1454. sol_list_insert(state, fargs, 0, tostrf);
  1455. sol_list_insert(state, fargs, 1, map);
  1456. res = CALL_METHOD(state, tostrf, call, fargs);
  1457. sol_obj_free(fargs);
  1458. } else {
  1459. res = sol_cast_repr(state, map);
  1460. }
  1461. sol_obj_free(tostrf);
  1462. sol_obj_free(map);
  1463. return res;
  1464. }
  1465. sol_object_t *sol_f_map_repr(sol_state_t *state, sol_object_t *args) {
  1466. 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;
  1467. dsl_seq_iter *iter;
  1468. char s[64];
  1469. if(!sol_is_none(state, reprf) && reprf->ops->call) {
  1470. sol_obj_free(cur);
  1471. fargs = sol_new_list(state);
  1472. sol_list_insert(state, fargs, 0, reprf);
  1473. sol_list_insert(state, fargs, 1, obj);
  1474. cur = CALL_METHOD(state, reprf, call, fargs);
  1475. sol_obj_free(fargs);
  1476. sol_obj_free(obj);
  1477. sol_obj_free(reprf);
  1478. return cur;
  1479. }
  1480. iter = dsl_new_seq_iter(obj->seq);
  1481. while(!dsl_seq_iter_is_invalid(iter)) {
  1482. item = AS_OBJ(dsl_seq_iter_at(iter));
  1483. if(test_seen(item)) {
  1484. snprintf(s, 64, "... (%p)", item);
  1485. next = sol_string_concat_cstr(state, cur, s);
  1486. } else {
  1487. str = sol_cast_repr(state, item);
  1488. next = sol_string_concat(state, cur, str);
  1489. sol_obj_free(str);
  1490. }
  1491. sol_obj_free(cur);
  1492. cur = next;
  1493. if(!dsl_seq_iter_at_end(iter)) {
  1494. next = sol_string_concat_cstr(state, cur, ", ");
  1495. sol_obj_free(cur);
  1496. cur = next;
  1497. }
  1498. dsl_seq_iter_next(iter);
  1499. }
  1500. next = sol_string_concat_cstr(state, cur, "}");
  1501. sol_obj_free(cur);
  1502. dsl_free_seq_iter(iter);
  1503. sol_obj_free(obj);
  1504. return next;
  1505. }
  1506. sol_object_t *sol_f_mcell_tostring(sol_state_t *state, sol_object_t *args) {
  1507. sol_object_t *mcell = sol_list_get_index(state, args, 0), *cur = sol_new_string(state, "["), *next, *str;
  1508. char s[64];
  1509. if(test_seen(mcell->key)) {
  1510. snprintf(s, 64, "... (%p)", mcell->key);
  1511. next = sol_string_concat_cstr(state, cur, s);
  1512. } else {
  1513. str = sol_cast_repr(state, mcell->key);
  1514. next = sol_string_concat(state, cur, str);
  1515. sol_obj_free(str);
  1516. }
  1517. sol_obj_free(cur);
  1518. cur = next;
  1519. next = sol_string_concat_cstr(state, cur, "] = ");
  1520. sol_obj_free(cur);
  1521. cur = next;
  1522. if(test_seen(mcell->val)) {
  1523. snprintf(s, 64, "... (%p)", mcell->val);
  1524. next = sol_string_concat_cstr(state, cur, s);
  1525. } else {
  1526. str = sol_cast_repr(state, mcell->val);
  1527. next = sol_string_concat(state, cur, str);
  1528. sol_obj_free(str);
  1529. }
  1530. sol_obj_free(cur);
  1531. sol_obj_free(mcell);
  1532. return next;
  1533. }
  1534. sol_object_t *sol_f_func_index(sol_state_t *state, sol_object_t *args) {
  1535. sol_object_t *func = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *res;
  1536. identlist_node *curi;
  1537. int i = 0;
  1538. if(!sol_is_name(key)) {
  1539. res = sol_map_get(state, func->udata, key);
  1540. } else {
  1541. if(sol_name_eq(state, key, "name")) {
  1542. if(func->fname) {
  1543. res = sol_new_string(state, func->fname);
  1544. } else {
  1545. res = sol_incref(state->None);
  1546. }
  1547. } else if(sol_name_eq(state, key, "closure")) {
  1548. res = sol_incref(func->closure);
  1549. } else if(sol_name_eq(state, key, "udata")) {
  1550. res = sol_incref(func->udata);
  1551. } else if(sol_name_eq(state, key, "stmt")) {
  1552. res = sol_new_stmtnode(state, st_copy((stmt_node *) func->func));
  1553. } else if(sol_name_eq(state, key, "args")) {
  1554. res = sol_new_list(state);
  1555. curi = func->args;
  1556. while(curi) {
  1557. sol_list_insert(state, res, i++, sol_new_string(state, curi->ident));
  1558. curi = curi->next;
  1559. }
  1560. } else if(sol_name_eq(state, key, "rest")) {
  1561. if(func->rest) {
  1562. res = sol_new_string(state, func->rest);
  1563. } else {
  1564. res = sol_incref(state->None);
  1565. }
  1566. } else if(sol_name_eq(state, key, "annos")) {
  1567. res = sol_incref(func->annos);
  1568. } else {
  1569. res = sol_map_get(state, func->udata, key);
  1570. }
  1571. }
  1572. sol_obj_free(func);
  1573. sol_obj_free(key);
  1574. return res;
  1575. }
  1576. sol_object_t *sol_f_func_setindex(sol_state_t *state, sol_object_t *args) {
  1577. 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, *str;
  1578. size_t i, len;
  1579. identlist_node *cur, *prev;
  1580. if(sol_name_eq(state, key, "name") && sol_is_name(val)) {
  1581. free(func->fname);
  1582. if(sol_is_string(val)) {
  1583. func->fname = strdup(val->str);
  1584. } else {
  1585. func->fname = sol_buffer_strdup(val);
  1586. }
  1587. } else if(sol_name_eq(state, key, "closure") && sol_is_map(val)) {
  1588. temp = func->closure;
  1589. func->closure = sol_incref(val);
  1590. sol_obj_free(temp);
  1591. } else if(sol_name_eq(state, key, "udata") && sol_is_map(val)) {
  1592. temp = func->udata;
  1593. func->udata = sol_incref(val);
  1594. sol_obj_free(temp);
  1595. } else if(sol_name_eq(state, key, "stmt") && sol_is_aststmt(val)) {
  1596. st_free(func->func);
  1597. func->func = st_copy(val->node);
  1598. } else if(sol_name_eq(state, key, "args") && sol_is_list(val)) {
  1599. idl_free(func->args);
  1600. func->args = NEW(identlist_node);
  1601. cur = func->args;
  1602. prev = cur;
  1603. len = sol_list_len(state, val);
  1604. for(i = 0; i < len; i++ ) {
  1605. temp = sol_list_get_index(state, val, i);
  1606. str = sol_cast_string(state, temp);
  1607. cur->ident = strdup(str->str);
  1608. sol_obj_free(temp);
  1609. sol_obj_free(str);
  1610. prev = cur;
  1611. cur->next = NEW(identlist_node);
  1612. cur = cur->next;
  1613. }
  1614. prev->next = NULL;
  1615. if(cur == func->args) func->args = NULL;
  1616. free(cur);
  1617. } else if(sol_name_eq(state, key, "rest") && sol_is_name(val)) {
  1618. free(func->rest);
  1619. if(sol_is_string(val)) {
  1620. func->rest = strdup(val->str);
  1621. } else {
  1622. func->rest = sol_buffer_strdup(val);
  1623. }
  1624. } else if(sol_name_eq(state, key, "annos") && sol_is_map(val)) {
  1625. sol_obj_free(func->annos);
  1626. func->annos = sol_incref(val);
  1627. } else {
  1628. sol_map_set(state, func->udata, key, val);
  1629. }
  1630. sol_obj_free(func);
  1631. sol_obj_free(key);
  1632. sol_obj_free(val);
  1633. return sol_incref(state->None);
  1634. }
  1635. sol_object_t *sol_f_func_tostring(sol_state_t *state, sol_object_t *args) {
  1636. sol_object_t *func = sol_list_get_index(state, args, 0), *ret;
  1637. char *s = malloc(256 * sizeof(char));
  1638. if(func->fname) {
  1639. snprintf(s, 256, "<Function %s>", func->fname);
  1640. } else {
  1641. snprintf(s, 256, "<Function>");
  1642. }
  1643. ret = sol_new_string(state, s);
  1644. free(s);
  1645. sol_obj_free(func);
  1646. return ret;
  1647. }
  1648. sol_object_t *sol_f_cfunc_call(sol_state_t *state, sol_object_t *args) {
  1649. sol_object_t *func = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  1650. sol_object_t *res = NULL, *tmp = NULL;
  1651. sol_list_insert(state, state->fnstack, 0, func);
  1652. res = func->cfunc(state, fargs);
  1653. tmp = sol_list_remove(state, state->fnstack, 0);
  1654. if(tmp != func) {
  1655. printf("ERROR: Function stack imbalance\n");
  1656. }
  1657. sol_obj_free(func);
  1658. sol_obj_free(fargs);
  1659. return res;
  1660. }
  1661. sol_object_t *sol_f_cfunc_tostring(sol_state_t *state, sol_object_t *args) {
  1662. sol_object_t *cfunc = sol_list_get_index(state, args, 0), *ret;
  1663. char *s = malloc(256 * sizeof(char));
  1664. if(cfunc->cfname) {
  1665. snprintf(s, 256, "<CFunction %s>", cfunc->cfname);
  1666. } else {
  1667. snprintf(s, 256, "<CFunction>");
  1668. }
  1669. ret = sol_new_string(state, s);
  1670. free(s);
  1671. sol_obj_free(cfunc);
  1672. return ret;
  1673. }
  1674. sol_object_t *sol_f_astnode_call(sol_state_t *state, sol_object_t *args) {
  1675. sol_object_t *obj = sol_list_get_index(state, args, 0), *env = NULL, *res;
  1676. stmt_node *stmt = (stmt_node *) obj->node;
  1677. expr_node *expr = (expr_node *) obj->node;
  1678. sol_obj_free(obj);
  1679. if(sol_list_len(state, args) > 1) {
  1680. env = sol_list_get_index(state, args, 1);
  1681. sol_state_push_scope(state, env);
  1682. }
  1683. if(sol_is_aststmt(obj)) {
  1684. sol_exec(state, stmt);
  1685. res = sol_incref(state->None);
  1686. } else {
  1687. res = sol_eval(state, expr);
  1688. }
  1689. if(env) {
  1690. sol_state_pop_scope(state);
  1691. sol_obj_free(env);
  1692. }
  1693. return res;
  1694. }
  1695. sol_object_t *sol_f_astnode_index(sol_state_t *state, sol_object_t *args) {
  1696. 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;
  1697. stmt_node *stmt = (stmt_node *) obj->node;
  1698. stmtlist_node *curs;
  1699. expr_node *expr = (expr_node *) obj->node;
  1700. exprlist_node *cure;
  1701. assoclist_node *cura;
  1702. identlist_node *curi;
  1703. int i = 0;
  1704. if(!stmt) {
  1705. sol_obj_free(obj);
  1706. sol_obj_free(key);
  1707. sol_obj_free(str);
  1708. return sol_set_error_string(state, "Access NULL AST node");
  1709. }
  1710. if(sol_is_aststmt(obj)) {
  1711. if(sol_string_eq(state, str, "kind")) {
  1712. res = sol_new_int(state, -1);
  1713. } else if(sol_string_eq(state, str, "type")) {
  1714. res = sol_new_int(state, stmt->type);
  1715. } else if(sol_string_eq(state, str, "loc")) {
  1716. res = sol_new_map(state);
  1717. sol_map_set_name(state, res, "line", sol_new_int(state, stmt->loc.line));
  1718. sol_map_set_name(state, res, "col", sol_new_int(state, stmt->loc.col));
  1719. } else {
  1720. switch(stmt->type) {
  1721. case ST_EXPR:
  1722. if(sol_string_eq(state, str, "expr")) {
  1723. res = sol_new_exprnode(state, ex_copy(stmt->expr));
  1724. }
  1725. break;
  1726. case ST_LIST:
  1727. if(sol_string_eq(state, str, "stmtlist")) {
  1728. res = sol_new_list(state);
  1729. curs = stmt->stmtlist;
  1730. while(curs) {
  1731. sol_list_insert(state, res, i++, sol_new_stmtnode(state, st_copy(curs->stmt)));
  1732. curs = curs->next;
  1733. }
  1734. }
  1735. break;
  1736. case ST_RET:
  1737. if(sol_string_eq(state, str, "ret")) {
  1738. res = sol_new_exprnode(state, ex_copy(stmt->ret->ret));
  1739. }
  1740. break;
  1741. case ST_CONT:
  1742. if(sol_string_eq(state, str, "val")) {
  1743. res = sol_new_exprnode(state, ex_copy(stmt->cont->val));
  1744. }
  1745. break;
  1746. case ST_BREAK:
  1747. if(sol_string_eq(state, str, "val")) {
  1748. res = sol_new_exprnode(state, ex_copy(stmt->brk->val));
  1749. }
  1750. break;
  1751. }
  1752. }
  1753. } else {
  1754. if(sol_string_eq(state, str, "kind")) {
  1755. res = sol_new_int(state, -2);
  1756. } else if(sol_string_eq(state, str, "type")) {
  1757. res = sol_new_int(state, expr->type);
  1758. } else if(sol_string_eq(state, str, "loc")) {
  1759. res = sol_new_map(state);
  1760. sol_map_set_name(state, res, "line", sol_new_int(state, expr->loc.line));
  1761. sol_map_set_name(state, res, "col", sol_new_int(state, expr->loc.col));
  1762. } else {
  1763. switch(expr->type) {
  1764. case EX_LIT:
  1765. if(sol_string_eq(state, str, "littype")) {
  1766. res = sol_new_int(state, expr->lit->type);
  1767. } else if(sol_string_eq(state, str, "ival")) {
  1768. res = sol_new_int(state, expr->lit->ival);
  1769. } else if(sol_string_eq(state, str, "fval")) {
  1770. res = sol_new_float(state, expr->lit->fval);
  1771. } else if(sol_string_eq(state, str, "str")) {
  1772. res = sol_new_string(state, expr->lit->str);
  1773. }
  1774. break;
  1775. case EX_LISTGEN:
  1776. if(sol_string_eq(state, str, "list")) {
  1777. res = sol_new_list(state);
  1778. cure = expr->listgen->list;
  1779. while(cure) {
  1780. sol_list_insert(state, res, i++, sol_new_exprnode(state, ex_copy(cure->expr)));
  1781. cure = cure->next;
  1782. }
  1783. }
  1784. break;
  1785. case EX_MAPGEN:
  1786. if(sol_string_eq(state, str, "map")) {
  1787. res = sol_new_list(state);
  1788. cura = expr->mapgen->map;
  1789. while(cura) {
  1790. pair = sol_new_list(state);
  1791. sol_list_insert(state, pair, 0, sol_new_exprnode(state, ex_copy(cura->item->key)));
  1792. sol_list_insert(state, pair, 1, sol_new_exprnode(state, ex_copy(cura->item->value)));
  1793. sol_list_insert(state, res, i++, pair);
  1794. sol_obj_free(pair);
  1795. }
  1796. }
  1797. break;
  1798. case EX_BINOP:
  1799. if(sol_string_eq(state, str, "binoptype")) {
  1800. res = sol_new_int(state, expr->binop->type);
  1801. } else if(sol_string_eq(state, str, "left")) {
  1802. res = sol_new_exprnode(state, ex_copy(expr->binop->left));
  1803. } else if(sol_string_eq(state, str, "right")) {
  1804. res = sol_new_exprnode(state, ex_copy(expr->binop->right));
  1805. }
  1806. break;
  1807. case EX_UNOP:
  1808. if(sol_string_eq(state, str, "unoptype")) {
  1809. res = sol_new_int(state, expr->unop->type);
  1810. } else if(sol_string_eq(state, str, "expr")) {
  1811. res = sol_new_exprnode(state, ex_copy(expr->unop->expr));
  1812. }
  1813. break;
  1814. case EX_INDEX:
  1815. if(sol_string_eq(state, str, "expr")) {
  1816. res = sol_new_exprnode(state, ex_copy(expr->index->expr));
  1817. } else if(sol_string_eq(state, str, "index")) {
  1818. res = sol_new_exprnode(state, ex_copy(expr->index->index));
  1819. }
  1820. break;
  1821. case EX_SETINDEX:
  1822. if(sol_string_eq(state, str, "expr")) {
  1823. res = sol_new_exprnode(state, ex_copy(expr->setindex->expr));
  1824. } else if(sol_string_eq(state, str, "index")) {
  1825. res = sol_new_exprnode(state, ex_copy(expr->setindex->index));
  1826. } else if(sol_string_eq(state, str, "value")) {
  1827. res = sol_new_exprnode(state, ex_copy(expr->setindex->value));
  1828. }
  1829. break;
  1830. case EX_ASSIGN:
  1831. if(sol_string_eq(state, str, "ident")) {
  1832. res = sol_new_string(state, expr->assign->ident);
  1833. } else if(sol_string_eq(state, str, "value")) {
  1834. res = sol_new_exprnode(state, ex_copy(expr->assign->value));
  1835. }
  1836. break;
  1837. case EX_REF:
  1838. if(sol_string_eq(state, str, "ident")) {
  1839. res = sol_new_string(state, expr->ref->ident);
  1840. }
  1841. break;
  1842. case EX_CALL:
  1843. if(sol_string_eq(state, str, "expr")) {
  1844. res = sol_new_exprnode(state, ex_copy(expr->call->expr));
  1845. } else if(sol_string_eq(state, str, "args")) {
  1846. res = sol_new_list(state);
  1847. cure = expr->call->args;
  1848. while(cure) {
  1849. sol_list_insert(state, res, i++, sol_new_exprnode(state, ex_copy(cure->expr)));
  1850. cure = cure->next;
  1851. }
  1852. }
  1853. break;
  1854. case EX_FUNCDECL:
  1855. if(sol_string_eq(state, str, "name")) {
  1856. res = sol_new_string(state, (expr->funcdecl->name ? expr->funcdecl->name : ""));
  1857. } else if(sol_string_eq(state, str, "args")) {
  1858. res = sol_new_list(state);
  1859. curi = expr->funcdecl->params ? expr->funcdecl->params->args : NULL;
  1860. while(curi) {
  1861. sol_list_insert(state, res, i++, sol_new_string(state, curi->ident));
  1862. curi = curi->next;
  1863. }
  1864. } else if(sol_string_eq(state, str, "body")) {
  1865. res = sol_new_stmtnode(state, st_copy(expr->funcdecl->body));
  1866. }
  1867. break;
  1868. case EX_IFELSE:
  1869. if(sol_string_eq(state, str, "cond")) {
  1870. res = sol_new_exprnode(state, ex_copy(expr->ifelse->cond));
  1871. } else if(sol_string_eq(state, str, "iftrue")) {
  1872. res = sol_new_stmtnode(state, st_copy(expr->ifelse->iftrue));
  1873. } else if(sol_string_eq(state, str, "iffalse")) {
  1874. res = sol_new_stmtnode(state, st_copy(expr->ifelse->iffalse));
  1875. }
  1876. break;
  1877. case EX_LOOP:
  1878. if(sol_string_eq(state, str, "cond")) {
  1879. res = sol_new_exprnode(state, ex_copy(expr->loop->cond));
  1880. } else if(sol_string_eq(state, str, "loop")) {
  1881. res = sol_new_stmtnode(state, st_copy(expr->loop->loop));
  1882. }
  1883. break;
  1884. case EX_ITER:
  1885. if(sol_string_eq(state, str, "var")) {
  1886. res = sol_new_string(state, expr->iter->var);
  1887. } else if(sol_string_eq(state, str, "iter")) {
  1888. res = sol_new_exprnode(state, ex_copy(expr->iter->iter));
  1889. } else if(sol_string_eq(state, str, "loop")) {
  1890. res = sol_new_stmtnode(state, st_copy(expr->iter->loop));
  1891. }
  1892. break;
  1893. }
  1894. }
  1895. }
  1896. sol_obj_free(obj);
  1897. sol_obj_free(key);
  1898. sol_obj_free(str);
  1899. if(!res) {
  1900. res = sol_incref(state->None);
  1901. }
  1902. return res;
  1903. }
  1904. sol_object_t *sol_f_astnode_setindex(sol_state_t *state, sol_object_t *args) {
  1905. 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;
  1906. sol_object_t *ival, *fval, *sval;
  1907. stmt_node *stmt = (stmt_node *) obj->node;
  1908. stmtlist_node *curs, *prevs;
  1909. expr_node *expr = (expr_node *) obj->node;
  1910. exprlist_node *cure, *preve = NULL;
  1911. assoclist_node *cura, *preva = NULL;
  1912. identlist_node *curi, *previ = NULL;
  1913. int i = 0, len;
  1914. if(!stmt) {
  1915. sol_obj_free(obj);
  1916. sol_obj_free(key);
  1917. sol_obj_free(str);
  1918. sol_obj_free(val);
  1919. return sol_set_error_string(state, "Access NULL AST node");
  1920. }
  1921. if(sol_is_aststmt(obj)) {
  1922. if(sol_string_eq(state, str, "type")) {
  1923. ival = sol_cast_int(state, val);
  1924. stmt->type = ival->ival;
  1925. sol_obj_free(ival);
  1926. } else if(sol_string_eq(state, str, "loc") && sol_is_map(val)) {
  1927. pair = sol_map_get_name(state, val, "line");
  1928. ival = sol_cast_int(state, pair);
  1929. stmt->loc.line = ival->ival;
  1930. sol_obj_free(ival);
  1931. sol_obj_free(pair);
  1932. pair = sol_map_get_name(state, val, "col");
  1933. ival = sol_cast_int(state, pair);
  1934. stmt->loc.col = ival->ival;
  1935. sol_obj_free(ival);
  1936. sol_obj_free(pair);
  1937. } else {
  1938. switch(stmt->type) {
  1939. case ST_EXPR:
  1940. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1941. ex_free(stmt->expr);
  1942. stmt->expr = ex_copy(val->node);
  1943. }
  1944. break;
  1945. case ST_LIST:
  1946. if(sol_string_eq(state, str, "stmtlist") && sol_is_list(val)) {
  1947. stl_free(stmt->stmtlist);
  1948. len = sol_list_len(state, val);
  1949. if(len > 0) {
  1950. curs = malloc(sizeof(stmtlist_node));
  1951. stmt->stmtlist = curs;
  1952. for(i = 0; i < len; i++) {
  1953. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1954. curs->stmt = st_copy(sol_list_get_index(state, val, i)->node);
  1955. prevs = curs;
  1956. curs = malloc(sizeof(stmtlist_node));
  1957. prevs->next = curs;
  1958. }
  1959. }
  1960. if(stmt->stmtlist == curs) {
  1961. stmt->stmtlist = NULL;
  1962. }
  1963. free(curs);
  1964. if(prevs) {
  1965. prevs->next = NULL;
  1966. }
  1967. } else {
  1968. stmt->stmtlist = NULL;
  1969. }
  1970. }
  1971. break;
  1972. case ST_RET:
  1973. if(sol_string_eq(state, str, "ret") && sol_is_astexpr(val)) {
  1974. ex_free(stmt->ret->ret);
  1975. stmt->ret->ret = ex_copy(val->node);
  1976. }
  1977. break;
  1978. case ST_CONT:
  1979. if(sol_string_eq(state, str, "val") && sol_is_astexpr(val)) {
  1980. ex_free(stmt->cont->val);
  1981. stmt->cont->val = ex_copy(val->node);
  1982. }
  1983. break;
  1984. case ST_BREAK:
  1985. if(sol_string_eq(state, str, "val") && sol_is_astexpr(val)) {
  1986. ex_free(stmt->brk->val);
  1987. stmt->brk->val = ex_copy(val->node);
  1988. }
  1989. break;
  1990. }
  1991. }
  1992. } else {
  1993. if(sol_string_eq(state, str, "type")) {
  1994. ival = sol_cast_int(state, val);
  1995. expr->type = ival->ival;
  1996. sol_obj_free(ival);
  1997. } else if(sol_string_eq(state, str, "loc") && sol_is_map(val)) {
  1998. pair = sol_map_get_name(state, val, "line");
  1999. ival = sol_cast_int(state, pair);
  2000. expr->loc.line = ival->ival;
  2001. sol_obj_free(ival);
  2002. sol_obj_free(pair);
  2003. pair = sol_map_get_name(state, val, "col");
  2004. ival = sol_cast_int(state, pair);
  2005. expr->loc.col = ival->ival;
  2006. sol_obj_free(ival);
  2007. sol_obj_free(pair);
  2008. } else {
  2009. switch(expr->type) {
  2010. case EX_LIT:
  2011. if(sol_string_eq(state, str, "littype")) {
  2012. ival = sol_cast_int(state, val);
  2013. expr->lit->type = ival->ival;
  2014. sol_obj_free(ival);
  2015. } else if(sol_string_eq(state, str, "ival")) {
  2016. ival = sol_cast_int(state, val);
  2017. expr->lit->ival = ival->ival;
  2018. sol_obj_free(ival);
  2019. } else if(sol_string_eq(state, str, "fval")) {
  2020. fval = sol_cast_float(state, val);
  2021. expr->lit->fval = fval->fval;
  2022. sol_obj_free(fval);
  2023. } else if(sol_string_eq(state, str, "str")) {
  2024. sval = sol_cast_string(state, val);
  2025. expr->lit->str = strdup(sval->str);
  2026. sol_obj_free(sval);
  2027. }
  2028. break;
  2029. case EX_LISTGEN:
  2030. if(sol_string_eq(state, str, "list") && sol_is_list(val)) {
  2031. exl_free(expr->listgen->list);
  2032. len = sol_list_len(state, val);
  2033. if(len > 0) {
  2034. cure = malloc(sizeof(exprlist_node));
  2035. expr->listgen->list = cure;
  2036. for(i = 0; i < len; i++) {
  2037. if(sol_is_astexpr(sol_list_get_index(state, val, i))) {
  2038. cure->expr = sol_list_get_index(state, val, i)->node;
  2039. preve = cure;
  2040. cure = malloc(sizeof(exprlist_node));
  2041. preve->next = cure;
  2042. }
  2043. }
  2044. if(expr->listgen->list == cure) {
  2045. expr->listgen->list = NULL;
  2046. }
  2047. free(cure);
  2048. if(preve) {
  2049. preve->next = NULL;
  2050. }
  2051. } else {
  2052. expr->listgen->list = NULL;
  2053. }
  2054. }
  2055. break;
  2056. case EX_MAPGEN:
  2057. if(sol_string_eq(state, str, "map") && sol_is_list(val)) {
  2058. asl_free(expr->mapgen->map);
  2059. len = sol_list_len(state, val);
  2060. if(len > 0) {
  2061. cura = malloc(sizeof(assoclist_node));
  2062. expr->mapgen->map = cura;
  2063. for(i = 0; i < len; i++) {
  2064. if(sol_is_list(sol_list_get_index(state, val, i))) {
  2065. pair = sol_list_get_index(state, val, i);
  2066. 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))) {
  2067. cura->item = malloc(sizeof(associtem_node));
  2068. cura->item->key = sol_list_get_index(state, pair, 0)->node;
  2069. cura->item->value = sol_list_get_index(state, pair, 1)->node;
  2070. preva = cura;
  2071. cura = malloc(sizeof(assoclist_node));
  2072. preva->next = cura;
  2073. }
  2074. }
  2075. }
  2076. if(expr->mapgen->map == cura) {
  2077. expr->mapgen->map = NULL;
  2078. }
  2079. free(cura);
  2080. if(preva) {
  2081. preva->next = NULL;
  2082. }
  2083. } else {
  2084. expr->mapgen->map = NULL;
  2085. }
  2086. }
  2087. break;
  2088. case EX_BINOP:
  2089. if(sol_string_eq(state, str, "binoptype")) {
  2090. ival = sol_cast_int(state, val);
  2091. expr->binop->type = ival->ival;
  2092. sol_obj_free(ival);
  2093. } else if(sol_string_eq(state, str, "left") && sol_is_astexpr(val)) {
  2094. ex_free(expr->binop->left);
  2095. expr->binop->left = ex_copy(val->node);
  2096. } else if(sol_string_eq(state, str, "right") && sol_is_astexpr(val)) {
  2097. ex_free(expr->binop->right);
  2098. expr->binop->right = ex_copy(val->node);
  2099. }
  2100. break;
  2101. case EX_UNOP:
  2102. if(sol_string_eq(state, str, "unoptype")) {
  2103. ival = sol_cast_int(state, val);
  2104. expr->unop->type = ival->ival;
  2105. sol_obj_free(ival);
  2106. } else if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  2107. ex_free(expr->unop->expr);
  2108. expr->unop->expr = ex_copy(val->node);
  2109. }
  2110. break;
  2111. case EX_INDEX:
  2112. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  2113. ex_free(expr->index->expr);
  2114. expr->index->expr = ex_copy(val->node);
  2115. } else if(sol_string_eq(state, str, "index") && sol_is_astexpr(val)) {
  2116. ex_free(expr->index->index);
  2117. expr->index->index = ex_copy(val->node);
  2118. }
  2119. break;
  2120. case EX_SETINDEX:
  2121. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  2122. ex_free(expr->setindex->expr);
  2123. expr->setindex->expr = ex_copy(val->node);
  2124. } else if(sol_string_eq(state, str, "index") && sol_is_astexpr(val)) {
  2125. ex_free(expr->setindex->index);
  2126. expr->setindex->index = ex_copy(val->node);
  2127. } else if(sol_string_eq(state, str, "value") && sol_is_astexpr(val)) {
  2128. ex_free(expr->setindex->value);
  2129. expr->setindex->value = ex_copy(val->node);
  2130. }
  2131. break;
  2132. case EX_ASSIGN:
  2133. if(sol_string_eq(state, str, "ident")) {
  2134. sval = sol_cast_string(state, val);
  2135. expr->assign->ident = strdup(sval->str);
  2136. sol_obj_free(sval);
  2137. } else if(sol_string_eq(state, str, "value") && sol_is_astexpr(val)) {
  2138. ex_free(expr->assign->value);
  2139. expr->assign->value = ex_copy(val->node);
  2140. }
  2141. break;
  2142. case EX_REF:
  2143. if(sol_string_eq(state, str, "ident")) {
  2144. sval = sol_cast_string(state, val);
  2145. expr->ref->ident = strdup(sval->str);
  2146. sol_obj_free(sval);
  2147. }
  2148. break;
  2149. case EX_CALL:
  2150. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  2151. ex_free(expr->call->expr);
  2152. expr->call->expr = ex_copy(val->node);
  2153. } else if(sol_string_eq(state, str, "args") && sol_is_list(val)) {
  2154. exl_free(expr->call->args);
  2155. len = sol_list_len(state, val);
  2156. if(len > 0) {
  2157. cure = malloc(sizeof(exprlist_node));
  2158. expr->call->args = cure;
  2159. for(i = 0; i < len; i++) {
  2160. if(sol_is_astexpr(sol_list_get_index(state, val, i))) {
  2161. cure->expr = sol_list_get_index(state, val, i)->node;
  2162. preve = cure;
  2163. cure = malloc(sizeof(exprlist_node));
  2164. preve->next = cure;
  2165. }
  2166. }
  2167. if(expr->call->args == cure) {
  2168. expr->call->args = NULL;
  2169. }
  2170. free(cure);
  2171. if(preve) {
  2172. preve->next = NULL;
  2173. }
  2174. } else {
  2175. expr->call->args = NULL;
  2176. }
  2177. }
  2178. break;
  2179. case EX_FUNCDECL:
  2180. if(sol_string_eq(state, str, "name")) {
  2181. sval = sol_cast_string(state, val);
  2182. expr->funcdecl->name = strdup(sval->str);
  2183. sol_obj_free(sval);
  2184. } else if(sol_string_eq(state, str, "args") && sol_is_list(val)) {
  2185. if(!expr->funcdecl->params) {
  2186. expr->funcdecl->params = malloc(sizeof(paramlist_node));
  2187. expr->funcdecl->params->args = NULL;
  2188. expr->funcdecl->params->clkeys = NULL;
  2189. expr->funcdecl->params->clvalues = NULL;
  2190. expr->funcdecl->params->rest = NULL;
  2191. }
  2192. idl_free(expr->funcdecl->params->args);
  2193. len = sol_list_len(state, val);
  2194. if(len > 0) {
  2195. curi = malloc(sizeof(identlist_node));
  2196. expr->funcdecl->params->args = curi;
  2197. for(i = 0; i < len; i++) {
  2198. sval = sol_cast_string(state, sol_list_get_index(state, val, i));
  2199. curi->ident = strdup(sval->str);
  2200. sol_obj_free(sval);
  2201. previ = curi;
  2202. curi = malloc(sizeof(identlist_node));
  2203. previ->next = curi;
  2204. }
  2205. if(expr->funcdecl->params->args == curi) {
  2206. expr->funcdecl->params->args = NULL;
  2207. }
  2208. free(curi);
  2209. if(previ) {
  2210. previ->next = NULL;
  2211. }
  2212. } else {
  2213. expr->funcdecl->params->args = NULL;
  2214. }
  2215. } else if(sol_string_eq(state, str, "body") && sol_is_aststmt(val)) {
  2216. st_free(expr->funcdecl->body);
  2217. expr->funcdecl->body = st_copy(val->node);
  2218. }
  2219. break;
  2220. case EX_IFELSE:
  2221. if(sol_string_eq(state, str, "cond") && sol_is_astexpr(val)) {
  2222. ex_free(expr->ifelse->cond);
  2223. expr->ifelse->cond = ex_copy(val->node);
  2224. } else if(sol_string_eq(state, str, "iftrue") && sol_is_aststmt(val)) {
  2225. st_free(expr->ifelse->iftrue);
  2226. expr->ifelse->iftrue = st_copy(val->node);
  2227. } else if(sol_string_eq(state, str, "iffalse") && sol_is_aststmt(val)) {
  2228. st_free(expr->ifelse->iffalse);
  2229. expr->ifelse->iffalse = st_copy(val->node);
  2230. }
  2231. break;
  2232. case EX_LOOP:
  2233. if(sol_string_eq(state, str, "cond") && sol_is_astexpr(val)) {
  2234. ex_free(expr->loop->cond);
  2235. expr->loop->cond = ex_copy(val->node);
  2236. } else if(sol_string_eq(state, str, "loop") && sol_is_aststmt(val)) {
  2237. st_free(expr->loop->loop);
  2238. expr->loop->loop = st_copy(val->node);
  2239. }
  2240. break;
  2241. case EX_ITER:
  2242. if(sol_string_eq(state, str, "var")) {
  2243. sval = sol_cast_string(state, val);
  2244. expr->iter->var = strdup(sval->str);
  2245. sol_obj_free(sval);
  2246. } else if(sol_string_eq(state, str, "iter") && sol_is_astexpr(val)) {
  2247. ex_free(expr->iter->iter);
  2248. expr->iter->iter = ex_copy(val->node);
  2249. } else if(sol_string_eq(state, str, "loop") && sol_is_aststmt(val)) {
  2250. st_free(expr->iter->loop);
  2251. expr->iter->loop = st_copy(val->node);
  2252. }
  2253. break;
  2254. }
  2255. }
  2256. }
  2257. sol_obj_free(obj);
  2258. sol_obj_free(key);
  2259. sol_obj_free(str);
  2260. return val;
  2261. }
  2262. static char *sol_StmtNames[] = {"EXPR", "LIST", "RET", "CONT", "BREAK"};
  2263. static char *sol_ExprNames[] = {"LIT", "LISTGEN", "MAPGEN", "BINOP", "UNOP", "INDEX", "SETINDEX", "ASSIGN", "REF", "CALL", "FUNCDECL", "IFELSE", "LOOP", "ITER"};
  2264. sol_object_t *sol_f_astnode_tostring(sol_state_t *state, sol_object_t *args) {
  2265. sol_object_t *obj = sol_list_get_index(state, args, 0), *res;
  2266. char s[64];
  2267. if(sol_is_aststmt(obj)) {
  2268. if(!obj->node) {
  2269. snprintf(s, 64, "<NULL Stmt>");
  2270. } else {
  2271. stmt_t type = ((stmt_node *) obj->node)->type;
  2272. if(type < sizeof(sol_StmtNames)/sizeof(char *)) {
  2273. snprintf(s, 64, "<Stmt[%s]>", sol_StmtNames[type]);
  2274. } else {
  2275. snprintf(s, 64, "<Invalid Stmt>");
  2276. }
  2277. }
  2278. } else {
  2279. if(!obj->node) {
  2280. snprintf(s, 64, "<NULL Expr>");
  2281. } else {
  2282. expr_t type = ((stmt_node *) obj->node)->type;
  2283. if(type < sizeof(sol_ExprNames)/sizeof(char *)) {
  2284. snprintf(s, 64, "<Expr[%s]>", sol_ExprNames[type]);
  2285. } else {
  2286. snprintf(s, 64, "<Invalid Expr>");
  2287. }
  2288. }
  2289. }
  2290. sol_obj_free(obj);
  2291. return sol_new_string(state, s);
  2292. }
  2293. sol_object_t *sol_f_buffer_index(sol_state_t *state, sol_object_t *args) {
  2294. sol_object_t *a = sol_list_get_index(state, args, 0);
  2295. sol_object_t *key = sol_list_get_index(state, args, 1), *funcs = sol_get_methods_name(state, "buffer");
  2296. sol_object_t *res;
  2297. if(sol_is_name(key)) {
  2298. res = sol_map_get(state, funcs, key);
  2299. } else if(sol_is_int(key)) {
  2300. res = sol_new_buffer(state, ((char *) a->buffer) + key->ival, (a->sz < 0) ? a->sz : (a->sz - key->ival), OWN_NONE, NULL, NULL);
  2301. } else {
  2302. res = sol_f_not_impl(state, args);
  2303. }
  2304. sol_obj_free(a);
  2305. sol_obj_free(key);
  2306. sol_obj_free(funcs);
  2307. return res;
  2308. }
  2309. sol_object_t *sol_f_buffer_add(sol_state_t *state, sol_object_t *args) {
  2310. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bb = sol_cast_buffer(state, b);
  2311. sol_object_t *res = sol_buffer_concat(state, a, bb);
  2312. sol_obj_free(a);
  2313. sol_obj_free(b);
  2314. if(sol_has_error(state)) {
  2315. sol_obj_free(res);
  2316. return sol_incref(state->None);
  2317. }
  2318. return res;
  2319. }
  2320. sol_object_t *sol_f_buffer_mul(sol_state_t *state, sol_object_t *args) {
  2321. 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);
  2322. long ival, i;
  2323. size_t sz;
  2324. char *buf;
  2325. sol_obj_free(b);
  2326. if(sol_has_error(state)) {
  2327. sol_obj_free(a);
  2328. sol_obj_free(bint);
  2329. return sol_incref(state->None);
  2330. }
  2331. ival = bint->ival;
  2332. sol_obj_free(bint);
  2333. if(a->sz < 0) {
  2334. sol_obj_free(a);
  2335. return sol_set_error_string(state, "Multiply unsized buffer");
  2336. }
  2337. if(ival < 0) {
  2338. ival = 0;
  2339. }
  2340. sz = a->sz * ival;
  2341. buf = malloc(sz * sizeof(char));
  2342. for(i = 0; i < ival; i++) {
  2343. memcpy(buf + (i * a->sz), a->buffer, a->sz);
  2344. }
  2345. sol_obj_free(a);
  2346. return sol_new_buffer(state, buf, sz, OWN_FREE, NULL, NULL);
  2347. }
  2348. sol_object_t *sol_f_buffer_cmp(sol_state_t *state, sol_object_t *args) {
  2349. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  2350. sol_object_t *bb, *res;
  2351. if(sol_is_string(b)) {
  2352. bb = sol_cast_buffer(state, b);
  2353. sol_obj_free(b);
  2354. b = bb;
  2355. }
  2356. if(sol_is_buffer(b)) {
  2357. ssize_t len = a->sz;
  2358. if(a->sz >= 0 && b->sz >= 0 && a->sz != b->sz) {
  2359. res = sol_new_int(state, 1);
  2360. } else {
  2361. if(len < 0) {
  2362. len = b->sz;
  2363. }
  2364. if(a->sz >= 0 && len > a->sz) len = a->sz;
  2365. if(b->sz >= 0 && len > b->sz) len = b->sz;
  2366. if(len < 0) {
  2367. res = sol_new_int(state, 1);
  2368. } else {
  2369. res = sol_new_int(state, memcmp(a->buffer, b->buffer, len));
  2370. }
  2371. }
  2372. } else {
  2373. res = sol_new_int(state, 1);
  2374. }
  2375. sol_obj_free(a);
  2376. sol_obj_free(b);
  2377. return res;
  2378. }
  2379. sol_object_t *sol_f_buffer_len(sol_state_t *state, sol_object_t *args) {
  2380. sol_object_t *a = sol_list_get_index(state, args, 0);
  2381. sol_object_t *res = sol_new_int(state, a->sz);
  2382. sol_obj_free(a);
  2383. return res;
  2384. }
  2385. sol_object_t *sol_f_buffer_iter(sol_state_t *state, sol_object_t *args) {
  2386. return sol_new_cfunc(state, sol_f_iter_buffer, "iter.buffer");
  2387. }
  2388. sol_object_t *sol_f_buffer_tostring(sol_state_t *state, sol_object_t *args) {
  2389. sol_object_t *buf = sol_list_get_index(state, args, 0), *res;
  2390. char *b;
  2391. /*
  2392. char s[64];
  2393. if(buf->sz == -1) {
  2394. snprintf(s, 64, "<Buffer @%p>", buf->buffer);
  2395. } else {
  2396. snprintf(s, 64, "<Buffer @%p size %ld>", buf->buffer, buf->sz);
  2397. }
  2398. */
  2399. if(buf->sz < 0) {
  2400. res = sol_new_string(state, "<UNSIZED_BUFFER>");
  2401. } else {
  2402. b = malloc(buf->sz + 1);
  2403. strncpy(b, buf->buffer, buf->sz);
  2404. b[buf->sz] = '\0';
  2405. res = sol_new_string(state, b);
  2406. free(b);
  2407. }
  2408. sol_obj_free(buf);
  2409. return res;
  2410. }
  2411. sol_object_t *sol_f_buffer_repr(sol_state_t *state, sol_object_t *args) {
  2412. sol_object_t *obj = sol_list_get_index(state, args, 0), *cur = sol_new_buffer(state, "'", 1, OWN_NONE, NULL, NULL), *next = sol_buffer_concat(state, cur, obj);
  2413. sol_object_t *termin = sol_new_buffer(state, "'", 2, OWN_NONE, NULL, NULL);
  2414. sol_obj_free(cur);
  2415. cur = next;
  2416. next = sol_buffer_concat(state, cur, termin);
  2417. sol_obj_free(cur);
  2418. return next;
  2419. }
  2420. sol_object_t *sol_f_buffer_toint(sol_state_t *state, sol_object_t *args) {
  2421. sol_object_t *a = sol_list_get_index(state, args, 0);
  2422. sol_object_t *res = sol_new_int(state, a->buffer ? atoi(a->buffer) : 0);
  2423. sol_obj_free(a);
  2424. return res;
  2425. }
  2426. sol_object_t *sol_f_buffer_tofloat(sol_state_t *state, sol_object_t *args) {
  2427. sol_object_t *a = sol_list_get_index(state, args, 0);
  2428. sol_object_t *res = sol_new_float(state, a->buffer ? atof(a->buffer) : 0.0);
  2429. sol_obj_free(a);
  2430. return res;
  2431. }
  2432. sol_object_t *sol_f_buffer_tobuffer(sol_state_t *state, sol_object_t *args) {
  2433. return sol_list_get_index(state, args, 0);
  2434. }
  2435. sol_object_t *sol_f_buffer_new(sol_state_t *state, sol_object_t *args) {
  2436. sol_object_t *sz = sol_list_get_index(state, args, 0), *isz = sol_cast_int(state, sz);
  2437. size_t bufsz = isz->ival;
  2438. void *buf = malloc(bufsz);
  2439. sol_obj_free(sz);
  2440. sol_obj_free(isz);
  2441. if(buf) {
  2442. return sol_new_buffer(state, buf, bufsz, OWN_FREE, NULL, NULL);
  2443. }
  2444. return sol_incref(state->None);
  2445. }
  2446. sol_object_t *sol_f_buffer_get(sol_state_t *state, sol_object_t *args) {
  2447. 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);
  2448. sol_object_t *itp = sol_cast_int(state, tp), *ioff, *res = NULL;
  2449. sol_buftype_t buftp = itp->ival;
  2450. char *data, cbuf[2] = {0, 0};
  2451. sol_obj_free(tp);
  2452. sol_obj_free(itp);
  2453. if(!sol_is_none(state, off)) {
  2454. ioff = sol_cast_int(state, off);
  2455. } else {
  2456. ioff = sol_new_int(state, 0);
  2457. }
  2458. sol_obj_free(off);
  2459. if(buf->sz != -1 && (ioff->ival >= buf->sz || ioff->ival < 0)) {
  2460. sol_obj_free(buf);
  2461. sol_obj_free(ioff);
  2462. return sol_incref(state->None);
  2463. }
  2464. data = ((char *) buf->buffer) + ioff->ival;
  2465. sol_obj_free(buf);
  2466. sol_obj_free(ioff);
  2467. switch(buftp) {
  2468. case BUF_UINT8:
  2469. res = sol_new_int(state, *((uint8_t *) data));
  2470. break;
  2471. case BUF_UINT16:
  2472. res = sol_new_int(state, *((uint16_t *) data));
  2473. break;
  2474. case BUF_UINT32:
  2475. res = sol_new_int(state, *((uint32_t *) data));
  2476. break;
  2477. case BUF_UINT64:
  2478. res = sol_new_int(state, *((uint64_t *) data));
  2479. break;
  2480. case BUF_INT8:
  2481. res = sol_new_int(state, *((int8_t *) data));
  2482. break;
  2483. case BUF_INT16:
  2484. res = sol_new_int(state, *((int16_t *) data));
  2485. break;
  2486. case BUF_INT32:
  2487. res = sol_new_int(state, *((int32_t *) data));
  2488. break;
  2489. case BUF_INT64:
  2490. res = sol_new_int(state, *((int64_t *) data));
  2491. break;
  2492. case BUF_CHAR:
  2493. cbuf[0] = *((char *) data);
  2494. res = sol_new_string(state, cbuf);
  2495. break;
  2496. case BUF_BYTE:
  2497. res = sol_new_int(state, *((unsigned char *) data));
  2498. break;
  2499. case BUF_INT:
  2500. res = sol_new_int(state, *((int *) data));
  2501. break;
  2502. case BUF_UINT:
  2503. res = sol_new_int(state, *((unsigned int *) data));
  2504. break;
  2505. case BUF_LONG:
  2506. res = sol_new_int(state, *((long *) data));
  2507. break;
  2508. case BUF_ULONG:
  2509. res = sol_new_int(state, *((unsigned long *) data));
  2510. break;
  2511. case BUF_FLOAT:
  2512. res = sol_new_float(state, *((float *) data));
  2513. break;
  2514. case BUF_DOUBLE:
  2515. res = sol_new_float(state, *((double *) data));
  2516. break;
  2517. case BUF_CSTR:
  2518. res = sol_new_string(state, (char *) data);
  2519. break;
  2520. case BUF_PTR:
  2521. res = sol_new_buffer(state, ((void *) * ((unsigned long *) data)), -1, OWN_NONE, NULL, NULL);
  2522. break;
  2523. }
  2524. if(!res) {
  2525. res = sol_incref(state->None);
  2526. }
  2527. return res;
  2528. }
  2529. sol_object_t *sol_f_buffer_set(sol_state_t *state, sol_object_t *args) {
  2530. 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);
  2531. sol_object_t *itp = sol_cast_int(state, tp), *ioff, *ival = NULL, *fval = NULL, *sval = NULL;
  2532. sol_buftype_t buftp = itp->ival;
  2533. char *data;
  2534. sol_obj_free(tp);
  2535. sol_obj_free(itp);
  2536. if(!sol_is_none(state, off)) {
  2537. ioff = sol_cast_int(state, off);
  2538. } else {
  2539. ioff = sol_new_int(state, 0);
  2540. }
  2541. sol_obj_free(off);
  2542. if(buf->sz != -1 && (ioff->ival >= buf->sz || ioff->ival < 0)) {
  2543. sol_obj_free(buf);
  2544. sol_obj_free(ioff);
  2545. return sol_incref(state->None);
  2546. }
  2547. data = ((char *) buf->buffer) + ioff->ival;
  2548. sol_obj_free(buf);
  2549. sol_obj_free(ioff);
  2550. switch(buftp) {
  2551. case BUF_UINT8:
  2552. ival = sol_cast_int(state, val);
  2553. *((uint8_t *) data) = ival->ival;
  2554. break;
  2555. case BUF_UINT16:
  2556. ival = sol_cast_int(state, val);
  2557. *((uint16_t *) data) = ival->ival;
  2558. break;
  2559. case BUF_UINT32:
  2560. ival = sol_cast_int(state, val);
  2561. *((uint32_t *) data) = ival->ival;
  2562. break;
  2563. case BUF_UINT64:
  2564. ival = sol_cast_int(state, val);
  2565. *((uint64_t *) data) = ival->ival;
  2566. break;
  2567. case BUF_INT8:
  2568. ival = sol_cast_int(state, val);
  2569. *((int8_t *) data) = ival->ival;
  2570. break;
  2571. case BUF_INT16:
  2572. ival = sol_cast_int(state, val);
  2573. *((int16_t *) data) = ival->ival;
  2574. break;
  2575. case BUF_INT32:
  2576. ival = sol_cast_int(state, val);
  2577. *((int32_t *) data) = ival->ival;
  2578. break;
  2579. case BUF_INT64:
  2580. ival = sol_cast_int(state, val);
  2581. *((int64_t *) data) = ival->ival;
  2582. break;
  2583. case BUF_CHAR:
  2584. sval = sol_cast_string(state, val);
  2585. *((char *) data) = sval->str[0];
  2586. break;
  2587. case BUF_BYTE:
  2588. ival = sol_cast_int(state, val);
  2589. *((unsigned char *) data) = ival->ival;
  2590. break;
  2591. case BUF_INT:
  2592. ival = sol_cast_int(state, val);
  2593. *((int *) data) = ival->ival;
  2594. break;
  2595. case BUF_UINT:
  2596. ival = sol_cast_int(state, val);
  2597. *((unsigned int *) data) = ival->ival;
  2598. break;
  2599. case BUF_LONG:
  2600. ival = sol_cast_int(state, val);
  2601. *((long *) data) = ival->ival;
  2602. break;
  2603. case BUF_ULONG:
  2604. ival = sol_cast_int(state, val);
  2605. *((unsigned long *) data) = ival->ival;
  2606. break;
  2607. case BUF_FLOAT:
  2608. fval = sol_cast_float(state, val);
  2609. *((float *) data) = fval->fval;
  2610. break;
  2611. case BUF_DOUBLE:
  2612. fval = sol_cast_float(state, val);
  2613. *((double *) data) = fval->fval;
  2614. break;
  2615. case BUF_CSTR:
  2616. sval = sol_cast_string(state, val);
  2617. strcpy((char *) data, sval->str);
  2618. break;
  2619. case BUF_PTR:
  2620. if(sol_is_buffer(val)) {
  2621. *((unsigned long *) data) = ((unsigned long) val->buffer);
  2622. if(val->own == OWN_CALLF) {
  2623. val->movef(val->buffer, val->sz);
  2624. }
  2625. } else {
  2626. ival = sol_cast_int(state, val);
  2627. *((unsigned long *) data) = ival->ival;
  2628. }
  2629. break;
  2630. }
  2631. if(ival) {
  2632. sol_obj_free(ival);
  2633. }
  2634. if(fval) {
  2635. sol_obj_free(fval);
  2636. }
  2637. if(sval) {
  2638. sol_obj_free(sval);
  2639. }
  2640. return sol_incref(state->None);
  2641. }
  2642. sol_object_t *sol_f_buffer_address(sol_state_t *state, sol_object_t *args) {
  2643. sol_object_t *buf = sol_list_get_index(state, args, 0);
  2644. sol_object_t *res = sol_new_int(state, (unsigned long) buf->buffer);
  2645. sol_obj_free(buf);
  2646. return res;
  2647. }
  2648. sol_object_t *sol_f_buffer_size(sol_state_t *state, sol_object_t *args) {
  2649. sol_object_t *buf = sol_list_get_index(state, args, 0);
  2650. sol_object_t *res = sol_new_int(state, (long) buf->sz);
  2651. sol_obj_free(buf);
  2652. return res;
  2653. }
  2654. sol_object_t *sol_f_buffer_sub(sol_state_t *state, sol_object_t *args) {
  2655. sol_object_t *buf = sol_list_get_index(state, args, 0);
  2656. sol_object_t *low = sol_list_get_index(state, args, 1), *high = sol_list_get_index(state, args, 2);
  2657. sol_object_t *ilow, *ihigh;
  2658. long l, h;
  2659. char *b;
  2660. if(sol_is_none(state, low)) {
  2661. ilow = sol_new_int(state, 0);
  2662. } else {
  2663. ilow = sol_cast_int(state, low);
  2664. }
  2665. if(sol_is_none(state, high)) {
  2666. ihigh = sol_new_int(state, buf->sz);
  2667. } else {
  2668. ihigh = sol_cast_int(state, high);
  2669. }
  2670. l = ilow->ival;
  2671. h = ihigh->ival;
  2672. sol_obj_free(low);
  2673. sol_obj_free(high);
  2674. sol_obj_free(ilow);
  2675. sol_obj_free(ihigh);
  2676. if(l < 0) {
  2677. l += buf->sz;
  2678. if(l < 0) {
  2679. l = 0;
  2680. }
  2681. }
  2682. if(l > buf->sz) {
  2683. l = buf->sz;
  2684. }
  2685. if(h < 0) {
  2686. h += buf->sz;
  2687. if(h < 0) {
  2688. h = 0;
  2689. }
  2690. }
  2691. if(h > buf->sz) {
  2692. h = buf->sz;
  2693. }
  2694. if(l >= h) {
  2695. sol_obj_free(buf);
  2696. return sol_new_buffer(state, NULL, 0, OWN_NONE, NULL, NULL);
  2697. }
  2698. b = malloc(sizeof(char) * (h - l));
  2699. memcpy(b, ((char *) buf->buffer) + l, h - l);
  2700. sol_obj_free(buf);
  2701. return sol_new_buffer(state, b, h - l, OWN_FREE, NULL, NULL);
  2702. }
  2703. sol_object_t *sol_f_buffer_split(sol_state_t *state, sol_object_t *args) {
  2704. sol_object_t *buf = sol_list_get_index(state, args, 0);
  2705. char *b;
  2706. sol_object_t *str, *res, *ls;
  2707. if(buf->sz < 0) {
  2708. sol_obj_free(buf);
  2709. return sol_set_error_string(state, "split unsized buffer");
  2710. }
  2711. b = malloc(sizeof(char) * (buf->sz + 1));
  2712. memcpy(b, buf->buffer, buf->sz);
  2713. b[buf->sz] = '\0';
  2714. str = sol_new_string(state, b);
  2715. free(b);
  2716. ls = sol_new_list(state);
  2717. sol_list_insert(state, ls, 0, str);
  2718. sol_obj_free(str);
  2719. str = sol_list_get_index(state, args, 1);
  2720. sol_list_insert(state, ls, 1, str);
  2721. sol_obj_free(str);
  2722. res = sol_f_str_split(state, ls);
  2723. sol_obj_free(ls);
  2724. return res;
  2725. }
  2726. sol_object_t *sol_f_buffer_find(sol_state_t *state, sol_object_t *args) {
  2727. sol_object_t *buf = sol_list_get_index(state, args, 0), *subbuf = sol_list_get_index(state, args, 1), *bsubbuf = sol_cast_buffer(state, subbuf);
  2728. sol_object_t *res;
  2729. char *ptr;
  2730. sol_obj_free(subbuf);
  2731. if(buf->sz < 0 || bsubbuf->sz < 0) {
  2732. sol_obj_free(buf);
  2733. sol_obj_free(bsubbuf);
  2734. return sol_set_error_string(state, "find with unsized buffer");
  2735. }
  2736. ptr = memmem(buf->buffer, buf->sz, bsubbuf->buffer, bsubbuf->sz);
  2737. res = sol_new_int(state, ptr ? (ptr - ((char *) buf->buffer)) : -1);
  2738. sol_obj_free(buf);
  2739. sol_obj_free(bsubbuf);
  2740. return res;
  2741. }
  2742. sol_object_t *sol_f_buffer_fromstring(sol_state_t *state, sol_object_t *args) {
  2743. sol_object_t *val = sol_list_get_index(state, args, 0), *sval = sol_cast_string(state, val);
  2744. size_t sz = strlen(sval->str) + 1;
  2745. sol_object_t *buf = sol_new_buffer(state, malloc(sz), sz, OWN_FREE, NULL, NULL);
  2746. strcpy(buf->buffer, sval->str);
  2747. sol_obj_free(val);
  2748. sol_obj_free(sval);
  2749. return buf;
  2750. }
  2751. sol_object_t *sol_f_buffer_fromobject(sol_state_t *state, sol_object_t *args) {
  2752. sol_object_t *obj = sol_list_get_index(state, args, 0);
  2753. 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);
  2754. //Keep ref to obj so buf remains alive
  2755. return buf;
  2756. }
  2757. sol_object_t *sol_f_buffer_fromaddress(sol_state_t *state, sol_object_t *args) {
  2758. sol_object_t *addr = sol_list_get_index(state, args, 0), *sz = sol_list_get_index(state, args, 1);
  2759. sol_object_t *iaddr = sol_cast_int(state, addr), *isz = sol_cast_int(state, sz);
  2760. sol_object_t *buf = sol_new_buffer(state, (void *) iaddr->ival, (ssize_t) isz->ival, OWN_NONE, NULL, NULL);
  2761. sol_obj_free(addr);
  2762. sol_obj_free(sz);
  2763. sol_obj_free(iaddr);
  2764. sol_obj_free(isz);
  2765. return buf;
  2766. }
  2767. sol_object_t *sol_f_dylib_index(sol_state_t *state, sol_object_t *args) {
  2768. 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);
  2769. void *handle = dlsym(dylib->dlhandle, skey->str);
  2770. sol_obj_free(dylib);
  2771. sol_obj_free(key);
  2772. sol_obj_free(skey);
  2773. if(handle) {
  2774. return sol_new_dysym(state, handle, NULL, BUF_NONE);
  2775. } else {
  2776. return sol_incref(state->None);
  2777. }
  2778. }
  2779. sol_object_t *sol_f_dylib_tostring(sol_state_t *state, sol_object_t *args) {
  2780. return sol_new_string(state, "<DyLib>");
  2781. }
  2782. sol_object_t *sol_f_dysym_call(sol_state_t *state, sol_object_t *args) {
  2783. //TODO
  2784. return sol_incref(state->None);
  2785. }
  2786. sol_object_t *sol_f_dysym_index(sol_state_t *state, sol_object_t *args) {
  2787. //TODO
  2788. return sol_incref(state->None);
  2789. }
  2790. sol_object_t *sol_f_dysym_setindex(sol_state_t *state, sol_object_t *args) {
  2791. //TODO
  2792. return sol_incref(state->None);
  2793. }
  2794. sol_object_t *sol_f_dysym_tostring(sol_state_t *state, sol_object_t *args) {
  2795. return sol_new_string(state, "<DySym>");
  2796. }
  2797. sol_object_t *sol_f_stream_blsh(sol_state_t *state, sol_object_t *args) {
  2798. return sol_f_stream_write(state, args);
  2799. }
  2800. sol_object_t *sol_f_stream_brsh(sol_state_t *state, sol_object_t *args) {
  2801. return sol_f_stream_read_buffer(state, args);
  2802. }
  2803. sol_object_t *sol_f_stream_index(sol_state_t *state, sol_object_t *args) {
  2804. sol_object_t *key = sol_list_get_index(state, args, 1), *funcs = sol_get_methods_name(state, "stream");
  2805. sol_object_t *res = sol_map_get(state, funcs, key);
  2806. sol_obj_free(key);
  2807. sol_obj_free(funcs);
  2808. return res;
  2809. }
  2810. sol_object_t *sol_f_stream_tostring(sol_state_t *state, sol_object_t *args) {
  2811. return sol_new_string(state, "<Stream>");
  2812. }
  2813. sol_object_t *sol_f_stream_write(sol_state_t *state, sol_object_t *args) {
  2814. sol_object_t *stream = sol_list_get_index(state, args, 0), *obj = sol_list_get_index(state, args, 1), *str;
  2815. size_t sz;
  2816. if(sol_is_buffer(obj)) {
  2817. sz = sol_stream_fwrite(state, stream, obj->buffer, sizeof(char), obj->sz);
  2818. } else {
  2819. str = sol_cast_string(state, obj);
  2820. sz = sol_stream_printf(state, stream, "%s", str->str);
  2821. sol_obj_free(str);
  2822. }
  2823. sol_obj_free(obj);
  2824. sol_obj_free(stream);
  2825. return sol_new_int(state, sz);
  2826. }
  2827. sol_object_t *sol_f_stream_read_buffer(sol_state_t *state, sol_object_t *args) {
  2828. sol_object_t *stream = sol_list_get_index(state, args, 0), *amt = sol_list_get_index(state, args, 1), *iamt, *res;
  2829. char *s = NULL, *p;
  2830. size_t count = 0, max = 0, pos, end;
  2831. if(sol_is_name(amt)) {
  2832. if(sol_name_eq(state, amt, "ALL")) {
  2833. pos = sol_stream_ftell(state, stream);
  2834. sol_stream_fseek(state, stream, 0, SEEK_END);
  2835. end = sol_stream_ftell(state, stream);
  2836. sol_stream_fseek(state, stream, pos, SEEK_SET);
  2837. //printf("IO: Reading %ld bytes starting at %ld\n", end-pos, pos);
  2838. s = malloc((end - pos + 1) * sizeof(char));
  2839. if(sol_stream_fread(state, stream, s, sizeof(char), end - pos) < (end - pos)) {
  2840. free(s);
  2841. sol_obj_free(stream);
  2842. sol_obj_free(amt);
  2843. return sol_set_error_string(state, "IO read error");
  2844. }
  2845. count = end - pos;
  2846. } else if(sol_name_eq(state, amt, "LINE")) {
  2847. s = malloc(STDIO_CHUNK_SIZE * sizeof(char));
  2848. sol_stream_fgets(state, stream, s, STDIO_CHUNK_SIZE);
  2849. count = strlen(s);
  2850. }
  2851. } else {
  2852. iamt = sol_cast_int(state, amt);
  2853. s = malloc((iamt->ival + 1) * sizeof(char));
  2854. count = sol_stream_fread(state, stream, s, sizeof(char), iamt->ival);
  2855. sol_obj_free(iamt);
  2856. }
  2857. if(s) {
  2858. //printf("IO: Read result: %s\n", s);
  2859. res = sol_new_buffer(state, s, count, OWN_FREE, NULL, NULL);
  2860. } else {
  2861. //printf("IO: No read result!\n");
  2862. res = sol_incref(state->None);
  2863. }
  2864. sol_obj_free(amt);
  2865. sol_obj_free(stream);
  2866. return res;
  2867. }
  2868. /*
  2869. sol_object_t *sol_f_stream_read(sol_state_t *state, sol_object_t *args) {
  2870. sol_object_t *buf = sol_f_stream_read_buffer(state, args);
  2871. sol_object_t *str = sol_new_string(state, buf->buffer);
  2872. sol_obj_free(buf);
  2873. return str;
  2874. }
  2875. */
  2876. sol_object_t *sol_f_stream_seek(sol_state_t *state, sol_object_t *args) {
  2877. 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);
  2878. sol_object_t *ioffset = sol_cast_int(state, offset), *iwhence = sol_cast_int(state, whence);
  2879. sol_object_t *res = sol_new_int(state, sol_stream_fseek(state, stream, ioffset->ival, iwhence->ival));
  2880. sol_obj_free(stream);
  2881. sol_obj_free(offset);
  2882. sol_obj_free(whence);
  2883. sol_obj_free(ioffset);
  2884. sol_obj_free(iwhence);
  2885. return res;
  2886. }
  2887. sol_object_t *sol_f_stream_tell(sol_state_t *state, sol_object_t *args) {
  2888. sol_object_t *stream = sol_list_get_index(state, args, 0), *res = sol_new_int(state, sol_stream_ftell(state, stream));
  2889. sol_obj_free(stream);
  2890. return res;
  2891. }
  2892. sol_object_t *sol_f_stream_flush(sol_state_t *state, sol_object_t *args) {
  2893. sol_object_t *stream = sol_list_get_index(state, args, 0), *res = sol_new_int(state, sol_stream_fflush(state, stream));
  2894. sol_obj_free(stream);
  2895. return res;
  2896. }
  2897. sol_object_t *sol_f_stream_eof(sol_state_t *state, sol_object_t *args) {
  2898. sol_object_t *stream = sol_list_get_index(state, args, 0), *res = sol_new_int(state, sol_stream_feof(state, stream));
  2899. sol_obj_free(stream);
  2900. return res;
  2901. }
  2902. sol_object_t *sol_f_stream_ioctl(sol_state_t *state, sol_object_t *args) {
  2903. sol_object_t *stream = sol_list_get_index(state, args, 0), *buf = sol_list_get_index(state, args, 2);
  2904. sol_object_t *req = sol_list_get_index(state, args, 1), *ireq = sol_cast_int(state, req);
  2905. sol_object_t *res = sol_new_int(state, ioctl(fileno(stream->stream), (unsigned long) ireq->ival, buf->buffer));
  2906. sol_obj_free(stream);
  2907. sol_obj_free(buf);
  2908. sol_obj_free(req);
  2909. sol_obj_free(ireq);
  2910. return res;
  2911. }
  2912. static char *sol_FileModes[] = {
  2913. NULL,
  2914. "r",
  2915. "w",
  2916. "r+",
  2917. NULL,
  2918. NULL,
  2919. "a",
  2920. "a+",
  2921. NULL,
  2922. NULL,
  2923. "w",
  2924. "w+",
  2925. NULL,
  2926. NULL,
  2927. NULL,
  2928. NULL,
  2929. NULL,
  2930. "rb",
  2931. "wb",
  2932. "r+b",
  2933. NULL,
  2934. NULL,
  2935. "ab",
  2936. "a+b",
  2937. NULL,
  2938. NULL,
  2939. "wb",
  2940. "w+b",
  2941. NULL,
  2942. NULL,
  2943. NULL,
  2944. NULL
  2945. };
  2946. sol_object_t *sol_f_stream_open(sol_state_t *state, sol_object_t *args) {
  2947. sol_object_t *fn = sol_list_get_index(state, args, 0), *mode = sol_list_get_index(state, args, 1);
  2948. sol_object_t *sfn = sol_cast_string(state, fn), *imode = sol_cast_int(state, mode);
  2949. sol_modes_t m = imode->ival;
  2950. char *smode = sol_FileModes[(m >= 0 && m < (sizeof(sol_FileModes) / sizeof(char *))) ? m : 0];
  2951. FILE *f;
  2952. sol_obj_free(mode);
  2953. sol_obj_free(imode);
  2954. if(!smode) {
  2955. sol_obj_free(fn);
  2956. sol_obj_free(sfn);
  2957. return sol_set_error_string(state, "Bad file open mode");
  2958. }
  2959. f = fopen(sfn->str, smode);
  2960. sol_obj_free(sfn);
  2961. sol_obj_free(fn);
  2962. if(!f) {
  2963. return sol_set_error_string(state, "File open failed");
  2964. }
  2965. return sol_new_stream(state, f, m);
  2966. }