A minimalistic programming language written in C89.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

2119 rader
57 KiB

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