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.

1807 lines
47 KiB

6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
5 months ago
5 months ago
6 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  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 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, Clear the routines if possible */
  948. for(i = 0; i < ctx->routines_top; ++i) {
  949. if(ctx->routines[i].panic == INK_ROUTINE_SUCCESS) {
  950. ctx->free(ctx->routines[i].stack);
  951. ctx->free(ctx->routines[i].function_stack);
  952. ctx->routines[i].panic = INK_ROUTINE_CAN_REUSE;
  953. }
  954. if(ctx->routines[i].panic == INK_ROUTINE_CAN_REUSE) {
  955. continue;
  956. }
  957. for(j = 0; j < ctx->routines[i].top; ++j) {
  958. v = ink_get_value_link(ctx, ctx->routines[i].stack[j]);
  959. if(v != NULL) ++v->uses;
  960. }
  961. }
  962. /* TODO: Mark objects contained within function code */
  963. /* Mark the rest of the data */
  964. do {
  965. marked = 0;
  966. for (i = 0; i < ctx->types_top; ++i) {
  967. for (j = 0; j < ctx->types[i].elements_top; ++j) {
  968. /* Only mark from things that are active and detected as in use */
  969. if (ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].uses) {
  970. struct ink_collection_list c;
  971. c = ctx->types[i].gc(ctx, ctx->types[i].elements[j].data);
  972. for (k = 0; k < c.count; ++k) {
  973. v = ink_get_value_link(ctx, c.elements[k]);
  974. /* Never mark twice to avoid infinite loops with e.g. arrays that contain themselves */
  975. if (v != NULL && !v->uses) {
  976. ++v->uses;
  977. marked = 1;
  978. }
  979. }
  980. if (c.elements != NULL) ctx->inner_free(c.elements);
  981. }
  982. }
  983. }
  984. } while(marked);
  985. /* Sweep phase: explore any allocated data and sweep the unused away */
  986. for(i = 0; i < ctx->types_top; ++i) {
  987. for(j = 0; j < ctx->types[i].elements_top; ++j) {
  988. if(ctx->types[i].elements[j].uses == 0 && ctx->types[i].elements[j].in_use) {
  989. ctx->collections++;
  990. ctx->types[i].collect(ctx, ctx->types[i].elements[j].data);
  991. if(ctx->types[i].element_size > 0) {
  992. ctx->free(ctx->types[i].elements[j].data);
  993. }
  994. ctx->types[i].elements[j].data = NULL;
  995. ctx->types[i].elements[j].uses = 0;
  996. ctx->types[i].elements[j].in_use = 0;
  997. }
  998. }
  999. }
  1000. }
  1001. /**********************************************************************************************************************/
  1002. static void print_stacktrace(struct context* _) {
  1003. int i;
  1004. struct ink_routine* currentRoutine;
  1005. currentRoutine = _->routines + _->routine_current;
  1006. for(i = 0; i < currentRoutine->function_stack_top; ++i) {
  1007. struct elem thing;
  1008. char *n;
  1009. thing = currentRoutine->function_stack[i].executing;
  1010. switch(thing.type) {
  1011. case INK_NATIVE_FUNCTION: {
  1012. n = _->native_words[thing.value].name;
  1013. while (*n) {
  1014. _->putchar(*n);
  1015. ++n;
  1016. }
  1017. _->putchar(10);
  1018. break;
  1019. }
  1020. case INK_FUNCTION:{
  1021. n = _->native_words[thing.value].name;
  1022. while (*n) {
  1023. _->putchar(*n);
  1024. ++n;
  1025. }
  1026. _->putchar(':');
  1027. n = ink_itoa(_, currentRoutine->function_stack[i].index);
  1028. while (*n) {
  1029. _->putchar(*n);
  1030. ++n;
  1031. }
  1032. _->free(n);
  1033. _->putchar(10);
  1034. break;
  1035. }
  1036. default:
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. static void add_int(struct context* ctx) {
  1042. struct ink_routine* currentRoutine;
  1043. struct elem a;
  1044. struct elem b;
  1045. currentRoutine = ctx->routines + ctx->routine_current;
  1046. if(currentRoutine->top < 2) {
  1047. currentRoutine->panic = -1;
  1048. return;
  1049. }
  1050. a = currentRoutine->stack[currentRoutine->top-1];
  1051. b = currentRoutine->stack[currentRoutine->top-2];
  1052. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1053. ctx->panic = 1;
  1054. return;
  1055. }
  1056. ink_pop(ctx);
  1057. currentRoutine->stack[currentRoutine->top-1].value = a.value + b.value;
  1058. }
  1059. static void sub_int(struct context* ctx) {
  1060. struct ink_routine* currentRoutine;
  1061. struct elem a;
  1062. struct elem b;
  1063. currentRoutine = ctx->routines + ctx->routine_current;
  1064. if(currentRoutine->top < 2) {
  1065. currentRoutine->panic = -1;
  1066. return;
  1067. }
  1068. a = currentRoutine->stack[currentRoutine->top-1];
  1069. b = currentRoutine->stack[currentRoutine->top-2];
  1070. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1071. currentRoutine->panic = -1;
  1072. return;
  1073. }
  1074. ink_pop(ctx);
  1075. currentRoutine->stack[currentRoutine->top-1].value = b.value - a.value;
  1076. }
  1077. static void mult_int(struct context* ctx) {
  1078. struct ink_routine* currentRoutine;
  1079. struct elem a;
  1080. struct elem b;
  1081. currentRoutine = ctx->routines + ctx->routine_current;
  1082. if(currentRoutine->top < 2) {
  1083. currentRoutine->panic = -1;
  1084. return;
  1085. }
  1086. a = currentRoutine->stack[currentRoutine->top-1];
  1087. b = currentRoutine->stack[currentRoutine->top-2];
  1088. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1089. currentRoutine->panic = -1;
  1090. return;
  1091. }
  1092. ink_pop(ctx);
  1093. currentRoutine->stack[currentRoutine->top-1].value = b.value * a.value;
  1094. }
  1095. static void div_int(struct context* ctx) {
  1096. struct ink_routine* currentRoutine;
  1097. struct elem a;
  1098. struct elem b;
  1099. currentRoutine = ctx->routines + ctx->routine_current;
  1100. if(currentRoutine->top < 2) {
  1101. currentRoutine->panic = -1;
  1102. return;
  1103. }
  1104. a = currentRoutine->stack[currentRoutine->top-1];
  1105. b = currentRoutine->stack[currentRoutine->top-2];
  1106. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1107. currentRoutine->panic = -1;
  1108. return;
  1109. }
  1110. ink_pop(ctx);
  1111. currentRoutine->stack[currentRoutine->top-1].value = b.value / a.value;
  1112. }
  1113. static void is_equal(struct context* ctx) {
  1114. struct ink_routine* currentRoutine;
  1115. struct elem a;
  1116. struct elem b;
  1117. struct elem ret;
  1118. currentRoutine = ctx->routines + ctx->routine_current;
  1119. if(currentRoutine->top < 2) {
  1120. currentRoutine->panic = -1;
  1121. return;
  1122. }
  1123. a = currentRoutine->stack[currentRoutine->top-1];
  1124. b = currentRoutine->stack[currentRoutine->top-2];
  1125. ink_pop(ctx);
  1126. ink_pop(ctx);
  1127. ret.type = INK_INTEGER;
  1128. ret.value = a.value == b.value && a.type == b.type;
  1129. }
  1130. static void is_different(struct context* ctx) {
  1131. struct ink_routine* currentRoutine;
  1132. struct elem a;
  1133. struct elem b;
  1134. struct elem ret;
  1135. currentRoutine = ctx->routines + ctx->routine_current;
  1136. if(currentRoutine->top < 2) {
  1137. currentRoutine->panic = -1;
  1138. return;
  1139. }
  1140. a = currentRoutine->stack[currentRoutine->top-1];
  1141. b = currentRoutine->stack[currentRoutine->top-2];
  1142. ink_pop(ctx);
  1143. ink_pop(ctx);
  1144. ret.type = INK_INTEGER;
  1145. ret.value = !(a.value == b.value && a.type == b.type);
  1146. }
  1147. #ifndef NOEXTRAARITHMETIC
  1148. static void rem_int(struct context* ctx) {
  1149. struct ink_routine* currentRoutine;
  1150. struct elem a;
  1151. struct elem b;
  1152. currentRoutine = ctx->routines + ctx->routine_current;
  1153. if(currentRoutine->top < 2) {
  1154. currentRoutine->panic = -1;
  1155. return;
  1156. }
  1157. a = currentRoutine->stack[currentRoutine->top-1];
  1158. b = currentRoutine->stack[currentRoutine->top-2];
  1159. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1160. currentRoutine->panic = -1;
  1161. return;
  1162. }
  1163. ink_pop(ctx);
  1164. currentRoutine->stack[currentRoutine->top-1].value = b.value % a.value;
  1165. }
  1166. static void gt_int(struct context* ctx) {
  1167. struct ink_routine* currentRoutine;
  1168. struct elem a;
  1169. struct elem b;
  1170. currentRoutine = ctx->routines + ctx->routine_current;
  1171. if(currentRoutine->top < 2) {
  1172. currentRoutine->panic = -1;
  1173. return;
  1174. }
  1175. a = currentRoutine->stack[currentRoutine->top-1];
  1176. b = currentRoutine->stack[currentRoutine->top-2];
  1177. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1178. currentRoutine->panic = -1;
  1179. return;
  1180. }
  1181. ink_pop(ctx);
  1182. currentRoutine->stack[currentRoutine->top-1].value = b.value > a.value;
  1183. }
  1184. static void gte_int(struct context* ctx) {
  1185. struct ink_routine* currentRoutine;
  1186. struct elem a;
  1187. struct elem b;
  1188. currentRoutine = ctx->routines + ctx->routine_current;
  1189. if(currentRoutine->top < 2) {
  1190. currentRoutine->panic = -1;
  1191. return;
  1192. }
  1193. a = currentRoutine->stack[currentRoutine->top-1];
  1194. b = currentRoutine->stack[currentRoutine->top-2];
  1195. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1196. currentRoutine->panic = -1;
  1197. return;
  1198. }
  1199. ink_pop(ctx);
  1200. currentRoutine->stack[currentRoutine->top-1].value = b.value >= a.value;
  1201. }
  1202. static void lte_int(struct context* ctx) {
  1203. struct ink_routine* currentRoutine;
  1204. struct elem a;
  1205. struct elem b;
  1206. currentRoutine = ctx->routines + ctx->routine_current;
  1207. if(currentRoutine->top < 2) {
  1208. currentRoutine->panic = -1;
  1209. return;
  1210. }
  1211. a = currentRoutine->stack[currentRoutine->top-1];
  1212. b = currentRoutine->stack[currentRoutine->top-2];
  1213. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1214. currentRoutine->panic = -1;
  1215. return;
  1216. }
  1217. ink_pop(ctx);
  1218. currentRoutine->stack[currentRoutine->top-1].value = b.value <= a.value;
  1219. }
  1220. #endif // NOEXTRAARITHMETIC
  1221. static void lt_int(struct context* ctx) {
  1222. struct ink_routine* currentRoutine;
  1223. struct elem a;
  1224. struct elem b;
  1225. currentRoutine = ctx->routines + ctx->routine_current;
  1226. if(currentRoutine->top < 2) {
  1227. currentRoutine->panic = -1;
  1228. return;
  1229. }
  1230. a = currentRoutine->stack[currentRoutine->top-1];
  1231. b = currentRoutine->stack[currentRoutine->top-2];
  1232. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  1233. currentRoutine->panic = -1;
  1234. return;
  1235. }
  1236. ink_pop(ctx);
  1237. currentRoutine->stack[currentRoutine->top-1].value = b.value < a.value;
  1238. }
  1239. static void dupe_elem(struct context* ctx) {
  1240. struct ink_routine* currentRoutine;
  1241. struct elem a;
  1242. int err;
  1243. currentRoutine = ctx->routines + ctx->routine_current;
  1244. if(currentRoutine->top < 1) {
  1245. ctx->panic = 1;
  1246. return;
  1247. }
  1248. a = currentRoutine->stack[currentRoutine->top-1];
  1249. err = ink_push(ctx, a);
  1250. if(err < 0) ctx->panic = 1;
  1251. }
  1252. static void drop_elem(struct context* ctx) {
  1253. struct ink_routine* currentRoutine;
  1254. currentRoutine = ctx->routines + ctx->routine_current;
  1255. if(currentRoutine->top < 1) {
  1256. ctx->panic = 1;
  1257. return;
  1258. }
  1259. ink_pop(ctx);
  1260. }
  1261. static void pluck_elem(struct context* ctx) {
  1262. struct ink_routine* currentRoutine;
  1263. struct elem a;
  1264. int position, err;
  1265. currentRoutine = ctx->routines + ctx->routine_current;
  1266. if(currentRoutine->top < 1) {
  1267. currentRoutine->panic = -1;
  1268. return;
  1269. }
  1270. a = currentRoutine->stack[currentRoutine->top-1];
  1271. if(a.type != INK_INTEGER) {
  1272. ctx->panic = 1;
  1273. return;
  1274. }
  1275. position = currentRoutine->top - (a.value + 1);
  1276. if(position >= currentRoutine->top || position < 0) {
  1277. ctx->panic = 1;
  1278. return;
  1279. }
  1280. ink_pop(ctx);
  1281. err = ink_push(ctx, currentRoutine->stack[position]);
  1282. if(err < 0) ctx->panic = 1;
  1283. }
  1284. static void swap_elem(struct context* ctx) {
  1285. struct ink_routine* currentRoutine;
  1286. struct elem a;
  1287. struct elem b;
  1288. currentRoutine = ctx->routines + ctx->routine_current;
  1289. if(currentRoutine->top < 2) {
  1290. currentRoutine->panic = -1;
  1291. return;
  1292. }
  1293. a = currentRoutine->stack[currentRoutine->top-1];
  1294. b = currentRoutine->stack[currentRoutine->top-2];
  1295. currentRoutine->stack[currentRoutine->top-2] = a;
  1296. currentRoutine->stack[currentRoutine->top-1] = b;
  1297. }
  1298. static void return_if(struct context* ctx) {
  1299. struct ink_routine* currentRoutine;
  1300. struct elem a;
  1301. currentRoutine = ctx->routines + ctx->routine_current;
  1302. if(currentRoutine->top < 1) {
  1303. ctx->panic = -1;
  1304. return;
  1305. }
  1306. a = currentRoutine->stack[currentRoutine->top-1];
  1307. if(a.type != INK_INTEGER) {
  1308. ctx->panic = 1;
  1309. return;
  1310. }
  1311. if(a.value) {
  1312. ink_pop_fn(ctx);
  1313. ink_pop_fn(ctx);
  1314. }
  1315. ink_pop(ctx);
  1316. return;
  1317. }
  1318. static void jump_if(struct context* ctx) {
  1319. struct ink_routine* currentRoutine;
  1320. struct elem a;
  1321. currentRoutine = ctx->routines + ctx->routine_current;
  1322. if(currentRoutine->top < 1) {
  1323. ctx->panic = -1;
  1324. return;
  1325. }
  1326. a = currentRoutine->stack[currentRoutine->top-1];
  1327. if(a.type != INK_INTEGER) {
  1328. ctx->panic = -1;
  1329. return;
  1330. }
  1331. ink_pop(ctx);
  1332. if(a.value) {
  1333. ink_pop_fn(ctx);
  1334. a = currentRoutine->stack[currentRoutine->top-1];
  1335. currentRoutine->function_stack[currentRoutine->function_stack_top - 1].index += a.value - 3;
  1336. }
  1337. ink_pop(ctx);
  1338. return;
  1339. }
  1340. static void print_int(struct context* ctx) {
  1341. struct ink_routine* currentRoutine;
  1342. struct elem a;
  1343. char* n;
  1344. char* str;
  1345. currentRoutine = ctx->routines + ctx->routine_current;
  1346. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  1347. currentRoutine->panic = -1;
  1348. return;
  1349. }
  1350. a = currentRoutine->stack[currentRoutine->top-1];
  1351. ink_pop(ctx);
  1352. n = ink_itoa(ctx, a.value);
  1353. str = n;
  1354. while (*str) {
  1355. ctx->putchar(*str);
  1356. ++str;
  1357. }
  1358. ctx->free(n);
  1359. }
  1360. static void print_as_utf8(struct context* ctx) {
  1361. struct ink_routine* currentRoutine;
  1362. struct elem a;
  1363. currentRoutine = ctx->routines + ctx->routine_current;
  1364. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  1365. ctx->panic = -1;
  1366. return;
  1367. }
  1368. a = currentRoutine->stack[currentRoutine->top-1];
  1369. if(a.value <= 0x7F) {
  1370. ctx->putchar(a.value);
  1371. } else if(a.value <= 0x7FF) {
  1372. ctx->putchar(((a.value & 0xFC0) >> 6) | 192);
  1373. ctx->putchar((a.value & 0x3F) | 128);
  1374. } else if(a.value <= 0xFFFF) {
  1375. ctx->putchar(((a.value & 0x3F000) >> 12) | 224);
  1376. ctx->putchar(((a.value & 0xFC0) >> 6) | 128);
  1377. ctx->putchar((a.value & 0x3F) | 128);
  1378. } else if(a.value <= 0x10FFFF) {
  1379. ctx->putchar(((a.value & 0x3C0000) >> 18) | 240);
  1380. ctx->putchar(((a.value & 0x3F000) >> 12) | 128);
  1381. ctx->putchar(((a.value & 0xFC0) >> 6) | 128);
  1382. ctx->putchar((a.value & 0x3F) | 128);
  1383. } else {
  1384. ctx->panic = -1;
  1385. return;
  1386. }
  1387. ink_pop(ctx);
  1388. }
  1389. static int get_type_by_name(struct context* ctx, const char* name) {
  1390. int i;
  1391. for(i = 0; i < ctx->types_top; ++i) {
  1392. if(strcmp(ctx->types[i].name, name) == 0) {
  1393. return i + 16;
  1394. }
  1395. }
  1396. return -1;
  1397. }
  1398. static void run_gc(struct context* ctx) {
  1399. ink_gc(ctx);
  1400. }
  1401. static void clear_stack(struct context* ctx) {
  1402. struct ink_routine* currentRoutine;
  1403. currentRoutine = ctx->routines + ctx->routine_current;
  1404. while (currentRoutine->top >= 1) {
  1405. ink_pop(ctx);
  1406. }
  1407. return;
  1408. }
  1409. static void collect_noop(struct context* _1, void* _2) {}
  1410. static struct ink_collection_list gc_noop(struct context* _1, void* _2) {
  1411. struct ink_collection_list c;
  1412. c.elements = NULL;
  1413. c.count = 0;
  1414. return c;
  1415. }
  1416. #ifndef NOARRAYLIB
  1417. struct ink_array {
  1418. int top;
  1419. int capacity;
  1420. struct elem* elements;
  1421. };
  1422. static void collect_array(struct context* ctx, void* array) {
  1423. struct ink_array* ary;
  1424. ary = array;
  1425. if(ary->elements != NULL) ctx->free(ary->elements);
  1426. }
  1427. static struct ink_collection_list gc_array(struct context* ctx, void* array) {
  1428. struct ink_array* ary;
  1429. struct ink_collection_list c;
  1430. ary = array;
  1431. c.elements = ctx->inner_malloc(sizeof(struct elem)*ary->top);
  1432. c.count = ary->top;
  1433. memcpy(c.elements, ary->elements, sizeof(struct elem)*ary->top);
  1434. return c;
  1435. }
  1436. static void new_array(struct context* ctx) {
  1437. int tid;
  1438. struct elem e;
  1439. struct ink_array ary;
  1440. tid = get_type_by_name(ctx, "array");
  1441. ary.elements = NULL;
  1442. ary.top = 0;
  1443. ary.capacity = 0;
  1444. e = ink_make_native(ctx, tid, &ary);
  1445. ink_push(ctx, e);
  1446. }
  1447. static void push_array_stack_delim(struct context* ctx) {
  1448. int tid;
  1449. struct elem e;
  1450. tid = get_type_by_name(ctx, "array_marker");
  1451. e.type = tid;
  1452. e.value = 0;
  1453. ink_push(ctx, e);
  1454. }
  1455. static void array_push(struct context* ctx, struct ink_routine* currentRoutine, struct ink_array* ary, struct elem value) {
  1456. if(ary->elements == NULL) {
  1457. ary->elements = ctx->malloc(sizeof(struct elem) * 8);
  1458. ary->top = 0;
  1459. ary->capacity = 8;
  1460. } else if(ary->top == ary->capacity) {
  1461. int new_count;
  1462. void* renewed;
  1463. new_count = (ary->capacity + ary->capacity/2);
  1464. renewed = ctx->realloc(ary->elements, sizeof(struct elem) * new_count);
  1465. if(renewed == NULL) {
  1466. currentRoutine->panic = -1;
  1467. return;
  1468. } else {
  1469. ary->elements = renewed;
  1470. ary->capacity = new_count;
  1471. }
  1472. }
  1473. ary->elements[ary->top] = value;
  1474. ary->top++;
  1475. }
  1476. static void push_array(struct context* ctx) {
  1477. int tid;
  1478. struct elem a;
  1479. struct ink_routine* currentRoutine;
  1480. struct ink_array* ary;
  1481. tid = get_type_by_name(ctx, "array");
  1482. currentRoutine = ctx->routines + ctx->routine_current;
  1483. if(currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top-1].type != tid) {
  1484. currentRoutine->panic = -1;
  1485. return;
  1486. }
  1487. a = currentRoutine->stack[currentRoutine->top-1];
  1488. ary= ink_get_value(ctx, a);
  1489. if(ary == NULL) {
  1490. currentRoutine->panic = -1;
  1491. return;
  1492. }
  1493. ink_pop(ctx);
  1494. array_push(ctx, currentRoutine, ary, currentRoutine->stack[currentRoutine->top-1]);
  1495. ink_pop(ctx);
  1496. }
  1497. static void push_delimited_array(struct context* ctx) {
  1498. int tid, idx, counter, i;
  1499. struct elem a;
  1500. struct ink_routine* currentRoutine;
  1501. struct ink_array* ary;
  1502. tid = get_type_by_name(ctx, "array_marker");
  1503. currentRoutine = ctx->routines + ctx->routine_current;
  1504. if(currentRoutine->top < 1) {
  1505. currentRoutine->panic = -1;
  1506. return;
  1507. }
  1508. new_array(ctx);
  1509. a = currentRoutine->stack[currentRoutine->top-1];
  1510. ink_pop(ctx);
  1511. ary= ink_get_value(ctx, a);
  1512. for(idx = 1; idx <= currentRoutine->top; ++idx) {
  1513. struct elem maybe_delim;
  1514. if(currentRoutine->stack[currentRoutine->top-idx].type == tid) {
  1515. break;
  1516. }
  1517. }
  1518. /* Save for cleanup */
  1519. counter = idx;
  1520. /* Don't copy the delimiter */
  1521. idx -= 1;
  1522. ary->elements = malloc(sizeof(struct elem) * idx);
  1523. if(ary->elements == NULL) {
  1524. currentRoutine->panic = -541;
  1525. return;
  1526. }
  1527. ary->capacity = idx;
  1528. ary->top = 0;
  1529. /* Copy the data */
  1530. for(i = currentRoutine->top - idx; i < currentRoutine->top; ++i) {
  1531. ary->elements[ary->top] = currentRoutine->stack[i];
  1532. ++(ary->top);
  1533. }
  1534. /* Cleanup */
  1535. while(counter--) {
  1536. ink_pop(ctx);
  1537. }
  1538. /* Put value in place */
  1539. ink_push(ctx, a);
  1540. }
  1541. static void index_array(struct context* ctx) {
  1542. int tid;
  1543. struct ink_routine *currentRoutine;
  1544. struct elem a;
  1545. struct ink_array *ary;
  1546. struct elem idx;
  1547. tid = get_type_by_name(ctx, "array");
  1548. currentRoutine = ctx->routines + ctx->routine_current;
  1549. if (currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) {
  1550. currentRoutine->panic = -1;
  1551. return;
  1552. }
  1553. a = currentRoutine->stack[currentRoutine->top - 1];
  1554. ary = ink_get_value(ctx, a);
  1555. if (ary == NULL) {
  1556. currentRoutine->panic = -1;
  1557. return;
  1558. }
  1559. ink_pop(ctx);
  1560. idx = currentRoutine->stack[currentRoutine->top - 1];
  1561. ink_pop(ctx);
  1562. if(ary->top <= idx.value) {
  1563. currentRoutine->panic = -1;
  1564. return;
  1565. }
  1566. ink_push(ctx, ary->elements[idx.value]);
  1567. }
  1568. static void get_size_array(struct context* ctx) {
  1569. int tid;
  1570. struct ink_routine *currentRoutine;
  1571. struct elem a;
  1572. struct ink_array *ary;
  1573. struct elem sz;
  1574. tid = get_type_by_name(ctx, "array");
  1575. currentRoutine = ctx->routines + ctx->routine_current;
  1576. if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) {
  1577. currentRoutine->panic = -1;
  1578. return;
  1579. }
  1580. a = currentRoutine->stack[currentRoutine->top - 1];
  1581. ary = ink_get_value(ctx, a);
  1582. if (ary == NULL) {
  1583. currentRoutine->panic = -1;
  1584. return;
  1585. }
  1586. ink_pop(ctx);
  1587. sz.type = INK_INTEGER;
  1588. sz.value = ary->top;
  1589. ink_push(ctx, sz);
  1590. }
  1591. static void print_array_of_codepoints(struct context* ctx) {
  1592. int tid, i;
  1593. struct ink_routine *currentRoutine;
  1594. struct elem a;
  1595. struct ink_array *ary;
  1596. struct elem idx;
  1597. tid = get_type_by_name(ctx, "array");
  1598. currentRoutine = ctx->routines + ctx->routine_current;
  1599. if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) {
  1600. currentRoutine->panic = -1;
  1601. return;
  1602. }
  1603. a = currentRoutine->stack[currentRoutine->top - 1];
  1604. ary = ink_get_value(ctx, a);
  1605. for(i = 0; i < ary->top; ++i) {
  1606. if(ary->elements[i].type != INK_INTEGER) {
  1607. currentRoutine->panic = -1;
  1608. return;
  1609. }
  1610. }
  1611. ink_pop(ctx);
  1612. for(i = 0; i < ary->top; ++i) {
  1613. ink_push(ctx, ary->elements[i]);
  1614. print_as_utf8(ctx);
  1615. }
  1616. }
  1617. static void arrayify_stack(struct context* ctx) {
  1618. struct ink_routine* currentRoutine;
  1619. struct elem array_ref;
  1620. struct ink_array* ary;
  1621. int idx;
  1622. currentRoutine = ctx->routines + ctx->routine_current;
  1623. new_array(ctx);
  1624. if(currentRoutine->panic < 0) return;
  1625. array_ref = currentRoutine->stack[currentRoutine->top - 1];
  1626. ary = ink_get_value(ctx, array_ref);
  1627. if(ary == NULL) {
  1628. currentRoutine->panic = -717;
  1629. return;
  1630. }
  1631. ink_pop(ctx);
  1632. for(idx = 0; idx < currentRoutine->top; ++idx) {
  1633. array_push(ctx, currentRoutine, ary, currentRoutine->stack[idx]);
  1634. }
  1635. while (currentRoutine->top > 0) {
  1636. ink_pop(ctx);
  1637. }
  1638. ink_push(ctx, array_ref);
  1639. return;
  1640. }
  1641. #endif // NOARRAYLIB
  1642. int ink_std_library(struct context* ctx) {
  1643. int v;
  1644. v = 0;
  1645. v += ink_add_native(ctx, "sys.trace", print_stacktrace);
  1646. v += ink_add_native(ctx, "sys.gc", run_gc);
  1647. v += ink_add_native(ctx, "print_int", print_int);
  1648. v += ink_add_native(ctx, "print_utf8", print_as_utf8);
  1649. v += ink_add_native(ctx, "+", add_int);
  1650. v += ink_add_native(ctx, "-", sub_int);
  1651. v += ink_add_native(ctx, "*", mult_int);
  1652. v += ink_add_native(ctx, "/", div_int);
  1653. v += ink_add_native(ctx, "==", is_equal);
  1654. v += ink_add_native(ctx, "!=", is_different);
  1655. v += ink_add_native(ctx, "<", lt_int);
  1656. v += ink_add_native(ctx, "swap", swap_elem);
  1657. v += ink_add_native(ctx, "dup", dupe_elem);
  1658. v += ink_add_native(ctx, "drop", drop_elem);
  1659. v += ink_add_native(ctx, "stack.clear", clear_stack);
  1660. v += ink_add_native(ctx, "pluck", pluck_elem);
  1661. v += ink_add_native(ctx, "return_if", return_if);
  1662. v += ink_add_native(ctx, "jump_if", jump_if);
  1663. #ifndef NOEXTRAARITHMETIC
  1664. v += ink_add_native(ctx, ">", gt_int);
  1665. v += ink_add_native(ctx, ">=", gte_int);
  1666. v += ink_add_native(ctx, "=<", lte_int);
  1667. v += ink_add_native(ctx, "%", rem_int);
  1668. #endif // NOEXTRAARITHMETIC
  1669. #ifndef NOARRAYLIB
  1670. ink_new_type(ctx, "array", sizeof(struct ink_array), collect_array, gc_array);
  1671. ink_new_type(ctx, "array_marker", 0, collect_noop, gc_noop);
  1672. v += ink_add_native(ctx, "[", push_array_stack_delim);
  1673. v += ink_add_native(ctx, "]", push_delimited_array);
  1674. v += ink_add_native(ctx, "array.new", new_array);
  1675. v += ink_add_native(ctx, "array.push", push_array);
  1676. v += ink_add_native(ctx, "array.index", index_array);
  1677. v += ink_add_native(ctx, "array.size", get_size_array);
  1678. v += ink_add_native(ctx, "array.print_utf8", print_array_of_codepoints);
  1679. v += ink_add_native(ctx, "stack.to_array", arrayify_stack);
  1680. #endif // NOARRAYLIB
  1681. return v;
  1682. }