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.

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