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.

2183 lines
67 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. return sol_set_error_string(state, "Undefined method");
  25. }
  26. sol_object_t *sol_f_default_cmp(sol_state_t *state, sol_object_t *args) {
  27. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  28. sol_object_t *res = sol_new_int(state, a!=b);
  29. sol_obj_free(a);
  30. sol_obj_free(b);
  31. return res;
  32. }
  33. sol_object_t *sol_f_default_tostring(sol_state_t *state, sol_object_t *args) {
  34. sol_object_t *obj = sol_list_get_index(state, args, 0);
  35. char s[64];
  36. snprintf(s, 64, "<%s object at %p>", obj->ops->tname, obj);
  37. sol_obj_free(obj);
  38. return sol_new_string(state, s);
  39. }
  40. sol_object_t *sol_f_default_repr(sol_state_t *state, sol_object_t *args) {
  41. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = obj->ops->tostring(state, args);
  42. sol_obj_free(obj);
  43. return res;
  44. }
  45. sol_object_t *sol_f_no_op(sol_state_t *state, sol_object_t *args) {
  46. if(state) return sol_incref(state->None);
  47. return NULL;
  48. }
  49. sol_object_t *sol_f_toint(sol_state_t *state, sol_object_t *args) {
  50. sol_object_t *obj = sol_list_get_index(state, args, 0);
  51. sol_object_t *res = obj->ops->toint(state, args);
  52. sol_obj_free(obj);
  53. return res;
  54. }
  55. sol_object_t *sol_f_tofloat(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 = obj->ops->tofloat(state, args);
  58. sol_obj_free(obj);
  59. return res;
  60. }
  61. sol_object_t *sol_f_tostring(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 = obj->ops->tostring(state, args);
  64. sol_obj_free(obj);
  65. return res;
  66. }
  67. sol_object_t *sol_f_try(sol_state_t *state, sol_object_t *args) {
  68. sol_object_t *func = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  69. sol_object_t *ls = sol_new_list(state), *one = sol_new_int(state, 1);
  70. sol_object_t *res;
  71. sol_list_insert(state, fargs, 0, func);
  72. res = func->ops->call(state, fargs);
  73. sol_obj_free(func);
  74. sol_obj_free(fargs);
  75. if(sol_has_error(state)) {
  76. sol_object_t *err = sol_get_error(state);
  77. sol_clear_error(state);
  78. sol_object_t *zero = sol_new_int(state, 0);
  79. sol_obj_free(res);
  80. sol_obj_free(one);
  81. sol_list_insert(state, ls, 0, err);
  82. sol_obj_free(err);
  83. sol_list_insert(state, ls, 0, zero);
  84. sol_obj_free(zero);
  85. return ls;
  86. }
  87. sol_list_insert(state, ls, 0, res);
  88. sol_obj_free(res);
  89. sol_list_insert(state, ls, 0, one);
  90. sol_obj_free(one);
  91. return ls;
  92. }
  93. sol_object_t *sol_f_error(sol_state_t *state, sol_object_t *args) {
  94. sol_object_t *arg = sol_list_get_index(state, args, 0), *res;
  95. res = sol_set_error(state, arg);
  96. sol_obj_free(arg);
  97. return res;
  98. }
  99. sol_object_t *sol_f_type(sol_state_t *state, sol_object_t *args) {
  100. sol_object_t *obj = sol_list_get_index(state, args, 0);
  101. sol_object_t *res = sol_new_string(state, obj->ops->tname);
  102. sol_obj_free(obj);
  103. return res;
  104. }
  105. static dsl_seq *seen=NULL;
  106. int test_seen(sol_object_t *obj) {
  107. dsl_seq_iter *iter;
  108. if(seen) {
  109. iter = dsl_new_seq_iter(seen);
  110. while(!dsl_seq_iter_is_invalid(iter)) {
  111. if(dsl_seq_iter_at(iter) == obj) {
  112. return 1;
  113. }
  114. dsl_seq_iter_next(iter);
  115. }
  116. dsl_free_seq_iter(iter);
  117. dsl_seq_insert(seen, dsl_seq_len(seen), obj);
  118. }
  119. return 0;
  120. }
  121. void ob_print(sol_object_t *obj) {
  122. sol_object_t *cur;
  123. dsl_seq_iter *iter;
  124. int i;
  125. if(test_seen(obj)) {
  126. return;
  127. }
  128. switch(obj->type) {
  129. case SOL_SINGLET:
  130. printf("%s", obj->str);
  131. break;
  132. case SOL_INTEGER:
  133. printf("%ld", obj->ival);
  134. break;
  135. case SOL_FLOAT:
  136. printf("%f", obj->fval);
  137. break;
  138. case SOL_STRING:
  139. printf("\"%s\"", obj->str);
  140. break;
  141. case SOL_LIST:
  142. printf("[");
  143. iter = dsl_new_seq_iter(obj->seq);
  144. while(!dsl_seq_iter_is_invalid(iter)) {
  145. ob_print(dsl_seq_iter_at(iter));
  146. printf(", ");
  147. dsl_seq_iter_next(iter);
  148. }
  149. dsl_free_seq_iter(iter);
  150. printf("]");
  151. break;
  152. case SOL_MCELL:
  153. printf("<<");
  154. ob_print(obj->key);
  155. printf("=");
  156. ob_print(obj->val);
  157. printf(">>");
  158. case SOL_MAP:
  159. printf("{");
  160. iter = dsl_new_seq_iter(obj->seq);
  161. while(!dsl_seq_iter_is_invalid(iter)) {
  162. printf("[");
  163. ob_print(AS_OBJ(dsl_seq_iter_at(iter))->key);
  164. printf("] = ");
  165. ob_print(AS_OBJ(dsl_seq_iter_at(iter))->val);
  166. printf(", ");
  167. dsl_seq_iter_next(iter);
  168. }
  169. dsl_free_seq_iter(iter);
  170. printf("}");
  171. break;
  172. case SOL_FUNCTION:
  173. if(obj->fname) {
  174. printf("<Function %s>", obj->fname);
  175. } else {
  176. printf("<Function>");
  177. }
  178. break;
  179. case SOL_CFUNCTION:
  180. printf("<CFunction>");
  181. break;
  182. case SOL_STMT:
  183. st_print(obj->node);
  184. break;
  185. case SOL_EXPR:
  186. ex_print(obj->node);
  187. break;
  188. case SOL_BUFFER:
  189. if(obj->sz == -1) {
  190. printf("<Buffer @%p>", obj->buffer);
  191. } else {
  192. printf("<Buffer @%p size %ld>", obj->buffer, obj->sz);
  193. }
  194. break;
  195. case SOL_CDATA:
  196. printf("<CData>");
  197. break;
  198. /*default:
  199. cur = sol_cast_string(state, obj);
  200. printf("%s", cur->str);
  201. sol_obj_free(cur);*/
  202. }
  203. }
  204. sol_object_t *sol_f_prepr(sol_state_t *state, sol_object_t *args) {
  205. int i, sz = sol_list_len(state, args);
  206. sol_object_t *obj;
  207. seen = dsl_seq_new_array(NULL, NULL);
  208. for(i=0; i<sz; i++) {
  209. obj = sol_list_get_index(state, args, i);
  210. ob_print(obj);
  211. printf(" ");
  212. sol_obj_free(obj);
  213. }
  214. printf("\n");
  215. dsl_free_seq(seen);
  216. seen = NULL;
  217. return sol_incref(state->None);
  218. }
  219. sol_object_t *sol_f_print(sol_state_t *state, sol_object_t *args) {
  220. int i, sz = sol_list_len(state, args);
  221. sol_object_t *obj, *str;
  222. seen = dsl_seq_new_array(NULL, NULL);
  223. for(i=0; i<sz; i++) {
  224. obj = sol_list_get_index(state, args, i);
  225. str = sol_cast_string(state, obj);
  226. sol_printf(state, "%s", str->str);
  227. sol_printf(state, " ");
  228. sol_obj_free(obj);
  229. sol_obj_free(str);
  230. }
  231. sol_printf(state, "\n");
  232. dsl_free_seq(seen);
  233. seen = NULL;
  234. return sol_incref(state->None);
  235. }
  236. sol_object_t *sol_f_rawget(sol_state_t *state, sol_object_t *args) {
  237. sol_object_t *obj = sol_list_get_index(state, args, 0), *key, *res;
  238. if(!sol_is_map(obj)) return sol_set_error_string(state, "Rawset of non-map");
  239. key = sol_list_get_index(state, args, 1);
  240. res = sol_map_get(state, obj, key);
  241. sol_obj_free(key);
  242. sol_obj_free(obj);
  243. return res;
  244. }
  245. sol_object_t *sol_f_rawset(sol_state_t *state, sol_object_t *args) {
  246. sol_object_t *obj = sol_list_get_index(state, args, 0), *key, *val;
  247. if(!sol_is_map(obj)) return sol_set_error_string(state, "Rawset of non-map");
  248. key = sol_list_get_index(state, args, 1);
  249. val = sol_list_get_index(state, args, 2);
  250. sol_map_set(state, obj, key, val);
  251. sol_obj_free(val);
  252. sol_obj_free(key);
  253. sol_obj_free(obj);
  254. return sol_incref(state->None);
  255. }
  256. sol_object_t *sol_f_range(sol_state_t *state, sol_object_t *args) {
  257. sol_object_t *res = sol_new_list(state), *bound = sol_cast_int(state, sol_list_get_index(state, args, 0));
  258. int i;
  259. for(i=0; i<bound->ival; i++) {
  260. sol_list_insert(state, res, sol_list_len(state, res), sol_new_int(state, i));
  261. }
  262. sol_obj_free(bound);
  263. return res;
  264. }
  265. sol_object_t *sol_f_exec(sol_state_t *state, sol_object_t *args) {
  266. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  267. stmt_node *program;
  268. program = sol_compile(prgstr->str);
  269. if(!program) {
  270. return sol_set_error_string(state, "Compilation failure");
  271. }
  272. // XXX should st_free(program);
  273. sol_exec(state, program);
  274. return sol_incref(state->None);
  275. }
  276. sol_object_t *sol_f_eval(sol_state_t *state, sol_object_t *args) {
  277. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  278. stmt_node *program;
  279. program = sol_compile(prgstr->str);
  280. if(!program) {
  281. return sol_set_error_string(state, "Compilation failure");
  282. }
  283. if(program->type != ST_LIST || program->stmtlist->stmt->type != ST_EXPR) {
  284. return sol_set_error_string(state, "Not an expression");
  285. }
  286. // XXX should st_free(program);
  287. return sol_eval(state, program->stmtlist->stmt->expr);
  288. }
  289. sol_object_t *sol_f_execfile(sol_state_t *state, sol_object_t *args) {
  290. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  291. stmt_node *program;
  292. FILE *f = fopen(prgstr->str, "r");
  293. char *s;
  294. long sz;
  295. if(!f) {
  296. return sol_set_error_string(state, "File open failure");
  297. }
  298. fseek(f, 0, SEEK_END);
  299. sz = ftell(f);
  300. fseek(f, 0, SEEK_SET);
  301. s = malloc(sz+1);
  302. if(!s) {
  303. fclose(f);
  304. return sol_set_error_string(state, "File memory allocation failure");
  305. }
  306. fread(s, 1, sz, f);
  307. s[sz]=0;
  308. fclose(f);
  309. program = sol_compile(s);
  310. free(s);
  311. if(!program) {
  312. return sol_set_error_string(state, "Compilation failure");
  313. }
  314. // XXX should st_free(program);
  315. sol_exec(state, program);
  316. return sol_incref(state->None);
  317. }
  318. sol_object_t *sol_f_parse(sol_state_t *state, sol_object_t *args) {
  319. sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
  320. stmt_node *program = sol_compile(prgstr->str);
  321. sol_obj_free(prg);
  322. sol_obj_free(prgstr);
  323. if(!program) {
  324. return sol_set_error_string(state, "Compilation failure");
  325. }
  326. return sol_new_stmtnode(state, program);
  327. }
  328. sol_object_t *sol_f_debug_getref(sol_state_t *state, sol_object_t *args) {
  329. sol_object_t *obj = sol_list_get_index(state, args, 0);
  330. 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.
  331. sol_obj_free(obj);
  332. return res;
  333. }
  334. sol_object_t *sol_f_debug_setref(sol_state_t *state, sol_object_t *args) {
  335. sol_object_t *obj = sol_list_get_index(state, args, 0), *cnt = sol_list_get_index(state, args, 1);
  336. obj->refcnt = sol_cast_int(state, cnt)->ival + 2; // NB: As above.
  337. sol_obj_free(cnt);
  338. sol_obj_free(obj);
  339. return sol_incref(state->None);
  340. }
  341. sol_object_t *sol_f_debug_closure(sol_state_t *state, sol_object_t *args) {
  342. sol_object_t *func = sol_list_get_index(state, args, 0);
  343. sol_object_t *res = sol_incref(func->closure);
  344. sol_obj_free(func);
  345. return res;
  346. }
  347. sol_object_t *sol_f_debug_globals(sol_state_t *state, sol_object_t *args) {
  348. return sol_list_get_index(state, state->scopes, sol_list_len(state, state->scopes)-1);
  349. }
  350. sol_object_t *sol_f_debug_locals(sol_state_t *state, sol_object_t *args) {
  351. return sol_list_get_index(state, state->scopes, 0);
  352. }
  353. sol_object_t *sol_f_debug_scopes(sol_state_t *state, sol_object_t *args) {
  354. return sol_incref(state->scopes);
  355. }
  356. sol_object_t *sol_f_iter_str(sol_state_t *state, sol_object_t *args) {
  357. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  358. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  359. char temp[2] = {0, 0};
  360. if(sol_is_none(state, index)) {
  361. sol_obj_free(index);
  362. index = sol_new_int(state, 0);
  363. sol_map_set_name(state, local, "idx", index);
  364. }
  365. if(index->ival >= strlen(obj->str)) {
  366. sol_obj_free(index);
  367. sol_obj_free(obj);
  368. sol_obj_free(local);
  369. return sol_incref(state->StopIteration);
  370. }
  371. temp[0] = obj->str[index->ival];
  372. res = sol_new_string(state, temp);
  373. index->ival++;
  374. sol_obj_free(index);
  375. sol_obj_free(local);
  376. sol_obj_free(obj);
  377. return res;
  378. }
  379. sol_object_t *sol_f_iter_list(sol_state_t *state, sol_object_t *args) {
  380. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  381. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  382. if(sol_is_none(state, index)) {
  383. sol_obj_free(index);
  384. index = sol_new_int(state, 0);
  385. sol_map_set_name(state, local, "idx", index);
  386. sol_obj_free(index);
  387. }
  388. if(index->ival >= sol_list_len(state, obj)) {
  389. sol_obj_free(index);
  390. sol_obj_free(obj);
  391. sol_obj_free(local);
  392. return sol_incref(state->StopIteration);
  393. }
  394. res = sol_list_get_index(state, obj, index->ival);
  395. index->ival++;
  396. sol_obj_free(local);
  397. sol_obj_free(obj);
  398. return res;
  399. }
  400. sol_object_t *sol_f_iter_map(sol_state_t *state, sol_object_t *args) {
  401. sol_object_t *obj = sol_list_get_index(state, args, 0), *local = sol_list_get_index(state, args, 1);
  402. sol_object_t *index = sol_map_get_name(state, local, "idx"), *res;
  403. if(sol_is_none(state, index)) {
  404. sol_obj_free(index);
  405. index = sol_new_int(state, 0);
  406. sol_map_set_name(state, local, "idx", index);
  407. sol_obj_free(index);
  408. }
  409. if(index->ival >= dsl_seq_len(obj->seq)) {
  410. sol_obj_free(index);
  411. sol_obj_free(obj);
  412. sol_obj_free(local);
  413. return sol_incref(state->StopIteration);
  414. }
  415. res = sol_incref(AS_OBJ(dsl_seq_get(obj->seq, index->ival))->key);
  416. index->ival++;
  417. sol_obj_free(local);
  418. sol_obj_free(obj);
  419. return res;
  420. }
  421. sol_object_t *sol_f_singlet_tostring(sol_state_t *state, sol_object_t *args) {
  422. sol_object_t *obj = sol_list_get_index(state, args, 0), *res = sol_new_string(state, obj->str);
  423. sol_obj_free(obj);
  424. return res;
  425. }
  426. sol_object_t *sol_f_int_add(sol_state_t *state, sol_object_t *args) {
  427. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  428. sol_object_t *res = sol_new_int(state, a->ival + b->ival);
  429. sol_obj_free(a);
  430. sol_obj_free(b);
  431. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  432. return res;
  433. }
  434. sol_object_t *sol_f_int_sub(sol_state_t *state, sol_object_t *args) {
  435. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  436. sol_object_t *res = sol_new_int(state, a->ival - b->ival);
  437. sol_obj_free(a);
  438. sol_obj_free(b);
  439. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  440. return res;
  441. }
  442. sol_object_t *sol_f_int_mul(sol_state_t *state, sol_object_t *args) {
  443. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  444. sol_object_t *res = sol_new_int(state, a->ival * b->ival);
  445. sol_obj_free(a);
  446. sol_obj_free(b);
  447. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  448. return res;
  449. }
  450. sol_object_t *sol_f_int_div(sol_state_t *state, sol_object_t *args) {
  451. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  452. sol_object_t *res = sol_new_int(state, a->ival / b->ival);
  453. sol_obj_free(a);
  454. sol_obj_free(b);
  455. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  456. return res;
  457. }
  458. sol_object_t *sol_f_int_mod(sol_state_t *state, sol_object_t *args) {
  459. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  460. sol_object_t *res = sol_new_int(state, a->ival % b->ival);
  461. sol_obj_free(a);
  462. sol_obj_free(b);
  463. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  464. return res;
  465. }
  466. sol_object_t *sol_f_int_pow(sol_state_t *state, sol_object_t *args) {
  467. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  468. sol_object_t *res = sol_new_int(state, (long) pow((double) a->ival, b->ival));
  469. sol_obj_free(a);
  470. sol_obj_free(b);
  471. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  472. return res;
  473. }
  474. sol_object_t *sol_f_int_band(sol_state_t *state, sol_object_t *args) {
  475. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  476. sol_object_t *res = sol_new_int(state, a->ival & b->ival);
  477. sol_obj_free(a);
  478. sol_obj_free(b);
  479. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  480. return res;
  481. }
  482. sol_object_t *sol_f_int_bor(sol_state_t *state, sol_object_t *args) {
  483. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  484. sol_object_t *res = sol_new_int(state, a->ival | b->ival);
  485. sol_obj_free(a);
  486. sol_obj_free(b);
  487. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  488. return res;
  489. }
  490. sol_object_t *sol_f_int_bxor(sol_state_t *state, sol_object_t *args) {
  491. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  492. sol_object_t *res = sol_new_int(state, a->ival ^ b->ival);
  493. sol_obj_free(a);
  494. sol_obj_free(b);
  495. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  496. return res;
  497. }
  498. sol_object_t *sol_f_int_blsh(sol_state_t *state, sol_object_t *args) {
  499. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  500. sol_object_t *res = sol_new_int(state, a->ival << b->ival);
  501. sol_obj_free(a);
  502. sol_obj_free(b);
  503. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  504. return res;
  505. }
  506. sol_object_t *sol_f_int_brsh(sol_state_t *state, sol_object_t *args) {
  507. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  508. sol_object_t *res = sol_new_int(state, a->ival >> b->ival);
  509. sol_obj_free(a);
  510. sol_obj_free(b);
  511. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  512. return res;
  513. }
  514. sol_object_t *sol_f_int_bnot(sol_state_t *state, sol_object_t *args) {
  515. sol_object_t *a = sol_list_get_index(state, args, 0);
  516. sol_object_t *res = sol_new_int(state, ~a->ival);
  517. sol_obj_free(a);
  518. return res;
  519. }
  520. sol_object_t *sol_f_int_cmp(sol_state_t *state, sol_object_t *args) {
  521. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  522. sol_object_t *res = sol_new_int(state, a->ival==b->ival? 0 : (a->ival<b->ival? -1 : 1));
  523. sol_obj_free(a);
  524. sol_obj_free(b);
  525. return res;
  526. }
  527. sol_object_t *sol_f_int_toint(sol_state_t *state, sol_object_t *args) {
  528. return sol_list_get_index(state, args, 0);
  529. }
  530. sol_object_t *sol_f_int_tofloat(sol_state_t *state, sol_object_t *args) {
  531. sol_object_t *a = sol_list_get_index(state, args, 0);
  532. sol_object_t *res = sol_new_float(state, (double) a->ival);
  533. sol_obj_free(a);
  534. return res;
  535. }
  536. sol_object_t *sol_f_int_tostring(sol_state_t *state, sol_object_t *args) {
  537. sol_object_t *a = sol_list_get_index(state, args, 0);
  538. char *s = _itoa(a->ival);
  539. sol_object_t *res = sol_new_string(state, s);
  540. sol_obj_free(a);
  541. free(s);
  542. return res;
  543. }
  544. sol_object_t *sol_f_float_add(sol_state_t *state, sol_object_t *args) {
  545. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  546. sol_object_t *res = sol_new_float(state, a->fval + b->fval);
  547. sol_obj_free(a);
  548. sol_obj_free(b);
  549. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  550. return res;
  551. }
  552. sol_object_t *sol_f_float_sub(sol_state_t *state, sol_object_t *args) {
  553. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  554. sol_object_t *res = sol_new_float(state, a->fval - b->fval);
  555. sol_obj_free(a);
  556. sol_obj_free(b);
  557. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  558. return res;
  559. }
  560. sol_object_t *sol_f_float_mul(sol_state_t *state, sol_object_t *args) {
  561. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  562. sol_object_t *res = sol_new_float(state, a->fval * b->fval);
  563. sol_obj_free(a);
  564. sol_obj_free(b);
  565. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  566. return res;
  567. }
  568. sol_object_t *sol_f_float_div(sol_state_t *state, sol_object_t *args) {
  569. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  570. sol_object_t *res = sol_new_float(state, a->fval / b->fval);
  571. sol_obj_free(a);
  572. sol_obj_free(b);
  573. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  574. return res;
  575. }
  576. sol_object_t *sol_f_float_pow(sol_state_t *state, sol_object_t *args) {
  577. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  578. sol_object_t *res = sol_new_float(state, pow(a->fval, b->fval));
  579. sol_obj_free(a);
  580. sol_obj_free(b);
  581. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  582. return res;
  583. }
  584. sol_object_t *sol_f_float_cmp(sol_state_t *state, sol_object_t *args) {
  585. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_float(state, sol_list_get_index(state, args, 1));
  586. sol_object_t *res = sol_new_int(state, a->fval==b->fval? 0 : (a->fval<b->fval? -1 : 1));
  587. sol_obj_free(a);
  588. sol_obj_free(b);
  589. return res;
  590. }
  591. sol_object_t *sol_f_float_toint(sol_state_t *state, sol_object_t *args) {
  592. sol_object_t *a = sol_list_get_index(state, args, 0);
  593. sol_object_t *res = sol_new_int(state, (int) a->fval);
  594. sol_obj_free(a);
  595. return res;
  596. }
  597. sol_object_t *sol_f_float_tofloat(sol_state_t *state, sol_object_t *args) {
  598. return sol_list_get_index(state, args, 0);
  599. }
  600. sol_object_t *sol_f_float_tostring(sol_state_t *state, sol_object_t *args) {
  601. sol_object_t *a = sol_list_get_index(state, args, 0);
  602. char *s = _ftoa(a->fval);
  603. sol_object_t *res = sol_new_string(state, s);
  604. sol_obj_free(a);
  605. free(s);
  606. return res;
  607. }
  608. sol_object_t *sol_f_str_add(sol_state_t *state, sol_object_t *args) {
  609. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_string(state, sol_list_get_index(state, args, 1));
  610. sol_object_t *res = sol_string_concat(state, a, b);
  611. sol_obj_free(a);
  612. sol_obj_free(b);
  613. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  614. return res;
  615. }
  616. sol_object_t *sol_f_str_mul(sol_state_t *state, sol_object_t *args) {
  617. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
  618. int n = strlen(a->str) * b->ival + 1;
  619. char *s = malloc(n);
  620. int i;
  621. s[0]='\0';
  622. for(i = 0; i < b->ival; i++) strncat(s, a->str, n);
  623. sol_object_t *res = sol_new_string(state, s);
  624. sol_obj_free(a);
  625. sol_obj_free(b);
  626. free(s);
  627. if(sol_has_error(state)) {sol_obj_free(res); return sol_incref(state->None);}
  628. return res;
  629. }
  630. sol_object_t *sol_f_str_cmp(sol_state_t *state, sol_object_t *args) {
  631. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_string(state, sol_list_get_index(state, args, 1));
  632. sol_object_t *res = sol_new_int(state, strcmp(a->str, b->str));
  633. sol_obj_free(a);
  634. sol_obj_free(b);
  635. return res;
  636. }
  637. sol_object_t *sol_f_str_len(sol_state_t *state, sol_object_t *args) {
  638. sol_object_t *a = sol_list_get_index(state, args, 0);
  639. sol_object_t *res = sol_new_int(state, strlen(a->str));
  640. sol_obj_free(a);
  641. return res;
  642. }
  643. sol_object_t *sol_f_str_index(sol_state_t *state, sol_object_t *args) {
  644. sol_object_t *str = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *idx = sol_cast_int(state, key);
  645. char buf[2]={0, 0};
  646. if(idx->ival>=0 && idx->ival<strlen(str->str)) {
  647. buf[0] = str->str[idx->ival];
  648. }
  649. sol_obj_free(str);
  650. sol_obj_free(key);
  651. sol_obj_free(idx);
  652. return sol_new_string(state, buf);
  653. }
  654. sol_object_t *sol_f_str_iter(sol_state_t *state, sol_object_t *args) {
  655. return sol_new_cfunc(state, sol_f_iter_str);
  656. }
  657. sol_object_t *sol_f_str_toint(sol_state_t *state, sol_object_t *args) {
  658. sol_object_t *a = sol_list_get_index(state, args, 0);
  659. sol_object_t *res = sol_new_int(state, atoi(a->str));
  660. sol_obj_free(a);
  661. return res;
  662. }
  663. sol_object_t *sol_f_str_tofloat(sol_state_t *state, sol_object_t *args) {
  664. sol_object_t *a = sol_list_get_index(state, args, 0);
  665. sol_object_t *res = sol_new_float(state, atof(a->str));
  666. sol_obj_free(a);
  667. return res;
  668. }
  669. sol_object_t *sol_f_str_tostring(sol_state_t *state, sol_object_t *args) {
  670. return sol_list_get_index(state, args, 0);
  671. }
  672. sol_object_t *sol_f_str_repr(sol_state_t *state, sol_object_t *args) {
  673. sol_object_t *obj = sol_list_get_index(state, args, 0), *cur = sol_new_string(state, "\""), *next = sol_string_concat(state, cur, obj);
  674. sol_obj_free(cur);
  675. cur = next;
  676. next = sol_string_concat_cstr(state, cur, "\"");
  677. sol_obj_free(cur);
  678. return next;
  679. }
  680. sol_object_t *sol_f_list_add(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), *ls;
  682. if(!sol_is_list(b)) {
  683. sol_obj_free(a);
  684. sol_obj_free(b);
  685. return sol_set_error_string(state, "Adding list to non-list");
  686. }
  687. ls = sol_list_copy(state, a);
  688. sol_list_append(state, ls, b);
  689. sol_obj_free(a);
  690. sol_obj_free(b);
  691. return ls;
  692. }
  693. sol_object_t *sol_f_list_mul(sol_state_t *state, sol_object_t *args) {
  694. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1)), *ls;
  695. int i;
  696. if(sol_has_error(state)) {
  697. sol_obj_free(a);
  698. sol_obj_free(b);
  699. return sol_incref(state->None);
  700. }
  701. ls = sol_new_list(state);
  702. for(i = 0; i < b->ival; i++) {
  703. sol_list_append(state, ls, a);
  704. if(sol_has_error(state)) {
  705. sol_obj_free(a);
  706. sol_obj_free(b);
  707. return sol_incref(state->None);
  708. }
  709. }
  710. return ls;
  711. }
  712. sol_object_t *sol_f_list_index(sol_state_t *state, sol_object_t *args) {
  713. sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *ival;
  714. sol_object_t *res, *funcs;
  715. if(sol_is_string(b)) {
  716. funcs = sol_get_methods_name(state, "list");
  717. res = sol_map_get(state, funcs, b);
  718. sol_obj_free(funcs);
  719. } else {
  720. ival = sol_cast_int(state, b);
  721. res = sol_list_get_index(state, ls, ival->ival);
  722. sol_obj_free(ival);
  723. }
  724. sol_obj_free(ls);
  725. sol_obj_free(b);
  726. return res;
  727. }
  728. sol_object_t *sol_f_list_setindex(sol_state_t *state, sol_object_t *args) {
  729. sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args ,1));
  730. sol_object_t *val = sol_list_get_index(state, args, 2);
  731. sol_list_set_index(state, ls, b->ival, val);
  732. sol_obj_free(ls);
  733. sol_obj_free(b);
  734. sol_obj_free(val);
  735. return sol_incref(state->None);
  736. }
  737. sol_object_t *sol_f_list_len(sol_state_t *state, sol_object_t *args) {
  738. sol_object_t *ls = sol_list_get_index(state, args, 0);
  739. sol_object_t *res = sol_new_int(state, sol_list_len(state, ls));
  740. sol_obj_free(ls);
  741. return res;
  742. }
  743. sol_object_t *sol_f_list_iter(sol_state_t *state, sol_object_t *args) {
  744. return sol_new_cfunc(state, sol_f_iter_list);
  745. }
  746. sol_object_t *sol_f_list_tostring(sol_state_t *state, sol_object_t *args) {
  747. sol_object_t *cur = sol_new_string(state, "["), *next, *str, *obj = sol_list_get_index(state, args, 0), *item;
  748. dsl_seq_iter *iter = dsl_new_seq_iter(obj->seq);
  749. char s[64];
  750. while(!dsl_seq_iter_is_invalid(iter)) {
  751. item = AS_OBJ(dsl_seq_iter_at(iter));
  752. if(test_seen(item)) {
  753. snprintf(s, 64, "... (%p)", item);
  754. next = sol_string_concat_cstr(state, cur, s);
  755. } else {
  756. str = sol_cast_repr(state, item);
  757. next = sol_string_concat(state, cur, str);
  758. sol_obj_free(str);
  759. }
  760. sol_obj_free(cur);
  761. cur = next;
  762. if(!dsl_seq_iter_at_end(iter)) {
  763. next = sol_string_concat_cstr(state, cur, ", ");
  764. sol_obj_free(cur);
  765. cur = next;
  766. }
  767. dsl_seq_iter_next(iter);
  768. }
  769. next = sol_string_concat_cstr(state, cur, "]");
  770. sol_obj_free(cur);
  771. dsl_free_seq_iter(iter);
  772. return next;
  773. }
  774. sol_object_t *sol_f_list_copy(sol_state_t *state, sol_object_t *args) {
  775. sol_object_t *list = sol_list_get_index(state, args, 0);
  776. sol_object_t *res = sol_list_copy(state, list);
  777. sol_obj_free(list);
  778. return res;
  779. }
  780. sol_object_t *sol_f_list_insert(sol_state_t *state, sol_object_t *args) {
  781. sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_cast_int(state, sol_list_get_index(state, args, 1)), *obj = sol_list_get_index(state, args, 2);
  782. sol_list_insert(state, list, idx->ival, obj);
  783. sol_obj_free(list);
  784. return sol_incref(state->None);
  785. }
  786. sol_object_t *sol_f_list_remove(sol_state_t *state, sol_object_t *args) {
  787. sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_cast_int(state, sol_list_get_index(state, args, 1));
  788. sol_list_remove(state, list, idx->ival);
  789. sol_obj_free(list);
  790. return sol_incref(state->None);
  791. }
  792. sol_object_t *sol_f_list_truncate(sol_state_t *state, sol_object_t *args) {
  793. sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1);
  794. sol_object_t *res = sol_list_truncate(state, list, idx->ival);
  795. sol_obj_free(list);
  796. return res;
  797. }
  798. sol_object_t *sol_f_list_map(sol_state_t *state, sol_object_t *args) {
  799. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  800. sol_object_t *fargs = sol_new_list(state), *item;
  801. int idx=0, len = sol_list_len(state, list);
  802. sol_list_insert(state, fargs, 0, func);
  803. while(idx<len) {
  804. item = sol_list_get_index(state, list, idx);
  805. sol_list_insert(state, fargs, 1, item);
  806. sol_obj_free(item);
  807. item = func->ops->call(state, fargs);
  808. if(sol_has_error(state)) return list;
  809. sol_list_remove(state, fargs, 1);
  810. sol_list_set_index(state, list, idx, item);
  811. sol_obj_free(item);
  812. idx++;
  813. }
  814. sol_obj_free(fargs);
  815. sol_obj_free(func);
  816. return list;
  817. }
  818. sol_object_t *sol_f_list_filter(sol_state_t *state, sol_object_t *args) {
  819. sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
  820. sol_object_t *fargs = sol_new_list(state), *item, *ival;
  821. int idx=0, len = sol_list_len(state, list);
  822. sol_list_insert(state, fargs, 0, func);
  823. while(idx<len) {
  824. item = sol_list_get_index(state, list, idx);
  825. sol_list_insert(state, fargs, 1, item);
  826. sol_obj_free(item);
  827. item = func->ops->call(state, fargs);
  828. if(sol_has_error(state)) return list;
  829. ival = sol_cast_int(state, item);
  830. if(ival->ival) {
  831. idx++;
  832. } else {
  833. sol_list_remove(state, list, idx);
  834. len--;
  835. }
  836. sol_obj_free(item);
  837. sol_obj_free(ival);
  838. }
  839. sol_obj_free(fargs);
  840. sol_obj_free(func);
  841. return list;
  842. }
  843. sol_object_t *sol_f_map_add(sol_state_t *state, sol_object_t *args) {
  844. sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *map;
  845. if(!sol_is_map(b)) {
  846. sol_obj_free(a);
  847. sol_obj_free(b);
  848. return sol_set_error_string(state, "Adding map to non-map");
  849. }
  850. map = sol_map_copy(state, a);
  851. sol_map_merge(state, map, b);
  852. sol_obj_free(a);
  853. sol_obj_free(b);
  854. return map;
  855. }
  856. sol_object_t *sol_f_map_index(sol_state_t *state, sol_object_t *args) {
  857. sol_object_t *map = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  858. sol_object_t *indexf = sol_map_get_name(state, map, "__index");
  859. sol_object_t *res = NULL, *newls;
  860. if(!sol_is_none(state, indexf)) {
  861. if(indexf->ops->call && (sol_is_func(indexf) || sol_is_cfunc(indexf)) && indexf->ops->call != sol_f_not_impl) {
  862. newls = sol_new_list(state);
  863. sol_list_insert(state, newls, 0, indexf);
  864. sol_list_append(state, newls, args);
  865. res = indexf->ops->call(state, newls);
  866. sol_obj_free(newls);
  867. } else if(indexf->ops->index && indexf->ops->index != sol_f_not_impl) {
  868. newls = sol_new_list(state);
  869. sol_list_insert(state, newls, 0, indexf);
  870. sol_list_insert(state, newls, 1, b);
  871. res = indexf->ops->index(state, newls);
  872. sol_obj_free(newls);
  873. }
  874. }
  875. if(!res) res = sol_map_get(state, map, b);
  876. sol_obj_free(indexf);
  877. sol_obj_free(map);
  878. sol_obj_free(b);
  879. return res;
  880. }
  881. sol_object_t *sol_f_map_setindex(sol_state_t *state, sol_object_t *args) {
  882. sol_object_t *map = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1);
  883. sol_object_t *val = sol_list_get_index(state, args, 2);
  884. sol_object_t *setindexf = sol_map_get_name(state, map, "__setindex"), *newls;
  885. if(!sol_is_none(state, setindexf)) {
  886. if(setindexf->ops->call && (sol_is_func(setindexf) || sol_is_cfunc(setindexf)) && setindexf->ops->call != sol_f_not_impl) {
  887. newls = sol_new_list(state);
  888. sol_list_insert(state, newls, 0, setindexf);
  889. sol_list_append(state, newls, args);
  890. sol_obj_free(setindexf->ops->call(state, newls));
  891. sol_obj_free(newls);
  892. return sol_incref(state->None);
  893. } else if(setindexf->ops->setindex && setindexf->ops->setindex != sol_f_not_impl) {
  894. newls = sol_new_list(state);
  895. sol_list_insert(state, newls, 0, setindexf);
  896. sol_list_insert(state, newls, 1, b);
  897. sol_list_insert(state, newls, 2, val);
  898. sol_obj_free(setindexf->ops->index(state, newls));
  899. sol_obj_free(newls);
  900. return sol_incref(state->None);
  901. }
  902. }
  903. sol_obj_free(setindexf);
  904. sol_map_set(state, map, b, val);
  905. sol_obj_free(map);
  906. sol_obj_free(b);
  907. sol_obj_free(val);
  908. return sol_incref(state->None);
  909. }
  910. sol_object_t *sol_f_map_call(sol_state_t *state, sol_object_t *args) {
  911. sol_object_t *map = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  912. sol_object_t *callf = sol_map_get_name(state, map, "__call"), *res = NULL;
  913. if(!sol_is_none(state, callf)) {
  914. if(callf->ops->call) {
  915. sol_list_insert(state, fargs, 0, callf);
  916. sol_list_insert(state, fargs, 1, map);
  917. res = callf->ops->call(state, fargs);
  918. }
  919. }
  920. sol_obj_free(map);
  921. sol_obj_free(fargs);
  922. sol_obj_free(callf);
  923. if(res) return res;
  924. return sol_set_error_string(state, "Call map without call method");
  925. }
  926. sol_object_t *sol_f_map_len(sol_state_t *state, sol_object_t *args) {
  927. sol_object_t *map = sol_list_get_index(state, args, 0);
  928. sol_object_t *res = sol_new_int(state, sol_map_len(state, map));
  929. sol_obj_free(map);
  930. return res;
  931. }
  932. sol_object_t *sol_f_map_iter(sol_state_t *state, sol_object_t *args) {
  933. return sol_new_cfunc(state, sol_f_iter_map);
  934. }
  935. sol_object_t *sol_f_map_tostring(sol_state_t *state, sol_object_t *args) {
  936. sol_object_t *cur = sol_new_string(state, "{"), *next, *str, *obj = sol_list_get_index(state, args, 0), *item;
  937. dsl_seq_iter *iter = dsl_new_seq_iter(obj->seq);
  938. char s[64];
  939. while(!dsl_seq_iter_is_invalid(iter)) {
  940. item = AS_OBJ(dsl_seq_iter_at(iter));
  941. if(test_seen(item)) {
  942. snprintf(s, 64, "... (%p)", item);
  943. next = sol_string_concat_cstr(state, cur, s);
  944. } else {
  945. str = sol_cast_repr(state, item);
  946. next = sol_string_concat(state, cur, str);
  947. sol_obj_free(str);
  948. }
  949. sol_obj_free(cur);
  950. cur = next;
  951. if(!dsl_seq_iter_at_end(iter)) {
  952. next = sol_string_concat_cstr(state, cur, ", ");
  953. sol_obj_free(cur);
  954. cur = next;
  955. }
  956. dsl_seq_iter_next(iter);
  957. }
  958. next = sol_string_concat_cstr(state, cur, "}");
  959. sol_obj_free(cur);
  960. dsl_free_seq_iter(iter);
  961. sol_obj_free(obj);
  962. return next;
  963. }
  964. sol_object_t *sol_f_mcell_tostring(sol_state_t *state, sol_object_t *args) {
  965. sol_object_t *mcell = sol_list_get_index(state, args, 0), *cur = sol_new_string(state, "["), *next, *str;
  966. char s[64];
  967. if(test_seen(mcell->key)) {
  968. snprintf(s, 64, "... (%p)", mcell->key);
  969. next = sol_string_concat_cstr(state, cur, s);
  970. } else {
  971. str = sol_cast_repr(state, mcell->key);
  972. next = sol_string_concat(state, cur, str);
  973. sol_obj_free(str);
  974. }
  975. sol_obj_free(cur);
  976. cur = next;
  977. next = sol_string_concat_cstr(state, cur, "] = ");
  978. sol_obj_free(cur);
  979. cur = next;
  980. if(test_seen(mcell->val)) {
  981. snprintf(s, 64, "... (%p)", mcell->val);
  982. next = sol_string_concat_cstr(state, cur, s);
  983. } else {
  984. str = sol_cast_repr(state, mcell->val);
  985. next = sol_string_concat(state, cur, str);
  986. sol_obj_free(str);
  987. }
  988. sol_obj_free(cur);
  989. sol_obj_free(mcell);
  990. return next;
  991. }
  992. sol_object_t *sol_f_func_index(sol_state_t *state, sol_object_t *args) {
  993. sol_object_t *func = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *res;
  994. identlist_node *curi;
  995. int i=0;
  996. if(!sol_is_string(key)) {
  997. res = sol_map_get(state, func->udata, key);
  998. } else {
  999. if(sol_string_eq(state, key, "name")) {
  1000. res = sol_new_string(state, func->fname);
  1001. } else if(sol_string_eq(state, key, "closure")) {
  1002. res = sol_incref(func->closure);
  1003. } else if(sol_string_eq(state, key, "udata")) {
  1004. res = sol_incref(func->udata);
  1005. } else if(sol_string_eq(state, key, "stmt")) {
  1006. res = sol_new_stmtnode(state, (stmt_node *) func->func);
  1007. } else if(sol_string_eq(state, key, "args")) {
  1008. res = sol_new_list(state);
  1009. curi = func->args;
  1010. while(curi) {
  1011. sol_list_insert(state, res, i++, sol_new_string(state, curi->ident));
  1012. curi = curi->next;
  1013. }
  1014. } else {
  1015. res = sol_map_get(state, func->udata, key);
  1016. }
  1017. }
  1018. sol_obj_free(func);
  1019. sol_obj_free(key);
  1020. return res;
  1021. }
  1022. sol_object_t *sol_f_func_setindex(sol_state_t *state, sol_object_t *args) {
  1023. 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;
  1024. if(sol_string_eq(state, key, "name") && sol_is_string(val)) {
  1025. free(func->fname);
  1026. func->fname = strdup(val->str);
  1027. } else if(sol_string_eq(state, key, "closure") && sol_is_map(val)) {
  1028. temp = func->closure;
  1029. func->closure = sol_incref(val);
  1030. sol_obj_free(temp);
  1031. } else if(sol_string_eq(state, key, "udata") && sol_is_map(val)) {
  1032. temp = func->udata;
  1033. func->udata = sol_incref(val);
  1034. sol_obj_free(temp);
  1035. } else if(sol_string_eq(state, key, "stmt") && sol_is_aststmt(val)) {
  1036. func->func = val->node;
  1037. } else {
  1038. sol_map_set(state, func->udata, key, val);
  1039. }
  1040. sol_obj_free(func);
  1041. sol_obj_free(key);
  1042. sol_obj_free(val);
  1043. return sol_incref(state->None);
  1044. }
  1045. sol_object_t *sol_f_func_tostring(sol_state_t *state, sol_object_t *args) {
  1046. sol_object_t *func = sol_list_get_index(state, args, 0), *ret;
  1047. char *s = malloc(256 * sizeof(char));
  1048. if(func->fname) {
  1049. snprintf(s, 256, "<Function %s>", func->fname);
  1050. } else {
  1051. snprintf(s, 256, "<Function>");
  1052. }
  1053. ret = sol_new_string(state, s);
  1054. free(s);
  1055. sol_obj_free(func);
  1056. return ret;
  1057. }
  1058. sol_object_t *sol_f_cfunc_call(sol_state_t *state, sol_object_t *args) {
  1059. sol_object_t *func = sol_list_get_index(state, args, 0), *fargs = sol_list_sublist(state, args, 1);
  1060. sol_object_t *res = func->cfunc(state, fargs);
  1061. sol_obj_free(func);
  1062. sol_obj_free(fargs);
  1063. return res;
  1064. }
  1065. sol_object_t *sol_f_cfunc_tostring(sol_state_t *state, sol_object_t *args) {
  1066. return sol_new_string(state, "<CFunction>");
  1067. }
  1068. sol_object_t *sol_f_astnode_call(sol_state_t *state, sol_object_t *args) {
  1069. sol_object_t *obj = sol_list_get_index(state, args, 0), *env=NULL, *res;
  1070. stmt_node *stmt = (stmt_node *) obj->node;
  1071. expr_node *expr = (expr_node *) obj->node;
  1072. sol_obj_free(obj);
  1073. if(sol_list_len(state, args)>1) {
  1074. env = sol_list_get_index(state, args, 1);
  1075. sol_state_push_scope(state, env);
  1076. }
  1077. if(sol_is_aststmt(obj)) {
  1078. sol_exec(state, stmt);
  1079. res = sol_incref(state->None);
  1080. } else {
  1081. res = sol_eval(state, expr);
  1082. }
  1083. if(env) {
  1084. sol_state_pop_scope(state);
  1085. sol_obj_free(env);
  1086. }
  1087. return res;
  1088. }
  1089. sol_object_t *sol_f_astnode_index(sol_state_t *state, sol_object_t *args) {
  1090. sol_object_t *obj = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *str = sol_cast_string(state, key), *res=NULL, *pair;
  1091. stmt_node *stmt = (stmt_node *) obj->node;
  1092. stmtlist_node *curs;
  1093. expr_node *expr = (expr_node *) obj->node;
  1094. exprlist_node *cure;
  1095. assoclist_node *cura;
  1096. identlist_node *curi;
  1097. int i=0;
  1098. if(sol_is_aststmt(obj)) {
  1099. if(sol_string_eq(state, str, "type")) {
  1100. res = sol_new_int(state, stmt->type);
  1101. } else {
  1102. switch(stmt->type) {
  1103. case ST_EXPR:
  1104. if(sol_string_eq(state, str, "expr")) {
  1105. res = sol_new_exprnode(state, stmt->expr);
  1106. }
  1107. break;
  1108. case ST_IFELSE:
  1109. if(sol_string_eq(state, str, "cond")) {
  1110. res = sol_new_exprnode(state, stmt->ifelse->cond);
  1111. } else if(sol_string_eq(state, str, "iftrue")) {
  1112. res = sol_new_stmtnode(state, stmt->ifelse->iftrue);
  1113. } else if(sol_string_eq(state, str, "iffalse")) {
  1114. res = sol_new_stmtnode(state, stmt->ifelse->iffalse);
  1115. }
  1116. break;
  1117. case ST_LOOP:
  1118. if(sol_string_eq(state, str, "cond")) {
  1119. res = sol_new_exprnode(state, stmt->loop->cond);
  1120. } else if(sol_string_eq(state, str, "loop")) {
  1121. res = sol_new_stmtnode(state, stmt->loop->loop);
  1122. }
  1123. break;
  1124. case ST_ITER:
  1125. if(sol_string_eq(state, str, "var")) {
  1126. res = sol_new_string(state, stmt->iter->var);
  1127. } else if(sol_string_eq(state, str, "iter")) {
  1128. res = sol_new_exprnode(state, stmt->iter->iter);
  1129. } else if(sol_string_eq(state, str, "loop")) {
  1130. res = sol_new_stmtnode(state, stmt->iter->loop);
  1131. }
  1132. break;
  1133. case ST_LIST:
  1134. if(sol_string_eq(state, str, "stmtlist")) {
  1135. res = sol_new_list(state);
  1136. curs = stmt->stmtlist;
  1137. while(curs) {
  1138. sol_list_insert(state, res, i++, sol_new_stmtnode(state, curs->stmt));
  1139. curs = curs->next;
  1140. }
  1141. }
  1142. break;
  1143. case ST_RET:
  1144. if(sol_string_eq(state, str, "ret")) {
  1145. res = sol_new_exprnode(state, stmt->ret->ret);
  1146. }
  1147. break;
  1148. }
  1149. }
  1150. } else {
  1151. if(sol_string_eq(state, str, "type")) {
  1152. res = sol_new_int(state, expr->type);
  1153. } else {
  1154. switch(expr->type) {
  1155. case EX_LIT:
  1156. if(sol_string_eq(state, str, "littype")) {
  1157. res = sol_new_int(state, expr->lit->type);
  1158. } else if(sol_string_eq(state, str, "ival")) {
  1159. res = sol_new_int(state, expr->lit->ival);
  1160. } else if(sol_string_eq(state, str, "fval")) {
  1161. res = sol_new_float(state, expr->lit->fval);
  1162. } else if(sol_string_eq(state, str, "str")) {
  1163. res = sol_new_string(state, expr->lit->str);
  1164. }
  1165. break;
  1166. case EX_LISTGEN:
  1167. if(sol_string_eq(state, str, "list")) {
  1168. res = sol_new_list(state);
  1169. cure = expr->listgen->list;
  1170. while(cure) {
  1171. sol_list_insert(state, res, i++, sol_new_exprnode(state, cure->expr));
  1172. cure = cure->next;
  1173. }
  1174. }
  1175. break;
  1176. case EX_MAPGEN:
  1177. if(sol_string_eq(state, str, "map")) {
  1178. res = sol_new_list(state);
  1179. cura = expr->mapgen->map;
  1180. while(cura) {
  1181. pair = sol_new_list(state);
  1182. sol_list_insert(state, pair, 0, sol_new_exprnode(state, cura->item->key));
  1183. sol_list_insert(state, pair, 1, sol_new_exprnode(state, cura->item->value));
  1184. sol_list_insert(state, res, i++, pair);
  1185. sol_obj_free(pair);
  1186. }
  1187. }
  1188. break;
  1189. case EX_BINOP:
  1190. if(sol_string_eq(state, str, "binoptype")) {
  1191. res = sol_new_int(state, expr->binop->type);
  1192. } else if(sol_string_eq(state, str, "left")) {
  1193. res = sol_new_exprnode(state, expr->binop->left);
  1194. } else if(sol_string_eq(state, str, "right")) {
  1195. res = sol_new_exprnode(state, expr->binop->right);
  1196. }
  1197. break;
  1198. case EX_UNOP:
  1199. if(sol_string_eq(state, str, "unoptype")) {
  1200. res = sol_new_int(state, expr->unop->type);
  1201. } else if(sol_string_eq(state, str, "expr")) {
  1202. res = sol_new_exprnode(state, expr->unop->expr);
  1203. }
  1204. break;
  1205. case EX_INDEX:
  1206. if(sol_string_eq(state, str, "expr")) {
  1207. res = sol_new_exprnode(state, expr->index->expr);
  1208. } else if(sol_string_eq(state, str, "index")) {
  1209. res = sol_new_exprnode(state, expr->index->index);
  1210. }
  1211. break;
  1212. case EX_SETINDEX:
  1213. if(sol_string_eq(state, str, "expr")) {
  1214. res = sol_new_exprnode(state, expr->setindex->expr);
  1215. } else if(sol_string_eq(state, str, "index")) {
  1216. res = sol_new_exprnode(state, expr->setindex->index);
  1217. } else if(sol_string_eq(state, str, "value")) {
  1218. res = sol_new_exprnode(state, expr->setindex->value);
  1219. }
  1220. break;
  1221. case EX_ASSIGN:
  1222. if(sol_string_eq(state, str, "ident")) {
  1223. res = sol_new_string(state, expr->assign->ident);
  1224. } else if(sol_string_eq(state, str, "value")) {
  1225. res = sol_new_exprnode(state, expr->assign->value);
  1226. }
  1227. break;
  1228. case EX_REF:
  1229. if(sol_string_eq(state, str, "ident")) {
  1230. res = sol_new_string(state, expr->ref->ident);
  1231. }
  1232. break;
  1233. case EX_CALL:
  1234. if(sol_string_eq(state, str, "expr")) {
  1235. res = sol_new_exprnode(state, expr->call->expr);
  1236. } else if(sol_string_eq(state, str, "args")) {
  1237. res = sol_new_list(state);
  1238. cure = expr->call->args;
  1239. while(cure) {
  1240. sol_list_insert(state, res, i++, sol_new_exprnode(state, cure->expr));
  1241. cure = cure->next;
  1242. }
  1243. }
  1244. break;
  1245. case EX_FUNCDECL:
  1246. if(sol_string_eq(state, str, "name")) {
  1247. res = sol_new_string(state, (expr->funcdecl->name?expr->funcdecl->name:""));
  1248. } else if(sol_string_eq(state, str, "args")) {
  1249. res = sol_new_list(state);
  1250. curi = expr->funcdecl->args;
  1251. while(curi) {
  1252. sol_list_insert(state, res, i++, sol_new_string(state, curi->ident));
  1253. curi = curi->next;
  1254. }
  1255. } else if(sol_string_eq(state, str, "body")) {
  1256. res = sol_new_stmtnode(state, expr->funcdecl->body);
  1257. }
  1258. break;
  1259. }
  1260. }
  1261. }
  1262. sol_obj_free(obj);
  1263. sol_obj_free(key);
  1264. sol_obj_free(str);
  1265. if(!res) res = sol_incref(state->None);
  1266. return res;
  1267. }
  1268. sol_object_t *sol_f_astnode_setindex(sol_state_t *state, sol_object_t *args) {
  1269. sol_object_t *obj = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *str = sol_cast_string(state, key), *val = sol_list_get_index(state, args, 2), *pair;
  1270. sol_object_t *ival, *fval, *sval;
  1271. stmt_node *stmt = (stmt_node *) obj->node;
  1272. stmtlist_node *curs, *prevs;
  1273. expr_node *expr = (expr_node *) obj->node;
  1274. exprlist_node *cure, *preve;
  1275. assoclist_node *cura, *preva;
  1276. identlist_node *curi, *previ;
  1277. int i=0, len;
  1278. if(sol_is_aststmt(obj)) {
  1279. if(sol_string_eq(state, str, "type")) {
  1280. ival = sol_cast_int(state, val);
  1281. stmt->type = ival->ival;
  1282. sol_obj_free(ival);
  1283. } else {
  1284. switch(stmt->type) {
  1285. case ST_EXPR:
  1286. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1287. stmt->expr = val->node;
  1288. }
  1289. break;
  1290. case ST_IFELSE:
  1291. if(sol_string_eq(state, str, "cond") && sol_is_astexpr(val)) {
  1292. stmt->ifelse->cond = val->node;
  1293. } else if(sol_string_eq(state, str, "iftrue") && sol_is_aststmt(val)) {
  1294. stmt->ifelse->iftrue = val->node;
  1295. } else if(sol_string_eq(state, str, "iffalse") && sol_is_aststmt(val)) {
  1296. stmt->ifelse->iffalse = val->node;
  1297. }
  1298. break;
  1299. case ST_LOOP:
  1300. if(sol_string_eq(state, str, "cond") && sol_is_astexpr(val)) {
  1301. stmt->loop->cond = val->node;
  1302. } else if(sol_string_eq(state, str, "loop") && sol_is_aststmt(val)) {
  1303. stmt->loop->loop = val->node;
  1304. }
  1305. break;
  1306. case ST_ITER:
  1307. if(sol_string_eq(state, str, "var")) {
  1308. sval = sol_cast_string(state, val);
  1309. stmt->iter->var = strdup(sval->str);
  1310. sol_obj_free(sval);
  1311. } else if(sol_string_eq(state, str, "iter") && sol_is_astexpr(val)) {
  1312. stmt->iter->iter = val->node;
  1313. } else if(sol_string_eq(state, str, "loop") && sol_is_aststmt(val)) {
  1314. stmt->iter->loop = val->node;
  1315. }
  1316. break;
  1317. case ST_LIST:
  1318. if(sol_string_eq(state, str, "stmtlist") && sol_is_list(val)) {
  1319. len = sol_list_len(state, val);
  1320. if(len>0) {
  1321. curs = malloc(sizeof(stmtlist_node));
  1322. stmt->stmtlist = curs;
  1323. for(i=0; i<len; i++) {
  1324. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1325. curs->stmt = sol_list_get_index(state, val, i)->node;
  1326. prevs = curs;
  1327. curs = malloc(sizeof(stmtlist_node));
  1328. prevs->next = curs;
  1329. }
  1330. }
  1331. free(curs);
  1332. prevs->next = NULL;
  1333. } else {
  1334. stmt->stmtlist = NULL;
  1335. }
  1336. }
  1337. break;
  1338. case ST_RET:
  1339. if(sol_string_eq(state, str, "ret") && sol_is_astexpr(val)) {
  1340. stmt->ret->ret = val->node;
  1341. }
  1342. break;
  1343. }
  1344. }
  1345. } else {
  1346. if(sol_string_eq(state, str, "type")) {
  1347. ival = sol_cast_int(state, val);
  1348. expr->type = ival->ival;
  1349. sol_obj_free(ival);
  1350. } else {
  1351. switch(expr->type) {
  1352. case EX_LIT:
  1353. if(sol_string_eq(state, str, "littype")) {
  1354. ival = sol_cast_int(state, val);
  1355. expr->lit->type = ival->ival;
  1356. sol_obj_free(ival);
  1357. } else if(sol_string_eq(state, str, "ival")) {
  1358. ival = sol_cast_int(state, val);
  1359. expr->lit->ival = ival->ival;
  1360. sol_obj_free(ival);
  1361. } else if(sol_string_eq(state, str, "fval")) {
  1362. fval = sol_cast_float(state, val);
  1363. expr->lit->fval = fval->fval;
  1364. sol_obj_free(fval);
  1365. } else if(sol_string_eq(state, str, "str")) {
  1366. sval = sol_cast_string(state, val);
  1367. expr->lit->str = strdup(sval->str);
  1368. sol_obj_free(sval);
  1369. }
  1370. break;
  1371. case EX_LISTGEN:
  1372. if(sol_string_eq(state, str, "list") && sol_is_list(val)) {
  1373. len = sol_list_len(state, val);
  1374. if(len>0) {
  1375. cure = malloc(sizeof(exprlist_node));
  1376. expr->listgen->list = cure;
  1377. for(i=0; i<len; i++) {
  1378. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1379. cure->expr = sol_list_get_index(state, val, i)->node;
  1380. preve = cure;
  1381. cure = malloc(sizeof(exprlist_node));
  1382. preve->next = cure;
  1383. }
  1384. }
  1385. free(cure);
  1386. preve->next = NULL;
  1387. } else {
  1388. expr->listgen->list = NULL;
  1389. }
  1390. }
  1391. break;
  1392. case EX_MAPGEN:
  1393. if(sol_string_eq(state, str, "map") && sol_is_list(val)) {
  1394. len = sol_list_len(state, val);
  1395. if(len>0) {
  1396. cura = malloc(sizeof(assoclist_node));
  1397. expr->mapgen->map = cura;
  1398. for(i=0; i<len; i++) {
  1399. if(sol_is_list(sol_list_get_index(state, val, i))) {
  1400. pair = sol_list_get_index(state, val, i);
  1401. if(sol_list_len(state, pair) >= 2 && sol_is_astexpr(sol_list_get_index(state, pair, 0)) && sol_is_astexpr(sol_list_get_index(state, pair, 1))) {
  1402. cura->item = malloc(sizeof(associtem_node));
  1403. cura->item->key = sol_list_get_index(state, pair, 0)->node;
  1404. cura->item->value = sol_list_get_index(state, pair, 1)->node;
  1405. preva = cura;
  1406. cura = malloc(sizeof(assoclist_node));
  1407. preva->next = cura;
  1408. }
  1409. }
  1410. }
  1411. free(cura);
  1412. preva->next = NULL;
  1413. } else {
  1414. expr->mapgen->map = NULL;
  1415. }
  1416. }
  1417. break;
  1418. case EX_BINOP:
  1419. if(sol_string_eq(state, str, "binoptype")) {
  1420. ival = sol_cast_int(state, val);
  1421. expr->binop->type = ival->ival;
  1422. sol_obj_free(ival);
  1423. } else if(sol_string_eq(state, str, "left") && sol_is_astexpr(val)) {
  1424. expr->binop->left = val->node;
  1425. } else if(sol_string_eq(state, str, "right") && sol_is_astexpr(val)) {
  1426. expr->binop->right = val->node;
  1427. }
  1428. break;
  1429. case EX_UNOP:
  1430. if(sol_string_eq(state, str, "unoptype")) {
  1431. ival = sol_cast_int(state, val);
  1432. expr->unop->type = ival->ival;
  1433. sol_obj_free(ival);
  1434. } else if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1435. expr->unop->expr = val->node;
  1436. }
  1437. break;
  1438. case EX_INDEX:
  1439. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1440. expr->index->expr = val->node;
  1441. } else if(sol_string_eq(state, str, "index") && sol_is_astexpr(val)) {
  1442. expr->index->index = val->node;
  1443. }
  1444. break;
  1445. case EX_SETINDEX:
  1446. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1447. expr->setindex->expr = val->node;
  1448. } else if(sol_string_eq(state, str, "index") && sol_is_astexpr(val)) {
  1449. expr->setindex->index = val->node;
  1450. } else if(sol_string_eq(state, str, "value") && sol_is_astexpr(val)) {
  1451. expr->setindex->value = val->node;
  1452. }
  1453. break;
  1454. case EX_ASSIGN:
  1455. if(sol_string_eq(state, str, "ident")) {
  1456. sval = sol_cast_string(state, val);
  1457. expr->assign->ident = strdup(sval->str);
  1458. sol_obj_free(sval);
  1459. } else if(sol_string_eq(state, str, "value") && sol_is_astexpr(val)) {
  1460. expr->assign->value = val->node;
  1461. }
  1462. break;
  1463. case EX_REF:
  1464. if(sol_string_eq(state, str, "ident")) {
  1465. sval = sol_cast_string(state, val);
  1466. expr->ref->ident = strdup(sval->str);
  1467. sol_obj_free(sval);
  1468. }
  1469. break;
  1470. case EX_CALL:
  1471. if(sol_string_eq(state, str, "expr") && sol_is_astexpr(val)) {
  1472. expr->call->expr = val->node;
  1473. } else if(sol_string_eq(state, str, "args") && sol_is_list(val)) {
  1474. len = sol_list_len(state, val);
  1475. if(len>0) {
  1476. cure = malloc(sizeof(exprlist_node));
  1477. expr->call->args= cure;
  1478. for(i=0; i<len; i++) {
  1479. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1480. cure->expr = sol_list_get_index(state, val, i)->node;
  1481. preve = cure;
  1482. cure = malloc(sizeof(exprlist_node));
  1483. preve->next = cure;
  1484. }
  1485. }
  1486. free(cure);
  1487. preve->next = NULL;
  1488. } else {
  1489. expr->call->args = NULL;
  1490. }
  1491. }
  1492. break;
  1493. case EX_FUNCDECL:
  1494. if(sol_string_eq(state, str, "name")) {
  1495. sval = sol_cast_string(state, val);
  1496. expr->funcdecl->name = strdup(sval->str);
  1497. sol_obj_free(sval);
  1498. } else if(sol_string_eq(state, str, "args") && sol_is_list(val)) {
  1499. len = sol_list_len(state, val);
  1500. if(len>0) {
  1501. curi = malloc(sizeof(identlist_node));
  1502. expr->funcdecl->args= curi;
  1503. for(i=0; i<len; i++) {
  1504. if(sol_is_aststmt(sol_list_get_index(state, val, i))) {
  1505. sval = sol_cast_string(state, sol_list_get_index(state, val, i));
  1506. curi->ident = strdup(sval->str);
  1507. sol_obj_free(sval);
  1508. previ = curi;
  1509. curi = malloc(sizeof(identlist_node));
  1510. previ->next = curi;
  1511. }
  1512. }
  1513. free(curi);
  1514. previ->next = NULL;
  1515. } else {
  1516. expr->funcdecl->args = NULL;
  1517. }
  1518. } else if(sol_string_eq(state, str, "body") && sol_is_aststmt(val)) {
  1519. expr->funcdecl->body = val->node;
  1520. }
  1521. break;
  1522. }
  1523. }
  1524. }
  1525. sol_obj_free(obj);
  1526. sol_obj_free(key);
  1527. sol_obj_free(str);
  1528. sol_obj_free(val);
  1529. return sol_incref(state->None);
  1530. }
  1531. static char *sol_StmtNames[]={"EXPR", "IFSELSE", "LOOP", "ITER", "RET", "CONT", "BREAK"};
  1532. static char *sol_ExprNames[]={"LIT", "LISTGEN", "MAPGEN", "BINOP", "UNOP", "INDEX", "SETINDEX", "ASSIGN", "REF", "CALL", "FUNCDECL"};
  1533. sol_object_t *sol_f_astnode_tostring(sol_state_t *state, sol_object_t *args) {
  1534. sol_object_t *obj = sol_list_get_index(state, args, 0), *res;
  1535. char s[64];
  1536. if(sol_is_aststmt(obj)) {
  1537. snprintf(s, 64, "<Stmt[%s]>", sol_StmtNames[((stmt_node *)obj->node)->type]);
  1538. } else {
  1539. snprintf(s, 64, "<Expr[%s]>", sol_ExprNames[((expr_node *)obj->node)->type]);
  1540. }
  1541. sol_obj_free(obj);
  1542. return sol_new_string(state, s);
  1543. }
  1544. sol_object_t *sol_f_buffer_index(sol_state_t *state, sol_object_t *args) {
  1545. sol_object_t *key = sol_list_get_index(state, args, 1), *funcs = sol_get_methods_name(state, "buffer");
  1546. sol_object_t *res = sol_map_get(state, funcs, key);
  1547. sol_obj_free(key);
  1548. sol_obj_free(funcs);
  1549. return res;
  1550. }
  1551. sol_object_t *sol_f_buffer_tostring(sol_state_t *state, sol_object_t *args) {
  1552. sol_object_t *buf = sol_list_get_index(state, args, 0), *res;
  1553. char s[64];
  1554. if(buf->sz == -1) {
  1555. snprintf(s, 64, "<Buffer @%p>", buf->buffer);
  1556. } else {
  1557. snprintf(s, 64, "<Buffer @%p size %ld", buf->buffer, buf->sz);
  1558. }
  1559. res = sol_new_string(state, s);
  1560. sol_obj_free(buf);
  1561. return res;
  1562. }
  1563. sol_object_t *sol_f_buffer_new(sol_state_t *state, sol_object_t *args) {
  1564. sol_object_t *sz = sol_list_get_index(state, args, 0), *isz = sol_cast_int(state, sz);
  1565. size_t bufsz = isz->ival;
  1566. void *buf = malloc(bufsz);
  1567. sol_obj_free(sz);
  1568. sol_obj_free(isz);
  1569. if(buf) {
  1570. return sol_new_buffer(state, buf, bufsz, OWN_FREE, NULL, NULL);
  1571. }
  1572. return sol_incref(state->None);
  1573. }
  1574. sol_object_t *sol_f_buffer_get(sol_state_t *state, sol_object_t *args) {
  1575. sol_object_t *buf = sol_list_get_index(state, args, 0), *tp = sol_list_get_index(state, args, 1), *off = sol_list_get_index(state, args, 2);
  1576. sol_object_t *itp = sol_cast_int(state, tp), *ioff, *res=NULL;
  1577. sol_buftype_t buftp = itp->ival;
  1578. char *data, cbuf[2]={0, 0};
  1579. sol_obj_free(tp);
  1580. sol_obj_free(itp);
  1581. if(!sol_is_none(state, off)) {
  1582. ioff = sol_cast_int(state, off);
  1583. } else {
  1584. ioff = sol_new_int(state, 0);
  1585. }
  1586. sol_obj_free(off);
  1587. if(buf->sz != -1 && (ioff->ival >= buf->sz || ioff->ival < 0)) {
  1588. sol_obj_free(buf);
  1589. sol_obj_free(ioff);
  1590. return sol_incref(state->None);
  1591. }
  1592. data = ((char *) buf->buffer)+ioff->ival;
  1593. sol_obj_free(buf);
  1594. sol_obj_free(ioff);
  1595. switch(buftp) {
  1596. case BUF_UINT8:
  1597. res = sol_new_int(state, *((uint8_t *) data));
  1598. break;
  1599. case BUF_UINT16:
  1600. res = sol_new_int(state, *((uint16_t *) data));
  1601. break;
  1602. case BUF_UINT32:
  1603. res = sol_new_int(state, *((uint32_t *) data));
  1604. break;
  1605. case BUF_UINT64:
  1606. res = sol_new_int(state, *((uint64_t *) data));
  1607. break;
  1608. case BUF_INT8:
  1609. res = sol_new_int(state, *((int8_t *) data));
  1610. break;
  1611. case BUF_INT16:
  1612. res = sol_new_int(state, *((int16_t *) data));
  1613. break;
  1614. case BUF_INT32:
  1615. res = sol_new_int(state, *((int32_t *) data));
  1616. break;
  1617. case BUF_INT64:
  1618. res = sol_new_int(state, *((int64_t *) data));
  1619. break;
  1620. case BUF_CHAR:
  1621. cbuf[0] = *((char *) data);
  1622. res = sol_new_string(state, cbuf);
  1623. break;
  1624. case BUF_BYTE:
  1625. res = sol_new_int(state, *((unsigned char *) data));
  1626. break;
  1627. case BUF_INT:
  1628. res = sol_new_int(state, *((int *) data));
  1629. break;
  1630. case BUF_UINT:
  1631. res = sol_new_int(state, *((unsigned int *) data));
  1632. break;
  1633. case BUF_LONG:
  1634. res = sol_new_int(state, *((long *) data));
  1635. break;
  1636. case BUF_ULONG:
  1637. res = sol_new_int(state, *((unsigned long *) data));
  1638. break;
  1639. case BUF_FLOAT:
  1640. res = sol_new_float(state, *((float *) data));
  1641. break;
  1642. case BUF_DOUBLE:
  1643. res = sol_new_float(state, *((double *) data));
  1644. break;
  1645. case BUF_CSTR:
  1646. res = sol_new_string(state, (char *) data);
  1647. break;
  1648. case BUF_PTR:
  1649. res = sol_new_buffer(state, ((void *) *((unsigned long *) data)), -1, OWN_NONE, NULL, NULL);
  1650. break;
  1651. }
  1652. if(!res) res = sol_incref(state->None);
  1653. return res;
  1654. }
  1655. sol_object_t *sol_f_buffer_set(sol_state_t *state, sol_object_t *args) {
  1656. sol_object_t *buf = sol_list_get_index(state, args, 0), *tp = sol_list_get_index(state, args, 1), *val = sol_list_get_index(state, args, 2), *off = sol_list_get_index(state, args, 3);
  1657. sol_object_t *itp = sol_cast_int(state, tp), *ioff, *ival=NULL, *fval=NULL, *sval=NULL;
  1658. sol_buftype_t buftp = itp->ival;
  1659. char *data;
  1660. sol_obj_free(tp);
  1661. sol_obj_free(itp);
  1662. if(!sol_is_none(state, off)) {
  1663. ioff = sol_cast_int(state, off);
  1664. } else {
  1665. ioff = sol_new_int(state, 0);
  1666. }
  1667. sol_obj_free(off);
  1668. if(buf->sz != -1 && (ioff->ival >= buf->sz || ioff->ival < 0)) {
  1669. sol_obj_free(buf);
  1670. sol_obj_free(ioff);
  1671. return sol_incref(state->None);
  1672. }
  1673. data = ((char *) buf->buffer)+ioff->ival;
  1674. sol_obj_free(buf);
  1675. sol_obj_free(ioff);
  1676. switch(buftp) {
  1677. case BUF_UINT8:
  1678. ival = sol_cast_int(state, val);
  1679. *((uint8_t *) data) = ival->ival;
  1680. break;
  1681. case BUF_UINT16:
  1682. ival = sol_cast_int(state, val);
  1683. *((uint16_t *) data) = ival->ival;
  1684. break;
  1685. case BUF_UINT32:
  1686. ival = sol_cast_int(state, val);
  1687. *((uint32_t *) data) = ival->ival;
  1688. break;
  1689. case BUF_UINT64:
  1690. ival = sol_cast_int(state, val);
  1691. *((uint64_t *) data) = ival->ival;
  1692. break;
  1693. case BUF_INT8:
  1694. ival = sol_cast_int(state, val);
  1695. *((int8_t *) data) = ival->ival;
  1696. break;
  1697. case BUF_INT16:
  1698. ival = sol_cast_int(state, val);
  1699. *((int16_t *) data) = ival->ival;
  1700. break;
  1701. case BUF_INT32:
  1702. ival = sol_cast_int(state, val);
  1703. *((int32_t *) data) = ival->ival;
  1704. break;
  1705. case BUF_INT64:
  1706. ival = sol_cast_int(state, val);
  1707. *((int64_t *) data) = ival->ival;
  1708. break;
  1709. case BUF_CHAR:
  1710. sval = sol_cast_string(state, val);
  1711. *((char *) data) = sval->str[0];
  1712. break;
  1713. case BUF_BYTE:
  1714. ival = sol_cast_int(state, val);
  1715. *((unsigned char *) data) = ival->ival;
  1716. break;
  1717. case BUF_INT:
  1718. ival = sol_cast_int(state, val);
  1719. *((int *) data) = ival->ival;
  1720. break;
  1721. case BUF_UINT:
  1722. ival = sol_cast_int(state, val);
  1723. *((unsigned int *) data) = ival->ival;
  1724. break;
  1725. case BUF_LONG:
  1726. ival = sol_cast_int(state, val);
  1727. *((long *) data) = ival->ival;
  1728. break;
  1729. case BUF_ULONG:
  1730. ival = sol_cast_int(state, val);
  1731. *((unsigned long *) data) = ival->ival;
  1732. break;
  1733. case BUF_FLOAT:
  1734. fval = sol_cast_float(state, val);
  1735. *((float *) data) = fval->fval;
  1736. break;
  1737. case BUF_DOUBLE:
  1738. fval = sol_cast_float(state, val);
  1739. *((double *) data) = fval->fval;
  1740. break;
  1741. case BUF_CSTR:
  1742. sval = sol_cast_string(state, val);
  1743. strcpy((char *) data, sval->str);
  1744. break;
  1745. case BUF_PTR:
  1746. if(sol_is_buffer(val)) {
  1747. *((unsigned long *) data) = ((unsigned long) val->buffer);
  1748. if(val->own == OWN_CALLF) {
  1749. val->movef(val->buffer, val->sz);
  1750. }
  1751. } else {
  1752. ival = sol_cast_int(state, val);
  1753. *((unsigned long *) data) = ival->ival;
  1754. }
  1755. break;
  1756. }
  1757. if(ival) sol_obj_free(ival);
  1758. if(fval) sol_obj_free(fval);
  1759. if(sval) sol_obj_free(sval);
  1760. return sol_incref(state->None);
  1761. }
  1762. sol_object_t *sol_f_buffer_address(sol_state_t *state, sol_object_t *args) {
  1763. sol_object_t *buf = sol_list_get_index(state, args, 0);
  1764. sol_object_t *res = sol_new_int(state, (unsigned long) buf->buffer);
  1765. sol_obj_free(buf);
  1766. return res;
  1767. }
  1768. sol_object_t *sol_f_buffer_size(sol_state_t *state, sol_object_t *args) {
  1769. sol_object_t *buf = sol_list_get_index(state, args, 0);
  1770. sol_object_t *res = sol_new_int(state, (long) buf->sz);
  1771. sol_obj_free(buf);
  1772. return res;
  1773. }
  1774. sol_object_t *sol_f_buffer_fromstring(sol_state_t *state, sol_object_t *args) {
  1775. sol_object_t *val = sol_list_get_index(state, args, 0), *sval = sol_cast_string(state, val);
  1776. size_t sz = strlen(sval->str)+1;
  1777. sol_object_t *buf = sol_new_buffer(state, malloc(sz), sz, OWN_FREE, NULL, NULL);
  1778. strcpy(buf->buffer, sval->str);
  1779. sol_obj_free(val);
  1780. sol_obj_free(sval);
  1781. return buf;
  1782. }
  1783. sol_object_t *sol_f_buffer_fromobject(sol_state_t *state, sol_object_t *args) {
  1784. sol_object_t *obj = sol_list_get_index(state, args, 0);
  1785. sol_object_t *buf = sol_new_buffer(state, obj, sizeof(sol_object_t), OWN_CALLF, (sol_freefunc_t) sol_obj_free, (sol_movefunc_t) sol_obj_acquire);
  1786. //Keep ref to obj so buf remains alive
  1787. return buf;
  1788. }
  1789. sol_object_t *sol_f_buffer_fromaddress(sol_state_t *state, sol_object_t *args) {
  1790. sol_object_t *addr = sol_list_get_index(state, args, 0), *sz = sol_list_get_index(state, args, 1);
  1791. sol_object_t *iaddr = sol_cast_int(state, addr), *isz = sol_cast_int(state, sz);
  1792. sol_object_t *buf = sol_new_buffer(state, (void *) iaddr->ival, (ssize_t) isz->ival, OWN_NONE, NULL, NULL);
  1793. sol_obj_free(addr);
  1794. sol_obj_free(sz);
  1795. sol_obj_free(iaddr);
  1796. sol_obj_free(isz);
  1797. return buf;
  1798. }
  1799. sol_object_t *sol_f_dylib_index(sol_state_t *state, sol_object_t *args) {
  1800. sol_object_t *dylib = sol_list_get_index(state, args, 0), *key = sol_list_get_index(state, args, 1), *skey = sol_cast_string(state, key);
  1801. void *handle = dlsym(dylib->dlhandle, skey->str);
  1802. sol_obj_free(dylib);
  1803. sol_obj_free(key);
  1804. sol_obj_free(skey);
  1805. if(handle) {
  1806. return sol_new_dysym(state, handle, NULL, BUF_NONE);
  1807. } else {
  1808. return sol_incref(state->None);
  1809. }
  1810. }
  1811. sol_object_t *sol_f_dylib_tostring(sol_state_t *state, sol_object_t *args) {
  1812. return sol_new_string(state, "<DyLib>");
  1813. }
  1814. sol_object_t *sol_f_dysym_call(sol_state_t *state, sol_object_t *args) {
  1815. //TODO
  1816. return sol_incref(state->None);
  1817. }
  1818. sol_object_t *sol_f_dysym_index(sol_state_t *state, sol_object_t *args) {
  1819. //TODO
  1820. return sol_incref(state->None);
  1821. }
  1822. sol_object_t *sol_f_dysym_setindex(sol_state_t *state, sol_object_t *args) {
  1823. //TODO
  1824. return sol_incref(state->None);
  1825. }
  1826. sol_object_t *sol_f_dysym_tostring(sol_state_t *state, sol_object_t *args) {
  1827. return sol_new_string(state, "<DySym>");
  1828. }
  1829. sol_object_t *sol_f_stream_blsh(sol_state_t *state, sol_object_t *args) {
  1830. return sol_f_stream_write(state, args);
  1831. }
  1832. sol_object_t *sol_f_stream_brsh(sol_state_t *state, sol_object_t *args) {
  1833. return sol_f_stream_read(state, args);
  1834. }
  1835. sol_object_t *sol_f_stream_index(sol_state_t *state, sol_object_t *args) {
  1836. sol_object_t *key = sol_list_get_index(state, args, 1), *funcs = sol_get_methods_name(state, "stream");
  1837. sol_object_t *res = sol_map_get(state, funcs, key);
  1838. sol_obj_free(key);
  1839. sol_obj_free(funcs);
  1840. return res;
  1841. }
  1842. sol_object_t *sol_f_stream_tostring(sol_state_t *state, sol_object_t *args) {
  1843. return sol_new_string(state, "<Stream>");
  1844. }
  1845. sol_object_t *sol_f_stream_write(sol_state_t *state, sol_object_t *args) {
  1846. sol_object_t *stream = sol_list_get_index(state, args, 0), *obj = sol_list_get_index(state, args, 1), *str = sol_cast_string(state, obj);
  1847. size_t sz = sol_stream_printf(state, stream, "%s", str->str);
  1848. sol_obj_free(obj);
  1849. sol_obj_free(str);
  1850. sol_obj_free(stream);
  1851. return sol_new_int(state, sz);
  1852. }
  1853. sol_object_t *sol_f_stream_read(sol_state_t *state, sol_object_t *args) {
  1854. sol_object_t *stream = sol_list_get_index(state, args, 0), *amt = sol_list_get_index(state, args, 1), *iamt, *res;
  1855. char *s=NULL, *p;
  1856. size_t count=0, max=0, pos, end;
  1857. if(sol_is_string(amt)) {
  1858. if(sol_string_eq(state, amt, "ALL")) {
  1859. pos = sol_stream_ftell(state, stream);
  1860. sol_stream_fseek(state, stream, 0, SEEK_END);
  1861. end = sol_stream_ftell(state, stream);
  1862. sol_stream_fseek(state, stream, pos, SEEK_SET);
  1863. //printf("IO: Reading %ld bytes starting at %ld\n", end-pos, pos);
  1864. s = malloc((end-pos+1)*sizeof(char));
  1865. if(sol_stream_fread(state, stream, s, sizeof(char), end-pos)<(end-pos)) {
  1866. free(s);
  1867. sol_obj_free(stream);
  1868. sol_obj_free(amt);
  1869. return sol_set_error_string(state, "IO read error");
  1870. }
  1871. s[end-pos]='\0';
  1872. } else if(sol_string_eq(state, amt, "LINE")) {
  1873. s = malloc(STDIO_CHUNK_SIZE*sizeof(char));
  1874. sol_stream_fgets(state, stream, s, STDIO_CHUNK_SIZE);
  1875. }
  1876. } else {
  1877. iamt = sol_cast_int(state, amt);
  1878. s = malloc((iamt->ival + 1)*sizeof(char));
  1879. count = sol_stream_fread(state, stream, s, sizeof(char), iamt->ival);
  1880. s[iamt->ival]='\0';
  1881. sol_obj_free(iamt);
  1882. }
  1883. if(s) {
  1884. //printf("IO: Read result: %s\n", s);
  1885. res = sol_new_string(state, s);
  1886. free(s);
  1887. } else {
  1888. //printf("IO: No read result!\n");
  1889. res = sol_incref(state->None);
  1890. }
  1891. sol_obj_free(amt);
  1892. sol_obj_free(stream);
  1893. return res;
  1894. }
  1895. sol_object_t *sol_f_stream_seek(sol_state_t *state, sol_object_t *args) {
  1896. sol_object_t *stream = sol_list_get_index(state, args, 0), *offset = sol_list_get_index(state, args, 1), *whence = sol_list_get_index(state, args, 2);
  1897. sol_object_t *ioffset = sol_cast_int(state, offset), *iwhence = sol_cast_int(state, whence);
  1898. sol_object_t *res = sol_new_int(state, sol_stream_fseek(state, stream, ioffset->ival, iwhence->ival));
  1899. sol_obj_free(stream);
  1900. sol_obj_free(offset);
  1901. sol_obj_free(whence);
  1902. sol_obj_free(ioffset);
  1903. sol_obj_free(iwhence);
  1904. return res;
  1905. }
  1906. sol_object_t *sol_f_stream_tell(sol_state_t *state, sol_object_t *args) {
  1907. sol_object_t *stream = sol_list_get_index(state, args, 0), *res = sol_new_int(state, sol_stream_ftell(state, stream));
  1908. sol_obj_free(stream);
  1909. return res;
  1910. }
  1911. sol_object_t *sol_f_stream_flush(sol_state_t *state, sol_object_t *args) {
  1912. sol_object_t *stream = sol_list_get_index(state, args, 0), *res = sol_new_int(state, sol_stream_fflush(state, stream));
  1913. sol_obj_free(stream);
  1914. return res;
  1915. }
  1916. static char *sol_FileModes[]={
  1917. NULL,
  1918. "r",
  1919. "w",
  1920. "r+",
  1921. NULL,
  1922. NULL,
  1923. "a",
  1924. "a+",
  1925. NULL,
  1926. NULL,
  1927. "w",
  1928. "w+",
  1929. NULL,
  1930. NULL,
  1931. NULL,
  1932. NULL,
  1933. NULL,
  1934. "rb",
  1935. "wb",
  1936. "r+b",
  1937. NULL,
  1938. NULL,
  1939. "ab",
  1940. "a+b",
  1941. NULL,
  1942. NULL,
  1943. "wb",
  1944. "w+b",
  1945. NULL,
  1946. NULL,
  1947. NULL,
  1948. NULL
  1949. };
  1950. sol_object_t *sol_f_stream_open(sol_state_t *state, sol_object_t *args) {
  1951. sol_object_t *fn = sol_list_get_index(state, args, 0), *mode = sol_list_get_index(state, args, 1);
  1952. sol_object_t *sfn = sol_cast_string(state, fn), *imode = sol_cast_int(state, mode);
  1953. sol_modes_t m = imode->ival;
  1954. char *smode = sol_FileModes[m];
  1955. FILE *f;
  1956. sol_obj_free(mode);
  1957. sol_obj_free(imode);
  1958. if(!smode) {
  1959. sol_obj_free(fn);
  1960. sol_obj_free(sfn);
  1961. return sol_set_error_string(state, "Bad file open mode");
  1962. }
  1963. f = fopen(sfn->str, smode);
  1964. sol_obj_free(sfn);
  1965. sol_obj_free(fn);
  1966. if(!f) return sol_set_error_string(state, "File open failed");
  1967. return sol_new_stream(state, f, m);
  1968. }