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.

2670 lines
78 KiB

  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_no_op(sol_state_t *state, sol_object_t *args) {
  50. if(state) {
  51. return sol_incref(state->None);
  52. }
  53. return NULL;
  54. }
  55. sol_object_t *sol_f_toint(sol_state_t *state, sol_object_t *args) {
  56. sol_object_t *obj = sol_list_get_index(state, args, 0);
  57. sol_object_t *res = CALL_METHOD(state, obj, toint, args);
  58. sol_obj_free(obj);
  59. return res;
  60. }
  61. sol_object_t *sol_f_tofloat(sol_state_t *state, sol_object_t *args) {
  62. sol_object_t *obj = sol_list_get_index(state, args, 0);
  63. sol_object_t *res = CALL_METHOD(state, obj, tofloat, args);
  64. sol_obj_free(obj);
  65. return res;
  66. }
  67. sol_object_t *sol_f_tostring(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, tostring, args);
  70. sol_obj_free(obj);
  71. return res;
  72. }
  73. sol_object_t *sol_f_try(sol_state_t *state, sol_object_t *args) {
  74. sol_object_t *func = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  75. sol_object_t *ls = sol_new_list(state), *one = sol_new_int(state, 1);
  76. sol_object_t *res;
  77. sol_list_insert(state, fargs, 0, func);
  78. res = CALL_METHOD(state, func, call, fargs);
  79. sol_obj_free(func);
  80. sol_obj_free(fargs);
  81. if(sol_has_error(state)) {
  82. sol_object_t *err = sol_get_error(state);
  83. sol_clear_error(state);
  84. sol_object_t *zero = sol_new_int(state, 0);
  85. sol_obj_free(res);
  86. sol_obj_free(one);
  87. sol_list_insert(state, ls, 0, err);
  88. sol_obj_free(err);
  89. sol_list_insert(state, ls, 0, zero);
  90. sol_obj_free(zero);
  91. sol_list_insert(state, ls, 2, state->traceback);
  92. sol_obj_free(state->traceback);
  93. state->traceback = NULL;
  94. return ls;
  95. }
  96. sol_list_insert(state, ls, 0, res);
  97. sol_obj_free(res);
  98. sol_list_insert(state, ls, 0, one);
  99. sol_obj_free(one);
  100. return ls;
  101. }
  102. sol_object_t *sol_f_error(sol_state_t *state, sol_object_t *args) {
  103. sol_object_t *arg = sol_list_get_index(state, args, 0), *res;
  104. res = sol_set_error(state, arg);
  105. sol_obj_free(arg);
  106. return res;
  107. }
  108. sol_object_t *sol_f_type(sol_state_t *state, sol_object_t *args) {
  109. sol_object_t *obj = sol_list_get_index(state, args, 0);
  110. sol_object_t *res = sol_new_string(state, obj->ops->tname);
  111. sol_obj_free(obj);
  112. return res;
  113. }
  114. static dsl_seq *seen = NULL;
  115. int test_seen(sol_object_t *obj) {
  116. dsl_seq_iter *iter;
  117. if(seen) {
  118. iter = dsl_new_seq_iter(seen);
  119. while(!dsl_seq_iter_is_invalid(iter)) {
  120. if(dsl_seq_iter_at(iter) == obj) {
  121. return 1;
  122. }
  123. dsl_seq_iter_next(iter);
  124. }
  125. dsl_free_seq_iter(iter);
  126. dsl_seq_insert(seen, dsl_seq_len(seen), obj);
  127. }
  128. return 0;
  129. }
  130. void ob_print(sol_object_t *obj) {
  131. sol_object_t *cur;
  132. dsl_seq_iter *iter;
  133. int i;
  134. if(test_seen(obj)) {
  135. return;
  136. }
  137. switch(obj->type) {
  138. case SOL_SINGLET:
  139. printf("%s", obj->str);
  140. break;
  141. case SOL_INTEGER:
  142. printf("%ld", obj->ival);
  143. break;
  144. case SOL_FLOAT:
  145. printf("%f", obj->fval);
  146. break;
  147. case SOL_STRING:
  148. printf("\"%s\"", obj->str);
  149. break;
  150. case SOL_LIST:
  151. printf("[");
  152. iter = dsl_new_seq_iter(obj->seq);
  153. while(!dsl_seq_iter_is_invalid(iter)) {
  154. ob_print(dsl_seq_iter_at(iter));
  155. printf(", ");
  156. dsl_seq_iter_next(iter);
  157. }
  158. dsl_free_seq_iter(iter);
  159. printf("]");
  160. break;
  161. case SOL_MCELL:
  162. printf("<<");
  163. ob_print(obj->key);
  164. printf("=");
  165. ob_print(obj->val);
  166. printf(">>");
  167. case SOL_MAP:
  168. printf("{");
  169. iter = dsl_new_seq_iter(obj->seq);
  170. while(!dsl_seq_iter_is_invalid(iter)) {
  171. printf("[");
  172. ob_print(AS_OBJ(dsl_seq_iter_at(iter))->key);
  173. printf("] = ");
  174. ob_print(AS_OBJ(dsl_seq_iter_at(iter))->val);
  175. printf(", ");
  176. dsl_seq_iter_next(iter);
  177. }
  178. dsl_free_seq_iter(iter);
  179. printf("}");
  180. break;
  181. case SOL_FUNCTION:
  182. if(obj->fname) {
  183. printf("<Function %s>", obj->fname);
  184. } else {
  185. printf("<Function>");
  186. }
  187. break;
  188. case SOL_CFUNCTION:
  189. printf("<CFunction>");
  190. break;
  191. case SOL_STMT:
  192. st_print(NULL, obj->node); //TODO: FIXME
  193. break;
  194. case SOL_EXPR:
  195. ex_print(NULL, obj->node); //TODO: FIXME
  196. break;
  197. case SOL_BUFFER:
  198. if(obj->sz == -1) {
  199. printf("<Buffer @%p>", obj->buffer);
  200. } else {
  201. printf("<Buffer @%p size %ld>", obj->buffer, obj->sz);
  202. }
  203. break;
  204. case SOL_CDATA:
  205. printf("<CData>");
  206. break;
  207. /*default:
  208. cur = sol_cast_string(state, obj);
  209. printf("%s", cur->str);
  210. sol_obj_free(cur);*/
  211. }
  212. }
  213. sol_object_t *sol_f_prepr(sol_state_t *state, sol_object_t *args) {
  214. int i, sz = sol_list_len(state, args);
  215. sol_object_t *obj, *str;
  216. seen = dsl_seq_new_array(NULL, NULL);
  217. for(i = 0; i < sz; i++) {
  218. obj = sol_list_get_index(state, args, i);
  219. str = sol_cast_repr(state, obj);
  220. sol_printf(state, "%s", str->str);
  221. sol_printf(state, " ");
  222. sol_obj_free(obj);
  223. sol_obj_free(str);
  224. }
  225. sol_printf(state, "\n");
  226. printf("\n");
  227. dsl_free_seq(seen);
  228. seen = NULL;
  229. return sol_incref(state->None);
  230. }
  231. sol_object_t *sol_f_print(sol_state_t *state, sol_object_t *args) {
  232. int i, sz = sol_list_len(state, args);
  233. sol_object_t *obj, *str;
  234. seen = dsl_seq_new_array(NULL, NULL);
  235. for(i = 0; i < sz; i++) {
  236. obj = sol_list_get_index(state, args, i);
  237. str = sol_cast_string(state, obj);
  238. sol_printf(state, "%s", str->str);
  239. sol_printf(state, " ");
  240. sol_obj_free(obj);
  241. sol_obj_free(str);
  242. }
  243. sol_printf(state, "\n");
  244. dsl_free_seq(seen);
  245. seen = NULL;
  246. return sol_incref(state->None);
  247. }
  248. sol_object_t *sol_f_rawget(sol_state_t *state, sol_object_t *args) {
  249. sol_object_t *obj = sol_list_get_index(state, args, 0), *key, *res;
  250. if(!sol_is_map(obj)) {
  251. return sol_set_error_string(state, "Rawset of non-map");
  252. }
  253. key = sol_list_get_index(state, args, 1);
  254. res = sol_map_get(state, obj, key);
  255. sol_obj_free(key);
  256. sol_obj_free(obj);
  257. return res;
  258. }
  259. sol_object_t *sol_f_rawset(sol_state_t *state, sol_object_t *args) {
  260. sol_object_t *obj = sol_list_get_index(state, args, 0), *key, *val;
  261. if(!sol_is_map(obj)) {
  262. return sol_set_error_string(state, "Rawset of non-map");
  263. }
  264. key = sol_list_get_index(state, args, 1);
  265. val = sol_list_get_index(state, args, 2);
  266. sol_map_set(state, obj, key, val);
  267. sol_obj_free(val);
  268. sol_obj_free(key);
  269. sol_obj_free(obj);
  270. return sol_incref(state->None);
  271. }
  272. sol_object_t *sol_f_range(sol_state_t *state, sol_object_t *args) {
  273. sol_object_t *res = sol_new_list(state), *bound = sol_cast_int(state, sol_list_get_index(state, args, 0));
  274. int i;
  275. for(i = 0; i < bound->ival; i++) {
  276. sol_list_insert(state, res, sol_list_len(state, res), sol_new_int(state, i));
  277. }
  278. sol_obj_free(bound);
  279. return res;
  280. }
  281. sol_object_t *sol_f_exec(sol_state_t *state, sol_object_t *args) {
  282. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  283. stmt_node *program;
  284. program = sol_compile(prgstr->str);
  285. if(!program) {
  286. return sol_set_error_string(state, "Compilation failure");
  287. }
  288. // XXX should st_free(program);
  289. sol_exec(state, program);
  290. return sol_incref(state->None);
  291. }
  292. sol_object_t *sol_f_eval(sol_state_t *state, sol_object_t *args) {
  293. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  294. stmt_node *program;
  295. program = sol_compile(prgstr->str);
  296. if(!program) {
  297. return sol_set_error_string(state, "Compilation failure");
  298. }
  299. if(program->type != ST_LIST || program->stmtlist->stmt->type != ST_EXPR) {
  300. return sol_set_error_string(state, "Not an expression");
  301. }
  302. // XXX should st_free(program);
  303. return sol_eval(state, program->stmtlist->stmt->expr);
  304. }
  305. sol_object_t *sol_f_execfile(sol_state_t *state, sol_object_t *args) {
  306. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  307. stmt_node *program;
  308. FILE *f = fopen(prgstr->str, "r");
  309. char *s;
  310. long sz;
  311. if(!f) {
  312. return sol_set_error_string(state, "File open failure");
  313. }
  314. fseek(f, 0, SEEK_END);
  315. sz = ftell(f);
  316. fseek(f, 0, SEEK_SET);
  317. s = malloc(sz + 1);
  318. if(!s) {
  319. fclose(f);
  320. return sol_set_error_string(state, "File memory allocation failure");
  321. }
  322. fread(s, 1, sz, f);
  323. s[sz] = 0;
  324. fclose(f);
  325. program = sol_compile(s);
  326. free(s);
  327. if(!program) {
  328. return sol_set_error_string(state, "Compilation failure");
  329. }
  330. // XXX should st_free(program);
  331. sol_exec(state, program);
  332. return sol_incref(state->None);
  333. }
  334. sol_object_t *sol_f_parse(sol_state_t *state, sol_object_t *args) {
  335. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  336. stmt_node *program = sol_compile(prgstr->str);
  337. sol_obj_free(prg);
  338. sol_obj_free(prgstr);
  339. if(!program) {
  340. return sol_set_error_string(state, "Compilation failure");
  341. }
  342. return sol_new_stmtnode(state, program);
  343. }
  344. sol_object_t *sol_f_ord(sol_state_t *state, sol_object_t *args) {
  345. sol_object_t *arg = sol_list_get_index(state, args, 0), *str = sol_cast_string(state, arg);
  346. sol_object_t *arg2, *iarg, *res;
  347. long idx = 0;
  348. size_t len = strlen(str->str);
  349. sol_obj_free(arg);
  350. if(sol_list_len(state, args) > 1) {
  351. arg2 = sol_list_get_index(state, args, 1);
  352. iarg = sol_cast_int(state, arg2);
  353. sol_obj_free(arg2);
  354. idx = iarg->ival;
  355. sol_obj_free(iarg);
  356. }
  357. if(idx < 0) {
  358. idx += len;
  359. }
  360. if(idx < 0 || idx >= len) {
  361. sol_obj_free(str);
  362. return sol_set_error_string(state, "Compute ord of out-of-bounds index");
  363. }
  364. res = sol_new_int(state, str->str[idx]);
  365. sol_obj_free(str);
  366. return res;
  367. }
  368. sol_object_t *sol_f_chr(sol_state_t *state, sol_object_t *args) {
  369. sol_object_t *arg = sol_list_get_index(state, args, 0), *iarg = sol_cast_int(state, arg);
  370. char cbuf[2] = {iarg->ival, 0};
  371. sol_object_t *res = sol_new_string(state, cbuf);
  372. sol_obj_free(arg);
  373. sol_obj_free(iarg);
  374. return res;
  375. }
  376. sol_object_t *sol_f_debug_getref(sol_state_t *state, sol_object_t *args) {
  377. sol_object_t *obj = sol_list_get_index(state, args, 0);
  378. 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.
  379. sol_obj_free(obj);
  380. return res;
  381. }
  382. sol_object_t *sol_f_debug_setref(sol_state_t *state, sol_object_t *args) {
  383. sol_object_t *obj = sol_list_get_index(state, args, 0), *cnt = sol_list_get_index(state, args, 1);
  384. obj->refcnt = sol_cast_int(state, cnt)->ival + 2; // NB: As above.
  385. sol_obj_free(cnt);
  386. sol_obj_free(obj);
  387. return sol_incref(state->None);
  388. }
  389. sol_object_t *sol_f_debug_closure(sol_state_t *state, sol_object_t *args) {
  390. sol_object_t *func = sol_list_get_index(state, args, 0);
  391. sol_object_t *res = sol_incref(func->closure);
  392. sol_obj_free(func);
  393. return res;
  394. }
  395. sol_object_t *sol_f_debug_globals(sol_state_t *state, sol_object_t *args) {
  396. return sol_list_get_index(state, state->scopes, sol_list_len(state, state->scopes) - 1);
  397. }
  398. sol_object_t *sol_f_debug_locals(sol_state_t *state, sol_object_t *args) {
  399. return sol_list_get_index(state, state->scopes, 0);
  400. }
  401. sol_object_t *sol_f_debug_scopes(sol_state_t *state, sol_object_t *args) {
  402. return sol_incref(state->scopes);
  403. }
  404. sol_object_t *sol_f_readline_readline(sol_state_t *state, sol_object_t *args) {
  405. sol_object_t *obj, *objstr, *res;
  406. char *line;
  407. if(sol_list_len(state, args) > 0) {
  408. obj = sol_list_get_index(state, args, 0);
  409. objstr = sol_cast_string(state, obj);
  410. line = readline(objstr->str);
  411. sol_obj_free(obj);
  412. sol_obj_free(objstr);
  413. } else {
  414. line = readline("");
  415. }
  416. if(line) {
  417. res = sol_new_string(state, line);
  418. free(line);
  419. } else {
  420. res = sol_new_string(state, "");
  421. }
  422. return res;
  423. }
  424. sol_object_t *sol_f_readline_add_history(sol_state_t *state, sol_object_t *args) {
  425. sol_object_t *line = sol_list_get_index(state, args, 0), *linestr = sol_cast_string(state, line);
  426. add_history(linestr->str);
  427. sol_obj_free(linestr);
  428. sol_obj_free(line);
  429. return sol_incref(state->None);
  430. }
  431. void _sol_freef_seq_iter(void *iter, size_t sz) {
  432. dsl_free_seq_iter((dsl_seq_iter *) iter);
  433. }
  434. sol_object_t *sol_f_iter_str(sol_state_t *state, sol_object_t *args) {
  435. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  436. sol_object_t *index = sol_map_get_name(state, local, "idx"), *max = sol_map_get_name(state, local, "sz"), *res;
  437. char temp[2] = {0, 0};
  438. if(sol_is_none(state, index) || sol_is_none(state, max)) {
  439. sol_obj_free(index);
  440. index = sol_new_buffer(state, (void *) 0, sizeof(void *), OWN_NONE, NULL, NULL);
  441. sol_map_set_name(state, local, "idx", index);
  442. sol_obj_free(max);
  443. max = sol_new_int(state, strlen(obj->str));
  444. sol_map_set_name(state, local, "sz", max);
  445. }
  446. if(((size_t) index->buffer) >= max->ival) {
  447. sol_obj_free(index);
  448. sol_obj_free(obj);
  449. sol_obj_free(local);
  450. return sol_incref(state->StopIteration);
  451. }
  452. temp[0] = obj->str[((size_t) index->buffer)];
  453. res = sol_new_string(state, temp);
  454. index->buffer = (void *) ((size_t) index->buffer + 1);
  455. sol_obj_free(index);
  456. sol_obj_free(local);
  457. sol_obj_free(obj);
  458. return res;
  459. }
  460. sol_object_t *sol_f_iter_list(sol_state_t *state, sol_object_t *args) {
  461. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  462. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  463. if(sol_is_none(state, index)) {
  464. sol_obj_free(index);
  465. index = sol_new_buffer(state, dsl_new_seq_iter(obj->seq), sizeof(dsl_seq_iter), OWN_CALLF, _sol_freef_seq_iter, NULL);
  466. sol_map_set_name(state, local, "idx", index);
  467. sol_obj_free(index);
  468. }
  469. if(dsl_seq_iter_is_invalid(index->buffer)) {
  470. sol_obj_free(index);
  471. sol_obj_free(obj);
  472. sol_obj_free(local);
  473. return sol_incref(state->StopIteration);
  474. }
  475. res = sol_incref(AS_OBJ(dsl_seq_iter_at(index->buffer)));
  476. dsl_seq_iter_next(index->buffer);
  477. sol_obj_free(local);
  478. sol_obj_free(obj);
  479. return res;
  480. }
  481. sol_object_t *sol_f_iter_map(sol_state_t *state, sol_object_t *args) {
  482. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  483. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  484. if(sol_is_none(state, index)) {
  485. sol_obj_free(index);
  486. index = sol_new_buffer(state, dsl_new_seq_iter(obj->seq), sizeof(dsl_seq_iter), OWN_CALLF, _sol_freef_seq_iter, NULL);
  487. sol_map_set_name(state, local, "idx", index);
  488. sol_obj_free(index);
  489. }
  490. if(dsl_seq_iter_is_invalid(index->buffer)) {
  491. sol_obj_free(index);
  492. sol_obj_free(obj);
  493. sol_obj_free(local);
  494. return sol_incref(state->StopIteration);
  495. }
  496. res = sol_incref(AS_OBJ(dsl_seq_iter_at(index->buffer))->key);
  497. dsl_seq_iter_next(index->buffer);
  498. sol_obj_free(local);
  499. sol_obj_free(obj);
  500. return res;
  501. }
  502. sol_object_t *sol_f_ast_print(sol_state_t *state, sol_object_t *args) {
  503. sol_object_t *obj = sol_list_get_index(state, args, 0);
  504. if(sol_is_aststmt(obj)) {
  505. st_print(state, obj->node);
  506. } else {
  507. ex_print(state, obj->node);
  508. }
  509. sol_obj_free(obj);
  510. return sol_incref(state->None);
  511. }
  512. sol_object_t *sol_f_singlet_tostring(sol_state_t *state, sol_object_t *args) {
  513. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = sol_new_string(state, obj->str);
  514. sol_obj_free(obj);
  515. return res;
  516. }
  517. sol_object_t *sol_f_int_add(sol_state_t *state, sol_object_t *args) {
  518. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
  519. sol_object_t *res = sol_new_int(state, a->ival + bint->ival);
  520. sol_obj_free(a);
  521. sol_obj_free(b);
  522. if(sol_has_error(state)) {
  523. sol_obj_free(res);
  524. return sol_incref(state->None);
  525. }
  526. return res;
  527. }
  528. sol_object_t *sol_f_int_sub(sol_state_t *state, sol_object_t *args) {
  529. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
  530. sol_object_t *res = sol_new_int(state, a->ival - bint->ival);
  531. sol_obj_free(a);
  532. sol_obj_free(b);
  533. if(sol_has_error(state)) {
  534. sol_obj_free(res);
  535. return sol_incref(state->None);
  536. }
  537. return res;
  538. }
  539. sol_object_t *sol_f_int_mul(sol_state_t *state, sol_object_t *args) {
  540. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
  541. sol_object_t *res = sol_new_int(state, a->ival * bint->ival);
  542. sol_obj_free(a);
  543. sol_obj_free(b);
  544. if(sol_has_error(state)) {
  545. sol_obj_free(res);
  546. return sol_incref(state->None);
  547. }
  548. return res;
  549. }
  550. sol_object_t *sol_f_int_div(sol_state_t *state, sol_object_t *args) {
  551. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
  552. sol_object_t *res = sol_new_int(state, a->ival / bint->ival);
  553. sol_obj_free(a);
  554. sol_obj_free(b);
  555. if(sol_has_error(state)) {
  556. sol_obj_free(res);
  557. return sol_incref(state->None);
  558. }
  559. return res;
  560. }
  561. sol_object_t *sol_f_int_mod(sol_state_t *state, sol_object_t *args) {
  562. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
  563. sol_object_t *res = sol_new_int(state, a->ival % bint->ival);
  564. sol_obj_free(a);
  565. sol_obj_free(b);
  566. if(sol_has_error(state)) {
  567. sol_obj_free(res);
  568. return sol_incref(state->None);
  569. }
  570. return res;
  571. }
  572. sol_object_t *sol_f_int_pow(sol_state_t *state, sol_object_t *args) {
  573. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
  574. sol_object_t *res = sol_new_int(state, (long) pow((double) a->ival, bint->ival));
  575. sol_obj_free(a);
  576. sol_obj_free(b);
  577. if(sol_has_error(state)) {
  578. sol_obj_free(res);
  579. return sol_incref(state->None);
  580. }
  581. return res;
  582. }
  583. sol_object_t *sol_f_int_band(sol_state_t *state, sol_object_t *args) {
  584. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
  585. sol_object_t *res = sol_new_int(state, a->ival & bint->ival);
  586. sol_obj_free(a);
  587. sol_obj_free(b);
  588. if(sol_has_error(state)) {
  589. sol_obj_free(res);
  590. return sol_incref(state->None);
  591. }
  592. return res;
  593. }
  594. sol_object_t *sol_f_int_bor(sol_state_t *state, sol_object_t *args) {
  595. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
  596. sol_object_t *res = sol_new_int(state, a->ival | bint->ival);
  597. sol_obj_free(a);
  598. sol_obj_free(b);
  599. if(sol_has_error(state)) {
  600. sol_obj_free(res);
  601. return sol_incref(state->None);
  602. }
  603. return res;
  604. }
  605. sol_object_t *sol_f_int_bxor(sol_state_t *state, sol_object_t *args) {
  606. 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);
  607. sol_object_t *res = sol_new_int(state, a->ival ^ bint->ival);
  608. sol_obj_free(a);
  609. sol_obj_free(b);
  610. if(sol_has_error(state)) {
  611. sol_obj_free(res);
  612. return sol_incref(state->None);
  613. }
  614. return res;
  615. }
  616. sol_object_t *sol_f_int_blsh(sol_state_t *state, sol_object_t *args) {
  617. 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);
  618. sol_object_t *res = sol_new_int(state, a->ival << bint->ival);
  619. sol_obj_free(a);
  620. sol_obj_free(b);
  621. if(sol_has_error(state)) {
  622. sol_obj_free(res);
  623. return sol_incref(state->None);
  624. }
  625. return res;
  626. }
  627. sol_object_t *sol_f_int_brsh(sol_state_t *state, sol_object_t *args) {
  628. 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);
  629. sol_object_t *res = sol_new_int(state, a->ival >> bint->ival);
  630. sol_obj_free(a);
  631. sol_obj_free(b);
  632. if(sol_has_error(state)) {
  633. sol_obj_free(res);
  634. return sol_incref(state->None);
  635. }
  636. return res;
  637. }
  638. sol_object_t *sol_f_int_bnot(sol_state_t *state, sol_object_t *args) {
  639. sol_object_t *a = sol_list_get_index(state, args, 0);
  640. sol_object_t *res = sol_new_int(state, ~a->ival);
  641. sol_obj_free(a);
  642. return res;
  643. }
  644. sol_object_t *sol_f_int_cmp(sol_state_t *state, sol_object_t *args) {
  645. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  646. sol_object_t *res;
  647. if(sol_is_int(b)) {
  648. res = sol_new_int(state, a->ival == b->ival ? 0 : (a->ival < b->ival ? -1 : 1));
  649. } else {
  650. res = sol_new_int(state, 1);
  651. }
  652. sol_obj_free(a);
  653. sol_obj_free(b);
  654. return res;
  655. }
  656. sol_object_t *sol_f_int_toint(sol_state_t *state, sol_object_t *args) {
  657. return sol_list_get_index(state, args, 0);
  658. }
  659. sol_object_t *sol_f_int_tofloat(sol_state_t *state, sol_object_t *args) {
  660. sol_object_t *a = sol_list_get_index(state, args, 0);
  661. sol_object_t *res = sol_new_float(state, (double) a->ival);
  662. sol_obj_free(a);
  663. return res;
  664. }
  665. sol_object_t *sol_f_int_tostring(sol_state_t *state, sol_object_t *args) {
  666. sol_object_t *a = sol_list_get_index(state, args, 0);
  667. char *s = _itoa(a->ival);
  668. sol_object_t *res = sol_new_string(state, s);
  669. sol_obj_free(a);
  670. free(s);
  671. return res;
  672. }
  673. sol_object_t *sol_f_float_add(sol_state_t *state, sol_object_t *args) {
  674. 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);
  675. sol_object_t *res = sol_new_float(state, a->fval + bflt->fval);
  676. sol_obj_free(a);
  677. sol_obj_free(b);
  678. if(sol_has_error(state)) {
  679. sol_obj_free(res);
  680. return sol_incref(state->None);
  681. }
  682. return res;
  683. }
  684. sol_object_t *sol_f_float_sub(sol_state_t *state, sol_object_t *args) {
  685. 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);
  686. sol_object_t *res = sol_new_float(state, a->fval - bflt->fval);
  687. sol_obj_free(a);
  688. sol_obj_free(b);
  689. if(sol_has_error(state)) {
  690. sol_obj_free(res);
  691. return sol_incref(state->None);
  692. }
  693. return res;
  694. }
  695. sol_object_t *sol_f_float_mul(sol_state_t *state, sol_object_t *args) {
  696. 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);
  697. sol_object_t *res = sol_new_float(state, a->fval * bflt->fval);
  698. sol_obj_free(a);
  699. sol_obj_free(b);
  700. if(sol_has_error(state)) {
  701. sol_obj_free(res);
  702. return sol_incref(state->None);
  703. }
  704. return res;
  705. }
  706. sol_object_t *sol_f_float_div(sol_state_t *state, sol_object_t *args) {
  707. 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);
  708. sol_object_t *res = sol_new_float(state, a->fval / bflt->fval);
  709. sol_obj_free(a);
  710. sol_obj_free(b);
  711. if(sol_has_error(state)) {
  712. sol_obj_free(res);
  713. return sol_incref(state->None);
  714. }
  715. return res;
  716. }
  717. sol_object_t *sol_f_float_pow(sol_state_t *state, sol_object_t *args) {
  718. 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);
  719. sol_object_t *res = sol_new_float(state, pow(a->fval, bflt->fval));
  720. sol_obj_free(a);
  721. sol_obj_free(b);
  722. if(sol_has_error(state)) {
  723. sol_obj_free(res);
  724. return sol_incref(state->None);
  725. }
  726. return res;
  727. }
  728. sol_object_t *sol_f_float_cmp(sol_state_t *state, sol_object_t *args) {
  729. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  730. sol_object_t *res;
  731. if(sol_is_float(b)) {
  732. res = sol_new_int(state, a->fval==b->fval? 0 : (a->fval<b->fval? -1 : 1));
  733. } else {
  734. res = sol_new_int(state, 1);
  735. }
  736. sol_obj_free(a);
  737. sol_obj_free(b);
  738. return res;
  739. }
  740. sol_object_t *sol_f_float_toint(sol_state_t *state, sol_object_t *args) {
  741. sol_object_t *a = sol_list_get_index(state, args, 0);
  742. sol_object_t *res = sol_new_int(state, (int) a->fval);
  743. sol_obj_free(a);
  744. return res;
  745. }
  746. sol_object_t *sol_f_float_tofloat(sol_state_t *state, sol_object_t *args) {
  747. return sol_list_get_index(state, args, 0);
  748. }
  749. sol_object_t *sol_f_float_tostring(sol_state_t *state, sol_object_t *args) {
  750. sol_object_t *a = sol_list_get_index(state, args, 0);
  751. char *s = _ftoa(a->fval);
  752. sol_object_t *res = sol_new_string(state, s);
  753. sol_obj_free(a);
  754. free(s);
  755. return res;
  756. }
  757. sol_object_t *sol_f_str_add(sol_state_t *state, sol_object_t *args) {
  758. 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);
  759. sol_object_t *res = sol_string_concat(state, a, bstr);
  760. sol_obj_free(a);
  761. sol_obj_free(b);
  762. if(sol_has_error(state)) {
  763. sol_obj_free(res);
  764. return sol_incref(state->None);
  765. }
  766. return res;
  767. }
  768. sol_object_t *sol_f_str_mul(sol_state_t *state, sol_object_t *args) {
  769. 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);
  770. int n = strlen(a->str) * bint->ival + 1;
  771. char *s = malloc(n);
  772. int i;
  773. s[0] = '\0';
  774. for(i = 0; i < b->ival; i++) {
  775. strncat(s, a->str, n);
  776. }
  777. sol_object_t *res = sol_new_string(state, s);
  778. sol_obj_free(a);
  779. sol_obj_free(b);
  780. sol_obj_free(bint);
  781. free(s);
  782. if(sol_has_error(state)) {
  783. sol_obj_free(res);
  784. return sol_incref(state->None);
  785. }
  786. return res;
  787. }
  788. sol_object_t *sol_f_str_cmp(sol_state_t *state, sol_object_t *args) {
  789. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  790. sol_object_t *res;
  791. if(sol_is_string(b)) {
  792. res = sol_new_int(state, strcmp(a->str, b->str));
  793. } else {
  794. res = sol_new_int(state, 1);
  795. }
  796. sol_obj_free(a);
  797. sol_obj_free(b);
  798. return res;
  799. }
  800. sol_object_t *sol_f_str_len(sol_state_t *state, sol_object_t *args) {
  801. sol_object_t *a = sol_list_get_index(state, args, 0);
  802. sol_object_t *res = sol_new_int(state, strlen(a->str));
  803. sol_obj_free(a);
  804. return res;
  805. }
  806. sol_object_t *sol_f_str_index(sol_state_t *state, sol_object_t *args) {
  807. sol_object_t *str = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *idx, *funcs, *res;
  808. char buf[2] = {0, 0};
  809. if(sol_is_string(key)) {
  810. funcs = sol_get_methods_name(state, "string");
  811. res = sol_map_get(state, funcs, key);
  812. sol_obj_free(funcs);
  813. return res;
  814. }
  815. idx = sol_cast_int(state, key);
  816. if(idx->ival >= 0 && idx->ival < strlen(str->str)) {
  817. buf[0] = str->str[idx->ival];
  818. }
  819. sol_obj_free(str);
  820. sol_obj_free(key);
  821. sol_obj_free(idx);
  822. return sol_new_string(state, buf);
  823. }
  824. sol_object_t *sol_f_str_iter(sol_state_t *state, sol_object_t *args) {
  825. return sol_new_cfunc(state, sol_f_iter_str);
  826. }
  827. sol_object_t *sol_f_str_toint(sol_state_t *state, sol_object_t *args) {
  828. sol_object_t *a = sol_list_get_index(state, args, 0);
  829. sol_object_t *res = sol_new_int(state, atoi(a->str));
  830. sol_obj_free(a);
  831. return res;
  832. }
  833. sol_object_t *sol_f_str_tofloat(sol_state_t *state, sol_object_t *args) {
  834. sol_object_t *a = sol_list_get_index(state, args, 0);
  835. sol_object_t *res = sol_new_float(state, atof(a->str));
  836. sol_obj_free(a);
  837. return res;
  838. }
  839. sol_object_t *sol_f_str_tostring(sol_state_t *state, sol_object_t *args) {
  840. return sol_list_get_index(state, args, 0);
  841. }
  842. sol_object_t *sol_f_str_repr(sol_state_t *state, sol_object_t *args) {
  843. sol_object_t *obj = sol_list_get_index(state, args, 0), *cur = sol_new_string(state, "\""), *next = sol_string_concat(state, cur, obj);
  844. sol_obj_free(cur);
  845. cur = next;
  846. next = sol_string_concat_cstr(state, cur, "\"");
  847. sol_obj_free(cur);
  848. return next;
  849. }
  850. sol_object_t *sol_f_str_sub(sol_state_t *state, sol_object_t *args) {
  851. 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);
  852. sol_object_t *ilow, *ihigh;
  853. long l, h;
  854. size_t len = strlen(str->str), i;
  855. char *s;
  856. if(sol_is_none(state, low)) {
  857. ilow = sol_new_int(state, 0);
  858. } else {
  859. ilow = sol_cast_int(state, low);
  860. }
  861. if(sol_is_none(state, high)) {
  862. ihigh = sol_new_int(state, len);
  863. } else {
  864. ihigh = sol_cast_int(state, high);
  865. }
  866. sol_obj_free(low);
  867. sol_obj_free(high);
  868. l = ilow->ival;
  869. h = ihigh->ival;
  870. sol_obj_free(ihigh);
  871. sol_obj_free(ilow);
  872. if(l < 0) {
  873. l += len;
  874. if(l < 0) {
  875. l = 0;
  876. }
  877. }
  878. if(l > len) {
  879. l = len;
  880. }
  881. if(h < 0) {
  882. h += len;
  883. if(h < 0) {
  884. h = 0;
  885. }
  886. }
  887. if(h > len) {
  888. h = len;
  889. }
  890. if(l >= h) {
  891. sol_obj_free(str);
  892. return sol_new_string(state, "");
  893. }
  894. s = malloc(h - l + 1);
  895. for(i = l; i < h; i++) {
  896. s[i - l] = str->str[i];
  897. }
  898. s[h - l] = '\0';
  899. sol_obj_free(str);
  900. return sol_new_string(state, s);
  901. }
  902. sol_object_t *sol_f_str_split(sol_state_t *state, sol_object_t *args) {
  903. 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);
  904. sol_object_t *res = sol_new_list(state), *opart;
  905. char *s = strdup(str->str);
  906. char *part = strtok(s, stok->str);
  907. sol_obj_free(tok);
  908. sol_obj_free(str);
  909. if(!part) {
  910. sol_obj_free(res);
  911. sol_obj_free(stok);
  912. return sol_incref(state->None);
  913. }
  914. opart = sol_new_string(state, part);
  915. sol_list_insert(state, res, 0, opart);
  916. sol_obj_free(opart);
  917. while(part = strtok(NULL, stok->str)) {
  918. opart = sol_new_string(state, part);
  919. sol_list_insert(state, res, sol_list_len(state, res), opart);
  920. sol_obj_free(opart);
  921. }
  922. sol_obj_free(stok);
  923. return res;
  924. }
  925. sol_object_t *sol_f_str_find(sol_state_t *state, sol_object_t *args) {
  926. 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);
  927. char *ptr = strstr(str->str, ssubstr->str);
  928. sol_object_t *res = sol_new_int(state, ptr ? ptr - str->str : -1);
  929. sol_obj_free(str);
  930. sol_obj_free(substr);
  931. sol_obj_free(ssubstr);
  932. return res;
  933. }
  934. sol_object_t *sol_f_list_add(sol_state_t *state, sol_object_t *args) {
  935. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *ls;
  936. if(!sol_is_list(b)) {
  937. sol_obj_free(a);
  938. sol_obj_free(b);
  939. return sol_set_error_string(state, "Adding list to non-list");
  940. }
  941. ls = sol_list_copy(state, a);
  942. sol_list_append(state, ls, b);
  943. sol_obj_free(a);
  944. sol_obj_free(b);
  945. return ls;
  946. }
  947. sol_object_t *sol_f_list_mul(sol_state_t *state, sol_object_t *args) {
  948. 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;
  949. int i;
  950. if(sol_has_error(state)) {
  951. sol_obj_free(a);
  952. sol_obj_free(b);
  953. sol_obj_free(bint);
  954. return sol_incref(state->None);
  955. }
  956. ls = sol_new_list(state);
  957. for(i = 0; i < b->ival; i++) {
  958. sol_list_append(state, ls, a);
  959. if(sol_has_error(state)) {
  960. sol_obj_free(a);
  961. sol_obj_free(b);
  962. sol_obj_free(bint);
  963. return sol_incref(state->None);
  964. }
  965. }
  966. return ls;
  967. }
  968. sol_object_t *sol_f_list_index(sol_state_t *state, sol_object_t *args) {
  969. sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *ival;
  970. sol_object_t *res, *funcs;
  971. if(sol_is_string(b)) {
  972. funcs = sol_get_methods_name(state, "list");
  973. res = sol_map_get(state, funcs, b);
  974. sol_obj_free(funcs);
  975. } else {
  976. ival = sol_cast_int(state, b);
  977. res = sol_list_get_index(state, ls, ival->ival);
  978. sol_obj_free(ival);
  979. }
  980. sol_obj_free(ls);
  981. sol_obj_free(b);
  982. return res;
  983. }
  984. sol_object_t *sol_f_list_setindex(sol_state_t *state, sol_object_t *args) {
  985. 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);
  986. sol_object_t *val = sol_list_get_index(state, args, 2);
  987. sol_list_set_index(state, ls, bint->ival, val);
  988. sol_obj_free(ls);
  989. sol_obj_free(b);
  990. sol_obj_free(bint);
  991. sol_obj_free(val);
  992. return sol_incref(state->None);
  993. }
  994. sol_object_t *sol_f_list_len(sol_state_t *state, sol_object_t *args) {
  995. sol_object_t *ls = sol_list_get_index(state, args, 0);
  996. sol_object_t *res = sol_new_int(state, sol_list_len(state, ls));
  997. sol_obj_free(ls);
  998. return res;
  999. }
  1000. sol_object_t *sol_f_list_iter(sol_state_t *state, sol_object_t *args) {
  1001. return sol_new_cfunc(state, sol_f_iter_list);
  1002. }
  1003. sol_object_t *sol_f_list_tostring(sol_state_t *state, sol_object_t *args) {
  1004. sol_object_t *cur = sol_new_string(state, "["), *next, *str, *obj = sol_list_get_index(state, args, 0), *item;
  1005. dsl_seq_iter *iter = dsl_new_seq_iter(obj->seq);
  1006. char s[64];
  1007. while(!dsl_seq_iter_is_invalid(iter)) {
  1008. item = AS_OBJ(dsl_seq_iter_at(iter));
  1009. if(test_seen(item)) {
  1010. snprintf(s, 64, "... (%p)", item);
  1011. next = sol_string_concat_cstr(state, cur, s);
  1012. } else {
  1013. str = sol_cast_repr(state, item);
  1014. next = sol_string_concat(state, cur, str);
  1015. sol_obj_free(str);
  1016. }
  1017. sol_obj_free(cur);
  1018. cur = next;
  1019. if(!dsl_seq_iter_at_end(iter)) {
  1020. next = sol_string_concat_cstr(state, cur, ", ");
  1021. sol_obj_free(cur);
  1022. cur = next;
  1023. }
  1024. dsl_seq_iter_next(iter);
  1025. }
  1026. next = sol_string_concat_cstr(state, cur, "]");
  1027. sol_obj_free(cur);
  1028. dsl_free_seq_iter(iter);
  1029. return next;
  1030. }
  1031. sol_object_t *sol_f_list_copy(sol_state_t *state, sol_object_t *args) {
  1032. sol_object_t *list = sol_list_get_index(state, args, 0);
  1033. sol_object_t *res = sol_list_copy(state, list);
  1034. sol_obj_free(list);
  1035. return res;
  1036. }
  1037. sol_object_t *sol_f_list_insert(sol_state_t *state, sol_object_t *args) {
  1038. 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);
  1039. sol_list_insert(state, list, iidx->ival, obj);
  1040. sol_obj_free(list);
  1041. sol_obj_free(idx);
  1042. sol_obj_free(iidx);
  1043. sol_obj_free(obj);
  1044. return sol_incref(state->None);
  1045. }
  1046. sol_object_t *sol_f_list_remove(sol_state_t *state, sol_object_t *args) {
  1047. 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);
  1048. sol_object_t *res = sol_list_remove(state, list, iidx->ival);
  1049. sol_obj_free(list);
  1050. sol_obj_free(idx);
  1051. sol_obj_free(iidx);
  1052. return res;
  1053. }
  1054. sol_object_t *sol_f_list_truncate(sol_state_t *state, sol_object_t *args) {
  1055. 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);
  1056. sol_object_t *res = sol_list_truncate(state, list, iidx->ival);
  1057. sol_obj_free(list);
  1058. sol_obj_free(idx);
  1059. sol_obj_free(iidx);
  1060. return res;
  1061. }
  1062. sol_object_t *sol_f_list_map(sol_state_t *state, sol_object_t *args) {
  1063. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  1064. sol_object_t *fargs = sol_new_list(state), *item;
  1065. int idx = 0, len = sol_list_len(state, list);
  1066. sol_list_insert(state, fargs, 0, func);
  1067. while(idx < len) {
  1068. item = sol_list_get_index(state, list, idx);
  1069. sol_list_insert(state, fargs, 1, item);
  1070. sol_obj_free(item);
  1071. item = CALL_METHOD(state, func, call, fargs);
  1072. if(sol_has_error(state)) {
  1073. return list;
  1074. }
  1075. sol_list_remove(state, fargs, 1);
  1076. sol_list_set_index(state, list, idx, item);
  1077. sol_obj_free(item);
  1078. idx++;
  1079. }
  1080. sol_obj_free(fargs);
  1081. sol_obj_free(func);
  1082. return list;
  1083. }
  1084. sol_object_t *sol_f_list_filter(sol_state_t *state, sol_object_t *args) {
  1085. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  1086. sol_object_t *fargs = sol_new_list(state), *item, *ival;
  1087. int idx = 0, len = sol_list_len(state, list);
  1088. sol_list_insert(state, fargs, 0, func);
  1089. while(idx < len) {
  1090. item = sol_list_get_index(state, list, idx);
  1091. sol_list_insert(state, fargs, 1, item);
  1092. sol_obj_free(item);
  1093. item = CALL_METHOD(state, func, call, fargs);
  1094. if(sol_has_error(state)) {
  1095. return list;
  1096. }
  1097. ival = sol_cast_int(state, item);
  1098. if(ival->ival) {
  1099. idx++;
  1100. } else {
  1101. sol_list_remove(state, list, idx);
  1102. len--;
  1103. }
  1104. sol_obj_free(item);
  1105. sol_obj_free(ival);
  1106. }
  1107. sol_obj_free(fargs);
  1108. sol_obj_free(func);
  1109. return list;
  1110. }
  1111. sol_object_t *sol_f_map_add(sol_state_t *state, sol_object_t *args) {
  1112. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *map;
  1113. if(!sol_is_map(b)) {
  1114. sol_obj_free(a);
  1115. sol_obj_free(b);
  1116. return sol_set_error_string(state, "Adding map to non-map");
  1117. }
  1118. map = sol_map_copy(state, a);
  1119. sol_map_merge(state, map, b);
  1120. sol_obj_free(a);
  1121. sol_obj_free(b);
  1122. return map;
  1123. }
  1124. sol_object_t *sol_f_map_index(sol_state_t *state, sol_object_t *args) {
  1125. sol_object_t *map = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  1126. sol_object_t *indexf = sol_map_get_name(state, map, "__index");
  1127. sol_object_t *res = NULL, *newls;
  1128. res = sol_map_get(state, map, b);