A minimalistic programming language written in C89.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1497 rindas
39 KiB

pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
pirms 5 mēnešiem
pirms 6 mēnešiem
  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 stack_frame) * 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, 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. /* Mark the rest of the data */
  955. do {
  956. marked = 0;
  957. for (i = 0; i < ctx->types_top; ++i) {
  958. for (j = 0; j < ctx->types[i].elements_top; ++j) {
  959. /* Only mark from things that are active and detected as in use */
  960. if (ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].uses) {
  961. struct ink_collection_list c;
  962. c = ctx->types[i].gc(ctx, ctx->types[i].elements[j].data);
  963. for (k = 0; k < c.count; ++k) {
  964. v = ink_get_value_link(ctx, c.elements[k]);
  965. /* Never mark twice to avoid infinite loops with e.g. arrays that contain themselves */
  966. if (v != NULL && !v->uses) {
  967. ++v->uses;
  968. marked = 1;
  969. }
  970. }
  971. if (c.elements != NULL) ctx->inner_free(c.elements);
  972. }
  973. }
  974. }
  975. } while(marked);
  976. /* Sweep phase: explore any allocated data and sweep the unused away */
  977. for(i = 0; i < ctx->types_top; ++i) {
  978. for(j = 0; j < ctx->types[i].elements_top; ++j) {
  979. if(ctx->types[i].elements[j].uses == 0 && ctx->types[i].elements[j].in_use) {
  980. ctx->collections++;
  981. ctx->types[i].collect(ctx, ctx->types[i].elements[j].data);
  982. if(ctx->types[i].element_size > 0) {
  983. ctx->free(ctx->types[i].elements[j].data);
  984. }
  985. ctx->types[i].elements[j].data = NULL;
  986. ctx->types[i].elements[j].uses = 0;
  987. ctx->types[i].elements[j].in_use = 0;
  988. }
  989. }
  990. }
  991. }
  992. /**********************************************************************************************************************/
  993. static void print_stacktrace(struct context* _) {
  994. int i;
  995. struct ink_routine* currentRoutine;
  996. currentRoutine = _->routines + _->routine_current;
  997. for(i = 0; i < currentRoutine->function_stack_top; ++i) {
  998. struct elem thing;
  999. char *n;
  1000. thing = currentRoutine->function_stack[i].executing;
  1001. switch(thing.type) {
  1002. case INK_NATIVE_FUNCTION: {
  1003. n = _->native_words[thing.value].name;
  1004. while (*n) {
  1005. _->putchar(*n);
  1006. ++n;
  1007. }
  1008. _->putchar(10);
  1009. break;
  1010. }
  1011. case INK_FUNCTION:{
  1012. n = _->native_words[thing.value].name;
  1013. while (*n) {
  1014. _->putchar(*n);
  1015. ++n;
  1016. }
  1017. _->putchar(':');
  1018. n = ink_itoa(_, currentRoutine->function_stack[i].index);
  1019. while (*n) {
  1020. _->putchar(*n);
  1021. ++n;
  1022. }
  1023. _->free(n);
  1024. _->putchar(10);
  1025. break;
  1026. }
  1027. default:
  1028. break;
  1029. }
  1030. }
  1031. }
  1032. static void add_int(struct context* ctx) {
  1033. struct ink_routine* currentRoutine;
  1034. struct elem a;
  1035. struct elem b;
  1036. currentRoutine = ctx->routines + ctx->routine_current;
  1037. if(currentRoutine->top < 2) {
  1038. currentRoutine->panic = 1;
  1039. return;
  1040. }
  1041. a = currentRoutine->stack[currentRoutine->top-1];
  1042. b = currentRoutine->stack[currentRoutine->top-2];
  1043. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1044. ctx->panic = 1;
  1045. return;
  1046. }
  1047. ink_pop(ctx);
  1048. currentRoutine->stack[currentRoutine->top-1].value = a.value + b.value;
  1049. }
  1050. static void sub_int(struct context* ctx) {
  1051. struct ink_routine* currentRoutine;
  1052. struct elem a;
  1053. struct elem b;
  1054. currentRoutine = ctx->routines + ctx->routine_current;
  1055. if(currentRoutine->top < 2) {
  1056. currentRoutine->panic = 1;
  1057. return;
  1058. }
  1059. a = currentRoutine->stack[currentRoutine->top-1];
  1060. b = currentRoutine->stack[currentRoutine->top-2];
  1061. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1062. currentRoutine->panic = 1;
  1063. return;
  1064. }
  1065. ink_pop(ctx);
  1066. currentRoutine->stack[currentRoutine->top-1].value = b.value - a.value;
  1067. }
  1068. static void mult_int(struct context* ctx) {
  1069. struct ink_routine* currentRoutine;
  1070. struct elem a;
  1071. struct elem b;
  1072. currentRoutine = ctx->routines + ctx->routine_current;
  1073. if(currentRoutine->top < 2) {
  1074. currentRoutine->panic = 1;
  1075. return;
  1076. }
  1077. a = currentRoutine->stack[currentRoutine->top-1];
  1078. b = currentRoutine->stack[currentRoutine->top-2];
  1079. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1080. currentRoutine->panic = 1;
  1081. return;
  1082. }
  1083. ink_pop(ctx);
  1084. currentRoutine->stack[currentRoutine->top-1].value = b.value * a.value;
  1085. }
  1086. static void div_int(struct context* ctx) {
  1087. struct ink_routine* currentRoutine;
  1088. struct elem a;
  1089. struct elem b;
  1090. currentRoutine = ctx->routines + ctx->routine_current;
  1091. if(currentRoutine->top < 2) {
  1092. currentRoutine->panic = 1;
  1093. return;
  1094. }
  1095. a = currentRoutine->stack[currentRoutine->top-1];
  1096. b = currentRoutine->stack[currentRoutine->top-2];
  1097. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1098. currentRoutine->panic = 1;
  1099. return;
  1100. }
  1101. ink_pop(ctx);
  1102. currentRoutine->stack[currentRoutine->top-1].value = b.value / a.value;
  1103. }
  1104. static void rem_int(struct context* ctx) {
  1105. struct ink_routine* currentRoutine;
  1106. struct elem a;
  1107. struct elem b;
  1108. currentRoutine = ctx->routines + ctx->routine_current;
  1109. if(currentRoutine->top < 2) {
  1110. currentRoutine->panic = 1;
  1111. return;
  1112. }
  1113. a = currentRoutine->stack[currentRoutine->top-1];
  1114. b = currentRoutine->stack[currentRoutine->top-2];
  1115. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1116. currentRoutine->panic = 1;
  1117. return;
  1118. }
  1119. ink_pop(ctx);
  1120. currentRoutine->stack[currentRoutine->top-1].value = b.value % a.value;
  1121. }
  1122. static void dupe_elem(struct context* ctx) {
  1123. struct ink_routine* currentRoutine;
  1124. struct elem a;
  1125. int err;
  1126. currentRoutine = ctx->routines + ctx->routine_current;
  1127. if(currentRoutine->top < 1) {
  1128. ctx->panic = 1;
  1129. return;
  1130. }
  1131. a = currentRoutine->stack[currentRoutine->top-1];
  1132. err = ink_push(ctx, a);
  1133. if(err < 0) ctx->panic = 1;
  1134. }
  1135. static void drop_elem(struct context* ctx) {
  1136. struct ink_routine* currentRoutine;
  1137. currentRoutine = ctx->routines + ctx->routine_current;
  1138. if(currentRoutine->top < 1) {
  1139. ctx->panic = 1;
  1140. return;
  1141. }
  1142. ink_pop(ctx);
  1143. }
  1144. static void pluck_elem(struct context* ctx) {
  1145. struct ink_routine* currentRoutine;
  1146. struct elem a;
  1147. int position, err;
  1148. currentRoutine = ctx->routines + ctx->routine_current;
  1149. if(currentRoutine->top < 1) {
  1150. currentRoutine->panic = 1;
  1151. return;
  1152. }
  1153. a = currentRoutine->stack[currentRoutine->top-1];
  1154. if(a.type != INK_INTEGER) {
  1155. ctx->panic = 1;
  1156. return;
  1157. }
  1158. position = currentRoutine->top - (a.value + 1);
  1159. if(position >= currentRoutine->top || position < 0) {
  1160. ctx->panic = 1;
  1161. return;
  1162. }
  1163. ink_pop(ctx);
  1164. err = ink_push(ctx, currentRoutine->stack[position]);
  1165. if(err < 0) ctx->panic = 1;
  1166. }
  1167. static void swap_elem(struct context* ctx) {
  1168. struct ink_routine* currentRoutine;
  1169. struct elem a;
  1170. struct elem b;
  1171. currentRoutine = ctx->routines + ctx->routine_current;
  1172. if(currentRoutine->top < 2) {
  1173. currentRoutine->panic = 1;
  1174. return;
  1175. }
  1176. a = currentRoutine->stack[currentRoutine->top-1];
  1177. b = currentRoutine->stack[currentRoutine->top-2];
  1178. currentRoutine->stack[currentRoutine->top-2] = a;
  1179. currentRoutine->stack[currentRoutine->top-1] = b;
  1180. }
  1181. static void return_if(struct context* ctx) {
  1182. struct ink_routine* currentRoutine;
  1183. struct elem a;
  1184. currentRoutine = ctx->routines + ctx->routine_current;
  1185. if(currentRoutine->top < 1) {
  1186. ctx->panic = 1;
  1187. return;
  1188. }
  1189. a = currentRoutine->stack[currentRoutine->top-1];
  1190. if(a.type != INK_INTEGER) {
  1191. ctx->panic = 1;
  1192. return;
  1193. }
  1194. if(a.value) {
  1195. ink_pop_fn(ctx);
  1196. ink_pop_fn(ctx);
  1197. }
  1198. ink_pop(ctx);
  1199. return;
  1200. }
  1201. static void jump_if(struct context* ctx) {
  1202. struct ink_routine* currentRoutine;
  1203. struct elem a;
  1204. currentRoutine = ctx->routines + ctx->routine_current;
  1205. if(currentRoutine->top < 1) {
  1206. ctx->panic = 1;
  1207. return;
  1208. }
  1209. a = currentRoutine->stack[currentRoutine->top-1];
  1210. if(a.type != INK_INTEGER) {
  1211. ctx->panic = 1;
  1212. return;
  1213. }
  1214. ink_pop(ctx);
  1215. if(a.value) {
  1216. ink_pop_fn(ctx);
  1217. a = currentRoutine->stack[currentRoutine->top-1];
  1218. currentRoutine->function_stack[currentRoutine->function_stack_top - 1].index += a.value - 3;
  1219. }
  1220. ink_pop(ctx);
  1221. return;
  1222. }
  1223. static void print_int(struct context* ctx) {
  1224. struct ink_routine* currentRoutine;
  1225. struct elem a;
  1226. char* n;
  1227. char* str;
  1228. currentRoutine = ctx->routines + ctx->routine_current;
  1229. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  1230. currentRoutine->panic = 1;
  1231. return;
  1232. }
  1233. a = currentRoutine->stack[currentRoutine->top-1];
  1234. ink_pop(ctx);
  1235. n = ink_itoa(ctx, a.value);
  1236. str = n;
  1237. while (*str) {
  1238. ctx->putchar(*str);
  1239. ++str;
  1240. }
  1241. ctx->free(n);
  1242. }
  1243. static void print_as_utf8(struct context* ctx) {
  1244. struct ink_routine* currentRoutine;
  1245. struct elem a;
  1246. currentRoutine = ctx->routines + ctx->routine_current;
  1247. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  1248. ctx->panic = 1;
  1249. return;
  1250. }
  1251. a = currentRoutine->stack[currentRoutine->top-1];
  1252. if(a.value <= 0x7F) {
  1253. ctx->putchar(a.value);
  1254. } else if(a.value <= 0x7FF) {
  1255. ctx->putchar(((a.value & 0xFC0) >> 6) | 192);
  1256. ctx->putchar((a.value & 0x3F) | 128);
  1257. } else if(a.value <= 0xFFFF) {
  1258. ctx->putchar(((a.value & 0x3F000) >> 12) | 224);
  1259. ctx->putchar(((a.value & 0xFC0) >> 6) | 128);
  1260. ctx->putchar((a.value & 0x3F) | 128);
  1261. } else if(a.value <= 0x10FFFF) {
  1262. ctx->putchar(((a.value & 0x3C0000) >> 18) | 240);
  1263. ctx->putchar(((a.value & 0x3F000) >> 12) | 128);
  1264. ctx->putchar(((a.value & 0xFC0) >> 6) | 128);
  1265. ctx->putchar((a.value & 0x3F) | 128);
  1266. } else {
  1267. ctx->panic = 1;
  1268. return;
  1269. }
  1270. ink_pop(ctx);
  1271. }
  1272. struct ink_array {
  1273. int top;
  1274. int capacity;
  1275. struct elem* elements;
  1276. };
  1277. static int get_type_by_name(struct context* ctx, const char* name) {
  1278. int i;
  1279. for(i = 0; i < ctx->types_top; ++i) {
  1280. if(strcmp(ctx->types[i].name, name) == 0) {
  1281. return i + 16;
  1282. }
  1283. }
  1284. return -1;
  1285. }
  1286. static void collect_array(struct context* ctx, void* array) {
  1287. struct ink_array* ary;
  1288. ary = array;
  1289. if(ary->elements != NULL) ctx->free(ary->elements);
  1290. }
  1291. static struct ink_collection_list gc_array(struct context* ctx, void* array) {
  1292. struct ink_array* ary;
  1293. struct ink_collection_list c;
  1294. ary = array;
  1295. c.elements = ctx->inner_malloc(sizeof(struct elem)*ary->top);
  1296. c.count = ary->top;
  1297. memcpy(c.elements, ary->elements, sizeof(struct elem)*ary->top);
  1298. return c;
  1299. }
  1300. static void new_array(struct context* ctx) {
  1301. int tid;
  1302. struct elem e;
  1303. struct ink_array ary;
  1304. tid = get_type_by_name(ctx, "array");
  1305. ary.elements = NULL;
  1306. ary.top = 0;
  1307. ary.capacity = 0;
  1308. e = ink_make_native(ctx, tid, &ary);
  1309. ink_push(ctx, e);
  1310. }
  1311. static void push_array(struct context* ctx) {
  1312. int tid;
  1313. struct elem a;
  1314. struct ink_routine* currentRoutine;
  1315. struct ink_array* ary;
  1316. tid = get_type_by_name(ctx, "array");
  1317. currentRoutine = ctx->routines + ctx->routine_current;
  1318. if(currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top-1].type != tid) {
  1319. currentRoutine->panic = 1;
  1320. return;
  1321. }
  1322. a = currentRoutine->stack[currentRoutine->top-1];
  1323. ary= ink_get_value(ctx, a);
  1324. if(ary == NULL) {
  1325. currentRoutine->panic = 1;
  1326. return;
  1327. }
  1328. ink_pop(ctx);
  1329. if(ary->elements == NULL) {
  1330. ary->elements = ctx->malloc(sizeof(struct elem) * 8);
  1331. ary->top = 0;
  1332. ary->capacity = 8;
  1333. } else if(ary->top == ary->capacity) {
  1334. int new_count;
  1335. void* renewed;
  1336. new_count = (ary->capacity + ary->capacity/2);
  1337. renewed = ctx->realloc(ary->elements, sizeof(struct elem) * new_count);
  1338. if(renewed == NULL) {
  1339. currentRoutine->panic = 1;
  1340. return;
  1341. } else {
  1342. ary->elements = renewed;
  1343. ary->capacity = new_count;
  1344. }
  1345. }
  1346. ary->elements[ary->top] = currentRoutine->stack[currentRoutine->top-1];
  1347. ary->top++;
  1348. ink_pop(ctx);
  1349. }
  1350. static void index_array(struct context* ctx) {
  1351. int tid;
  1352. struct ink_routine *currentRoutine;
  1353. struct elem a;
  1354. struct ink_array *ary;
  1355. struct elem idx;
  1356. tid = get_type_by_name(ctx, "array");
  1357. currentRoutine = ctx->routines + ctx->routine_current;
  1358. if (currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) {
  1359. currentRoutine->panic = 1;
  1360. return;
  1361. }
  1362. a = currentRoutine->stack[currentRoutine->top - 1];
  1363. ary = ink_get_value(ctx, a);
  1364. if (ary == NULL) {
  1365. currentRoutine->panic = 1;
  1366. return;
  1367. }
  1368. ink_pop(ctx);
  1369. idx = currentRoutine->stack[currentRoutine->top - 1];
  1370. ink_pop(ctx);
  1371. if(ary->top <= idx.value) {
  1372. currentRoutine->panic = 1;
  1373. return;
  1374. }
  1375. ink_push(ctx, ary->elements[idx.value]);
  1376. }
  1377. static void run_gc(struct context* ctx) {
  1378. ink_gc(ctx);
  1379. }
  1380. int ink_std_library(struct context* ctx) {
  1381. int v;
  1382. v = 0;
  1383. ink_new_type(ctx, "array", sizeof(struct ink_array), collect_array, gc_array);
  1384. v += ink_add_native(ctx, "array.new", new_array);
  1385. v += ink_add_native(ctx, "array.push", push_array);
  1386. v += ink_add_native(ctx, "array.index", index_array);
  1387. v += ink_add_native(ctx, "sys.trace", print_stacktrace);
  1388. v += ink_add_native(ctx, "sys.gc", run_gc);
  1389. v += ink_add_native(ctx, "print_int", print_int);
  1390. v += ink_add_native(ctx, "print_utf8", print_as_utf8);
  1391. v += ink_add_native(ctx, "+", add_int);
  1392. v += ink_add_native(ctx, "-", sub_int);
  1393. v += ink_add_native(ctx, "*", mult_int);
  1394. v += ink_add_native(ctx, "/", div_int);
  1395. v += ink_add_native(ctx, "%", rem_int);
  1396. v += ink_add_native(ctx, "swap", swap_elem);
  1397. v += ink_add_native(ctx, "dup", dupe_elem);
  1398. v += ink_add_native(ctx, "drop", drop_elem);
  1399. v += ink_add_native(ctx, "pluck", pluck_elem);
  1400. v += ink_add_native(ctx, "return_if", return_if);
  1401. v += ink_add_native(ctx, "jump_if", jump_if);
  1402. return v;
  1403. }