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

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