A minimalistic programming language written in C89.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1089 lines
28 KiB

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