A minimalistic programming language written in C89.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

2373 lignes
71 KiB

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