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.

2149 lines
58 KiB

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