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ů.

131 řádky
3.6 KiB

před 5 měsíci
  1. # `ink`
  2. `ink` is a minimalistic interpreted programming language, tentatively implemented exclusively in C89. It features
  3. coroutines and can currently only manipulate integers. Part of the code may not be compliant with C89 and I will try to
  4. fix that in time.
  5. It is fully self-contained and doesn't rely on a working standard library beyond the following:
  6. - `malloc`
  7. - `realloc`
  8. - `free`
  9. - `putchar`
  10. To make the library not use the standard library, build it with `NOSTDLIB` defined as a preprocessor directive.
  11. All of these functions need to work for `ink` to work. It is easy to add new functions to the interpreter. I added a
  12. garbage collector to handle cleaning dynamically allocated resources.
  13. It is possible to segregate unsafe allocations (allocations that should be hidden from the interpreter) by setting the
  14. `inner_` versions of the library functions to different allocation functions.
  15. ## Limits
  16. - Token size is limited to 127 bytes (see `ink_lex`)
  17. - Values and indices are limited to the platform size of `int`
  18. - Main function has a size limit of 256 tokens (see `ink_compile`)
  19. - Functions have a size limit of 256 tokens (see `ink_parse`)
  20. - Functions have a count limit 128 labels (see `ink_parse`)
  21. - Only non-main functions can use labels
  22. ## Examples
  23. ### Hello World
  24. ```
  25. [ 72 101 108 108 111 32 87 111 114 108 100 10 ]
  26. array.print_utf8
  27. ```
  28. ### Clone array
  29. ```asm
  30. # Clones an array, creating a new array
  31. #
  32. # @param array The array to clone into a new array
  33. # @return a new array that contains the same elements as the source array
  34. #
  35. # array -> new_array
  36. fn array.clone do
  37. array.new 2 pluck array.size 0
  38. # array new_array end it
  39. 2 pluck 2 pluck == end_loop jump_if
  40. # array new_array end it
  41. loop:
  42. dup 5 pluck
  43. # array new_array end it it array
  44. array.index 4 pluck
  45. # array new_array end it v new_array
  46. array.push
  47. # array new_array end it
  48. 1 +
  49. 2 pluck 2 pluck > loop jump_if
  50. end_loop: drop drop swap drop
  51. # new_array
  52. end
  53. ```
  54. ### `+%` encryption
  55. Encrypts a string with `(v + add_key) % modulo_key`. It modifies the array that was passed in.
  56. ```asm
  57. # Encrypts things by doing `(v + add_key) % modulo_key`
  58. #
  59. # @param array An array of ints representing a string
  60. # @param add_key Should be lower than the add key
  61. # @param modulo_key Should ke higher than all the codepoints of the array
  62. #
  63. # array add_key modulo_key
  64. fn encrypt do
  65. 3 pluck array.size
  66. # array add_key modulo_key index
  67. loop:
  68. 1 - dup 5 pluck
  69. # array add_key modulo_key index index array
  70. array.index
  71. # array add_key modulo_key index v
  72. 4 pluck +
  73. # array add_key modulo_key index (v + add_key)
  74. 3 pluck %
  75. # array add_key modulo_key index ((v + add_key) % modulo_key)
  76. 2 pluck
  77. # array add_key modulo_key index ((v + add_key) % modulo_key) index
  78. 6 pluck
  79. # array add_key modulo_key index ((v + add_key) % modulo_key) index array
  80. array.set
  81. # array add_key modulo_key index
  82. dup 0 != loop jump_if drop drop drop drop
  83. end
  84. # Prints a string as an array of ints
  85. #
  86. # @param array An array of ints representing a string
  87. #
  88. # array
  89. fn string.dump do
  90. dup array.size 0
  91. # array end it
  92. 91 print_utf8
  93. 32 print_utf8
  94. loop:
  95. dup
  96. # array end it it
  97. 4 pluck
  98. # array end it it array
  99. array.index print_int
  100. 32 print_utf8
  101. 1 +
  102. # array end it
  103. 2 pluck 2 pluck > loop jump_if
  104. # array end it
  105. 93 print_utf8
  106. end
  107. ```
  108. ```asm
  109. [ 72 101 108 108 111 32 87 111 114 108 100 10 ]
  110. dup
  111. 32 128 encrypt
  112. string.dump
  113. ```