A minimalistic programming language written in C89.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

2029 行
53 KiB

4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
4ヶ月前
  1. #include "include/ink.h"
  2. #ifndef NOSTDLIB
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #ifdef INSTRUMENTATION
  8. #include <time.h>
  9. #endif
  10. #endif
  11. #define INK_RESERVED (-1)
  12. #define INK_FUNCTION_KW (-2)
  13. #define INK_DO_KW (-3)
  14. #define INK_END_KW (-4)
  15. #define INK_LABEL (-5)
  16. #define INK_RETURN (-6)
  17. #define _KEYWORD_INK_FUNCTION "fn"
  18. #define _KEYWORD_INK_DO "do"
  19. #define _KEYWORD_INK_END "end"
  20. #define _KEYWORD_INK_RETURN "return"
  21. #define min(x, y) ((x) > (y) ? (y) : (x))
  22. #define max(x, y) ((x) < (y) ? (y) : (x))
  23. struct label {
  24. int active;
  25. int dest;
  26. char* name;
  27. };
  28. #ifdef NOSTDLIB
  29. static size_t strlen(const char* c) {
  30. size_t j;
  31. j = 0;
  32. while(*(c++)) {
  33. j++;
  34. }
  35. return j;
  36. }
  37. static void* memcpy(void* _dest, const void* _src, size_t sz) {
  38. char* dest;
  39. const char* src;
  40. dest = _dest;
  41. src = _src;
  42. while(sz--) {
  43. *(dest++) = *(src++);
  44. }
  45. return dest;
  46. }
  47. static int strcmp(const char* dest, const char* src) {
  48. while(*dest != 0 && *src != 0) {
  49. if(*(dest++) != *(src++)) {
  50. return 1;
  51. }
  52. }
  53. return 0;
  54. }
  55. static void* memmove(void* _dest, const void* _src, size_t sz) {
  56. char* dest;
  57. const char* src;
  58. dest = _dest;
  59. src = _src;
  60. if (src < dest) {
  61. src += sz;
  62. dest += sz;
  63. while (sz-- > 0) {
  64. *--dest = *--src;
  65. }
  66. } else {
  67. while (sz-- > 0) {
  68. *dest++ = *src++;
  69. }
  70. }
  71. return dest;
  72. }
  73. static void* memset(void* _dest, int src, size_t sz) {
  74. char* dest;
  75. dest = _dest;
  76. while(sz--) {
  77. *(dest++) = src++;
  78. }
  79. return dest;
  80. }
  81. static int isspace(int d) {
  82. return d == ' ' || d == '\t' || d == '\n';
  83. }
  84. static int isdigit(int d) {
  85. return '0' <= d && d <= '9';
  86. }
  87. static int atoi(const char* c) {
  88. int ret;
  89. ret = 0;
  90. while(*c) {
  91. ret *= 10;
  92. ret += *c - '0';
  93. ++c;
  94. }
  95. return ret;
  96. }
  97. #endif
  98. int ink_add_native(struct context* ctx, const char* name, void(*value)(struct context*)) {
  99. int len;
  100. char* copy;
  101. if(ctx->native_words == NULL) {
  102. ctx->native_words = ctx->inner_malloc(sizeof(struct native_fn) * 8);
  103. ctx->native_words_top = 0;
  104. ctx->native_words_capacity = 8;
  105. } else if(ctx->native_words_top == ctx->native_words_capacity) {
  106. int new_count;
  107. void* renewed;
  108. new_count = (ctx->native_words_capacity + ctx->native_words_capacity/2);
  109. renewed = ctx->inner_realloc(ctx->native_words, sizeof(struct native_fn) * new_count);
  110. if(renewed == NULL) {
  111. return -3;
  112. } else {
  113. ctx->native_words = renewed;
  114. ctx->native_words_capacity = new_count;
  115. }
  116. }
  117. len = strlen(name);
  118. copy = ctx->inner_malloc(len+1);
  119. if(copy == NULL) {
  120. return -4;
  121. }
  122. memcpy(copy, name, len);
  123. copy[len] = 0;
  124. ctx->native_words[ctx->native_words_top].value = value;
  125. ctx->native_words[ctx->native_words_top].name = copy;
  126. ctx->native_words_top++;
  127. return 0;
  128. }
  129. static int ink_add_indigenous(struct context* ctx, const char* name, struct elem* m, size_t count) {
  130. int len, i;
  131. char* copy;
  132. if(ctx->words == NULL) {
  133. ctx->words = ctx->malloc(sizeof(struct fn) * 8);
  134. ctx->words_top = 0;
  135. ctx->words_capacity = 8;
  136. } else if(ctx->words_top == ctx->words_capacity) {
  137. int new_count;
  138. void* renewed;
  139. new_count = (ctx->words_capacity + ctx->words_capacity/2);
  140. renewed = ctx->realloc(ctx->words, sizeof(struct fn) * new_count);
  141. if(renewed == NULL) {
  142. return -1;
  143. } else {
  144. ctx->words = renewed;
  145. ctx->words_capacity = new_count;
  146. }
  147. }
  148. for(i = 0; i < ctx->words_top; ++i) {
  149. if(strcmp(name, ctx->words[i].name) == 0) {
  150. ctx->free(ctx->words[i].things);
  151. ctx->words[i].things = ctx->malloc(sizeof(struct elem) * count);
  152. memcpy(ctx->words[i].things, m, sizeof(struct elem) * count);
  153. ctx->words[i].size = count;
  154. return i;
  155. }
  156. }
  157. len = strlen(name);
  158. copy = ctx->malloc(len+1);
  159. if(copy == NULL) {
  160. return -2;
  161. }
  162. memcpy(copy, name, len);
  163. copy[len] = 0;
  164. ctx->words[ctx->words_top].things = ctx->malloc(sizeof(struct elem) * count);
  165. memcpy(ctx->words[ctx->words_top].things, m, sizeof(struct elem) * count);
  166. ctx->words[ctx->words_top].size = count;
  167. ctx->words[ctx->words_top].name = copy;
  168. return ctx->words_top++;
  169. }
  170. /**
  171. *
  172. * @param ctx The context
  173. * @param name The name to add
  174. * @internal add a lexed string to the parser
  175. * @return the id of the string in the list
  176. */
  177. static int ink_add_lex_string(struct context* ctx, const char* name) {
  178. int i;
  179. int len;
  180. if(ctx->lex_reserved_words == NULL) {
  181. ctx->lex_reserved_words = ctx->inner_malloc(sizeof(char*) * 8);
  182. ctx->lex_reserved_words_top = 0;
  183. ctx->lex_reserved_words_capacity = 8;
  184. } else if(ctx->lex_reserved_words_top == ctx->lex_reserved_words_capacity) {
  185. int new_count;
  186. void* renewed;
  187. new_count = (ctx->lex_reserved_words_capacity + ctx->lex_reserved_words_capacity/2);
  188. renewed = ctx->inner_realloc(ctx->lex_reserved_words, sizeof(struct native_fn) * new_count);
  189. if(renewed == NULL) {
  190. return -5;
  191. } else {
  192. ctx->lex_reserved_words = renewed;
  193. ctx->lex_reserved_words_capacity = new_count;
  194. }
  195. }
  196. for(i = 0; i < ctx->lex_reserved_words_top; i++) {
  197. if(strcmp(ctx->lex_reserved_words[i], name) == 0) {
  198. return i;
  199. }
  200. }
  201. len = strlen(name);
  202. i = ctx->lex_reserved_words_top;
  203. ctx->lex_reserved_words[i] = ctx->malloc(len+1);
  204. memcpy(ctx->lex_reserved_words[i], name, len);
  205. ctx->lex_reserved_words[i][len] = 0;
  206. ctx->lex_reserved_words_top++;
  207. return i;
  208. }
  209. int ink_push(struct context* ctx, struct elem value) {
  210. struct ink_routine* current;
  211. if(ctx->routine_current >= ctx->routines_top) return -65;
  212. current = ctx->routines + ctx->routine_current;
  213. if(current->stack == NULL) {
  214. current->stack = ctx->malloc(sizeof(struct elem) * 8);
  215. current->top = 0;
  216. current->capacity = 8;
  217. } else if(current->top == current->capacity) {
  218. int new_count;
  219. void* renewed;
  220. new_count = (current->capacity + current->capacity/2);
  221. renewed = ctx->realloc(current->stack, sizeof(struct elem) * new_count);
  222. if(renewed == NULL) {
  223. return -18;
  224. } else {
  225. current->stack = renewed;
  226. current->capacity = new_count;
  227. }
  228. }
  229. current->stack[current->top] = value;
  230. current->top++;
  231. return 0;
  232. }
  233. int ink_push_fn(struct context* ctx, struct stack_frame value) {
  234. struct ink_routine* current;
  235. if(ctx->routine_current >= ctx->routines_top) return -55;
  236. current = ctx->routines + ctx->routine_current;
  237. if(current->panic) return -56;
  238. if(current->function_stack == NULL) {
  239. current->function_stack = ctx->malloc(sizeof(struct stack_frame) * 8);
  240. current->function_stack_top = 0;
  241. current->function_stack_capacity = 8;
  242. } else if(current->function_stack_top == current->function_stack_capacity) {
  243. int new_count;
  244. void* renewed;
  245. new_count = (current->function_stack_capacity + current->function_stack_capacity/2);
  246. renewed = ctx->realloc(current->function_stack, sizeof(struct stack_frame) * new_count);
  247. if(renewed == NULL) {
  248. return -9;
  249. } else {
  250. current->function_stack = renewed;
  251. current->function_stack_capacity = new_count;
  252. }
  253. }
  254. current->function_stack[current->function_stack_top] = value;
  255. current->function_stack_top++;
  256. return 0;
  257. }
  258. void ink_pop_fn(struct context* ctx) {
  259. if(ctx->routine_current >= ctx->routines_top) return;
  260. if(ctx->routines[ctx->routine_current].panic) return;
  261. if(ctx->routines[ctx->routine_current].function_stack == NULL) return;
  262. if(ctx->routines[ctx->routine_current].function_stack_top == 0) return;
  263. ctx->routines[ctx->routine_current].function_stack_top--;
  264. }
  265. void ink_pop(struct context* ctx) {
  266. if(ctx->routine_current >= ctx->routines_top) return;
  267. if(ctx->routines[ctx->routine_current].panic) return;
  268. if(ctx->routines[ctx->routine_current].stack == NULL) return;
  269. if(ctx->routines[ctx->routine_current].top == 0) return;
  270. ctx->routines[ctx->routine_current].top--;
  271. }
  272. struct context* ink_make_context(void*(*malloc)(size_t), void*(*realloc)(void*, size_t), void(*free)(void*), int(*putchar)(int)) {
  273. struct context* ctx;
  274. ctx = (struct context*)malloc(sizeof(struct context));
  275. ctx->malloc = malloc;
  276. ctx->realloc = realloc;
  277. ctx->free = free;
  278. ctx->inner_malloc = malloc;
  279. ctx->inner_realloc = realloc;
  280. ctx->inner_free = free;
  281. ctx->putchar = putchar;
  282. ctx->panic = 0;
  283. ctx->routines = NULL;
  284. ctx->routines_capacity = 0;
  285. ctx->routines_top = 0;
  286. ctx->types = NULL;
  287. ctx->types_capacity = 0;
  288. ctx->types_top = 0;
  289. ctx->native_words = NULL;
  290. ctx->native_words_capacity = 0;
  291. ctx->native_words_top = 0;
  292. ctx->words = NULL;
  293. ctx->words_capacity = 0;
  294. ctx->words_top = 0;
  295. ctx->lex_reserved_words = NULL;
  296. ctx->lex_reserved_words_capacity = 0;
  297. ctx->lex_reserved_words_top = 0;
  298. ctx->collections = 0;
  299. ctx->steps = 0;
  300. return ctx;
  301. }
  302. /**
  303. * Allocates a string that contains the integer
  304. * @param _ context (used to allocate)
  305. * @param cpy the value
  306. * @return the allocated string, needs to be freed by ctx->free
  307. * @internal this function is slightly cursed
  308. */
  309. static char* ink_itoa(struct context* _, int cpy) {
  310. char* n;
  311. char* it;
  312. n = _->malloc(16);
  313. n[15] = 0;
  314. it = n+15;
  315. do {
  316. it--;
  317. *it = (cpy % 10) + '0';
  318. cpy = cpy / 10;
  319. } while(cpy);
  320. memmove(n, it, 16 - (it-n));
  321. return n;
  322. }
  323. #ifndef NOSTDLIB
  324. struct context* ink_make_default_context(void) {
  325. struct context* ctx;
  326. ctx = ink_make_context(malloc, realloc, free, putchar);
  327. ink_std_library(ctx);
  328. return ctx;
  329. }
  330. #endif
  331. #ifndef NOSTRINGLITERALS
  332. static void new_protected_array(struct context* ctx);
  333. #endif
  334. static int ink_consume_one(int* end, struct context* pContext, char* r, int is_str) {
  335. int i;
  336. int done;
  337. struct elem value;
  338. int err;
  339. #ifndef NOSTRINGLITERALS
  340. if(is_str) {
  341. struct ink_routine* routine = pContext->routines + pContext->routine_current;
  342. struct ink_array* ary;
  343. int it = 0;
  344. new_protected_array(pContext);
  345. if(routine->top < 1) {
  346. pContext->panic = -1;
  347. return -8746;
  348. }
  349. value = routine->stack[routine->top - 1];
  350. ary = ink_get_value(pContext, value);
  351. if(ary == NULL) {
  352. pContext->panic = -1;
  353. return -8747;
  354. }
  355. for(;it != *end;++it) {
  356. struct elem character;
  357. character.type = INK_INTEGER;
  358. /* TODO: codepoint conversion and coalescence is required here */
  359. character.value = r[it];
  360. array_push(pContext, routine, ary, character);
  361. }
  362. *end = 0;
  363. return 0;
  364. }
  365. #endif
  366. is_str = is_str;
  367. if(*end == 0) {
  368. return 0;
  369. }
  370. r[*end] = 0;
  371. done = 0;
  372. if (strcmp(r, _KEYWORD_INK_FUNCTION) == 0) {
  373. value.value = 0;
  374. value.type = INK_FUNCTION_KW;
  375. done = 1;
  376. }
  377. if (!done && strcmp(r, _KEYWORD_INK_DO) == 0) {
  378. value.value = 0;
  379. value.type = INK_DO_KW;
  380. done = 1;
  381. }
  382. if (!done && strcmp(r, _KEYWORD_INK_END) == 0) {
  383. value.value = 0;
  384. value.type = INK_END_KW;
  385. done = 1;
  386. }
  387. if (!done && strcmp(r, _KEYWORD_INK_RETURN) == 0) {
  388. value.value = 0;
  389. value.type = INK_RETURN;
  390. done = 1;
  391. }
  392. if(done) {
  393. err = ink_push(pContext, value);
  394. if(err < 0) {
  395. return -19;
  396. }
  397. }
  398. if (!done) {
  399. for (i = 0; i < pContext->words_top; ++i) {
  400. if (strcmp(r, pContext->words[i].name) == 0) {
  401. value.value = i;
  402. value.type = INK_FUNCTION;
  403. err = ink_push(pContext, value);
  404. if(err < 0) {
  405. return -20;
  406. }
  407. done = 1;
  408. break;
  409. }
  410. }
  411. }
  412. if (!done) {
  413. for (i = 0; i < pContext->native_words_top; ++i) {
  414. if (strcmp(r, pContext->native_words[i].name) == 0) {
  415. value.value = i;
  416. value.type = INK_NATIVE_FUNCTION;
  417. err = ink_push(pContext, value);
  418. if(err < 0) {
  419. return -21;
  420. }
  421. done = 1;
  422. break;
  423. }
  424. }
  425. }
  426. if (!done) {
  427. for(i = (r[0] == '-'); i < *end; i++) {
  428. if(!isdigit(r[i])){
  429. goto not_an_int;
  430. }
  431. }
  432. value.value = atoi(r);
  433. value.type = INK_INTEGER;
  434. err = ink_push(pContext, value);
  435. if(err < 0) {
  436. return -22;
  437. }
  438. done = 1;
  439. }
  440. not_an_int:
  441. if (!done) {
  442. i = ink_add_lex_string(pContext, r);
  443. if(i < 0) {
  444. pContext->panic = 1;
  445. return -7;
  446. }
  447. value.value = i;
  448. if(r[strlen(r) - 1] == ':') {
  449. value.type = INK_LABEL;
  450. } else {
  451. value.type = INK_RESERVED;
  452. }
  453. err = ink_push(pContext, value);
  454. if(err < 0) {
  455. return -23;
  456. }
  457. }
  458. *end = 0;
  459. return 0;
  460. }
  461. static int ink_lex(struct context *pContext, const char* buffer) {
  462. /* Limits the token size to 127 chars */
  463. char r[128];
  464. int end;
  465. int err;
  466. #ifndef NOSTRINGLITERALS
  467. int parses_string;
  468. #endif
  469. end = 0;
  470. restart_after_comment:
  471. #ifndef NOSTRINGLITERALS
  472. parses_string = 0;
  473. #endif
  474. while(*buffer != 0) {
  475. #ifndef NOSTRINGLITERALS
  476. if(parses_string) {
  477. switch(*buffer) {
  478. case '"': {
  479. if(*(buffer+1) == 0 || isspace(*(buffer+1))) {
  480. err = ink_consume_one(&end, pContext, r, 1);
  481. if(err < 0) {
  482. pContext->panic = 1;
  483. return -995;
  484. }
  485. parses_string = 0;
  486. } else if(*(buffer+1) == '"') {
  487. r[end] = *buffer;
  488. ++end;
  489. ++buffer;
  490. } else if(*(buffer+1) == '/' && *(buffer+2) == '"') {
  491. r[end] = '\n';
  492. ++end;
  493. ++buffer;
  494. ++buffer;
  495. } else {
  496. pContext->panic = 1;
  497. return -994;
  498. }
  499. }break;
  500. default:
  501. r[end] = *buffer;
  502. ++end;
  503. }
  504. } else /* ... */
  505. #endif
  506. if(isspace(*buffer)) {
  507. if(end == 1 && r[0] == '#') {
  508. while(*buffer != '\n' && *buffer != 0) {
  509. ++buffer;
  510. }
  511. end = 0;
  512. goto restart_after_comment;
  513. }
  514. err = ink_consume_one(&end, pContext, r, 0);
  515. if(err < 0) {
  516. pContext->panic = 1;
  517. return -8;
  518. }
  519. } else /* ... */
  520. #ifndef NOSTRINGLITERALS
  521. if(end == 0 && *buffer == '"' && !parses_string) {
  522. parses_string = 1;
  523. } else /* ... */
  524. #endif
  525. {
  526. r[end] = *buffer;
  527. ++end;
  528. }
  529. ++buffer;
  530. }
  531. err = ink_consume_one(&end, pContext, r, 0);
  532. if(err < 0) {
  533. pContext->panic = 1;
  534. return -9;
  535. }
  536. return 0;
  537. }
  538. static int lblcmp(const char* label, const char* other, size_t label_sz) {
  539. while (label_sz != 1) {
  540. if(*other == 0) return 1;
  541. if(*label != *other) return 1;
  542. ++label;
  543. ++other;
  544. label_sz--;
  545. }
  546. return 0;
  547. }
  548. int ink_make_routine(struct context* ctx) {
  549. struct ink_routine* it;
  550. struct ink_routine* end;
  551. /* Allocate space if needed */
  552. if(ctx->routines == NULL) {
  553. ctx->routines = ctx->inner_malloc(sizeof(struct ink_routine) * 8);
  554. ctx->routines_top = 0;
  555. ctx->routines_capacity = 8;
  556. it = ctx->routines;
  557. end = ctx->routines + 8;
  558. for(;it != end;++it) {
  559. it->stack = NULL;
  560. it->function_stack = NULL;
  561. it->panic = INK_ROUTINE_CAN_REUSE;
  562. }
  563. } else if(ctx->routines_top == ctx->routines_capacity) {
  564. int new_count;
  565. void* renewed;
  566. new_count = (ctx->routines_capacity + ctx->routines_capacity/2);
  567. renewed = ctx->inner_realloc(ctx->routines, sizeof(struct ink_routine) * new_count);
  568. if(renewed == NULL) {
  569. return -99;
  570. } else {
  571. ctx->routines = renewed;
  572. it = ctx->routines + ctx->routines_capacity;
  573. end = ctx->routines + new_count;
  574. for(;it != end;++it) {
  575. it->panic = INK_ROUTINE_CAN_REUSE;
  576. }
  577. ctx->routines_capacity = new_count;
  578. }
  579. }
  580. it = ctx->routines;
  581. end = ctx->routines + ctx->routines_capacity;
  582. /* Looks for a reusable routine space then uses it */
  583. for(;it != end;++it) {
  584. if(it->panic == INK_ROUTINE_CAN_REUSE) {
  585. int idx;
  586. it->panic = 0;
  587. it->stack = NULL;
  588. it->top = 0;
  589. it->capacity = 0;
  590. it->function_stack = NULL;
  591. it->function_stack_top = 0;
  592. it->function_stack_capacity = 0;
  593. idx = it - ctx->routines;
  594. if(idx >= ctx->routines_top) {
  595. ctx->routines_top = idx + 1;
  596. }
  597. return idx;
  598. }
  599. }
  600. return -758;
  601. }
  602. int ink_kill_routine(struct context* ctx, int routine){
  603. struct ink_routine* curr;
  604. if(routine < 0 || routine >= ctx->routines_top) {
  605. return 0;
  606. }
  607. curr = ctx->routines + routine;
  608. if(curr->panic == INK_ROUTINE_CAN_REUSE) {
  609. return 0;
  610. }
  611. if(curr->stack != NULL) {
  612. ctx->free(curr->stack);
  613. curr->stack = NULL;
  614. }
  615. if(curr->function_stack != NULL) {
  616. ctx->free(curr->function_stack);
  617. curr->function_stack = NULL;
  618. }
  619. curr->panic = INK_ROUTINE_CAN_REUSE;
  620. return 1;
  621. }
  622. /**
  623. *
  624. * @param pContext
  625. * @param executable_buffer
  626. * @param executable_buffer_top
  627. * @internal Loop from hell
  628. */
  629. static int ink_parse(struct context* pContext, struct elem* executable_buffer, int* executable_buffer_top) {
  630. struct ink_routine* currentRoutine;
  631. int i, function_buffer_top, function_name, mode;
  632. int err;
  633. #define LABEL_BUFFER 128
  634. #define FUNCTION_BUFFER 256
  635. struct label labels[LABEL_BUFFER];
  636. struct elem function_buffer[FUNCTION_BUFFER];
  637. /* TODO: add checks for overflows in these arrays */
  638. currentRoutine = pContext->routines + pContext->routine_current;
  639. function_buffer_top = 0;
  640. function_name = -1;
  641. #define MODE_EXECUTABLE 0
  642. #define MODE_FUNCTION 1
  643. #define MODE_DO 2
  644. mode = MODE_EXECUTABLE;
  645. memset(labels, 0, sizeof(struct label)*LABEL_BUFFER);
  646. /* Loop from hell, good luck, pro-tip: leave the parser alone */
  647. for(i = 0; i < currentRoutine->top; ++i) {
  648. struct elem current;
  649. current = currentRoutine->stack[i];
  650. switch (mode) {
  651. case MODE_EXECUTABLE:
  652. switch(current.type) {
  653. case INK_FUNCTION_KW:
  654. mode = MODE_FUNCTION;
  655. function_name = -1;
  656. goto next_token;
  657. case INK_DO_KW:
  658. case INK_END_KW:
  659. return -26;
  660. default:
  661. executable_buffer[*executable_buffer_top] = current;
  662. *executable_buffer_top += 1;
  663. }
  664. break;
  665. case MODE_FUNCTION:
  666. if(current.type == INK_DO_KW) {
  667. if(function_name == -1) {
  668. return -27;
  669. } else {
  670. mode = MODE_DO;
  671. memset(labels, 0, sizeof(struct label)*128);
  672. goto next_token;
  673. }
  674. }
  675. if(function_name != -1) {
  676. return -28;
  677. }
  678. if(current.type != INK_RESERVED) {
  679. return -29;
  680. }
  681. function_name = current.value;
  682. break;
  683. case MODE_DO:
  684. if(current.type == INK_END_KW) {
  685. int j;
  686. for(j = 0; j < function_buffer_top; j++) {
  687. struct elem pt;
  688. pt = function_buffer[j];
  689. if(pt.type == INK_LABEL) {
  690. int k;
  691. for(k = 0; k < LABEL_BUFFER; k++) {
  692. if(labels[k].active) {
  693. if(strcmp(labels[k].name, pContext->lex_reserved_words[pt.value]) == 0) {
  694. labels[k].dest = j;
  695. return -30;
  696. break;
  697. }
  698. } else {
  699. labels[k].active = 1;
  700. labels[k].name = pContext->lex_reserved_words[pt.value];
  701. labels[k].dest = j;
  702. memcpy(function_buffer+j, function_buffer+j+1, sizeof(struct elem)*(function_buffer_top-j-1));
  703. function_buffer_top--;
  704. j--;
  705. break;
  706. }
  707. }
  708. }
  709. }
  710. for(j = 0; j < function_buffer_top; j++) {
  711. struct elem pt;
  712. pt = function_buffer[j];
  713. if(pt.type == INK_RESERVED) {
  714. int k;
  715. for(k = 0; k < LABEL_BUFFER; k++) {
  716. if(labels[k].active) {
  717. int label_sz;
  718. const char* lbl;
  719. lbl = labels[k].name;
  720. label_sz = strlen(lbl);
  721. if(lblcmp(labels[k].name, pContext->lex_reserved_words[pt.value], label_sz) == 0) {
  722. function_buffer[j].type = INK_INTEGER;
  723. function_buffer[j].value = labels[k].dest - j;
  724. break;
  725. }
  726. } else break;
  727. }
  728. }
  729. }
  730. err = ink_add_indigenous(pContext, pContext->lex_reserved_words[function_name], function_buffer, function_buffer_top);
  731. if(err < 0) {
  732. pContext->panic = 1;
  733. return -33;
  734. }
  735. function_buffer_top = 0;
  736. mode = MODE_EXECUTABLE;
  737. goto next_token;
  738. }
  739. function_buffer[function_buffer_top] = current;
  740. function_buffer_top += 1;
  741. break;
  742. }
  743. next_token: i=i;
  744. }
  745. if(mode == MODE_FUNCTION || mode == MODE_DO) {
  746. return -32;
  747. }
  748. return 0;
  749. #undef MODE_EXECUTABLE
  750. #undef MODE_FUNCTION
  751. #undef MODE_DO
  752. #undef LABEL_BUFFER
  753. #undef FUNCTION_BUFFER
  754. }
  755. int ink_step(struct context *pContext) {
  756. struct ink_routine* currentRoutine;
  757. struct stack_frame frame;
  758. struct stack_frame* top;
  759. struct elem next;
  760. int t;
  761. currentRoutine = pContext->routines + pContext->routine_current;
  762. pContext->steps++;
  763. if(currentRoutine->function_stack_top == 0) return 0;
  764. if(pContext->panic) {
  765. return -1;
  766. }
  767. top = &currentRoutine->function_stack[currentRoutine->function_stack_top-1];
  768. t = top->executing.type;
  769. switch(t) {
  770. case INK_NATIVE_FUNCTION:
  771. if(top->index != 0) {
  772. ink_pop_fn(pContext);
  773. } else {
  774. top->index++;
  775. if(pContext->native_words_top <= top->executing.value) {
  776. pContext->panic = 1;
  777. return -1;
  778. }
  779. pContext->native_words[top->executing.value].value(pContext);
  780. }
  781. break;
  782. case INK_FUNCTION:
  783. if(pContext->words_top <= top->executing.value) {
  784. pContext->panic = 1;
  785. return -1;
  786. }
  787. if(top->index >= pContext->words[top->executing.value].size) {
  788. ink_pop_fn(pContext);
  789. } else {
  790. next = pContext->words[top->executing.value].things[top->index];
  791. if(next.type == INK_RETURN) {
  792. ink_pop_fn(pContext);
  793. return 1;
  794. }
  795. frame.executing = next;
  796. frame.index = 0;
  797. t = ink_push_fn(pContext, frame);
  798. if(t < 0) {
  799. pContext->panic = 1;
  800. return -11;
  801. }
  802. top->index++;
  803. }
  804. break;
  805. default:
  806. t = ink_push(pContext, top->executing);
  807. if(t < 0) {
  808. pContext->panic = 1;
  809. return -25;
  810. }
  811. ink_pop_fn(pContext);
  812. break;
  813. }
  814. return 1;
  815. }
  816. int ink_compile(struct context *pContext, const char* buffer) {
  817. int routine, saved, executable_buffer_top;
  818. /* Main function has a size limit of 256 (need to know that for REPL */
  819. struct elem executable_buffer[256];
  820. struct ink_routine* currentRoutine;
  821. int err;
  822. struct stack_frame frame;
  823. char* integer;
  824. size_t integer_size;
  825. char main_fn[32] = "__-MAIN-__";
  826. routine = ink_make_routine(pContext);
  827. saved = pContext->routine_current;
  828. pContext->routine_current = routine;
  829. currentRoutine = pContext->routines + routine;
  830. currentRoutine->stack = NULL;
  831. currentRoutine->top = 0;
  832. currentRoutine->capacity = 0;
  833. err = ink_lex(pContext, buffer);
  834. if(err < 0) {
  835. pContext->panic = 1;
  836. return -1;
  837. }
  838. executable_buffer_top = 0;
  839. err = ink_parse(pContext, executable_buffer, &executable_buffer_top);
  840. if(err < 0) {
  841. pContext->panic = 1;
  842. return -1;
  843. }
  844. if(executable_buffer_top != 0) {
  845. integer = ink_itoa(pContext, routine);
  846. integer_size = strlen(integer);
  847. memcpy(main_fn + 10, integer, integer_size);
  848. pContext->free(integer);
  849. main_fn[10 + integer_size] = 0;
  850. frame.executing.value = ink_add_indigenous(pContext, main_fn, executable_buffer, executable_buffer_top);
  851. if (frame.executing.value < 0) {
  852. pContext->panic = 1;
  853. return -1;
  854. }
  855. frame.executing.type = INK_FUNCTION;
  856. frame.index = 0;
  857. err = ink_push_fn(pContext, frame);
  858. pContext->routines[pContext->routine_current].top = 0;
  859. if (err < 0) {
  860. pContext->panic = 1;
  861. return -1;
  862. }
  863. } else {
  864. pContext->routines[pContext->routine_current].panic = INK_ROUTINE_SUCCESS;
  865. }
  866. pContext->routine_current = saved;
  867. return routine;
  868. }
  869. int ink_can_run(struct context* pContext) {
  870. int it;
  871. for(it = 0; it < pContext->routines_top; ++it) {
  872. if(pContext->routines[it].panic == 0) {
  873. return 1;
  874. }
  875. }
  876. return 0;
  877. }
  878. int ink_step_everyone(struct context* pContext) {
  879. int out;
  880. pContext->routine_current = -1;
  881. for(;;) {
  882. /* Increment to next runnable routine */
  883. do{
  884. ++(pContext->routine_current);
  885. } while(pContext->routine_current < pContext->routines_top && pContext->routines[pContext->routine_current].panic != 0);
  886. /* Exit condition */
  887. if(pContext->routine_current >= pContext->routines_top) break;
  888. /* Kill? */
  889. if(pContext->routines[pContext->routine_current].panic == INK_ROUTINE_SUCCESS) {
  890. ink_kill_routine(pContext, pContext->routine_current);
  891. }
  892. /* Step! */
  893. out = ink_step(pContext);
  894. if(out == 0) {
  895. pContext->routines[pContext->routine_current].panic = INK_ROUTINE_SUCCESS;
  896. } else if(out < 0) {
  897. pContext->routines[pContext->routine_current].panic = out;
  898. }
  899. }
  900. return 0;
  901. }
  902. int ink_new_type(
  903. struct context* ctx,
  904. const char* type_name,
  905. int size,
  906. void (*collect)(struct context*,void*),
  907. struct ink_collection_list (*gc)(struct context*,void*)
  908. ) {
  909. if(ctx->panic) return -128;
  910. /* Resize for push */
  911. if(ctx->types == NULL) {
  912. ctx->types = ctx->inner_malloc(sizeof(struct ink_type) * 8);
  913. ctx->types_top = 0;
  914. ctx->types_capacity = 8;
  915. } else if(ctx->types_top == ctx->types_capacity) {
  916. int new_count;
  917. void* renewed;
  918. new_count = (ctx->types_capacity + ctx->types_capacity/2);
  919. renewed = ctx->inner_realloc(ctx->types, sizeof(struct ink_type) * new_count);
  920. if(renewed == NULL) {
  921. return -129;
  922. } else {
  923. ctx->types = renewed;
  924. ctx->types_capacity = new_count;
  925. }
  926. }
  927. /* Push */
  928. ctx->types[ctx->types_top].name = type_name;
  929. ctx->types[ctx->types_top].element_size = size;
  930. ctx->types[ctx->types_top].elements = NULL;
  931. ctx->types[ctx->types_top].elements_top = 0;
  932. ctx->types[ctx->types_top].elements_capacity = 0;
  933. ctx->types[ctx->types_top].collect = collect;
  934. ctx->types[ctx->types_top].gc = gc;
  935. ctx->types_top++;
  936. /* Satisfying the minimal value requirement */
  937. return ctx->types_top - 1 + 16;
  938. }
  939. static struct element_slab* ink_get_value_link(struct context* ctx, struct elem ref) {
  940. int type_id;
  941. if(ref.type < 16) return NULL;
  942. type_id = ref.type - 16;
  943. if(type_id >= ctx->types_top) return NULL;
  944. if(ctx->types[type_id].element_size == 0) return NULL;
  945. if(ref.value < 0) return NULL;
  946. if(ref.value >= ctx->types[type_id].elements_top) return NULL;
  947. if(! ctx->types[type_id].elements[ref.value].in_use) return NULL;
  948. return ctx->types[type_id].elements + ref.value;
  949. }
  950. void* ink_get_value(struct context* ctx, struct elem ref) {
  951. struct element_slab* s;
  952. s = ink_get_value_link(ctx, ref);
  953. if(s == NULL) return NULL;
  954. return s->data;
  955. }
  956. struct elem ink_make_native_unsafe(struct context* ctx, int type, void* ptr, int is_protected) {
  957. int type_id;
  958. struct elem ret;
  959. int g, i;
  960. if(type < 16) {
  961. ret.type = 0;
  962. ret.value = -130;
  963. return ret;
  964. }
  965. /* Apply invariant of the user defined types */
  966. type_id = type - 16;
  967. if(type_id >= ctx->types_top) {
  968. ret.type = 0;
  969. ret.value = -129;
  970. return ret;
  971. }
  972. if(ctx->panic) {
  973. ret.type = 0;
  974. ret.value = -135;
  975. return ret;
  976. }
  977. /* Resize for push of value in store */
  978. if(ctx->types[type_id].elements == NULL) {
  979. ctx->types[type_id].elements = ctx->inner_malloc(sizeof(struct element_slab) * 8);
  980. ctx->types[type_id].elements_top = 0;
  981. ctx->types[type_id].elements_capacity = 8;
  982. memset(ctx->types[type_id].elements + ctx->types[type_id].elements_top, 0, sizeof(struct element_slab)*(ctx->types[type_id].elements_capacity - ctx->types[type_id].elements_top));
  983. } else if(ctx->types[type_id].elements_top == ctx->types[type_id].elements_capacity) {
  984. int new_count;
  985. void* renewed;
  986. new_count = (ctx->types[type_id].elements_capacity + ctx->types[type_id].elements_capacity/2);
  987. renewed = ctx->inner_realloc(ctx->types[type_id].elements, sizeof(struct element_slab) * new_count);
  988. if(renewed == NULL) {
  989. ret.type = 0;
  990. ret.value = -129;
  991. return ret;
  992. } else {
  993. ctx->types[type_id].elements = renewed;
  994. ctx->types[type_id].elements_capacity = new_count;
  995. memset(ctx->types[type_id].elements + ctx->types[type_id].elements_top, 0, sizeof(struct element_slab)*(ctx->types[type_id].elements_capacity - ctx->types[type_id].elements_top));
  996. }
  997. }
  998. /* Push value in store */
  999. g = ctx->types[type_id].elements_capacity;
  1000. for(i = 0; i < g; ++i) {
  1001. if(! ctx->types[type_id].elements[i].in_use) {
  1002. ctx->types[type_id].elements[i].in_use = 1;
  1003. ctx->types[type_id].elements[i].uses = 1;
  1004. ctx->types[type_id].elements[i].is_protected = is_protected;
  1005. if(ctx->types[type_id].element_size < 0) {
  1006. ctx->types[type_id].elements[i].data = ptr;
  1007. } else {
  1008. void* new_ptr = ctx->malloc(ctx->types[type_id].element_size);
  1009. if(new_ptr == NULL) {
  1010. ret.type = 0;
  1011. ret.value = -139;
  1012. return ret;
  1013. }
  1014. memcpy(new_ptr, ptr, ctx->types[type_id].element_size);
  1015. ctx->types[type_id].elements[i].data = new_ptr;
  1016. }
  1017. ctx->types[type_id].elements_top = max(ctx->types[type_id].elements_top, i+1);
  1018. ret.type = type;
  1019. ret.value = i;
  1020. return ret;
  1021. }
  1022. }
  1023. ret.type = 0;
  1024. ret.value = -140;
  1025. return ret;
  1026. }
  1027. struct elem ink_make_native(struct context* ctx, int type, void* ptr) {
  1028. return ink_make_native_unsafe(ctx, type, ptr, 0);
  1029. }
  1030. void ink_gc(struct context* ctx) {
  1031. int i, j, k;
  1032. int marked;
  1033. struct element_slab* v;
  1034. for(i = 0; i < ctx->types_top; ++i) {
  1035. for(j = 0; j < ctx->types[i].elements_top; ++j) {
  1036. ctx->types[i].elements[j].uses = 0;
  1037. }
  1038. }
  1039. /* Start by marking the roots of the routines, Clear the routines if possible */
  1040. for(i = 0; i < ctx->routines_top; ++i) {
  1041. if(ctx->routines[i].panic == INK_ROUTINE_SUCCESS) {
  1042. ctx->free(ctx->routines[i].stack);
  1043. ctx->free(ctx->routines[i].function_stack);
  1044. ctx->routines[i].panic = INK_ROUTINE_CAN_REUSE;
  1045. }
  1046. if(ctx->routines[i].panic == INK_ROUTINE_CAN_REUSE) {
  1047. continue;
  1048. }
  1049. for(j = 0; j < ctx->routines[i].top; ++j) {
  1050. v = ink_get_value_link(ctx, ctx->routines[i].stack[j]);
  1051. if(v != NULL) ++v->uses;
  1052. }
  1053. }
  1054. /* TODO: Mark objects contained within function code */
  1055. /* Mark the rest of the data */
  1056. do {
  1057. marked = 0;
  1058. for (i = 0; i < ctx->types_top; ++i) {
  1059. for (j = 0; j < ctx->types[i].elements_top; ++j) {
  1060. /* Only mark from things that are active and detected as in use */
  1061. if (ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].is_protected && ctx->types[i].elements[j].uses) {
  1062. struct ink_collection_list c;
  1063. c = ctx->types[i].gc(ctx, ctx->types[i].elements[j].data);
  1064. for (k = 0; k < c.count; ++k) {
  1065. v = ink_get_value_link(ctx, c.elements[k]);
  1066. /* Never mark twice to avoid infinite loops with e.g. arrays that contain themselves */
  1067. if (v != NULL && !v->uses) {
  1068. ++v->uses;
  1069. marked = 1;
  1070. }
  1071. }
  1072. if (c.elements != NULL) ctx->inner_free(c.elements);
  1073. }
  1074. }
  1075. }
  1076. } while(marked);
  1077. /* Sweep phase: explore any allocated data and sweep the unused away */
  1078. for(i = 0; i < ctx->types_top; ++i) {
  1079. for(j = 0; j < ctx->types[i].elements_top; ++j) {
  1080. if(ctx->types[i].elements[j].uses == 0 && ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].is_protected == 0) {
  1081. ctx->collections++;
  1082. ctx->types[i].collect(ctx, ctx->types[i].elements[j].data);
  1083. if(ctx->types[i].element_size > 0) {
  1084. ctx->free(ctx->types[i].elements[j].data);
  1085. }
  1086. ctx->types[i].elements[j].data = NULL;
  1087. ctx->types[i].elements[j].uses = 0;
  1088. ctx->types[i].elements[j].in_use = 0;
  1089. }
  1090. }
  1091. }
  1092. }
  1093. /**********************************************************************************************************************/
  1094. static void print_stacktrace(struct context* _) {
  1095. int i;
  1096. struct ink_routine* currentRoutine;
  1097. currentRoutine = _->routines + _->routine_current;
  1098. for(i = 0; i < currentRoutine->function_stack_top; ++i) {
  1099. struct elem thing;
  1100. char *n;
  1101. thing = currentRoutine->function_stack[i].executing;
  1102. switch(thing.type) {
  1103. case INK_NATIVE_FUNCTION: {
  1104. n = _->native_words[thing.value].name;
  1105. while (*n) {
  1106. _->putchar(*n);
  1107. ++n;
  1108. }
  1109. _->putchar(10);
  1110. break;
  1111. }
  1112. case INK_FUNCTION:{
  1113. n = _->words[thing.value].name;
  1114. while (*n) {
  1115. _->putchar(*n);
  1116. ++n;
  1117. }
  1118. _->putchar(':');
  1119. n = ink_itoa(_, currentRoutine->function_stack[i].index);
  1120. while (*n) {
  1121. _->putchar(*n);
  1122. ++n;
  1123. }
  1124. _->putchar(10);
  1125. break;
  1126. }
  1127. default:
  1128. break;
  1129. }
  1130. }
  1131. }
  1132. static void add_int(struct context* ctx) {
  1133. struct ink_routine* currentRoutine;
  1134. struct elem a;
  1135. struct elem b;
  1136. currentRoutine = ctx->routines + ctx->routine_current;
  1137. if(currentRoutine->top < 2) {
  1138. currentRoutine->panic = -1;
  1139. return;
  1140. }
  1141. a = currentRoutine->stack[currentRoutine->top-1];
  1142. b = currentRoutine->stack[currentRoutine->top-2];
  1143. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1144. ctx->panic = 1;
  1145. return;
  1146. }
  1147. ink_pop(ctx);
  1148. currentRoutine->stack[currentRoutine->top-1].value = a.value + b.value;
  1149. }
  1150. static void sub_int(struct context* ctx) {
  1151. struct ink_routine* currentRoutine;
  1152. struct elem a;
  1153. struct elem b;
  1154. currentRoutine = ctx->routines + ctx->routine_current;
  1155. if(currentRoutine->top < 2) {
  1156. currentRoutine->panic = -1;
  1157. return;
  1158. }
  1159. a = currentRoutine->stack[currentRoutine->top-1];
  1160. b = currentRoutine->stack[currentRoutine->top-2];
  1161. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1162. currentRoutine->panic = -1;
  1163. return;
  1164. }
  1165. ink_pop(ctx);
  1166. currentRoutine->stack[currentRoutine->top-1].value = b.value - a.value;
  1167. }
  1168. static void mult_int(struct context* ctx) {
  1169. struct ink_routine* currentRoutine;
  1170. struct elem a;
  1171. struct elem b;
  1172. currentRoutine = ctx->routines + ctx->routine_current;
  1173. if(currentRoutine->top < 2) {
  1174. currentRoutine->panic = -1;
  1175. return;
  1176. }
  1177. a = currentRoutine->stack[currentRoutine->top-1];
  1178. b = currentRoutine->stack[currentRoutine->top-2];
  1179. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1180. currentRoutine->panic = -1;
  1181. return;
  1182. }
  1183. ink_pop(ctx);
  1184. currentRoutine->stack[currentRoutine->top-1].value = b.value * a.value;
  1185. }
  1186. static void div_int(struct context* ctx) {
  1187. struct ink_routine* currentRoutine;
  1188. struct elem a;
  1189. struct elem b;
  1190. currentRoutine = ctx->routines + ctx->routine_current;
  1191. if(currentRoutine->top < 2) {
  1192. currentRoutine->panic = -1;
  1193. return;
  1194. }
  1195. a = currentRoutine->stack[currentRoutine->top-1];
  1196. b = currentRoutine->stack[currentRoutine->top-2];
  1197. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1198. currentRoutine->panic = -1;
  1199. return;
  1200. }
  1201. ink_pop(ctx);
  1202. currentRoutine->stack[currentRoutine->top-1].value = b.value / a.value;
  1203. }
  1204. static void is_equal(struct context* ctx) {
  1205. struct ink_routine* currentRoutine;
  1206. struct elem a;
  1207. struct elem b;
  1208. struct elem ret;
  1209. currentRoutine = ctx->routines + ctx->routine_current;
  1210. if(currentRoutine->top < 2) {
  1211. currentRoutine->panic = -1;
  1212. return;
  1213. }
  1214. a = currentRoutine->stack[currentRoutine->top-1];
  1215. b = currentRoutine->stack[currentRoutine->top-2];
  1216. ink_pop(ctx);
  1217. ink_pop(ctx);
  1218. ret.type = INK_INTEGER;
  1219. ret.value = a.value == b.value && a.type == b.type;
  1220. ink_push(ctx, ret);
  1221. }
  1222. static void is_different(struct context* ctx) {
  1223. struct ink_routine* currentRoutine;
  1224. struct elem a;
  1225. struct elem b;
  1226. struct elem ret;
  1227. currentRoutine = ctx->routines + ctx->routine_current;
  1228. if(currentRoutine->top < 2) {
  1229. currentRoutine->panic = -1;
  1230. return;
  1231. }
  1232. a = currentRoutine->stack[currentRoutine->top-1];
  1233. b = currentRoutine->stack[currentRoutine->top-2];
  1234. ink_pop(ctx);
  1235. ink_pop(ctx);
  1236. ret.type = INK_INTEGER;
  1237. ret.value = !(a.value == b.value && a.type == b.type);
  1238. ink_push(ctx, ret);
  1239. }
  1240. #ifndef NOEXTRAARITHMETIC
  1241. static void rem_int(struct context* ctx) {
  1242. struct ink_routine* currentRoutine;
  1243. struct elem a;
  1244. struct elem b;
  1245. currentRoutine = ctx->routines + ctx->routine_current;
  1246. if(currentRoutine->top < 2) {
  1247. currentRoutine->panic = -1;
  1248. return;
  1249. }
  1250. a = currentRoutine->stack[currentRoutine->top-1];
  1251. b = currentRoutine->stack[currentRoutine->top-2];
  1252. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1253. currentRoutine->panic = -1;
  1254. return;
  1255. }
  1256. ink_pop(ctx);
  1257. currentRoutine->stack[currentRoutine->top-1].value = b.value % a.value;
  1258. }
  1259. static void xor_int(struct context* ctx) {
  1260. struct ink_routine* currentRoutine;
  1261. struct elem a;
  1262. struct elem b;
  1263. currentRoutine = ctx->routines + ctx->routine_current;
  1264. if(currentRoutine->top < 2) {
  1265. currentRoutine->panic = -1;
  1266. return;
  1267. }
  1268. a = currentRoutine->stack[currentRoutine->top-1];
  1269. b = currentRoutine->stack[currentRoutine->top-2];
  1270. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1271. currentRoutine->panic = -1;
  1272. return;
  1273. }
  1274. ink_pop(ctx);
  1275. currentRoutine->stack[currentRoutine->top-1].value = b.value ^ a.value;
  1276. }
  1277. static void gt_int(struct context* ctx) {
  1278. struct ink_routine* currentRoutine;
  1279. struct elem a;
  1280. struct elem b;
  1281. currentRoutine = ctx->routines + ctx->routine_current;
  1282. if(currentRoutine->top < 2) {
  1283. currentRoutine->panic = -1;
  1284. return;
  1285. }
  1286. a = currentRoutine->stack[currentRoutine->top-1];
  1287. b = currentRoutine->stack[currentRoutine->top-2];
  1288. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1289. currentRoutine->panic = -1;
  1290. return;
  1291. }
  1292. ink_pop(ctx);
  1293. currentRoutine->stack[currentRoutine->top-1].value = b.value > a.value;
  1294. }
  1295. static void gte_int(struct context* ctx) {
  1296. struct ink_routine* currentRoutine;
  1297. struct elem a;
  1298. struct elem b;
  1299. currentRoutine = ctx->routines + ctx->routine_current;
  1300. if(currentRoutine->top < 2) {
  1301. currentRoutine->panic = -1;
  1302. return;
  1303. }
  1304. a = currentRoutine->stack[currentRoutine->top-1];
  1305. b = currentRoutine->stack[currentRoutine->top-2];
  1306. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1307. currentRoutine->panic = -1;
  1308. return;
  1309. }
  1310. ink_pop(ctx);
  1311. currentRoutine->stack[currentRoutine->top-1].value = b.value >= a.value;
  1312. }
  1313. static void lte_int(struct context* ctx) {
  1314. struct ink_routine* currentRoutine;
  1315. struct elem a;
  1316. struct elem b;
  1317. currentRoutine = ctx->routines + ctx->routine_current;
  1318. if(currentRoutine->top < 2) {
  1319. currentRoutine->panic = -1;
  1320. return;
  1321. }
  1322. a = currentRoutine->stack[currentRoutine->top-1];
  1323. b = currentRoutine->stack[currentRoutine->top-2];
  1324. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1325. currentRoutine->panic = -1;
  1326. return;
  1327. }
  1328. ink_pop(ctx);
  1329. currentRoutine->stack[currentRoutine->top-1].value = b.value <= a.value;
  1330. }
  1331. #endif /* NOEXTRAARITHMETIC */
  1332. static void lt_int(struct context* ctx) {
  1333. struct ink_routine* currentRoutine;
  1334. struct elem a;
  1335. struct elem b;
  1336. currentRoutine = ctx->routines + ctx->routine_current;
  1337. if(currentRoutine->top < 2) {
  1338. currentRoutine->panic = -1;
  1339. return;
  1340. }
  1341. a = currentRoutine->stack[currentRoutine->top-1];
  1342. b = currentRoutine->stack[currentRoutine->top-2];
  1343. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1344. currentRoutine->panic = -1;
  1345. return;
  1346. }
  1347. ink_pop(ctx);
  1348. currentRoutine->stack[currentRoutine->top-1].value = b.value < a.value;
  1349. }
  1350. static void dupe_elem(struct context* ctx) {
  1351. struct ink_routine* currentRoutine;
  1352. struct elem a;
  1353. int err;
  1354. currentRoutine = ctx->routines + ctx->routine_current;
  1355. if(currentRoutine->top < 1) {
  1356. ctx->panic = 1;
  1357. return;
  1358. }
  1359. a = currentRoutine->stack[currentRoutine->top-1];
  1360. err = ink_push(ctx, a);
  1361. if(err < 0) ctx->panic = 1;
  1362. }
  1363. static void drop_elem(struct context* ctx) {
  1364. struct ink_routine* currentRoutine;
  1365. currentRoutine = ctx->routines + ctx->routine_current;
  1366. if(currentRoutine->top < 1) {
  1367. ctx->panic = 1;
  1368. return;
  1369. }
  1370. ink_pop(ctx);
  1371. }
  1372. static void pluck_elem(struct context* ctx) {
  1373. struct ink_routine* currentRoutine;
  1374. struct elem a;
  1375. int position, err;
  1376. currentRoutine = ctx->routines + ctx->routine_current;
  1377. if(currentRoutine->top < 1) {
  1378. currentRoutine->panic = -1;
  1379. return;
  1380. }
  1381. a = currentRoutine->stack[currentRoutine->top-1];
  1382. if(a.type != INK_INTEGER) {
  1383. ctx->panic = 1;
  1384. return;
  1385. }
  1386. position = currentRoutine->top - (a.value + 1);
  1387. if(position >= currentRoutine->top || position < 0) {
  1388. ctx->panic = 1;
  1389. return;
  1390. }
  1391. ink_pop(ctx);
  1392. err = ink_push(ctx, currentRoutine->stack[position]);
  1393. if(err < 0) ctx->panic = 1;
  1394. }
  1395. static void swap_elem(struct context* ctx) {
  1396. struct ink_routine* currentRoutine;
  1397. struct elem a;
  1398. struct elem b;
  1399. currentRoutine = ctx->routines + ctx->routine_current;
  1400. if(currentRoutine->top < 2) {
  1401. currentRoutine->panic = -1;
  1402. return;
  1403. }
  1404. a = currentRoutine->stack[currentRoutine->top-1];
  1405. b = currentRoutine->stack[currentRoutine->top-2];
  1406. currentRoutine->stack[currentRoutine->top-2] = a;
  1407. currentRoutine->stack[currentRoutine->top-1] = b;
  1408. }
  1409. static void return_if(struct context* ctx) {
  1410. struct ink_routine* currentRoutine;
  1411. struct elem a;
  1412. currentRoutine = ctx->routines + ctx->routine_current;
  1413. if(currentRoutine->top < 1) {
  1414. ctx->panic = -1;
  1415. return;
  1416. }
  1417. a = currentRoutine->stack[currentRoutine->top-1];
  1418. if(a.type != INK_INTEGER) {
  1419. ctx->panic = 1;
  1420. return;
  1421. }
  1422. if(a.value) {
  1423. ink_pop_fn(ctx);
  1424. ink_pop_fn(ctx);
  1425. }
  1426. ink_pop(ctx);
  1427. return;
  1428. }
  1429. static void jump_if(struct context* ctx) {
  1430. struct ink_routine* currentRoutine;
  1431. struct elem label;
  1432. struct elem condition;
  1433. currentRoutine = ctx->routines + ctx->routine_current;
  1434. if(currentRoutine->top < 2) {
  1435. ctx->panic = -1;
  1436. return;
  1437. }
  1438. label = currentRoutine->stack[currentRoutine->top-1];
  1439. condition = currentRoutine->stack[currentRoutine->top-2];
  1440. if(label.type != INK_INTEGER || condition.type != INK_INTEGER) {
  1441. ctx->panic = -1;
  1442. return;
  1443. }
  1444. ink_pop(ctx);
  1445. ink_pop(ctx);
  1446. ink_pop_fn(ctx);
  1447. if(condition.value) {
  1448. currentRoutine->function_stack[currentRoutine->function_stack_top - 1].index += label.value - 2;
  1449. }
  1450. return;
  1451. }
  1452. static void print_int(struct context* ctx) {
  1453. struct ink_routine* currentRoutine;
  1454. struct elem a;
  1455. char* n;
  1456. char* str;
  1457. currentRoutine = ctx->routines + ctx->routine_current;
  1458. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  1459. currentRoutine->panic = -1;
  1460. return;
  1461. }
  1462. a = currentRoutine->stack[currentRoutine->top-1];
  1463. ink_pop(ctx);
  1464. n = ink_itoa(ctx, a.value);
  1465. str = n;
  1466. while (*str) {
  1467. ctx->putchar(*str);
  1468. ++str;
  1469. }
  1470. ctx->free(n);
  1471. }
  1472. static void print_as_utf8(struct context* ctx) {
  1473. struct ink_routine* currentRoutine;
  1474. struct elem a;
  1475. currentRoutine = ctx->routines + ctx->routine_current;
  1476. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  1477. ctx->panic = -1;
  1478. return;
  1479. }
  1480. a = currentRoutine->stack[currentRoutine->top-1];
  1481. if(a.value <= 0x7F) {
  1482. ctx->putchar(a.value);
  1483. } else if(a.value <= 0x7FF) {
  1484. ctx->putchar(((a.value & 0xFC0) >> 6) | 192);
  1485. ctx->putchar((a.value & 0x3F) | 128);
  1486. } else if(a.value <= 0xFFFF) {
  1487. ctx->putchar(((a.value & 0x3F000) >> 12) | 224);
  1488. ctx->putchar(((a.value & 0xFC0) >> 6) | 128);
  1489. ctx->putchar((a.value & 0x3F) | 128);
  1490. } else if(a.value <= 0x10FFFF) {
  1491. ctx->putchar(((a.value & 0x3C0000) >> 18) | 240);
  1492. ctx->putchar(((a.value & 0x3F000) >> 12) | 128);
  1493. ctx->putchar(((a.value & 0xFC0) >> 6) | 128);
  1494. ctx->putchar((a.value & 0x3F) | 128);
  1495. } else {
  1496. ctx->panic = -1;
  1497. return;
  1498. }
  1499. ink_pop(ctx);
  1500. }
  1501. int get_type_by_name(struct context* ctx, const char* name) {
  1502. int i;
  1503. for(i = 0; i < ctx->types_top; ++i) {
  1504. if(strcmp(ctx->types[i].name, name) == 0) {
  1505. return i + 16;
  1506. }
  1507. }
  1508. return -1;
  1509. }
  1510. static void run_gc(struct context* ctx) {
  1511. ink_gc(ctx);
  1512. }
  1513. static void clear_stack(struct context* ctx) {
  1514. struct ink_routine* currentRoutine;
  1515. currentRoutine = ctx->routines + ctx->routine_current;
  1516. while (currentRoutine->top >= 1) {
  1517. ink_pop(ctx);
  1518. }
  1519. return;
  1520. }
  1521. static void dump_stack(struct context* ctx) {
  1522. struct ink_routine* currentRoutine;
  1523. int index;
  1524. char* idx;
  1525. char* type;
  1526. char* value;
  1527. char* it;
  1528. currentRoutine = ctx->routines + ctx->routine_current;
  1529. index = currentRoutine->top;
  1530. while (index) {
  1531. --index;
  1532. idx = ink_itoa(ctx,index);
  1533. type = ink_itoa(ctx, currentRoutine->stack[index].type);
  1534. value = ink_itoa(ctx,currentRoutine->stack[index].value);
  1535. for(it = idx; *it; ++it) ctx->putchar(*it);
  1536. ctx->putchar(' ');ctx->putchar('|');ctx->putchar(' ');
  1537. for(it = type; *it; ++it) ctx->putchar(*it);
  1538. ctx->putchar(' ');ctx->putchar('|');ctx->putchar(' ');
  1539. for(it = value; *it; ++it) ctx->putchar(*it);
  1540. ctx->putchar('\n');
  1541. ctx->free(value);
  1542. ctx->free(type);
  1543. ctx->free(idx);
  1544. }
  1545. return;
  1546. }
  1547. static void collect_noop() {}
  1548. static struct ink_collection_list gc_noop() {
  1549. struct ink_collection_list c;
  1550. c.elements = NULL;
  1551. c.count = 0;
  1552. return c;
  1553. }
  1554. #ifndef NOARRAYLIB
  1555. static void collect_array(struct context* ctx, void* array) {
  1556. struct ink_array* ary;
  1557. ary = array;
  1558. if(ary->elements != NULL) ctx->free(ary->elements);
  1559. }
  1560. static struct ink_collection_list gc_array(struct context* ctx, void* array) {
  1561. struct ink_array* ary;
  1562. struct ink_collection_list c;
  1563. ary = array;
  1564. c.elements = ctx->inner_malloc(sizeof(struct elem)*ary->top);
  1565. c.count = ary->top;
  1566. memcpy(c.elements, ary->elements, sizeof(struct elem)*ary->top);
  1567. return c;
  1568. }
  1569. static void new_array(struct context* ctx) {
  1570. int tid;
  1571. struct elem e;
  1572. struct ink_array ary;
  1573. tid = get_type_by_name(ctx, "array");
  1574. ary.elements = NULL;
  1575. ary.top = 0;
  1576. ary.capacity = 0;
  1577. e = ink_make_native(ctx, tid, &ary);
  1578. ink_push(ctx, e);
  1579. }
  1580. #ifndef NOSTRINGLITERALS
  1581. static void new_protected_array(struct context* ctx) {
  1582. int tid;
  1583. struct elem e;
  1584. struct ink_array ary;
  1585. tid = get_type_by_name(ctx, "array");
  1586. ary.elements = NULL;
  1587. ary.top = 0;
  1588. ary.capacity = 0;
  1589. e = ink_make_native_unsafe(ctx, tid, &ary, 1);
  1590. ink_push(ctx, e);
  1591. }
  1592. #endif
  1593. static void push_array_stack_delim(struct context* ctx) {
  1594. int tid;
  1595. struct elem e;
  1596. tid = get_type_by_name(ctx, "array_marker");
  1597. e.type = tid;
  1598. e.value = 0;
  1599. ink_push(ctx, e);
  1600. }
  1601. void array_push(struct context* ctx, struct ink_routine* currentRoutine, struct ink_array* ary, struct elem value) {
  1602. if(ary->elements == NULL) {
  1603. ary->elements = ctx->malloc(sizeof(struct elem) * 8);
  1604. ary->top = 0;
  1605. ary->capacity = 8;
  1606. } else if(ary->top == ary->capacity) {
  1607. int new_count;
  1608. void* renewed;
  1609. new_count = (ary->capacity + ary->capacity/2);
  1610. renewed = ctx->realloc(ary->elements, sizeof(struct elem) * new_count);
  1611. if(renewed == NULL) {
  1612. currentRoutine->panic = -1;
  1613. return;
  1614. } else {
  1615. ary->elements = renewed;
  1616. ary->capacity = new_count;
  1617. }
  1618. }
  1619. ary->elements[ary->top] = value;
  1620. ary->top++;
  1621. }
  1622. static void push_array(struct context* ctx) {
  1623. int tid;
  1624. struct elem a;
  1625. struct ink_routine* currentRoutine;
  1626. struct ink_array* ary;
  1627. tid = get_type_by_name(ctx, "array");
  1628. currentRoutine = ctx->routines + ctx->routine_current;
  1629. if(currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top-1].type != tid) {
  1630. currentRoutine->panic = -1;
  1631. return;
  1632. }
  1633. a = currentRoutine->stack[currentRoutine->top-1];
  1634. ary= ink_get_value(ctx, a);
  1635. if(ary == NULL) {
  1636. currentRoutine->panic = -1;
  1637. return;
  1638. }
  1639. ink_pop(ctx);
  1640. array_push(ctx, currentRoutine, ary, currentRoutine->stack[currentRoutine->top-1]);
  1641. ink_pop(ctx);
  1642. }
  1643. static void push_delimited_array(struct context* ctx) {
  1644. int tid, idx, counter, i;
  1645. struct elem a;
  1646. struct ink_routine* currentRoutine;
  1647. struct ink_array* ary;
  1648. tid = get_type_by_name(ctx, "array_marker");
  1649. currentRoutine = ctx->routines + ctx->routine_current;
  1650. if(currentRoutine->top < 1) {
  1651. currentRoutine->panic = -1;
  1652. return;
  1653. }
  1654. new_array(ctx);
  1655. a = currentRoutine->stack[currentRoutine->top-1];
  1656. ink_pop(ctx);
  1657. ary= ink_get_value(ctx, a);
  1658. for(idx = 1; idx <= currentRoutine->top; ++idx) {
  1659. if(currentRoutine->stack[currentRoutine->top-idx].type == tid) {
  1660. break;
  1661. }
  1662. }
  1663. /* Save for cleanup */
  1664. counter = idx;
  1665. /* Don't copy the delimiter */
  1666. idx -= 1;
  1667. ary->elements = malloc(sizeof(struct elem) * idx);
  1668. if(ary->elements == NULL) {
  1669. currentRoutine->panic = -541;
  1670. return;
  1671. }
  1672. ary->capacity = idx;
  1673. ary->top = 0;
  1674. /* Copy the data */
  1675. for(i = currentRoutine->top - idx; i < currentRoutine->top; ++i) {
  1676. ary->elements[ary->top] = currentRoutine->stack[i];
  1677. ++(ary->top);
  1678. }
  1679. /* Cleanup */
  1680. while(counter--) {
  1681. ink_pop(ctx);
  1682. }
  1683. /* Put value in place */
  1684. ink_push(ctx, a);
  1685. }
  1686. static void index_array(struct context* ctx) {
  1687. int tid;
  1688. struct ink_routine *currentRoutine;
  1689. struct elem a;
  1690. struct ink_array *ary;
  1691. struct elem idx;
  1692. tid = get_type_by_name(ctx, "array");
  1693. currentRoutine = ctx->routines + ctx->routine_current;
  1694. if (currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) {
  1695. currentRoutine->panic = -1;
  1696. return;
  1697. }
  1698. a = currentRoutine->stack[currentRoutine->top - 1];
  1699. ary = ink_get_value(ctx, a);
  1700. if (ary == NULL) {
  1701. currentRoutine->panic = -1;
  1702. return;
  1703. }
  1704. ink_pop(ctx);
  1705. idx = currentRoutine->stack[currentRoutine->top - 1];
  1706. ink_pop(ctx);
  1707. if(ary->top <= idx.value) {
  1708. currentRoutine->panic = -1;
  1709. return;
  1710. }
  1711. ink_push(ctx, ary->elements[idx.value]);
  1712. }
  1713. static void set_array(struct context* ctx) {
  1714. int tid;
  1715. struct ink_routine *currentRoutine;
  1716. struct elem a;
  1717. struct ink_array *ary;
  1718. struct elem idx;
  1719. struct elem value;
  1720. tid = get_type_by_name(ctx, "array");
  1721. currentRoutine = ctx->routines + ctx->routine_current;
  1722. if (currentRoutine->top < 3 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) {
  1723. currentRoutine->panic = -1;
  1724. return;
  1725. }
  1726. a = currentRoutine->stack[currentRoutine->top - 1];
  1727. ary = ink_get_value(ctx, a);
  1728. if (ary == NULL) {
  1729. currentRoutine->panic = -1;
  1730. return;
  1731. }
  1732. idx = currentRoutine->stack[currentRoutine->top - 2];
  1733. value = currentRoutine->stack[currentRoutine->top - 3];
  1734. if(ary->top <= idx.value) {
  1735. currentRoutine->panic = -1;
  1736. return;
  1737. }
  1738. ink_pop(ctx);
  1739. ink_pop(ctx);
  1740. ink_pop(ctx);
  1741. ary->elements[idx.value] = value;
  1742. }
  1743. static void get_size_array(struct context* ctx) {
  1744. int tid;
  1745. struct ink_routine *currentRoutine;
  1746. struct elem a;
  1747. struct ink_array *ary;
  1748. struct elem sz;
  1749. tid = get_type_by_name(ctx, "array");
  1750. currentRoutine = ctx->routines + ctx->routine_current;
  1751. if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) {
  1752. currentRoutine->panic = -1;
  1753. return;
  1754. }
  1755. a = currentRoutine->stack[currentRoutine->top - 1];
  1756. ary = ink_get_value(ctx, a);
  1757. if (ary == NULL) {
  1758. currentRoutine->panic = -1;
  1759. return;
  1760. }
  1761. ink_pop(ctx);
  1762. sz.type = INK_INTEGER;
  1763. sz.value = ary->top;
  1764. ink_push(ctx, sz);
  1765. }
  1766. static void is_array(struct context* ctx) {
  1767. int tid;
  1768. struct ink_routine *currentRoutine;
  1769. struct elem a;
  1770. tid = get_type_by_name(ctx, "array");
  1771. currentRoutine = ctx->routines + ctx->routine_current;
  1772. if (currentRoutine->top < 1) {
  1773. currentRoutine->panic = -1;
  1774. return;
  1775. }
  1776. a.type = INK_INTEGER;
  1777. a.value = currentRoutine->stack[currentRoutine->top - 1].type == tid;
  1778. ink_pop(ctx);
  1779. ink_push(ctx, a);
  1780. }
  1781. static void is_int(struct context* ctx) {
  1782. struct ink_routine *currentRoutine;
  1783. struct elem a;
  1784. currentRoutine = ctx->routines + ctx->routine_current;
  1785. if (currentRoutine->top < 1) {
  1786. currentRoutine->panic = -1;
  1787. return;
  1788. }
  1789. a.type = INK_INTEGER;
  1790. a.value = currentRoutine->stack[currentRoutine->top - 1].type == INK_INTEGER;
  1791. ink_pop(ctx);
  1792. ink_push(ctx, a);
  1793. }
  1794. static void print_array_of_codepoints(struct context* ctx) {
  1795. int tid, i;
  1796. struct ink_routine *currentRoutine;
  1797. struct elem a;
  1798. struct ink_array *ary;
  1799. tid = get_type_by_name(ctx, "array");
  1800. currentRoutine = ctx->routines + ctx->routine_current;
  1801. if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) {
  1802. currentRoutine->panic = -1;
  1803. return;
  1804. }
  1805. a = currentRoutine->stack[currentRoutine->top - 1];
  1806. ary = ink_get_value(ctx, a);
  1807. for(i = 0; i < ary->top; ++i) {
  1808. if(ary->elements[i].type != INK_INTEGER) {
  1809. currentRoutine->panic = -1;
  1810. return;
  1811. }
  1812. }
  1813. ink_pop(ctx);
  1814. for(i = 0; i < ary->top; ++i) {
  1815. ink_push(ctx, ary->elements[i]);
  1816. print_as_utf8(ctx);
  1817. }
  1818. }
  1819. static void arrayify_stack(struct context* ctx) {
  1820. struct ink_routine* currentRoutine;
  1821. struct elem array_ref;
  1822. struct ink_array* ary;
  1823. int idx;
  1824. currentRoutine = ctx->routines + ctx->routine_current;
  1825. new_array(ctx);
  1826. if(currentRoutine->panic < 0) return;
  1827. array_ref = currentRoutine->stack[currentRoutine->top - 1];
  1828. ary = ink_get_value(ctx, array_ref);
  1829. if(ary == NULL) {
  1830. currentRoutine->panic = -717;
  1831. return;
  1832. }
  1833. ink_pop(ctx);
  1834. for(idx = 0; idx < currentRoutine->top; ++idx) {
  1835. array_push(ctx, currentRoutine, ary, currentRoutine->stack[idx]);
  1836. }
  1837. while (currentRoutine->top > 0) {
  1838. ink_pop(ctx);
  1839. }
  1840. ink_push(ctx, array_ref);
  1841. return;
  1842. }
  1843. #endif /* NOARRAYLIB */
  1844. int ink_std_library(struct context* ctx) {
  1845. int v;
  1846. v = 0;
  1847. v += ink_add_native(ctx, "sys.trace", print_stacktrace);
  1848. v += ink_add_native(ctx, "sys.gc", run_gc);
  1849. v += ink_add_native(ctx, "print_int", print_int);
  1850. v += ink_add_native(ctx, "print_utf8", print_as_utf8);
  1851. v += ink_add_native(ctx, "+", add_int);
  1852. v += ink_add_native(ctx, "-", sub_int);
  1853. v += ink_add_native(ctx, "*", mult_int);
  1854. v += ink_add_native(ctx, "/", div_int);
  1855. v += ink_add_native(ctx, "==", is_equal);
  1856. v += ink_add_native(ctx, "!=", is_different);
  1857. v += ink_add_native(ctx, "<", lt_int);
  1858. v += ink_add_native(ctx, "swap", swap_elem);
  1859. v += ink_add_native(ctx, "dup", dupe_elem);
  1860. v += ink_add_native(ctx, "drop", drop_elem);
  1861. v += ink_add_native(ctx, "stack.clear", clear_stack);
  1862. v += ink_add_native(ctx, "stack.dump", dump_stack);
  1863. v += ink_add_native(ctx, "pluck", pluck_elem);
  1864. v += ink_add_native(ctx, "return_if", return_if);
  1865. v += ink_add_native(ctx, "jump_if", jump_if);
  1866. v += ink_add_native(ctx, "is.int", is_int);
  1867. #ifndef NOEXTRAARITHMETIC
  1868. v += ink_add_native(ctx, ">", gt_int);
  1869. v += ink_add_native(ctx, ">=", gte_int);
  1870. v += ink_add_native(ctx, "=<", lte_int);
  1871. v += ink_add_native(ctx, "%", rem_int);
  1872. v += ink_add_native(ctx, "int.xor", xor_int);
  1873. #endif /* NOEXTRAARITHMETIC */
  1874. #ifndef NOARRAYLIB
  1875. ink_new_type(ctx, "array", sizeof(struct ink_array), collect_array, gc_array);
  1876. ink_new_type(ctx, "array_marker", 0, collect_noop, gc_noop);
  1877. v += ink_add_native(ctx, "[", push_array_stack_delim);
  1878. v += ink_add_native(ctx, "]", push_delimited_array);
  1879. v += ink_add_native(ctx, "array.new", new_array);
  1880. v += ink_add_native(ctx, "array.push", push_array);
  1881. v += ink_add_native(ctx, "array.index", index_array);
  1882. v += ink_add_native(ctx, "array.set", set_array);
  1883. v += ink_add_native(ctx, "array.size", get_size_array);
  1884. v += ink_add_native(ctx, "array.print_utf8", print_array_of_codepoints);
  1885. v += ink_add_native(ctx, "is.array", is_array);
  1886. v += ink_add_native(ctx, "stack.to_array", arrayify_stack);
  1887. #endif /* NOARRAYLIB */
  1888. return v;
  1889. }