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.

134 rivejä
3.8 KiB

4 kuukautta sitten
4 kuukautta sitten
4 kuukautta sitten
4 kuukautta sitten
  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. These functions need to be wrapped, the wrapper allows to make them stateful to keep individual heaps per context,
  11. allowing to clean the context by cleaning up its allocations.
  12. To make the library not use the standard library, build it with `NOSTDLIB` defined as a preprocessor directive.
  13. All of these functions need to work for `ink` to work. It is easy to add new functions to the interpreter. I added a
  14. garbage collector to handle cleaning dynamically allocated resources.
  15. It is possible to segregate unsafe allocations (allocations that should be hidden from the interpreter) by setting the
  16. `inner_` versions of the library functions to different allocation functions.
  17. ## Limits
  18. - Token size is limited to 127 bytes (see `ink_lex`)
  19. - Values and indices are limited to the platform size of `int`
  20. - Main function has a size limit of 256 tokens (see `ink_compile`)
  21. - Functions have a size limit of 256 tokens (see `ink_parse`)
  22. - Functions have a count limit 128 labels (see `ink_parse`)
  23. - Only non-main functions can use labels
  24. ## Examples
  25. ### Hello World
  26. ```
  27. [ 72 101 108 108 111 32 87 111 114 108 100 10 ]
  28. array.print_utf8
  29. ```
  30. ### Clone array
  31. ```asm
  32. # Clones an array, creating a new array
  33. #
  34. # @param array The array to clone into a new array
  35. # @return a new array that contains the same elements as the source array
  36. #
  37. # array -> new_array
  38. fn array.clone do
  39. array.new 2 pluck array.size 0
  40. # array new_array end it
  41. 2 pluck 2 pluck == end_loop jump_if
  42. # array new_array end it
  43. loop:
  44. dup 5 pluck
  45. # array new_array end it it array
  46. array.index 4 pluck
  47. # array new_array end it v new_array
  48. array.push
  49. # array new_array end it
  50. 1 +
  51. 2 pluck 2 pluck > loop jump_if
  52. end_loop: drop drop swap drop
  53. # new_array
  54. end
  55. ```
  56. ### `+%` encryption
  57. Encrypts a string with `(v + add_key) % modulo_key`. It modifies the array that was passed in.
  58. ```asm
  59. # Encrypts things by doing `(v + add_key) % modulo_key`
  60. #
  61. # @param array An array of ints representing a string
  62. # @param add_key Should be lower than the add key
  63. # @param modulo_key Should ke higher than all the codepoints of the array
  64. #
  65. # array add_key modulo_key
  66. fn encrypt do
  67. 3 pluck array.size
  68. # array add_key modulo_key index
  69. loop:
  70. 1 - dup 5 pluck
  71. # array add_key modulo_key index index array
  72. array.index
  73. # array add_key modulo_key index v
  74. 4 pluck +
  75. # array add_key modulo_key index (v + add_key)
  76. 3 pluck %
  77. # array add_key modulo_key index ((v + add_key) % modulo_key)
  78. 2 pluck
  79. # array add_key modulo_key index ((v + add_key) % modulo_key) index
  80. 6 pluck
  81. # array add_key modulo_key index ((v + add_key) % modulo_key) index array
  82. array.set
  83. # array add_key modulo_key index
  84. dup 0 != loop jump_if drop drop drop drop
  85. end
  86. # Prints a string as an array of ints
  87. #
  88. # @param array An array of ints representing a string
  89. #
  90. # array
  91. fn string.dump do
  92. dup array.size 0
  93. # array end it
  94. 91 print_utf8
  95. 32 print_utf8
  96. loop:
  97. dup
  98. # array end it it
  99. 4 pluck
  100. # array end it it array
  101. array.index print_int
  102. 32 print_utf8
  103. 1 +
  104. # array end it
  105. 2 pluck 2 pluck > loop jump_if
  106. # array end it
  107. 93 print_utf8
  108. end
  109. ```
  110. ```asm
  111. [ 72 101 108 108 111 32 87 111 114 108 100 10 ]
  112. dup
  113. 32 128 encrypt
  114. string.dump
  115. ```