A minimalistic programming language written in C89.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2388 lines
62 KiB

пре 5 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 5 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 5 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 5 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 6 месеци
пре 5 месеци
пре 5 месеци
пре 5 месеци
пре 5 месеци
пре 5 месеци
  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->top = it->capacity = 0;
  623. it->function_stack_top = it->function_stack_capacity = 0;
  624. it->panic = INK_ROUTINE_CAN_REUSE;
  625. it->parse_error.is_set = 0;
  626. it->runtime_error.is_set = 0;
  627. }
  628. } else if(ctx->routines_top == ctx->routines_capacity) {
  629. int new_count;
  630. void* renewed;
  631. new_count = (ctx->routines_capacity + ctx->routines_capacity/2);
  632. renewed = ctx->inner_realloc(ctx, ctx->routines, sizeof(struct ink_routine) * new_count);
  633. #ifndef NOEXTRACHECKS
  634. if(renewed == NULL) {
  635. return -99;
  636. }
  637. #endif
  638. ctx->routines = renewed;
  639. it = ctx->routines + ctx->routines_capacity;
  640. end = ctx->routines + new_count;
  641. for(;it != end;++it) {
  642. it->stack = NULL;
  643. it->function_stack = NULL;
  644. it->top = it->capacity = 0;
  645. it->function_stack_top = it->function_stack_capacity = 0;
  646. it->panic = INK_ROUTINE_CAN_REUSE;
  647. it->parse_error.is_set = 0;
  648. it->runtime_error.is_set = 0;
  649. }
  650. ctx->routines_capacity = new_count;
  651. }
  652. it = ctx->routines;
  653. end = ctx->routines + ctx->routines_capacity;
  654. /* Looks for a reusable routine space then uses it */
  655. for(;it != end;++it) {
  656. if(it->panic == INK_ROUTINE_CAN_REUSE) {
  657. int idx;
  658. it->panic = 0;
  659. it->top = 0;
  660. it->function_stack_top = 0;
  661. idx = it - ctx->routines;
  662. if(idx >= ctx->routines_top) {
  663. ctx->routines_top = idx + 1;
  664. }
  665. return idx;
  666. }
  667. }
  668. /* FIXME: Maybe we need to abort here, this seems like quite an unsteady state */
  669. return -758;
  670. }
  671. int ink_kill_routine(struct context* ctx, int routine){
  672. struct ink_routine* curr;
  673. if(routine < 0 || routine >= ctx->routines_top) {
  674. return 0;
  675. }
  676. curr = ctx->routines + routine;
  677. if(curr->panic == INK_ROUTINE_CAN_REUSE) {
  678. return 0;
  679. }
  680. if(curr->stack != NULL) {
  681. ctx->free(ctx, curr->stack);
  682. curr->stack = NULL;
  683. curr->top = curr->capacity = 0;
  684. }
  685. if(curr->function_stack != NULL) {
  686. ctx->free(ctx, curr->function_stack);
  687. curr->function_stack = NULL;
  688. curr->function_stack_top = curr->function_stack_capacity = 0;
  689. }
  690. curr->panic = INK_ROUTINE_CAN_REUSE;
  691. return 1;
  692. }
  693. /**
  694. *
  695. * @param pContext
  696. * @param executable_buffer
  697. * @param executable_buffer_top
  698. * @internal Loop from hell
  699. */
  700. static int ink_parse(struct context* pContext, struct elem* executable_buffer, int* executable_buffer_top) {
  701. struct ink_routine* currentRoutine;
  702. int i, function_buffer_top, function_name, mode;
  703. #pragma GCC diagnostic push
  704. #pragma GCC diagnostic ignored "-Wunused-parameter"
  705. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  706. int err;
  707. #pragma GCC diagnostic pop
  708. #define LABEL_BUFFER 128
  709. #define FUNCTION_BUFFER 256
  710. struct label labels[LABEL_BUFFER];
  711. struct elem function_buffer[FUNCTION_BUFFER];
  712. /* TODO: add checks for overflows in these arrays */
  713. currentRoutine = pContext->routines + pContext->routine_current;
  714. function_buffer_top = 0;
  715. function_name = -1;
  716. #define MODE_EXECUTABLE 0
  717. #define MODE_FUNCTION 1
  718. #define MODE_DO 2
  719. mode = MODE_EXECUTABLE;
  720. memset(labels, 0, sizeof(struct label)*LABEL_BUFFER);
  721. /* Loop from hell, good luck, pro-tip: leave the parser alone */
  722. for(i = 0; i < currentRoutine->top; ++i) {
  723. struct elem current;
  724. current = currentRoutine->stack[i];
  725. switch (mode) {
  726. case MODE_EXECUTABLE:
  727. switch(current.type) {
  728. case INK_FUNCTION_KW:
  729. mode = MODE_FUNCTION;
  730. function_name = -1;
  731. goto next_token;
  732. #ifndef NOEXTRACHECKS
  733. case INK_DO_KW:
  734. currentRoutine->parse_error.is_set = 1;
  735. currentRoutine->parse_error.error_message = "Found start of function body unexpectedly";
  736. currentRoutine->parse_error.offset= i;
  737. return -26;
  738. case INK_END_KW:
  739. currentRoutine->parse_error.is_set = 1;
  740. currentRoutine->parse_error.error_message = "Found end of function unexpectedly";
  741. currentRoutine->parse_error.offset= i;
  742. return -26;
  743. #endif
  744. default:
  745. executable_buffer[*executable_buffer_top] = current;
  746. *executable_buffer_top += 1;
  747. }
  748. break;
  749. case MODE_FUNCTION:
  750. if(current.type == INK_DO_KW) {
  751. #ifndef NOEXTRACHECKS
  752. if(function_name == -1) {
  753. currentRoutine->parse_error.is_set = 1;
  754. currentRoutine->parse_error.error_message = "Found start of function body before the name of the function was provided";
  755. currentRoutine->parse_error.offset= i;
  756. return -27;
  757. }
  758. #endif
  759. mode = MODE_DO;
  760. memset(labels, 0, sizeof(struct label)*128);
  761. goto next_token;
  762. }
  763. #ifndef NOEXTRACHECKS
  764. if(function_name != -1) {
  765. currentRoutine->parse_error.is_set = 1;
  766. currentRoutine->parse_error.error_message = "Function name was not found";
  767. currentRoutine->parse_error.offset= i;
  768. return -28;
  769. }
  770. if(current.type != INK_RESERVED) {
  771. currentRoutine->parse_error.is_set = 1;
  772. currentRoutine->parse_error.error_message = "Expected special token";
  773. currentRoutine->parse_error.offset= i;
  774. return -29;
  775. }
  776. #endif
  777. function_name = current.value;
  778. break;
  779. case MODE_DO:
  780. if(current.type == INK_END_KW) {
  781. int j;
  782. for(j = 0; j < function_buffer_top; j++) {
  783. struct elem pt;
  784. pt = function_buffer[j];
  785. if(pt.type == INK_LABEL) {
  786. int k;
  787. for(k = 0; k < LABEL_BUFFER; k++) {
  788. if(labels[k].active) {
  789. #ifndef NOEXTRACHECKS
  790. if(strcmp(labels[k].name, pContext->lex_reserved_words[pt.value]) == 0) {
  791. labels[k].dest = j;
  792. currentRoutine->parse_error.is_set = 1;
  793. currentRoutine->parse_error.error_message = "Label duplicate label in function";
  794. currentRoutine->parse_error.offset= i;
  795. return -30;
  796. break;
  797. }
  798. #endif
  799. } else {
  800. labels[k].active = 1;
  801. labels[k].name = pContext->lex_reserved_words[pt.value];
  802. labels[k].dest = j;
  803. memcpy(function_buffer+j, function_buffer+j+1, sizeof(struct elem)*(function_buffer_top-j-1));
  804. function_buffer_top--;
  805. j--;
  806. break;
  807. }
  808. }
  809. }
  810. }
  811. for(j = 0; j < function_buffer_top; j++) {
  812. struct elem pt;
  813. pt = function_buffer[j];
  814. if(pt.type == INK_RESERVED) {
  815. int k;
  816. for(k = 0; k < LABEL_BUFFER; k++) {
  817. if(labels[k].active) {
  818. int label_sz;
  819. const char* lbl;
  820. lbl = labels[k].name;
  821. label_sz = strlen(lbl);
  822. if(lblcmp(labels[k].name, pContext->lex_reserved_words[pt.value], label_sz) == 0) {
  823. function_buffer[j].type = INK_INTEGER;
  824. function_buffer[j].value = labels[k].dest - j;
  825. break;
  826. }
  827. } else break;
  828. }
  829. }
  830. }
  831. err = ink_add_indigenous(pContext, pContext->lex_reserved_words[function_name], function_buffer, function_buffer_top);
  832. #ifndef NOEXTRACHECKS
  833. if(err < 0) {
  834. pContext->panic = 1;
  835. return -33;
  836. }
  837. #endif
  838. function_buffer_top = 0;
  839. mode = MODE_EXECUTABLE;
  840. goto next_token;
  841. }
  842. function_buffer[function_buffer_top] = current;
  843. function_buffer_top += 1;
  844. break;
  845. }
  846. next_token: i=i;
  847. }
  848. #ifndef NOEXTRACHECKS
  849. if(mode == MODE_FUNCTION || mode == MODE_DO) {
  850. currentRoutine->parse_error.is_set = 1;
  851. currentRoutine->parse_error.error_message = "Expected a function to be complete";
  852. currentRoutine->parse_error.offset= i;
  853. return -32;
  854. }
  855. #endif
  856. return 0;
  857. #undef MODE_EXECUTABLE
  858. #undef MODE_FUNCTION
  859. #undef MODE_DO
  860. #undef LABEL_BUFFER
  861. #undef FUNCTION_BUFFER
  862. }
  863. int ink_step(struct context *pContext) {
  864. struct ink_routine* currentRoutine;
  865. struct stack_frame frame;
  866. struct stack_frame* top;
  867. struct elem next;
  868. int t;
  869. currentRoutine = pContext->routines + pContext->routine_current;
  870. pContext->steps++;
  871. if(currentRoutine->function_stack_top == 0) return 0;
  872. if(pContext->panic) {
  873. return -1;
  874. }
  875. top = &currentRoutine->function_stack[currentRoutine->function_stack_top-1];
  876. t = top->executing.type;
  877. switch(t) {
  878. case INK_NATIVE_FUNCTION:
  879. if(top->index != 0) {
  880. ink_pop_fn(pContext);
  881. } else {
  882. top->index++;
  883. #ifndef NOEXTRACHECKS
  884. if(pContext->native_words_top <= top->executing.value) {
  885. currentRoutine->runtime_error.is_set = 1;
  886. currentRoutine->runtime_error.error_message = "Bytecode contained out of bound executable word";
  887. pContext->panic = 1;
  888. return -1;
  889. }
  890. #endif
  891. pContext->native_words[top->executing.value].value(pContext);
  892. }
  893. break;
  894. case INK_FUNCTION:
  895. #ifndef NOEXTRACHECKS
  896. if(pContext->words_top <= top->executing.value) {
  897. currentRoutine->runtime_error.is_set = 1;
  898. currentRoutine->runtime_error.error_message = "Bytecode contained out of bound artificial word";
  899. pContext->panic = 1;
  900. return -1;
  901. }
  902. #endif
  903. if(top->index >= pContext->words[top->executing.value].size) {
  904. ink_pop_fn(pContext);
  905. } else {
  906. next = pContext->words[top->executing.value].things[top->index];
  907. if(next.type == INK_RETURN) {
  908. ink_pop_fn(pContext);
  909. return 1;
  910. }
  911. frame.executing = next;
  912. frame.index = 0;
  913. t = ink_push_fn(pContext, frame);
  914. #ifndef NOEXTRACHECKS
  915. if(t < 0) {
  916. pContext->panic = 1;
  917. currentRoutine->runtime_error.is_set = 1;
  918. currentRoutine->runtime_error.error_message = "Instruction pointer underflow";
  919. return -11;
  920. }
  921. #endif
  922. top->index++;
  923. }
  924. break;
  925. default:
  926. t = ink_push(pContext, top->executing);
  927. #ifndef NOEXTRACHECKS
  928. if(t < 0) {
  929. currentRoutine->runtime_error.is_set = 1;
  930. currentRoutine->runtime_error.error_message = "Literal token could not be pushed";
  931. pContext->panic = 1;
  932. return -25;
  933. }
  934. #endif
  935. ink_pop_fn(pContext);
  936. break;
  937. }
  938. return 1;
  939. }
  940. int ink_compile(struct context *pContext, const char* buffer) {
  941. int routine, saved, executable_buffer_top;
  942. /* Main function has a size limit of 256 (need to know that for REPL */
  943. struct elem executable_buffer[256];
  944. struct ink_routine* currentRoutine;
  945. int err;
  946. struct stack_frame frame;
  947. char* integer;
  948. size_t integer_size;
  949. char main_fn[32] = "__-MAIN-__";
  950. routine = ink_make_routine(pContext);
  951. saved = pContext->routine_current;
  952. pContext->routine_current = routine;
  953. currentRoutine = pContext->routines + routine;
  954. currentRoutine->stack = NULL;
  955. currentRoutine->top = 0;
  956. currentRoutine->capacity = 0;
  957. err = ink_lex(pContext, buffer);
  958. if(err < 0) {
  959. #ifndef NOEXTRACHECKS
  960. if(!currentRoutine->parse_error.is_set) {
  961. currentRoutine->parse_error.is_set = 1;
  962. currentRoutine->parse_error.error_message = "Unknown lexer error";
  963. currentRoutine->parse_error.offset = -1;
  964. }
  965. #endif
  966. pContext->panic = 1;
  967. return -1;
  968. }
  969. executable_buffer_top = 0;
  970. err = ink_parse(pContext, executable_buffer, &executable_buffer_top);
  971. if(err < 0) {
  972. #ifndef NOEXTRACHECKS
  973. if(!currentRoutine->parse_error.is_set) {
  974. currentRoutine->parse_error.is_set = 1;
  975. currentRoutine->parse_error.error_message = "Unknown parser error";
  976. currentRoutine->parse_error.offset = -1;
  977. }
  978. #endif
  979. pContext->panic = 1;
  980. return -1;
  981. }
  982. if(executable_buffer_top != 0) {
  983. integer = ink_itoa(pContext, routine);
  984. integer_size = strlen(integer);
  985. memcpy(main_fn + 10, integer, integer_size);
  986. pContext->free(pContext, integer);
  987. integer = NULL;
  988. main_fn[10 + integer_size] = 0;
  989. frame.executing.value = ink_add_indigenous(pContext, main_fn, executable_buffer, executable_buffer_top);
  990. if (frame.executing.value < 0) {
  991. #ifndef NOEXTRACHECKS
  992. if(!currentRoutine->parse_error.is_set) {
  993. currentRoutine->parse_error.is_set = 1;
  994. currentRoutine->parse_error.error_message = "Could not start execution: no valid way to create a frame";
  995. currentRoutine->parse_error.offset = -1;
  996. }
  997. #endif
  998. pContext->panic = 1;
  999. return -1;
  1000. }
  1001. frame.executing.type = INK_FUNCTION;
  1002. frame.index = 0;
  1003. err = ink_push_fn(pContext, frame);
  1004. pContext->routines[pContext->routine_current].top = 0;
  1005. #ifndef NOEXTRACHECKS
  1006. if (err < 0) {
  1007. if(!currentRoutine->parse_error.is_set) {
  1008. currentRoutine->parse_error.is_set = 1;
  1009. currentRoutine->parse_error.error_message = "Could not push any executable frame: push failed";
  1010. currentRoutine->parse_error.offset = -1;
  1011. }
  1012. pContext->panic = 1;
  1013. return -1;
  1014. }
  1015. #endif
  1016. } else {
  1017. pContext->routines[pContext->routine_current].panic = INK_ROUTINE_SUCCESS;
  1018. }
  1019. pContext->routine_current = saved;
  1020. return routine;
  1021. }
  1022. int ink_can_run(struct context* pContext) {
  1023. int it;
  1024. if(pContext->panic) return 0;
  1025. if(pContext->routines_top == 0) return 0;
  1026. for(it = 0; it < pContext->routines_top; ++it) {
  1027. if(
  1028. pContext->routines[it].panic == 0
  1029. && !pContext->routines[it].parse_error.is_set
  1030. && !pContext->routines[it].runtime_error.is_set
  1031. ) {
  1032. return 1;
  1033. }
  1034. }
  1035. return 0;
  1036. }
  1037. int ink_step_everyone(struct context* pContext) {
  1038. int idx;
  1039. int out;
  1040. pContext->routine_current = -1;
  1041. for(;;) {
  1042. top_label:
  1043. /* Increment to next runnable routine */
  1044. do{
  1045. ++(pContext->routine_current);
  1046. } while(
  1047. pContext->routine_current < pContext->routines_top
  1048. && pContext->routines[pContext->routine_current].panic != 0
  1049. && pContext->routines[pContext->routine_current].parse_error.is_set
  1050. && pContext->routines[pContext->routine_current].runtime_error.is_set
  1051. );
  1052. /* Exit condition */
  1053. if(pContext->routine_current >= pContext->routines_top) break;
  1054. /* Kill? */
  1055. if(pContext->routines[pContext->routine_current].panic == INK_ROUTINE_SUCCESS) {
  1056. goto top_label;
  1057. /* We used to kill the routines here, idk if this cleanup is wise as it may happen in somewhat interstitial
  1058. * moments and affect performance
  1059. * ink_kill_routine(pContext, pContext->routine_current); */
  1060. }
  1061. /* Step! */
  1062. for(idx = 0; idx < INK_STEP_BATCH_COUNT; ++idx) {
  1063. out = ink_step(pContext);
  1064. if (unlikely(out == 0)) {
  1065. pContext->routines[pContext->routine_current].panic = INK_ROUTINE_SUCCESS;
  1066. goto top_label;
  1067. } else if (unlikely(out < 0)) {
  1068. pContext->routines[pContext->routine_current].panic = out;
  1069. goto top_label;
  1070. }
  1071. }
  1072. }
  1073. return 0;
  1074. }
  1075. int ink_new_type(
  1076. struct context* ctx,
  1077. const char* type_name,
  1078. int size,
  1079. void (*collect)(struct context*,void*),
  1080. struct ink_collection_list (*gc)(struct context*,void*)
  1081. ) {
  1082. if(ctx->panic) return -128;
  1083. /* Resize for push */
  1084. if(ctx->types == NULL) {
  1085. ctx->types = ctx->inner_malloc(ctx, sizeof(struct ink_type) * 8);
  1086. ctx->types_top = 0;
  1087. ctx->types_capacity = 8;
  1088. } else if(ctx->types_top == ctx->types_capacity) {
  1089. int new_count;
  1090. void* renewed;
  1091. new_count = (ctx->types_capacity + ctx->types_capacity/2);
  1092. renewed = ctx->inner_realloc(ctx, ctx->types, sizeof(struct ink_type) * new_count);
  1093. if(renewed == NULL) {
  1094. return -129;
  1095. } else {
  1096. ctx->types = renewed;
  1097. ctx->types_capacity = new_count;
  1098. }
  1099. }
  1100. /* Push */
  1101. ctx->types[ctx->types_top].name = type_name;
  1102. ctx->types[ctx->types_top].element_size = size;
  1103. ctx->types[ctx->types_top].elements = NULL;
  1104. ctx->types[ctx->types_top].elements_top = 0;
  1105. ctx->types[ctx->types_top].elements_capacity = 0;
  1106. ctx->types[ctx->types_top].collect = collect;
  1107. ctx->types[ctx->types_top].gc = gc;
  1108. ctx->types_top++;
  1109. /* Satisfying the minimal value requirement */
  1110. return ctx->types_top - 1 + 16;
  1111. }
  1112. static struct element_slab* ink_get_value_link(struct context* ctx, struct elem ref) {
  1113. int type_id;
  1114. if(ref.type < 16) return NULL;
  1115. type_id = ref.type - 16;
  1116. if(type_id >= ctx->types_top) return NULL;
  1117. if(ctx->types[type_id].element_size == 0) return NULL;
  1118. if(ref.value < 0) return NULL;
  1119. if(ref.value >= ctx->types[type_id].elements_top) return NULL;
  1120. if(! ctx->types[type_id].elements[ref.value].in_use) return NULL;
  1121. return ctx->types[type_id].elements + ref.value;
  1122. }
  1123. void* ink_get_value(struct context* ctx, struct elem ref) {
  1124. struct element_slab* s;
  1125. s = ink_get_value_link(ctx, ref);
  1126. if(s == NULL) return NULL;
  1127. return s->data;
  1128. }
  1129. struct elem ink_make_native_unsafe(struct context* ctx, int type, void* ptr, int is_protected) {
  1130. int type_id;
  1131. struct elem ret;
  1132. int g, i;
  1133. if(type < 16) {
  1134. ret.type = 0;
  1135. ret.value = -130;
  1136. return ret;
  1137. }
  1138. /* Apply invariant of the user defined types */
  1139. type_id = type - 16;
  1140. if(type_id >= ctx->types_top) {
  1141. ret.type = 0;
  1142. ret.value = -129;
  1143. return ret;
  1144. }
  1145. if(ctx->panic) {
  1146. ret.type = 0;
  1147. ret.value = -135;
  1148. return ret;
  1149. }
  1150. /* Resize for push of value in store */
  1151. if(ctx->types[type_id].elements == NULL) {
  1152. ctx->types[type_id].elements = ctx->inner_malloc(ctx, sizeof(struct element_slab) * 8);
  1153. ctx->types[type_id].elements_top = 0;
  1154. ctx->types[type_id].elements_capacity = 8;
  1155. 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));
  1156. } else if(ctx->types[type_id].elements_top == ctx->types[type_id].elements_capacity) {
  1157. int new_count;
  1158. void* renewed;
  1159. new_count = (ctx->types[type_id].elements_capacity + ctx->types[type_id].elements_capacity/2);
  1160. renewed = ctx->inner_realloc(ctx, ctx->types[type_id].elements, sizeof(struct element_slab) * new_count);
  1161. if(renewed == NULL) {
  1162. ret.type = 0;
  1163. ret.value = -129;
  1164. return ret;
  1165. } else {
  1166. ctx->types[type_id].elements = renewed;
  1167. ctx->types[type_id].elements_capacity = new_count;
  1168. 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));
  1169. }
  1170. }
  1171. /* Push value in store */
  1172. g = ctx->types[type_id].elements_capacity;
  1173. for(i = 0; i < g; ++i) {
  1174. if(! ctx->types[type_id].elements[i].in_use) {
  1175. ctx->types[type_id].elements[i].in_use = 1;
  1176. ctx->types[type_id].elements[i].uses = 1;
  1177. ctx->types[type_id].elements[i].is_protected = is_protected;
  1178. if(ctx->types[type_id].element_size < 0) {
  1179. ctx->types[type_id].elements[i].data = ptr;
  1180. } else {
  1181. void* new_ptr = ctx->malloc(ctx, ctx->types[type_id].element_size);
  1182. if(new_ptr == NULL) {
  1183. ret.type = 0;
  1184. ret.value = -139;
  1185. return ret;
  1186. }
  1187. memcpy(new_ptr, ptr, ctx->types[type_id].element_size);
  1188. ctx->types[type_id].elements[i].data = new_ptr;
  1189. }
  1190. ctx->types[type_id].elements_top = max(ctx->types[type_id].elements_top, i+1);
  1191. ret.type = type;
  1192. ret.value = i;
  1193. return ret;
  1194. }
  1195. }
  1196. ret.type = 0;
  1197. ret.value = -140;
  1198. return ret;
  1199. }
  1200. struct elem ink_make_native(struct context* ctx, int type, void* ptr) {
  1201. return ink_make_native_unsafe(ctx, type, ptr, 0);
  1202. }
  1203. void ink_clean_routines(struct context* ctx) {
  1204. int i;
  1205. #ifndef NOEXTRACHECKS
  1206. int j;
  1207. struct elem null;
  1208. null.value = 0;
  1209. null.type = INK_INTEGER;
  1210. #endif
  1211. for(i = 0; i < ctx->routines_top; ++i) {
  1212. if(ctx->routines[i].panic == INK_ROUTINE_CAN_REUSE || ctx->routines[i].panic == INK_ROUTINE_SUCCESS) {
  1213. #ifndef NOEXTRACHECKS
  1214. if(ctx->routines[i].stack != NULL) {
  1215. for (j = 0; j < ctx->routines[i].top; ++j) {
  1216. ctx->routines[i].stack[j] = null;
  1217. }
  1218. }
  1219. if(ctx->routines[i].function_stack != NULL) {
  1220. for (j = 0; j < ctx->routines[i].function_stack_top; ++j) {
  1221. ctx->routines[i].function_stack[j].executing = null;
  1222. ctx->routines[i].function_stack[j].index = 0;
  1223. }
  1224. }
  1225. #endif
  1226. ctx->routines[i].top = 0;
  1227. ctx->routines[i].function_stack_top = 0;
  1228. ctx->routines[i].panic = INK_ROUTINE_CAN_REUSE;
  1229. }
  1230. }
  1231. }
  1232. void ink_gc(struct context* ctx) {
  1233. int i, j, k;
  1234. int marked;
  1235. struct element_slab* v;
  1236. for(i = 0; i < ctx->types_top; ++i) {
  1237. for(j = 0; j < ctx->types[i].elements_top; ++j) {
  1238. ctx->types[i].elements[j].uses = 0;
  1239. }
  1240. }
  1241. /* Start by marking the roots of the routines, Clear the routines if possible */
  1242. for(i = 0; i < ctx->routines_top; ++i) {
  1243. if(ctx->routines[i].panic == INK_ROUTINE_SUCCESS) {
  1244. #ifndef NOEXTRACHECKS
  1245. if(ctx->routines[i].stack != NULL) {
  1246. ctx->free(ctx, ctx->routines[i].stack);
  1247. ctx->routines[i].stack = NULL;
  1248. }
  1249. #endif
  1250. ctx->routines[i].top = 0;
  1251. #ifndef NOEXTRACHECKS
  1252. if(ctx->routines[i].function_stack != NULL) {
  1253. ctx->free(ctx, ctx->routines[i].function_stack);
  1254. ctx->routines[i].function_stack = NULL;
  1255. }
  1256. #endif
  1257. ctx->routines[i].function_stack_top = 0;
  1258. ctx->routines[i].panic = INK_ROUTINE_CAN_REUSE;
  1259. } else if(ctx->routines[i].panic == INK_ROUTINE_CAN_REUSE) {
  1260. continue;
  1261. } else for(j = 0; j < ctx->routines[i].top; ++j) {
  1262. v = ink_get_value_link(ctx, ctx->routines[i].stack[j]);
  1263. if(v != NULL) ++v->uses;
  1264. }
  1265. }
  1266. /* TODO: Mark objects contained within function code */
  1267. /* Mark the rest of the data */
  1268. do {
  1269. marked = 0;
  1270. for (i = 0; i < ctx->types_top; ++i) {
  1271. for (j = 0; j < ctx->types[i].elements_top; ++j) {
  1272. /* Only mark from things that are active and detected as in use */
  1273. if (ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].is_protected && ctx->types[i].elements[j].uses) {
  1274. struct ink_collection_list c;
  1275. c = ctx->types[i].gc(ctx, ctx->types[i].elements[j].data);
  1276. for (k = 0; k < c.count; ++k) {
  1277. v = ink_get_value_link(ctx, c.elements[k]);
  1278. /* Never mark twice to avoid infinite loops with e.g. arrays that contain themselves */
  1279. if (v != NULL && !v->uses) {
  1280. ++v->uses;
  1281. marked = 1;
  1282. }
  1283. }
  1284. if (c.elements != NULL) {
  1285. ctx->inner_free(ctx, c.elements);
  1286. c.elements = NULL;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. } while(marked);
  1292. /* Sweep phase: explore any allocated data and sweep the unused away */
  1293. for(i = 0; i < ctx->types_top; ++i) {
  1294. for(j = 0; j < ctx->types[i].elements_top; ++j) {
  1295. if(ctx->types[i].elements[j].uses == 0 && ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].is_protected == 0) {
  1296. ctx->collections++;
  1297. ctx->types[i].collect(ctx, ctx->types[i].elements[j].data);
  1298. if(ctx->types[i].element_size > 0 && ctx->types[i].elements[j].data != NULL) {
  1299. ctx->free(ctx, ctx->types[i].elements[j].data);
  1300. }
  1301. ctx->types[i].elements[j].data = NULL;
  1302. ctx->types[i].elements[j].uses = 0;
  1303. ctx->types[i].elements[j].in_use = 0;
  1304. }
  1305. }
  1306. }
  1307. }
  1308. /**********************************************************************************************************************/
  1309. static void print_stacktrace(struct context* _) {
  1310. int i;
  1311. struct ink_routine* currentRoutine;
  1312. currentRoutine = _->routines + _->routine_current;
  1313. for(i = 0; i < currentRoutine->function_stack_top; ++i) {
  1314. struct elem thing;
  1315. char *n;
  1316. thing = currentRoutine->function_stack[i].executing;
  1317. switch(thing.type) {
  1318. case INK_NATIVE_FUNCTION: {
  1319. n = _->native_words[thing.value].name;
  1320. while (*n) {
  1321. _->putchar(_, *n);
  1322. ++n;
  1323. }
  1324. _->putchar(_, 10);
  1325. break;
  1326. }
  1327. case INK_FUNCTION:{
  1328. n = _->words[thing.value].name;
  1329. while (*n) {
  1330. _->putchar(_, *n);
  1331. ++n;
  1332. }
  1333. _->putchar(_, ':');
  1334. n = ink_itoa(_, currentRoutine->function_stack[i].index);
  1335. while (*n) {
  1336. _->putchar(_, *n);
  1337. ++n;
  1338. }
  1339. _->putchar(_, 10);
  1340. break;
  1341. }
  1342. default:
  1343. break;
  1344. }
  1345. }
  1346. }
  1347. static void add_int(struct context* ctx) {
  1348. struct ink_routine* currentRoutine;
  1349. struct elem a;
  1350. struct elem b;
  1351. currentRoutine = ctx->routines + ctx->routine_current;
  1352. #ifndef NOEXTRACHECKS
  1353. if(currentRoutine->top < 2) {
  1354. currentRoutine->panic = -1;
  1355. return;
  1356. }
  1357. #endif
  1358. a = currentRoutine->stack[currentRoutine->top-1];
  1359. b = currentRoutine->stack[currentRoutine->top-2];
  1360. #ifndef NOEXTRACHECKS
  1361. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1362. ctx->panic = 1;
  1363. return;
  1364. }
  1365. #endif
  1366. ink_pop(ctx);
  1367. currentRoutine->stack[currentRoutine->top-1].value = a.value + b.value;
  1368. }
  1369. static void sub_int(struct context* ctx) {
  1370. struct ink_routine* currentRoutine;
  1371. struct elem a;
  1372. struct elem b;
  1373. currentRoutine = ctx->routines + ctx->routine_current;
  1374. #ifndef NOEXTRACHECKS
  1375. if(currentRoutine->top < 2) {
  1376. currentRoutine->panic = -1;
  1377. return;
  1378. }
  1379. #endif
  1380. a = currentRoutine->stack[currentRoutine->top-1];
  1381. b = currentRoutine->stack[currentRoutine->top-2];
  1382. #ifndef NOEXTRACHECKS
  1383. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1384. currentRoutine->panic = -1;
  1385. return;
  1386. }
  1387. #endif
  1388. ink_pop(ctx);
  1389. currentRoutine->stack[currentRoutine->top-1].value = b.value - a.value;
  1390. }
  1391. static void mult_int(struct context* ctx) {
  1392. struct ink_routine* currentRoutine;
  1393. struct elem a;
  1394. struct elem b;
  1395. currentRoutine = ctx->routines + ctx->routine_current;
  1396. #ifndef NOEXTRACHECKS
  1397. if(currentRoutine->top < 2) {
  1398. currentRoutine->panic = -1;
  1399. return;
  1400. }
  1401. #endif
  1402. a = currentRoutine->stack[currentRoutine->top-1];
  1403. b = currentRoutine->stack[currentRoutine->top-2];
  1404. #ifndef NOEXTRACHECKS
  1405. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1406. currentRoutine->panic = -1;
  1407. return;
  1408. }
  1409. #endif
  1410. ink_pop(ctx);
  1411. currentRoutine->stack[currentRoutine->top-1].value = b.value * a.value;
  1412. }
  1413. static void div_int(struct context* ctx) {
  1414. struct ink_routine* currentRoutine;
  1415. struct elem a;
  1416. struct elem b;
  1417. currentRoutine = ctx->routines + ctx->routine_current;
  1418. #ifndef NOEXTRACHECKS
  1419. if(currentRoutine->top < 2) {
  1420. currentRoutine->panic = -1;
  1421. return;
  1422. }
  1423. #endif
  1424. a = currentRoutine->stack[currentRoutine->top-1];
  1425. b = currentRoutine->stack[currentRoutine->top-2];
  1426. #ifndef NOEXTRACHECKS
  1427. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1428. currentRoutine->panic = -1;
  1429. return;
  1430. }
  1431. #endif
  1432. ink_pop(ctx);
  1433. currentRoutine->stack[currentRoutine->top-1].value = b.value / a.value;
  1434. }
  1435. static void is_equal(struct context* ctx) {
  1436. struct ink_routine* currentRoutine;
  1437. struct elem a;
  1438. struct elem b;
  1439. struct elem ret;
  1440. currentRoutine = ctx->routines + ctx->routine_current;
  1441. #ifndef NOEXTRACHECKS
  1442. if(currentRoutine->top < 2) {
  1443. currentRoutine->panic = -1;
  1444. return;
  1445. }
  1446. #endif
  1447. a = currentRoutine->stack[currentRoutine->top-1];
  1448. b = currentRoutine->stack[currentRoutine->top-2];
  1449. ink_pop(ctx);
  1450. ink_pop(ctx);
  1451. ret.type = INK_INTEGER;
  1452. ret.value = a.value == b.value && a.type == b.type;
  1453. ink_push(ctx, ret);
  1454. }
  1455. static void is_different(struct context* ctx) {
  1456. struct ink_routine* currentRoutine;
  1457. struct elem a;
  1458. struct elem b;
  1459. struct elem ret;
  1460. currentRoutine = ctx->routines + ctx->routine_current;
  1461. #ifndef NOEXTRACHECKS
  1462. if(currentRoutine->top < 2) {
  1463. currentRoutine->panic = -1;
  1464. return;
  1465. }
  1466. #endif
  1467. a = currentRoutine->stack[currentRoutine->top-1];
  1468. b = currentRoutine->stack[currentRoutine->top-2];
  1469. ink_pop(ctx);
  1470. ink_pop(ctx);
  1471. ret.type = INK_INTEGER;
  1472. ret.value = !(a.value == b.value && a.type == b.type);
  1473. ink_push(ctx, ret);
  1474. }
  1475. #ifndef NOEXTRAARITHMETIC
  1476. static void rem_int(struct context* ctx) {
  1477. struct ink_routine* currentRoutine;
  1478. struct elem a;
  1479. struct elem b;
  1480. currentRoutine = ctx->routines + ctx->routine_current;
  1481. #ifndef NOEXTRACHECKS
  1482. if(currentRoutine->top < 2) {
  1483. currentRoutine->panic = -1;
  1484. return;
  1485. }
  1486. #endif
  1487. a = currentRoutine->stack[currentRoutine->top-1];
  1488. b = currentRoutine->stack[currentRoutine->top-2];
  1489. #ifndef NOEXTRACHECKS
  1490. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1491. currentRoutine->panic = -1;
  1492. return;
  1493. }
  1494. #endif
  1495. ink_pop(ctx);
  1496. currentRoutine->stack[currentRoutine->top-1].value = b.value % a.value;
  1497. }
  1498. static void xor_int(struct context* ctx) {
  1499. struct ink_routine* currentRoutine;
  1500. struct elem a;
  1501. struct elem b;
  1502. currentRoutine = ctx->routines + ctx->routine_current;
  1503. #ifndef NOEXTRACHECKS
  1504. if(currentRoutine->top < 2) {
  1505. currentRoutine->panic = -1;
  1506. return;
  1507. }
  1508. #endif
  1509. a = currentRoutine->stack[currentRoutine->top-1];
  1510. b = currentRoutine->stack[currentRoutine->top-2];
  1511. #ifndef NOEXTRACHECKS
  1512. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1513. currentRoutine->panic = -1;
  1514. return;
  1515. }
  1516. #endif
  1517. ink_pop(ctx);
  1518. currentRoutine->stack[currentRoutine->top-1].value = b.value ^ a.value;
  1519. }
  1520. static void gt_int(struct context* ctx) {
  1521. struct ink_routine* currentRoutine;
  1522. struct elem a;
  1523. struct elem b;
  1524. currentRoutine = ctx->routines + ctx->routine_current;
  1525. #ifndef NOEXTRACHECKS
  1526. if(currentRoutine->top < 2) {
  1527. currentRoutine->panic = -1;
  1528. return;
  1529. }
  1530. #endif
  1531. a = currentRoutine->stack[currentRoutine->top-1];
  1532. b = currentRoutine->stack[currentRoutine->top-2];
  1533. #ifndef NOEXTRACHECKS
  1534. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1535. currentRoutine->panic = -1;
  1536. return;
  1537. }
  1538. #endif
  1539. ink_pop(ctx);
  1540. currentRoutine->stack[currentRoutine->top-1].value = b.value > a.value;
  1541. }
  1542. static void gte_int(struct context* ctx) {
  1543. struct ink_routine* currentRoutine;
  1544. struct elem a;
  1545. struct elem b;
  1546. currentRoutine = ctx->routines + ctx->routine_current;
  1547. #ifndef NOEXTRACHECKS
  1548. if(currentRoutine->top < 2) {
  1549. currentRoutine->panic = -1;
  1550. return;
  1551. }
  1552. #endif
  1553. a = currentRoutine->stack[currentRoutine->top-1];
  1554. b = currentRoutine->stack[currentRoutine->top-2];
  1555. #ifndef NOEXTRACHECKS
  1556. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1557. currentRoutine->panic = -1;
  1558. return;
  1559. }
  1560. #endif
  1561. ink_pop(ctx);
  1562. currentRoutine->stack[currentRoutine->top-1].value = b.value >= a.value;
  1563. }
  1564. static void lte_int(struct context* ctx) {
  1565. struct ink_routine* currentRoutine;
  1566. struct elem a;
  1567. struct elem b;
  1568. currentRoutine = ctx->routines + ctx->routine_current;
  1569. #ifndef NOEXTRACHECKS
  1570. if(currentRoutine->top < 2) {
  1571. currentRoutine->panic = -1;
  1572. return;
  1573. }
  1574. #endif
  1575. a = currentRoutine->stack[currentRoutine->top-1];
  1576. b = currentRoutine->stack[currentRoutine->top-2];
  1577. #ifndef NOEXTRACHECKS
  1578. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1579. currentRoutine->panic = -1;
  1580. return;
  1581. }
  1582. #endif
  1583. ink_pop(ctx);
  1584. currentRoutine->stack[currentRoutine->top-1].value = b.value <= a.value;
  1585. }
  1586. #endif /* NOEXTRAARITHMETIC */
  1587. static void lt_int(struct context* ctx) {
  1588. struct ink_routine* currentRoutine;
  1589. struct elem a;
  1590. struct elem b;
  1591. currentRoutine = ctx->routines + ctx->routine_current;
  1592. #ifndef NOEXTRACHECKS
  1593. if(currentRoutine->top < 2) {
  1594. currentRoutine->panic = -1;
  1595. return;
  1596. }
  1597. #endif
  1598. a = currentRoutine->stack[currentRoutine->top-1];
  1599. b = currentRoutine->stack[currentRoutine->top-2];
  1600. #ifndef NOEXTRACHECKS
  1601. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1602. currentRoutine->panic = -1;
  1603. return;
  1604. }
  1605. #endif
  1606. ink_pop(ctx);
  1607. currentRoutine->stack[currentRoutine->top-1].value = b.value < a.value;
  1608. }
  1609. static void dupe_elem(struct context* ctx) {
  1610. struct ink_routine* currentRoutine;
  1611. struct elem a;
  1612. #pragma GCC diagnostic push
  1613. #pragma GCC diagnostic ignored "-Wunused-parameter"
  1614. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  1615. int err;
  1616. #pragma GCC diagnostic pop
  1617. currentRoutine = ctx->routines + ctx->routine_current;
  1618. #ifndef NOEXTRACHECKS
  1619. if(currentRoutine->top < 1) {
  1620. ctx->panic = 1;
  1621. return;
  1622. }
  1623. #endif
  1624. a = currentRoutine->stack[currentRoutine->top-1];
  1625. err = ink_push(ctx, a);
  1626. #ifndef NOEXTRACHECKS
  1627. if(err < 0) ctx->panic = 1;
  1628. #endif
  1629. }
  1630. static void drop_elem(struct context* ctx) {
  1631. #ifndef NOEXTRACHECKS
  1632. struct ink_routine* currentRoutine;
  1633. currentRoutine = ctx->routines + ctx->routine_current;
  1634. if(currentRoutine->top < 1) {
  1635. ctx->panic = 1;
  1636. return;
  1637. }
  1638. #endif
  1639. ink_pop(ctx);
  1640. }
  1641. static void pluck_elem(struct context* ctx) {
  1642. struct ink_routine* currentRoutine;
  1643. struct elem a;
  1644. int position;
  1645. #pragma GCC diagnostic push
  1646. #pragma GCC diagnostic ignored "-Wunused-parameter"
  1647. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  1648. int err;
  1649. #pragma GCC diagnostic pop
  1650. currentRoutine = ctx->routines + ctx->routine_current;
  1651. #ifndef NOEXTRACHECKS
  1652. if(currentRoutine->top < 1) {
  1653. currentRoutine->panic = -1;
  1654. return;
  1655. }
  1656. #endif
  1657. a = currentRoutine->stack[currentRoutine->top-1];
  1658. #ifndef NOEXTRACHECKS
  1659. if(a.type != INK_INTEGER) {
  1660. ctx->panic = 1;
  1661. return;
  1662. }
  1663. #endif
  1664. position = currentRoutine->top - (a.value + 1);
  1665. #ifndef NOEXTRACHECKS
  1666. if(position >= currentRoutine->top || position < 0) {
  1667. ctx->panic = 1;
  1668. return;
  1669. }
  1670. #endif
  1671. ink_pop(ctx);
  1672. err = ink_push(ctx, currentRoutine->stack[position]);
  1673. #ifndef NOEXTRACHECKS
  1674. if(err < 0) ctx->panic = 1;
  1675. #endif
  1676. }
  1677. static void swap_elem(struct context* ctx) {
  1678. struct ink_routine* currentRoutine;
  1679. struct elem a;
  1680. struct elem b;
  1681. currentRoutine = ctx->routines + ctx->routine_current;
  1682. #ifndef NOEXTRACHECKS
  1683. if(currentRoutine->top < 2) {
  1684. currentRoutine->panic = -1;
  1685. return;
  1686. }
  1687. #endif
  1688. a = currentRoutine->stack[currentRoutine->top-1];
  1689. b = currentRoutine->stack[currentRoutine->top-2];
  1690. currentRoutine->stack[currentRoutine->top-2] = a;
  1691. currentRoutine->stack[currentRoutine->top-1] = b;
  1692. }
  1693. static void return_if(struct context* ctx) {
  1694. struct ink_routine* currentRoutine;
  1695. struct elem a;
  1696. currentRoutine = ctx->routines + ctx->routine_current;
  1697. #ifndef NOEXTRACHECKS
  1698. if(currentRoutine->top < 1) {
  1699. ctx->panic = -1;
  1700. return;
  1701. }
  1702. #endif
  1703. a = currentRoutine->stack[currentRoutine->top-1];
  1704. #ifndef NOEXTRACHECKS
  1705. if(a.type != INK_INTEGER) {
  1706. ctx->panic = 1;
  1707. return;
  1708. }
  1709. #endif
  1710. if(a.value) {
  1711. ink_pop_fn(ctx);
  1712. ink_pop_fn(ctx);
  1713. }
  1714. ink_pop(ctx);
  1715. return;
  1716. }
  1717. static void jump_if(struct context* ctx) {
  1718. struct ink_routine* currentRoutine;
  1719. struct elem label;
  1720. struct elem condition;
  1721. currentRoutine = ctx->routines + ctx->routine_current;
  1722. #ifndef NOEXTRACHECKS
  1723. if(currentRoutine->top < 2) {
  1724. ctx->panic = -1;
  1725. return;
  1726. }
  1727. #endif
  1728. label = currentRoutine->stack[currentRoutine->top-1];
  1729. condition = currentRoutine->stack[currentRoutine->top-2];
  1730. #ifndef NOEXTRACHECKS
  1731. if(label.type != INK_INTEGER || condition.type != INK_INTEGER) {
  1732. ctx->panic = -1;
  1733. return;
  1734. }
  1735. #endif
  1736. ink_pop(ctx);
  1737. ink_pop(ctx);
  1738. ink_pop_fn(ctx);
  1739. if(condition.value) {
  1740. currentRoutine->function_stack[currentRoutine->function_stack_top - 1].index += label.value - 2;
  1741. }
  1742. return;
  1743. }
  1744. static void print_int(struct context* ctx) {
  1745. struct ink_routine* currentRoutine;
  1746. struct elem a;
  1747. char* n;
  1748. char* str;
  1749. currentRoutine = ctx->routines + ctx->routine_current;
  1750. #ifndef NOEXTRACHECKS
  1751. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  1752. currentRoutine->panic = -1;
  1753. return;
  1754. }
  1755. #endif
  1756. a = currentRoutine->stack[currentRoutine->top-1];
  1757. ink_pop(ctx);
  1758. n = ink_itoa(ctx, a.value);
  1759. str = n;
  1760. while (*str) {
  1761. ctx->putchar(ctx, *str);
  1762. ++str;
  1763. }
  1764. ctx->free(ctx, n);
  1765. n = NULL;
  1766. }
  1767. static void print_as_utf8(struct context* ctx) {
  1768. struct ink_routine* currentRoutine;
  1769. struct elem a;
  1770. currentRoutine = ctx->routines + ctx->routine_current;
  1771. #ifndef NOEXTRACHECKS
  1772. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  1773. ctx->panic = -1;
  1774. return;
  1775. }
  1776. #endif
  1777. a = currentRoutine->stack[currentRoutine->top-1];
  1778. if(a.value <= 0x7F) {
  1779. ctx->putchar(ctx, a.value);
  1780. } else if(a.value <= 0x7FF) {
  1781. ctx->putchar(ctx, ((a.value & 0xFC0) >> 6) | 192);
  1782. ctx->putchar(ctx, (a.value & 0x3F) | 128);
  1783. } else if(a.value <= 0xFFFF) {
  1784. ctx->putchar(ctx, ((a.value & 0x3F000) >> 12) | 224);
  1785. ctx->putchar(ctx, ((a.value & 0xFC0) >> 6) | 128);
  1786. ctx->putchar(ctx, (a.value & 0x3F) | 128);
  1787. } else if(a.value <= 0x10FFFF) {
  1788. ctx->putchar(ctx, ((a.value & 0x3C0000) >> 18) | 240);
  1789. ctx->putchar(ctx, ((a.value & 0x3F000) >> 12) | 128);
  1790. ctx->putchar(ctx, ((a.value & 0xFC0) >> 6) | 128);
  1791. ctx->putchar(ctx, (a.value & 0x3F) | 128);
  1792. } else {
  1793. ctx->panic = -1;
  1794. return;
  1795. }
  1796. ink_pop(ctx);
  1797. }
  1798. int get_type_by_name(struct context* ctx, const char* name) {
  1799. int i;
  1800. for(i = 0; i < ctx->types_top; ++i) {
  1801. if(strcmp(ctx->types[i].name, name) == 0) {
  1802. return i + 16;
  1803. }
  1804. }
  1805. return -1;
  1806. }
  1807. static void run_gc(struct context* ctx) {
  1808. ink_gc(ctx);
  1809. }
  1810. static void clear_stack(struct context* ctx) {
  1811. struct ink_routine* currentRoutine;
  1812. currentRoutine = ctx->routines + ctx->routine_current;
  1813. while (currentRoutine->top >= 1) {
  1814. ink_pop(ctx);
  1815. }
  1816. return;
  1817. }
  1818. static void dump_stack(struct context* ctx) {
  1819. struct ink_routine* currentRoutine;
  1820. int index;
  1821. char* idx;
  1822. char* type;
  1823. char* value;
  1824. char* it;
  1825. currentRoutine = ctx->routines + ctx->routine_current;
  1826. index = currentRoutine->top;
  1827. while (index) {
  1828. --index;
  1829. idx = ink_itoa(ctx,index);
  1830. type = ink_itoa(ctx, currentRoutine->stack[index].type);
  1831. value = ink_itoa(ctx,currentRoutine->stack[index].value);
  1832. for(it = idx; *it; ++it) ctx->putchar(ctx, *it);
  1833. ctx->putchar(ctx, ' ');ctx->putchar(ctx, '|');ctx->putchar(ctx, ' ');
  1834. for(it = type; *it; ++it) ctx->putchar(ctx, *it);
  1835. ctx->putchar(ctx, ' ');ctx->putchar(ctx, '|');ctx->putchar(ctx, ' ');
  1836. for(it = value; *it; ++it) ctx->putchar(ctx, *it);
  1837. ctx->putchar(ctx, '\n');
  1838. if(value != NULL) ctx->free(ctx, value);
  1839. if(type != NULL) ctx->free(ctx, type);
  1840. if(idx != NULL) ctx->free(ctx, idx);
  1841. value = type = idx = NULL;
  1842. }
  1843. return;
  1844. }
  1845. static void collect_noop() {}
  1846. static struct ink_collection_list gc_noop() {
  1847. struct ink_collection_list c;
  1848. c.elements = NULL;
  1849. c.count = 0;
  1850. return c;
  1851. }
  1852. #ifndef NOARRAYLIB
  1853. static void collect_array(struct context* ctx, void* array) {
  1854. struct ink_array* ary;
  1855. ary = array;
  1856. if(ary->elements != NULL) {
  1857. ctx->free(ctx, ary->elements);
  1858. ary->elements = NULL;
  1859. }
  1860. }
  1861. static struct ink_collection_list gc_array(struct context* ctx, void* array) {
  1862. struct ink_array* ary;
  1863. struct ink_collection_list c;
  1864. ary = array;
  1865. c.elements = ctx->inner_malloc(ctx, sizeof(struct elem)*ary->top);
  1866. c.count = ary->top;
  1867. memcpy(c.elements, ary->elements, sizeof(struct elem)*ary->top);
  1868. return c;
  1869. }
  1870. static void new_array(struct context* ctx) {
  1871. int tid;
  1872. struct elem e;
  1873. struct ink_array ary;
  1874. tid = get_type_by_name(ctx, "array");
  1875. ary.elements = NULL;
  1876. ary.top = 0;
  1877. ary.capacity = 0;
  1878. ary.flags = 0;
  1879. e = ink_make_native(ctx, tid, &ary);
  1880. ink_push(ctx, e);
  1881. }
  1882. #ifndef NOSTRINGLITERALS
  1883. static void new_protected_array(struct context* ctx) {
  1884. int tid;
  1885. struct elem e;
  1886. struct ink_array ary;
  1887. tid = get_type_by_name(ctx, "array");
  1888. ary.elements = NULL;
  1889. ary.top = 0;
  1890. ary.capacity = 0;
  1891. e = ink_make_native_unsafe(ctx, tid, &ary, 1);
  1892. ink_push(ctx, e);
  1893. }
  1894. #endif
  1895. static void push_array_stack_delim(struct context* ctx) {
  1896. int tid;
  1897. struct elem e;
  1898. tid = get_type_by_name(ctx, "array_marker");
  1899. e.type = tid;
  1900. e.value = 0;
  1901. ink_push(ctx, e);
  1902. }
  1903. int array_push_s(struct context* ctx, struct ink_array* ary, struct elem value) {
  1904. if(ary->elements == NULL) {
  1905. ary->elements = ctx->malloc(ctx, sizeof(struct elem) * 8);
  1906. ary->top = 0;
  1907. ary->capacity = 8;
  1908. } else if(ary->top == ary->capacity) {
  1909. int new_count;
  1910. void* renewed;
  1911. new_count = (ary->capacity + ary->capacity/2);
  1912. renewed = ctx->realloc(ctx, ary->elements, sizeof(struct elem) * new_count);
  1913. if(renewed == NULL) {
  1914. return 1;
  1915. } else {
  1916. ary->elements = renewed;
  1917. ary->capacity = new_count;
  1918. }
  1919. }
  1920. ary->elements[ary->top] = value;
  1921. ary->top++;
  1922. return 0;
  1923. }
  1924. void array_push(struct context* ctx, struct ink_routine* currentRoutine, struct ink_array* ary, struct elem value) {
  1925. if(array_push_s(ctx, ary, value)) {
  1926. currentRoutine->panic = -1;
  1927. }
  1928. }
  1929. static void push_array(struct context* ctx) {
  1930. struct elem a;
  1931. struct ink_routine* currentRoutine;
  1932. struct ink_array* ary;
  1933. #ifndef NOEXTRACHECKS
  1934. int tid;
  1935. tid = get_type_by_name(ctx, "array");
  1936. #endif
  1937. currentRoutine = ctx->routines + ctx->routine_current;
  1938. #ifndef NOEXTRACHECKS
  1939. if(currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top-1].type != tid) {
  1940. currentRoutine->panic = -1;
  1941. return;
  1942. }
  1943. #endif
  1944. a = currentRoutine->stack[currentRoutine->top-1];
  1945. ary= ink_get_value(ctx, a);
  1946. #ifndef NOEXTRACHECKS
  1947. if(ary == NULL) {
  1948. currentRoutine->panic = -1;
  1949. return;
  1950. }
  1951. #endif
  1952. ink_pop(ctx);
  1953. array_push(ctx, currentRoutine, ary, currentRoutine->stack[currentRoutine->top-1]);
  1954. ink_pop(ctx);
  1955. }
  1956. static void push_delimited_array(struct context* ctx) {
  1957. int tid, idx, counter, i;
  1958. struct elem a;
  1959. struct ink_routine* currentRoutine;
  1960. struct ink_array* ary;
  1961. tid = get_type_by_name(ctx, "array_marker");
  1962. currentRoutine = ctx->routines + ctx->routine_current;
  1963. #ifndef NOEXTRACHECKS
  1964. if(currentRoutine->top < 1) {
  1965. currentRoutine->panic = -1;
  1966. return;
  1967. }
  1968. #endif
  1969. new_array(ctx);
  1970. a = currentRoutine->stack[currentRoutine->top-1];
  1971. ink_pop(ctx);
  1972. ary= ink_get_value(ctx, a);
  1973. for(idx = 1; idx <= currentRoutine->top; ++idx) {
  1974. if(currentRoutine->stack[currentRoutine->top-idx].type == tid) {
  1975. break;
  1976. }
  1977. }
  1978. /* Save for cleanup */
  1979. counter = idx;
  1980. /* Don't copy the delimiter */
  1981. idx -= 1;
  1982. ary->elements = malloc(sizeof(struct elem) * idx);
  1983. #ifndef NOEXTRACHECKS
  1984. if(ary->elements == NULL) {
  1985. currentRoutine->panic = -541;
  1986. return;
  1987. }
  1988. #endif
  1989. ary->capacity = idx;
  1990. ary->top = 0;
  1991. /* Copy the data */
  1992. for(i = currentRoutine->top - idx; i < currentRoutine->top; ++i) {
  1993. ary->elements[ary->top] = currentRoutine->stack[i];
  1994. ++(ary->top);
  1995. }
  1996. /* Cleanup */
  1997. while(counter--) {
  1998. ink_pop(ctx);
  1999. }
  2000. /* Put value in place */
  2001. ink_push(ctx, a);
  2002. }
  2003. static void index_array(struct context* ctx) {
  2004. struct ink_routine *currentRoutine;
  2005. struct elem a;
  2006. struct ink_array *ary;
  2007. struct elem idx;
  2008. #ifndef NOEXTRACHECKS
  2009. int tid;
  2010. tid = get_type_by_name(ctx, "array");
  2011. #endif
  2012. currentRoutine = ctx->routines + ctx->routine_current;
  2013. #ifndef NOEXTRACHECKS
  2014. if (currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) {
  2015. currentRoutine->panic = -1;
  2016. return;
  2017. }
  2018. #endif
  2019. a = currentRoutine->stack[currentRoutine->top - 1];
  2020. ary = ink_get_value(ctx, a);
  2021. #ifndef NOEXTRACHECKS
  2022. if (ary == NULL) {
  2023. currentRoutine->panic = -1;
  2024. return;
  2025. }
  2026. #endif
  2027. ink_pop(ctx);
  2028. idx = currentRoutine->stack[currentRoutine->top - 1];
  2029. ink_pop(ctx);
  2030. #ifndef NOEXTRACHECKS
  2031. if(ary->top <= idx.value) {
  2032. currentRoutine->panic = -1;
  2033. return;
  2034. }
  2035. #endif
  2036. ink_push(ctx, ary->elements[idx.value]);
  2037. }
  2038. static void set_array(struct context* ctx) {
  2039. struct ink_routine *currentRoutine;
  2040. struct elem a;
  2041. struct ink_array *ary;
  2042. struct elem idx;
  2043. struct elem value;
  2044. #ifndef NOEXTRACHECKS
  2045. int tid;
  2046. tid = get_type_by_name(ctx, "array");
  2047. #endif
  2048. currentRoutine = ctx->routines + ctx->routine_current;
  2049. #ifndef NOEXTRACHECKS
  2050. if (currentRoutine->top < 3 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) {
  2051. currentRoutine->panic = -1;
  2052. return;
  2053. }
  2054. #endif
  2055. a = currentRoutine->stack[currentRoutine->top - 1];
  2056. ary = ink_get_value(ctx, a);
  2057. #ifndef NOEXTRACHECKS
  2058. if (ary == NULL) {
  2059. currentRoutine->panic = -1;
  2060. return;
  2061. }
  2062. #endif
  2063. idx = currentRoutine->stack[currentRoutine->top - 2];
  2064. value = currentRoutine->stack[currentRoutine->top - 3];
  2065. #ifndef NOEXTRACHECKS
  2066. if(ary->top <= idx.value) {
  2067. currentRoutine->panic = -1;
  2068. return;
  2069. }
  2070. #endif
  2071. ink_pop(ctx);
  2072. ink_pop(ctx);
  2073. ink_pop(ctx);
  2074. ary->elements[idx.value] = value;
  2075. }
  2076. static void get_size_array(struct context* ctx) {
  2077. struct ink_routine *currentRoutine;
  2078. struct elem a;
  2079. struct ink_array *ary;
  2080. struct elem sz;
  2081. #ifndef NOEXTRACHECKS
  2082. int tid;
  2083. tid = get_type_by_name(ctx, "array");
  2084. #endif
  2085. currentRoutine = ctx->routines + ctx->routine_current;
  2086. #ifndef NOEXTRACHECKS
  2087. if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) {
  2088. currentRoutine->panic = -1;
  2089. return;
  2090. }
  2091. #endif
  2092. a = currentRoutine->stack[currentRoutine->top - 1];
  2093. ary = ink_get_value(ctx, a);
  2094. #ifndef NOEXTRACHECKS
  2095. if (ary == NULL) {
  2096. currentRoutine->panic = -1;
  2097. return;
  2098. }
  2099. #endif
  2100. ink_pop(ctx);
  2101. sz.type = INK_INTEGER;
  2102. sz.value = ary->top;
  2103. ink_push(ctx, sz);
  2104. }
  2105. static void is_array(struct context* ctx) {
  2106. int tid;
  2107. struct ink_routine *currentRoutine;
  2108. struct elem a;
  2109. tid = get_type_by_name(ctx, "array");
  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 == tid;
  2119. ink_pop(ctx);
  2120. ink_push(ctx, a);
  2121. }
  2122. static void is_int(struct context* ctx) {
  2123. struct ink_routine *currentRoutine;
  2124. struct elem a;
  2125. currentRoutine = ctx->routines + ctx->routine_current;
  2126. #ifndef NOEXTRACHECKS
  2127. if (currentRoutine->top < 1) {
  2128. currentRoutine->panic = -1;
  2129. return;
  2130. }
  2131. #endif
  2132. a.type = INK_INTEGER;
  2133. a.value = currentRoutine->stack[currentRoutine->top - 1].type == INK_INTEGER;
  2134. ink_pop(ctx);
  2135. ink_push(ctx, a);
  2136. }
  2137. static void print_array_of_codepoints(struct context* ctx) {
  2138. int i;
  2139. struct ink_routine *currentRoutine;
  2140. struct elem a;
  2141. struct ink_array *ary;
  2142. #ifndef NOEXTRACHECKS
  2143. int tid;
  2144. tid = get_type_by_name(ctx, "array");
  2145. #endif
  2146. currentRoutine = ctx->routines + ctx->routine_current;
  2147. #ifndef NOEXTRACHECKS
  2148. if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) {
  2149. currentRoutine->panic = -1;
  2150. return;
  2151. }
  2152. #endif
  2153. a = currentRoutine->stack[currentRoutine->top - 1];
  2154. ary = ink_get_value(ctx, a);
  2155. #ifndef NOEXTRACHECKS
  2156. for(i = 0; i < ary->top; ++i) {
  2157. if(ary->elements[i].type != INK_INTEGER) {
  2158. currentRoutine->panic = -1;
  2159. return;
  2160. }
  2161. }
  2162. #endif
  2163. ink_pop(ctx);
  2164. for(i = 0; i < ary->top; ++i) {
  2165. ink_push(ctx, ary->elements[i]);
  2166. print_as_utf8(ctx);
  2167. }
  2168. }
  2169. static void arrayify_stack(struct context* ctx) {
  2170. struct ink_routine* currentRoutine;
  2171. struct elem array_ref;
  2172. struct ink_array* ary;
  2173. int idx;
  2174. currentRoutine = ctx->routines + ctx->routine_current;
  2175. new_array(ctx);
  2176. if(currentRoutine->panic < 0) return;
  2177. array_ref = currentRoutine->stack[currentRoutine->top - 1];
  2178. ary = ink_get_value(ctx, array_ref);
  2179. #ifndef NOEXTRACHECKS
  2180. if(ary == NULL) {
  2181. currentRoutine->panic = -717;
  2182. return;
  2183. }
  2184. #endif
  2185. ink_pop(ctx);
  2186. for(idx = 0; idx < currentRoutine->top; ++idx) {
  2187. array_push(ctx, currentRoutine, ary, currentRoutine->stack[idx]);
  2188. }
  2189. while (currentRoutine->top > 0) {
  2190. ink_pop(ctx);
  2191. }
  2192. ink_push(ctx, array_ref);
  2193. return;
  2194. }
  2195. #endif /* NOARRAYLIB */
  2196. int ink_std_library(struct context* ctx) {
  2197. int v;
  2198. v = 0;
  2199. v += ink_add_native(ctx, "sys.trace", print_stacktrace);
  2200. v += ink_add_native(ctx, "sys.gc", run_gc);
  2201. v += ink_add_native(ctx, "print_int", print_int);
  2202. v += ink_add_native(ctx, "print_utf8", print_as_utf8);
  2203. v += ink_add_native(ctx, "+", add_int);
  2204. v += ink_add_native(ctx, "-", sub_int);
  2205. v += ink_add_native(ctx, "*", mult_int);
  2206. v += ink_add_native(ctx, "/", div_int);
  2207. v += ink_add_native(ctx, "==", is_equal);
  2208. v += ink_add_native(ctx, "!=", is_different);
  2209. v += ink_add_native(ctx, "<", lt_int);
  2210. v += ink_add_native(ctx, "swap", swap_elem);
  2211. v += ink_add_native(ctx, "dup", dupe_elem);
  2212. v += ink_add_native(ctx, "drop", drop_elem);
  2213. v += ink_add_native(ctx, "stack.clear", clear_stack);
  2214. v += ink_add_native(ctx, "stack.dump", dump_stack);
  2215. v += ink_add_native(ctx, "pluck", pluck_elem);
  2216. v += ink_add_native(ctx, "return_if", return_if);
  2217. v += ink_add_native(ctx, "jump_if", jump_if);
  2218. v += ink_add_native(ctx, "is.int", is_int);
  2219. #ifndef NOEXTRAARITHMETIC
  2220. v += ink_add_native(ctx, ">", gt_int);
  2221. v += ink_add_native(ctx, ">=", gte_int);
  2222. v += ink_add_native(ctx, "=<", lte_int);
  2223. v += ink_add_native(ctx, "%", rem_int);
  2224. v += ink_add_native(ctx, "int.xor", xor_int);
  2225. #endif /* NOEXTRAARITHMETIC */
  2226. #ifndef NOARRAYLIB
  2227. ink_new_type(ctx, "array", sizeof(struct ink_array), collect_array, gc_array);
  2228. ink_new_type(ctx, "array_marker", 0, collect_noop, gc_noop);
  2229. v += ink_add_native(ctx, "[", push_array_stack_delim);
  2230. v += ink_add_native(ctx, "]", push_delimited_array);
  2231. v += ink_add_native(ctx, "array.new", new_array);
  2232. v += ink_add_native(ctx, "array.push", push_array);
  2233. v += ink_add_native(ctx, "array.index", index_array);
  2234. v += ink_add_native(ctx, "array.set", set_array);
  2235. v += ink_add_native(ctx, "array.size", get_size_array);
  2236. v += ink_add_native(ctx, "array.print_utf8", print_array_of_codepoints);
  2237. v += ink_add_native(ctx, "is.array", is_array);
  2238. v += ink_add_native(ctx, "stack.to_array", arrayify_stack);
  2239. #endif /* NOARRAYLIB */
  2240. return v;
  2241. }