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

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