A minimalistic programming language written in C89.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

2188 linhas
68 KiB

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