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

2120 lines
58 KiB

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