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.

1799 lines
46 KiB

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