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.

135 lines
3.4 KiB

  1. #include "ink.h"
  2. int main(int argc, char** argv) {
  3. struct context* ctx;
  4. ctx = ink_make_default_context();
  5. ink_compile(
  6. ctx,
  7. "fn encrypt do\n"
  8. " 3 pluck array.size\n"
  9. " # array add_key modulo_key index\n"
  10. " loop:\n"
  11. " 1 - dup 5 pluck\n"
  12. " # array add_key modulo_key index index array\n"
  13. " array.index \n"
  14. " # array add_key modulo_key index v\n"
  15. " 4 pluck +\n"
  16. " # array add_key modulo_key index (v + add_key)\n"
  17. " 3 pluck %\n"
  18. " # array add_key modulo_key index ((v + add_key) % modulo_key)\n"
  19. " 2 pluck\n"
  20. " # array add_key modulo_key index ((v + add_key) % modulo_key) index\n"
  21. " 6 pluck\n"
  22. " # array add_key modulo_key index ((v + add_key) % modulo_key) index array\n"
  23. " array.set\n"
  24. " # array add_key modulo_key index\n"
  25. " dup 0 != loop jump_if drop drop drop drop\n"
  26. "end\n"
  27. "fn string.dump do\n"
  28. " dup array.size 0\n"
  29. " # array end it\n"
  30. " 91 print_utf8\n"
  31. " 32 print_utf8\n"
  32. " loop:\n"
  33. " dup\n"
  34. " # array end it it\n"
  35. " 4 pluck\n"
  36. " # array end it it array\n"
  37. " array.index print_int\n"
  38. " 32 print_utf8\n"
  39. " 1 +\n"
  40. " # array end it\n"
  41. " 2 pluck 2 pluck > loop jump_if\n"
  42. " # array end it\n"
  43. " 93 print_utf8\n"
  44. " drop drop drop\n"
  45. "end\n"
  46. "fn string.print do\n"
  47. " dup array.size 0\n"
  48. " # array end it\n"
  49. " loop:\n"
  50. " dup\n"
  51. " # array end it it\n"
  52. " 4 pluck\n"
  53. " # array end it it array\n"
  54. " array.index print_utf8\n"
  55. " 1 +\n"
  56. " # array end it\n"
  57. " 2 pluck 2 pluck > loop jump_if\n"
  58. " # array end it\n"
  59. " drop drop drop\n"
  60. "end"
  61. );
  62. ink_compile(
  63. ctx,
  64. "# Clones an array, creating a new array\n"
  65. "#\n"
  66. "# @param array The array to clone into a new array\n"
  67. "# @return a new array that contains the same elements as the source array\n"
  68. "#\n"
  69. "# array -> new_array\n"
  70. "fn array.clone do\n"
  71. " array.new 2 pluck array.size 0\n"
  72. " # array new_array end it\n"
  73. " 2 pluck 2 pluck == l jump_if\n"
  74. " # array new_array end it\n"
  75. " loop:\n"
  76. " dup 5 pluck\n"
  77. " # array new_array end it it array\n"
  78. " array.index 4 pluck\n"
  79. " # array new_array end it v new_array\n"
  80. " array.push\n"
  81. " # array new_array end it\n"
  82. " 1 +\n"
  83. " 2 pluck 2 pluck > loop jump_if\n"
  84. " l: drop drop swap drop\n"
  85. " # new_array\n"
  86. "end"
  87. );
  88. ink_compile(
  89. ctx,
  90. "fn witch do\n"
  91. " drop drop drop sys.trace\n"
  92. "end\n"
  93. );
  94. ink_compile(
  95. ctx,
  96. "fn golden do\n"
  97. " 42 64190 witch\n"
  98. "end\n"
  99. );
  100. ink_compile(
  101. ctx,
  102. "fn beatrice do\n"
  103. " 10 golden\n"
  104. "end\n"
  105. );
  106. ink_compile(
  107. ctx,
  108. "beatrice 4 8 15 16 23 stack.dump\n"
  109. );
  110. //ctx->routines[coro].panic = 0;
  111. int increment = 1 << 16;
  112. int counter = increment;
  113. int c;
  114. while(ink_can_run(ctx)) {
  115. for(c = 0; c < 64; ++c)
  116. ink_step_everyone(ctx);
  117. if(ctx->steps < counter) {
  118. ink_gc(ctx);
  119. counter += increment;
  120. }
  121. }
  122. //return ctx->routines[coro].panic != INK_ROUTINE_SUCCESS && ctx->routines[coro].panic != INK_ROUTINE_CAN_REUSE;
  123. }