A minimalistic programming language written in C89.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1941 行
50 KiB

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