A minimalistic programming language written in C89.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1085 řádky
28 KiB

před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
před 6 měsíci
  1. #include "ink.h"
  2. #ifndef NOSTDLIB
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #ifndef NOINSTR
  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. struct label {
  22. int active;
  23. int dest;
  24. char* name;
  25. };
  26. #ifdef NOSTDLIB
  27. static size_t strlen(const char* c) {
  28. size_t j = 0;
  29. while(*(c++)) {
  30. j++;
  31. }
  32. return j;
  33. }
  34. static void* memcpy(void* _dest, const void* _src, size_t sz) {
  35. char* dest = _dest;
  36. const char* src = _src;
  37. while(sz--) {
  38. *(dest++) = *(src++);
  39. }
  40. return dest;
  41. }
  42. static int strcmp(const char* dest, const char* src) {
  43. while(*dest != 0 && *src != 0) {
  44. if(*(dest++) != *(src++)) {
  45. return 1;
  46. }
  47. }
  48. return 0;
  49. }
  50. static void* memmove(void* _dest, const void* _src, size_t sz) {
  51. char* dest = _dest;
  52. const char* src = _src;
  53. if (src < dest) {
  54. src += sz;
  55. dest += sz;
  56. while (sz-- > 0) {
  57. *--dest = *--src;
  58. }
  59. } else {
  60. while (sz-- > 0) {
  61. *dest++ = *src++;
  62. }
  63. }
  64. return dest;
  65. }
  66. static void* memset(void* _dest, int src, size_t sz) {
  67. char* dest = _dest;
  68. while(sz--) {
  69. *(dest++) = src++;
  70. }
  71. return dest;
  72. }
  73. static int isspace(int d) {
  74. return d == ' ' || d == '\t' || d == '\n';
  75. }
  76. static int isdigit(int d) {
  77. return '0' <= d && d <= '9';
  78. }
  79. static int atoi(const char* c) {
  80. int ret = 0;
  81. while(*c) {
  82. ret *= 10;
  83. ret += *c - '0';
  84. ++c;
  85. }
  86. return ret;
  87. }
  88. #endif
  89. int ink_add_native(struct context* ctx, const char* name, void(*value)(struct context*)) {
  90. if(ctx->native_words == NULL) {
  91. ctx->native_words = ctx->malloc(sizeof(struct native_fn) * 8);
  92. ctx->native_words_top = 0;
  93. ctx->native_words_capacity = 8;
  94. } else if(ctx->native_words_top == ctx->native_words_capacity) {
  95. int new_count = (ctx->native_words_capacity + ctx->native_words_capacity/2);
  96. void* renewed = ctx->realloc(ctx->native_words, sizeof(struct native_fn) * new_count);
  97. if(renewed == NULL) {
  98. return -3;
  99. } else {
  100. ctx->native_words = renewed;
  101. ctx->native_words_capacity = new_count;
  102. }
  103. }
  104. int len = strlen(name);
  105. char* copy = ctx->malloc(len+1);
  106. if(copy == NULL) {
  107. return -4;
  108. }
  109. memcpy(copy, name, len);
  110. copy[len] = 0;
  111. ctx->native_words[ctx->native_words_top].value = value;
  112. ctx->native_words[ctx->native_words_top].name = copy;
  113. ctx->native_words_top++;
  114. return 0;
  115. }
  116. static int ink_add_indigenous(struct context* ctx, const char* name, struct elem* m, size_t count) {
  117. if(ctx->words == NULL) {
  118. ctx->words = ctx->malloc(sizeof(struct fn) * 8);
  119. ctx->words_top = 0;
  120. ctx->words_capacity = 8;
  121. } else if(ctx->words_top == ctx->words_capacity) {
  122. int new_count = (ctx->words_capacity + ctx->words_capacity/2);
  123. void* renewed = ctx->realloc(ctx->words, sizeof(struct native_fn) * new_count);
  124. if(renewed == NULL) {
  125. return -1;
  126. } else {
  127. ctx->words = renewed;
  128. ctx->words_capacity = new_count;
  129. }
  130. }
  131. int i;
  132. for(i = 0; i < ctx->words_top; ++i) {
  133. if(strcmp(name, ctx->words[i].name) == 0) {
  134. ctx->free(ctx->words[i].things);
  135. ctx->words[i].things = ctx->malloc(sizeof(struct elem) * count);
  136. memcpy(ctx->words[i].things, m, sizeof(struct elem) * count);
  137. ctx->words[i].size = count;
  138. return i;
  139. }
  140. }
  141. int len = strlen(name);
  142. char* copy = ctx->malloc(len+1);
  143. if(copy == NULL) {
  144. return -2;
  145. }
  146. memcpy(copy, name, len);
  147. copy[len] = 0;
  148. ctx->words[ctx->words_top].things = ctx->malloc(sizeof(struct elem) * count);
  149. memcpy(ctx->words[ctx->words_top].things, m, sizeof(struct elem) * count);
  150. ctx->words[ctx->words_top].size = count;
  151. ctx->words[ctx->words_top].name = copy;
  152. return ctx->words_top++;
  153. }
  154. static int ink_add_lex_string(struct context* ctx, const char* name) {
  155. int i;
  156. if(ctx->lex_reserved_words == NULL) {
  157. ctx->lex_reserved_words = ctx->malloc(sizeof(char*) * 8);
  158. ctx->lex_reserved_words_top = 0;
  159. ctx->lex_reserved_words_capacity = 8;
  160. } else if(ctx->lex_reserved_words_top == ctx->lex_reserved_words_capacity) {
  161. int new_count = (ctx->lex_reserved_words_capacity + ctx->lex_reserved_words_capacity/2);
  162. void* renewed = ctx->realloc(ctx->lex_reserved_words, sizeof(struct native_fn) * new_count);
  163. if(renewed == NULL) {
  164. return -5;
  165. } else {
  166. ctx->lex_reserved_words = renewed;
  167. ctx->lex_reserved_words_capacity = new_count;
  168. }
  169. }
  170. for(i = 0; i < ctx->lex_reserved_words_top; i++) {
  171. if(strcmp(ctx->lex_reserved_words[i], name) == 0) {
  172. return i;
  173. }
  174. }
  175. int len = strlen(name);
  176. i = ctx->lex_reserved_words_top;
  177. ctx->lex_reserved_words[i] = ctx->malloc(len+1);
  178. memcpy(ctx->lex_reserved_words[i], name, len);
  179. ctx->lex_reserved_words[i][len] = 0;
  180. ctx->lex_reserved_words_top++;
  181. return i;
  182. }
  183. int ink_push(struct context* ctx, struct elem value) {
  184. if(ctx->routine_current >= ctx->routines_top) return -65;
  185. struct ink_routine* current = ctx->routines + ctx->routine_current;
  186. if(current->stack == NULL) {
  187. current->stack = ctx->malloc(sizeof(struct elem) * 8);
  188. current->top = 0;
  189. current->capacity = 8;
  190. } else if(current->top == current->capacity) {
  191. int new_count = (current->capacity + current->capacity/2);
  192. void* renewed = ctx->realloc(current->stack, sizeof(struct elem) * new_count);
  193. if(renewed == NULL) {
  194. return -18;
  195. } else {
  196. current->stack = renewed;
  197. current->capacity = new_count;
  198. }
  199. }
  200. current->stack[current->top] = value;
  201. current->top++;
  202. return 0;
  203. }
  204. int ink_push_fn(struct context* ctx, struct stack_frame value) {
  205. if(ctx->routine_current >= ctx->routines_top) return -55;
  206. struct ink_routine* current = ctx->routines + ctx->routine_current;
  207. if(current->panic) return -56;
  208. if(current->function_stack == NULL) {
  209. current->function_stack = ctx->malloc(sizeof(struct stack_frame) * 8);
  210. current->function_stack_top = 0;
  211. current->function_stack_capacity = 8;
  212. } else if(current->function_stack_top == current->function_stack_capacity) {
  213. int new_count = (current->function_stack_capacity + current->function_stack_capacity/2);
  214. void* renewed = ctx->realloc(current->function_stack, sizeof(struct stack_frame) * new_count);
  215. if(renewed == NULL) {
  216. return -9;
  217. } else {
  218. current->function_stack = renewed;
  219. current->function_stack_capacity = new_count;
  220. }
  221. }
  222. current->function_stack[current->function_stack_top] = value;
  223. current->function_stack_top++;
  224. return 0;
  225. }
  226. void ink_pop_fn(struct context* ctx) {
  227. if(ctx->routine_current >= ctx->routines_top) return;
  228. if(ctx->routines[ctx->routine_current].panic) return;
  229. if(ctx->routines[ctx->routine_current].function_stack == NULL) return;
  230. if(ctx->routines[ctx->routine_current].function_stack_top == 0) return;
  231. ctx->routines[ctx->routine_current].function_stack_top--;
  232. }
  233. void ink_pop(struct context* ctx) {
  234. if(ctx->routine_current >= ctx->routines_top) return;
  235. if(ctx->routines[ctx->routine_current].panic) return;
  236. if(ctx->routines[ctx->routine_current].stack == NULL) return;
  237. if(ctx->routines[ctx->routine_current].top == 0) return;
  238. ctx->routines[ctx->routine_current].top--;
  239. }
  240. struct context* ink_make_context(void*(*malloc)(size_t), void*(*realloc)(void*, size_t), void(*free)(void*), int(*putchar)(int)) {
  241. struct context* ctx = (struct context*)malloc(sizeof(struct context));
  242. ctx->malloc = malloc;
  243. ctx->realloc = realloc;
  244. ctx->free = free;
  245. ctx->putchar = putchar;
  246. ctx->panic = 0;
  247. ctx->routines = NULL;
  248. ctx->routines_capacity = 0;
  249. ctx->routines_top = 0;
  250. ctx->native_words = NULL;
  251. ctx->native_words_capacity = 0;
  252. ctx->native_words_top = 0;
  253. ctx->words = NULL;
  254. ctx->words_capacity = 0;
  255. ctx->words_top = 0;
  256. ctx->lex_reserved_words = NULL;
  257. ctx->lex_reserved_words_capacity = 0;
  258. ctx->lex_reserved_words_top = 0;
  259. ctx->steps = 0;
  260. return ctx;
  261. }
  262. /**
  263. * Allocates a string that contains the integer
  264. * @param _ context (used to allocate)
  265. * @param cpy the value
  266. * @return the allocated string, needs to be freed by ctx->free
  267. * @internal this function is slightly cursed
  268. */
  269. static char* ink_itoa(struct context* _, int cpy) {
  270. char* n = _->malloc(16);
  271. n[15] = 0;
  272. char* it = n+15;
  273. do {
  274. it--;
  275. *it = (cpy % 10) + '0';
  276. cpy = cpy / 10;
  277. } while(cpy);
  278. memmove(n, it, 16 - (it-n));
  279. return n;
  280. }
  281. #ifndef NOSTDLIB
  282. struct context* ink_make_default_context() {
  283. struct context* ctx = ink_make_context(malloc, realloc, free, putchar);
  284. ink_std_library(ctx);
  285. return ctx;
  286. }
  287. #endif
  288. static int ink_consume_one(int* end, struct context* pContext, char** buffer, char* r) {
  289. int i;
  290. if(*end == 0) {
  291. return 0;
  292. }
  293. r[*end] = 0;
  294. int done = 0;
  295. struct elem value;
  296. if (strcmp(r, _KEYWORD_INK_FUNCTION) == 0) {
  297. value.value = 0;
  298. value.type = INK_FUNCTION_KW;
  299. done = 1;
  300. }
  301. if (!done && strcmp(r, _KEYWORD_INK_DO) == 0) {
  302. value.value = 0;
  303. value.type = INK_DO_KW;
  304. done = 1;
  305. }
  306. if (!done && strcmp(r, _KEYWORD_INK_END) == 0) {
  307. value.value = 0;
  308. value.type = INK_END_KW;
  309. done = 1;
  310. }
  311. if (!done && strcmp(r, _KEYWORD_INK_RETURN) == 0) {
  312. value.value = 0;
  313. value.type = INK_RETURN;
  314. done = 1;
  315. }
  316. if(done) {
  317. int err;
  318. err = ink_push(pContext, value);
  319. if(err < 0) {
  320. return -19;
  321. }
  322. }
  323. if (!done) {
  324. for (i = 0; i < pContext->words_top; ++i) {
  325. if (strcmp(r, pContext->words[i].name) == 0) {
  326. value.value = i;
  327. value.type = INK_FUNCTION;
  328. int err;
  329. err = ink_push(pContext, value);
  330. if(err < 0) {
  331. return -20;
  332. }
  333. done = 1;
  334. break;
  335. }
  336. }
  337. }
  338. if (!done) {
  339. for (i = 0; i < pContext->native_words_top; ++i) {
  340. if (strcmp(r, pContext->native_words[i].name) == 0) {
  341. value.value = i;
  342. value.type = INK_NATIVE_FUNCTION;
  343. int err;
  344. err = ink_push(pContext, value);
  345. if(err < 0) {
  346. return -21;
  347. }
  348. done = 1;
  349. break;
  350. }
  351. }
  352. }
  353. if (!done) {
  354. for(i = (r[0] == '-'); i < *end; i++) {
  355. if(!isdigit(r[i])){
  356. goto not_an_int;
  357. }
  358. }
  359. value.value = atoi(r);
  360. value.type = INK_INTEGER;
  361. int err;
  362. err = ink_push(pContext, value);
  363. if(err < 0) {
  364. return -22;
  365. }
  366. done = 1;
  367. }
  368. not_an_int:
  369. if (!done) {
  370. i = ink_add_lex_string(pContext, r);
  371. if(i < 0) {
  372. pContext->panic = 1;
  373. return -7;
  374. }
  375. value.value = i;
  376. if(r[strlen(r) - 1] == ':') {
  377. value.type = INK_LABEL;
  378. } else {
  379. value.type = INK_RESERVED;
  380. }
  381. int err;
  382. err = ink_push(pContext, value);
  383. if(err < 0) {
  384. return -23;
  385. }
  386. }
  387. *end = 0;
  388. return 0;
  389. }
  390. static int ink_lex(struct context *pContext, char* buffer) {
  391. int i;
  392. char r[128];
  393. int end = 0;
  394. int err;
  395. while(*buffer != 0) {
  396. if(isspace(*buffer)) {
  397. err = ink_consume_one(&end, pContext, &buffer, r);
  398. if(err < 0) {
  399. pContext->panic = 1;
  400. return -8;
  401. }
  402. } else {
  403. r[end] = *buffer;
  404. ++end;
  405. }
  406. ++buffer;
  407. }
  408. err = ink_consume_one(&end, pContext, &buffer, r);
  409. if(err < 0) {
  410. pContext->panic = 1;
  411. return -9;
  412. }
  413. return 0;
  414. }
  415. static int lblcmp(const char* label, const char* other, size_t label_sz) {
  416. while (label_sz != 1) {
  417. if(*other == 0) return 1;
  418. if(*label != *other) return 1;
  419. ++label;
  420. ++other;
  421. label_sz--;
  422. }
  423. return 0;
  424. }
  425. int ink_make_routine(struct context* ctx) {
  426. if(ctx->routines == NULL) {
  427. ctx->routines = ctx->malloc(sizeof(struct ink_routine) * 8);
  428. ctx->routines_top = 0;
  429. ctx->routines_capacity = 8;
  430. struct ink_routine* it = ctx->routines;
  431. struct ink_routine* end = ctx->routines + 8;
  432. for(;it != end;++it) {
  433. it->stack = NULL;
  434. it->function_stack = NULL;
  435. it->panic = INK_ROUTINE_CAN_REUSE;
  436. }
  437. } else if(ctx->routines_top == ctx->routines_capacity) {
  438. int new_count = (ctx->routines_capacity + ctx->routines_capacity/2);
  439. void* renewed = ctx->realloc(ctx->routines, sizeof(struct stack_frame) * new_count);
  440. if(renewed == NULL) {
  441. return -99;
  442. } else {
  443. ctx->routines = renewed;
  444. struct ink_routine* it = ctx->routines + ctx->routines_capacity;
  445. struct ink_routine* end = ctx->routines + new_count;
  446. for(;it != end;++it) {
  447. it->panic = INK_ROUTINE_CAN_REUSE;
  448. }
  449. ctx->routines_capacity = new_count;
  450. }
  451. }
  452. struct ink_routine* it = ctx->routines;
  453. struct ink_routine* end = ctx->routines + ctx->routines_capacity;
  454. for(;it != end;++it) {
  455. if(it->panic == INK_ROUTINE_CAN_REUSE) {
  456. it->panic = 0;
  457. it->stack = NULL;
  458. it->top = 0;
  459. it->capacity = 0;
  460. it->function_stack = NULL;
  461. it->function_stack_top = 0;
  462. it->function_stack_capacity = 0;
  463. int idx = it - ctx->routines;
  464. if(idx >= ctx->routines_top) {
  465. ctx->routines_top = idx + 1;
  466. }
  467. return idx;
  468. }
  469. }
  470. }
  471. int ink_kill_routine(struct context* ctx, int routine){
  472. if(routine < 0 || routine >= ctx->routines_top) {
  473. return 0;
  474. }
  475. struct ink_routine* curr = ctx->routines + routine;
  476. if(curr->panic == INK_ROUTINE_CAN_REUSE) {
  477. return 0;
  478. }
  479. if(curr->stack != NULL) {
  480. ctx->free(curr->stack);
  481. curr->stack = NULL;
  482. }
  483. if(curr->function_stack != NULL) {
  484. ctx->free(curr->function_stack);
  485. curr->function_stack = NULL;
  486. }
  487. curr->panic = INK_ROUTINE_CAN_REUSE;
  488. }
  489. /**
  490. *
  491. * @param pContext
  492. * @param executable_buffer
  493. * @param executable_buffer_top
  494. * @internal Loop from hell
  495. */
  496. static int ink_parse(struct context* pContext, struct elem* executable_buffer, int* executable_buffer_top) {
  497. struct ink_routine* currentRoutine = pContext->routines + pContext->routine_current;
  498. int i;
  499. #define LABEL_BUFFER 128
  500. #define FUNCTION_BUFFER 256
  501. struct label labels[LABEL_BUFFER];
  502. struct elem function_buffer[FUNCTION_BUFFER];
  503. int function_buffer_top = 0;
  504. int function_name = -1;
  505. #define MODE_EXECUTABLE 0
  506. #define MODE_FUNCTION 1
  507. #define MODE_DO 2
  508. int mode = 0;
  509. memset(labels, 0, sizeof(struct label)*LABEL_BUFFER);
  510. for(i = 0; i < currentRoutine->top; ++i) {
  511. struct elem current;
  512. current = currentRoutine->stack[i];
  513. switch (mode) {
  514. case MODE_EXECUTABLE:
  515. switch(current.type) {
  516. case INK_FUNCTION_KW:
  517. mode = MODE_FUNCTION;
  518. function_name = -1;
  519. goto next_token;
  520. case INK_DO_KW:
  521. case INK_END_KW:
  522. return -26;
  523. default:
  524. executable_buffer[*executable_buffer_top] = current;
  525. *executable_buffer_top += 1;
  526. }
  527. break;
  528. case MODE_FUNCTION:
  529. if(current.type == INK_DO_KW) {
  530. if(function_name == -1) {
  531. return -27;
  532. } else {
  533. mode = MODE_DO;
  534. memset(labels, 0, sizeof(struct label)*128);
  535. goto next_token;
  536. }
  537. }
  538. if(function_name != -1) {
  539. return -28;
  540. }
  541. if(current.type != INK_RESERVED) {
  542. return -29;
  543. }
  544. function_name = current.value;
  545. break;
  546. case MODE_DO:
  547. if(current.type == INK_END_KW) {
  548. int j;
  549. for(j = 0; j < function_buffer_top; j++) {
  550. struct elem pt;
  551. pt = function_buffer[j];
  552. if(pt.type == INK_LABEL) {
  553. int k;
  554. for(k = 0; k < LABEL_BUFFER; k++) {
  555. if(labels[k].active) {
  556. if(strcmp(labels[k].name, pContext->lex_reserved_words[pt.value]) == 0) {
  557. labels[k].dest = j;
  558. return -30;
  559. break;
  560. }
  561. } else {
  562. labels[k].active = 1;
  563. labels[k].name = pContext->lex_reserved_words[pt.value];
  564. labels[k].dest = j;
  565. memcpy(function_buffer+j, function_buffer+j+1, sizeof(struct elem)*(function_buffer_top-j-1));
  566. function_buffer_top--;
  567. j--;
  568. break;
  569. }
  570. }
  571. }
  572. }
  573. for(j = 0; j < function_buffer_top; j++) {
  574. struct elem pt;
  575. pt = function_buffer[j];
  576. if(pt.type == INK_RESERVED) {
  577. const char* str = pContext->lex_reserved_words[pt.value];
  578. int k;
  579. for(k = 0; k < LABEL_BUFFER; k++) {
  580. if(labels[k].active) {
  581. const char* lbl = labels[k].name;
  582. int label_sz = strlen(lbl);
  583. if(lblcmp(labels[k].name, pContext->lex_reserved_words[pt.value], label_sz) == 0) {
  584. function_buffer[j].type = INK_INTEGER;
  585. function_buffer[j].value = labels[k].dest - j;
  586. break;
  587. }
  588. } else break;
  589. }
  590. }
  591. }
  592. int err;
  593. err = ink_add_indigenous(pContext, pContext->lex_reserved_words[function_name], function_buffer, function_buffer_top);
  594. if(err < 0) {
  595. pContext->panic = 1;
  596. return -33;
  597. }
  598. function_buffer_top = 0;
  599. mode = MODE_EXECUTABLE;
  600. goto next_token;
  601. }
  602. function_buffer[function_buffer_top] = current;
  603. function_buffer_top += 1;
  604. break;
  605. }
  606. next_token: i=i;
  607. }
  608. if(mode == MODE_FUNCTION || mode == MODE_DO) {
  609. return -32;
  610. }
  611. return 0;
  612. #undef MODE_EXECUTABLE
  613. #undef MODE_FUNCTION
  614. #undef MODE_DO
  615. #undef LABEL_BUFFER
  616. #undef FUNCTION_BUFFER
  617. }
  618. int ink_step(struct context *pContext) {
  619. struct ink_routine* currentRoutine = pContext->routines + pContext->routine_current;
  620. pContext->steps++;
  621. if(currentRoutine->function_stack_top == 0) return 0;
  622. if(pContext->panic) {
  623. return -1;
  624. }
  625. struct stack_frame frame;
  626. struct stack_frame* top;
  627. struct elem next;
  628. int t;
  629. top = &currentRoutine->function_stack[currentRoutine->function_stack_top-1];
  630. t = top->executing.type;
  631. switch(t) {
  632. case INK_NATIVE_FUNCTION:
  633. if(top->index != 0) {
  634. ink_pop_fn(pContext);
  635. } else {
  636. top->index++;
  637. if(pContext->native_words_top <= top->executing.value) {
  638. pContext->panic = 1;
  639. return -1;
  640. }
  641. pContext->native_words[top->executing.value].value(pContext);
  642. }
  643. break;
  644. case INK_FUNCTION:
  645. if(pContext->words_top <= top->executing.value) {
  646. pContext->panic = 1;
  647. return -1;
  648. }
  649. if(top->index >= pContext->words[top->executing.value].size) {
  650. ink_pop_fn(pContext);
  651. } else {
  652. next = pContext->words[top->executing.value].things[top->index];
  653. if(next.type == INK_RETURN) {
  654. ink_pop_fn(pContext);
  655. return 1;
  656. }
  657. frame.executing = next;
  658. frame.index = 0;
  659. t = ink_push_fn(pContext, frame);
  660. if(t < 0) {
  661. pContext->panic = 1;
  662. return -11;
  663. }
  664. top->index++;
  665. }
  666. break;
  667. default:
  668. t = ink_push(pContext, top->executing);
  669. if(t < 0) {
  670. pContext->panic = 1;
  671. return -25;
  672. }
  673. ink_pop_fn(pContext);
  674. break;
  675. }
  676. return 1;
  677. }
  678. void ink_compile(struct context *pContext, char* buffer) {
  679. int routine = ink_make_routine(pContext);
  680. int saved = pContext->routine_current;
  681. pContext->routine_current = routine;
  682. struct ink_routine* currentRoutine = pContext->routines + routine;
  683. currentRoutine->stack = NULL;
  684. currentRoutine->top = 0;
  685. currentRoutine->capacity = 0;
  686. int err;
  687. err = ink_lex(pContext, buffer);
  688. if(err < 0) {
  689. pContext->panic = 1;
  690. return;
  691. }
  692. int i = 0;
  693. struct elem executable_buffer[256];
  694. int executable_buffer_top = 0;
  695. err = ink_parse(pContext, executable_buffer, &executable_buffer_top);
  696. if(err < 0) {
  697. pContext->panic = 1;
  698. return;
  699. }
  700. struct stack_frame frame;
  701. char main_fn[32] = "__-MAIN-__";
  702. char* integer = ink_itoa(pContext, routine);
  703. size_t integer_size = strlen(integer);
  704. memcpy(main_fn+10, integer, integer_size);
  705. pContext->free(integer);
  706. main_fn[10+integer_size] = 0;
  707. frame.executing.value = ink_add_indigenous(pContext, main_fn, executable_buffer, executable_buffer_top);
  708. if(frame.executing.value < 0) {
  709. pContext->panic = 1;
  710. return;
  711. }
  712. frame.executing.type = INK_FUNCTION;
  713. frame.index = 0;
  714. err = ink_push_fn(pContext, frame);
  715. if(err < 0) {
  716. pContext->panic = 1;
  717. return;
  718. }
  719. pContext->routine_current = saved;
  720. return;
  721. }
  722. int ink_can_run(struct context* pContext) {
  723. int it = 0;
  724. for(;it < pContext->routines_top; ++it) {
  725. if(pContext->routines[it].panic == 0) {
  726. return 1;
  727. }
  728. }
  729. return 0;
  730. }
  731. int ink_step_everyone(struct context* pContext) {
  732. int out;
  733. pContext->routine_current = -1;
  734. for(;;) {
  735. do{
  736. ++(pContext->routine_current);
  737. } while(pContext->routine_current < pContext->routines_top && pContext->routines[pContext->routine_current].panic != 0);
  738. if(pContext->routine_current >= pContext->routines_top) break;
  739. if(pContext->routines[pContext->routine_current].panic == INK_ROUTINE_SUCCESS) {
  740. ink_kill_routine(pContext, pContext->routine_current);
  741. }
  742. out = ink_step(pContext);
  743. if(out == 0) {
  744. pContext->routines[pContext->routine_current].panic = INK_ROUTINE_SUCCESS;
  745. } else if(out < 0) {
  746. pContext->routines[pContext->routine_current].panic = out;
  747. }
  748. }
  749. return 0;
  750. }
  751. /**********************************************************************************************************************/
  752. static void print_stacktrace(struct context* _) {
  753. int i = 0;
  754. struct ink_routine* currentRoutine = _->routines + _->routine_current;
  755. for(; i < currentRoutine->function_stack_top; ++i) {
  756. struct elem thing;
  757. thing = currentRoutine->function_stack[i].executing;
  758. switch(thing.type) {
  759. case INK_NATIVE_FUNCTION: {
  760. char *n = _->native_words[thing.value].name;
  761. while (*n) {
  762. _->putchar(*n);
  763. ++n;
  764. }
  765. _->putchar(10);
  766. break;
  767. }
  768. case INK_FUNCTION:{
  769. char *n = _->native_words[thing.value].name;
  770. while (*n) {
  771. _->putchar(*n);
  772. ++n;
  773. }
  774. _->putchar(':');
  775. n = ink_itoa(_, currentRoutine->function_stack[i].index);
  776. while (*n) {
  777. _->putchar(*n);
  778. ++n;
  779. }
  780. _->free(n);
  781. _->putchar(10);
  782. break;
  783. }
  784. default:
  785. break;
  786. }
  787. }
  788. }
  789. static void add_int(struct context* ctx) {
  790. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  791. if(currentRoutine->top < 2) {
  792. currentRoutine->panic = 1;
  793. return;
  794. }
  795. struct elem a;
  796. struct elem b;
  797. a = currentRoutine->stack[currentRoutine->top-1];
  798. b = currentRoutine->stack[currentRoutine->top-2];
  799. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  800. ctx->panic = 1;
  801. return;
  802. }
  803. ink_pop(ctx);
  804. currentRoutine->stack[currentRoutine->top-1].value = a.value + b.value;
  805. }
  806. static void sub_int(struct context* ctx) {
  807. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  808. if(currentRoutine->top < 2) {
  809. currentRoutine->panic = 1;
  810. return;
  811. }
  812. struct elem a;
  813. struct elem b;
  814. a = currentRoutine->stack[currentRoutine->top-1];
  815. b = currentRoutine->stack[currentRoutine->top-2];
  816. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  817. currentRoutine->panic = 1;
  818. return;
  819. }
  820. ink_pop(ctx);
  821. currentRoutine->stack[currentRoutine->top-1].value = b.value - a.value;
  822. }
  823. static void mult_int(struct context* ctx) {
  824. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  825. if(currentRoutine->top < 2) {
  826. currentRoutine->panic = 1;
  827. return;
  828. }
  829. struct elem a;
  830. struct elem b;
  831. a = currentRoutine->stack[currentRoutine->top-1];
  832. b = currentRoutine->stack[currentRoutine->top-2];
  833. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  834. currentRoutine->panic = 1;
  835. return;
  836. }
  837. ink_pop(ctx);
  838. currentRoutine->stack[currentRoutine->top-1].value = b.value * a.value;
  839. }
  840. static void div_int(struct context* ctx) {
  841. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  842. if(currentRoutine->top < 2) {
  843. currentRoutine->panic = 1;
  844. return;
  845. }
  846. struct elem a;
  847. struct elem b;
  848. a = currentRoutine->stack[currentRoutine->top-1];
  849. b = currentRoutine->stack[currentRoutine->top-2];
  850. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  851. currentRoutine->panic = 1;
  852. return;
  853. }
  854. ink_pop(ctx);
  855. currentRoutine->stack[currentRoutine->top-1].value = b.value / a.value;
  856. }
  857. static void rem_int(struct context* ctx) {
  858. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  859. if(currentRoutine->top < 2) {
  860. currentRoutine->panic = 1;
  861. return;
  862. }
  863. struct elem a;
  864. struct elem b;
  865. a = currentRoutine->stack[currentRoutine->top-1];
  866. b = currentRoutine->stack[currentRoutine->top-2];
  867. if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) {
  868. currentRoutine->panic = 1;
  869. return;
  870. }
  871. ink_pop(ctx);
  872. currentRoutine->stack[currentRoutine->top-1].value = b.value % a.value;
  873. }
  874. static void dupe_elem(struct context* ctx) {
  875. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  876. if(currentRoutine->top < 1) {
  877. ctx->panic = 1;
  878. return;
  879. }
  880. struct elem a;
  881. a = currentRoutine->stack[currentRoutine->top-1];
  882. int err;
  883. err = ink_push(ctx, a);
  884. if(err < 0) ctx->panic;
  885. }
  886. static void drop_elem(struct context* ctx) {
  887. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  888. if(currentRoutine->top < 1) {
  889. ctx->panic = 1;
  890. return;
  891. }
  892. ink_pop(ctx);
  893. }
  894. static void pluck_elem(struct context* ctx) {
  895. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  896. if(currentRoutine->top < 1) {
  897. currentRoutine->panic = 1;
  898. return;
  899. }
  900. struct elem a;
  901. a = currentRoutine->stack[currentRoutine->top-1];
  902. if(a.type != INK_INTEGER) {
  903. ctx->panic = 1;
  904. return;
  905. }
  906. int position = currentRoutine->top - (a.value + 1);
  907. if(position >= currentRoutine->top || position < 0) {
  908. ctx->panic = 1;
  909. return;
  910. }
  911. ink_pop(ctx);
  912. int err;
  913. err = ink_push(ctx, currentRoutine->stack[position]);
  914. if(err < 0) ctx->panic;
  915. }
  916. static void swap_elem(struct context* ctx) {
  917. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  918. if(currentRoutine->top < 2) {
  919. currentRoutine->panic = 1;
  920. return;
  921. }
  922. struct elem a;
  923. struct elem b;
  924. a = currentRoutine->stack[currentRoutine->top-1];
  925. b = currentRoutine->stack[currentRoutine->top-2];
  926. currentRoutine->stack[currentRoutine->top-2] = a;
  927. currentRoutine->stack[currentRoutine->top-1] = b;
  928. }
  929. static void return_if(struct context* ctx) {
  930. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  931. if(currentRoutine->top < 1) {
  932. ctx->panic = 1;
  933. return;
  934. }
  935. struct elem a;
  936. a = currentRoutine->stack[currentRoutine->top-1];
  937. if(a.type != INK_INTEGER) {
  938. ctx->panic = 1;
  939. return;
  940. }
  941. if(a.value) {
  942. ink_pop_fn(ctx);
  943. ink_pop_fn(ctx);
  944. }
  945. ink_pop(ctx);
  946. return;
  947. }
  948. static void jump_if(struct context* ctx) {
  949. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  950. if(currentRoutine->top < 1) {
  951. ctx->panic = 1;
  952. return;
  953. }
  954. struct elem a;
  955. a = currentRoutine->stack[currentRoutine->top-1];
  956. if(a.type != INK_INTEGER) {
  957. ctx->panic = 1;
  958. return;
  959. }
  960. ink_pop(ctx);
  961. if(a.value) {
  962. ink_pop_fn(ctx);
  963. a = currentRoutine->stack[currentRoutine->top-1];
  964. currentRoutine->function_stack[currentRoutine->function_stack_top - 1].index += a.value - 3;
  965. }
  966. ink_pop(ctx);
  967. return;
  968. }
  969. static void print_int(struct context* ctx) {
  970. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  971. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  972. currentRoutine->panic = 1;
  973. return;
  974. }
  975. struct elem a;
  976. a = currentRoutine->stack[currentRoutine->top-1];
  977. ink_pop(ctx);
  978. char* n = ink_itoa(ctx, a.value);
  979. char* str = n;
  980. while (*str) {
  981. ctx->putchar(*str);
  982. ++str;
  983. }
  984. ctx->free(n);
  985. }
  986. static void print_as_utf8(struct context* ctx) {
  987. struct ink_routine* currentRoutine = ctx->routines + ctx->routine_current;
  988. if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) {
  989. ctx->panic = 1;
  990. return;
  991. }
  992. struct elem a;
  993. a = currentRoutine->stack[currentRoutine->top-1];
  994. if(a.value <= 0x7F) {
  995. ctx->putchar(a.value);
  996. } else if(a.value <= 0x7FF) {
  997. ctx->putchar(((a.value & 0xFC0) >> 6) | 192);
  998. ctx->putchar((a.value & 0x3F) | 128);
  999. } else if(a.value <= 0xFFFF) {
  1000. ctx->putchar(((a.value & 0x3F000) >> 12) | 224);
  1001. ctx->putchar(((a.value & 0xFC0) >> 6) | 128);
  1002. ctx->putchar((a.value & 0x3F) | 128);
  1003. } else if(a.value <= 0x10FFFF) {
  1004. ctx->putchar(((a.value & 0x3C0000) >> 18) | 240);
  1005. ctx->putchar(((a.value & 0x3F000) >> 12) | 128);
  1006. ctx->putchar(((a.value & 0xFC0) >> 6) | 128);
  1007. ctx->putchar((a.value & 0x3F) | 128);
  1008. } else {
  1009. ctx->panic = 1;
  1010. return;
  1011. }
  1012. ink_pop(ctx);
  1013. }
  1014. int ink_std_library(struct context* ctx) {
  1015. int v;
  1016. v = 0;
  1017. v += ink_add_native(ctx, "trace", print_stacktrace);
  1018. v += ink_add_native(ctx, "print_int", print_int);
  1019. v += ink_add_native(ctx, "print_utf8", print_as_utf8);
  1020. v += ink_add_native(ctx, "+", add_int);
  1021. v += ink_add_native(ctx, "-", sub_int);
  1022. v += ink_add_native(ctx, "*", mult_int);
  1023. v += ink_add_native(ctx, "/", div_int);
  1024. v += ink_add_native(ctx, "%", rem_int);
  1025. v += ink_add_native(ctx, "swap", swap_elem);
  1026. v += ink_add_native(ctx, "dup", dupe_elem);
  1027. v += ink_add_native(ctx, "drop", drop_elem);
  1028. v += ink_add_native(ctx, "pluck", pluck_elem);
  1029. v += ink_add_native(ctx, "return_if", return_if);
  1030. v += ink_add_native(ctx, "jump_if", jump_if);
  1031. return v;
  1032. }