A minimalistic programming language written in C89.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

239 lignes
6.3 KiB

il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
il y a 4 mois
  1. #pragma once
  2. #include "stddef.h"
  3. /**
  4. * Represents the natively defined type of integers
  5. */
  6. #define INK_INTEGER 0
  7. /**
  8. * Represents the natively defined type of natively defined functions
  9. */
  10. #define INK_NATIVE_FUNCTION 1
  11. /**
  12. * Represents the natively defined type of functions defined from within ink
  13. */
  14. #define INK_FUNCTION 2
  15. /**
  16. * Represents the special coroutine state that means that it was disposed of and is ready for reuse
  17. */
  18. #define INK_ROUTINE_CAN_REUSE 32
  19. /**
  20. * Represents the special coroutine state that means that it was terminated without errors
  21. */
  22. #define INK_ROUTINE_SUCCESS 1
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * Represents arbitrary values within ink
  28. */
  29. struct elem {
  30. int type;
  31. int value;
  32. };
  33. /**
  34. * Represents a stack-frame within the execution context
  35. */
  36. struct stack_frame {
  37. struct elem executing;
  38. int index;
  39. };
  40. /**
  41. * Represents a function within ink, defined in ink, using the homoiconic representation of ink
  42. */
  43. struct fn {
  44. char *name;
  45. struct elem *things;
  46. int size;
  47. };
  48. struct context;
  49. /**
  50. * Represents natively defined words within ink
  51. */
  52. struct native_fn {
  53. char *name;
  54. void (*value)(struct context *);
  55. };
  56. /**
  57. * Represents the narrow execution context of a single thread of execution within ink.
  58. */
  59. struct ink_routine {
  60. int panic;
  61. struct elem *stack;
  62. int capacity;
  63. int top;
  64. struct stack_frame *function_stack;
  65. int function_stack_capacity;
  66. int function_stack_top;
  67. /**
  68. * This user data can be set to any value convenient for the user to track a state local to the routine that is executing
  69. */
  70. void *routine_userdata;
  71. };
  72. /**
  73. * Represents a complete execution context for the ink interpreter
  74. */
  75. struct context {
  76. int panic;
  77. void *(*inner_malloc)(size_t);
  78. void *(*inner_realloc)(void *, size_t);
  79. void (*inner_free)(void *);
  80. void *(*malloc)(size_t);
  81. void *(*realloc)(void *, size_t);
  82. void (*free)(void *);
  83. int (*putchar)(int);
  84. struct ink_routine *routines;
  85. int routines_capacity;
  86. int routines_top;
  87. /**
  88. * Contains the id of the routine that is currently being manipulated
  89. */
  90. int routine_current;
  91. struct native_fn *native_words;
  92. int native_words_capacity;
  93. int native_words_top;
  94. struct fn *words;
  95. int words_capacity;
  96. int words_top;
  97. char **lex_reserved_words;
  98. int lex_reserved_words_capacity;
  99. int lex_reserved_words_top;
  100. unsigned int steps;
  101. /**
  102. * Can be set to any data that is convenient to the user to track and use within natively defined functions
  103. */
  104. void *persistent_userdata;
  105. };
  106. /**
  107. * Creates a routine to execute within the context
  108. * @param ctx The context in which to create the routine
  109. * @warning Does not set the `routine_current` of the context to the newly created routine
  110. * @return either a negative error value or the id of the created routine
  111. */
  112. int ink_make_routine(struct context *ctx);
  113. /**
  114. * Cleans the targeted routine id data from the context
  115. * @param ctx The context to operate in
  116. * @param routine The id of the routine to destroy
  117. * @return 0 if nothing could or needed to be performed, 1 otherwise
  118. */
  119. int ink_kill_routine(struct context *ctx, int routine);
  120. /**
  121. * Declares and defines a native function within the context
  122. * @param ctx The context tpo operate in
  123. * @param name The name to give to the newly declared word
  124. * @param value A pointer to a valid word-function
  125. * @return a negative value in case of error, 0 otherwise
  126. */
  127. int ink_add_native(struct context *ctx, const char *name, void(*value)(struct context *));
  128. /**
  129. * Pushes a value on the current routine's value stack
  130. * @param ctx The context to manipulate
  131. * @param value The value to push
  132. * @return 0 on success, a negative value on failure
  133. */
  134. int ink_push(struct context *ctx, struct elem value);
  135. /**
  136. * Pushes a function on the execution stack of the current routine of the context
  137. * @param ctx The context to operate in
  138. * @param value the function that will be pushed. in the state one wants it to run from
  139. * @return 0 on success, a negative value on failure
  140. */
  141. int ink_push_fn(struct context *ctx, struct stack_frame value);
  142. /**
  143. * Create a context to execute routines
  144. * @param malloc the memory allocation function, with a signature similar to the standard malloc
  145. * @param realloc the memory allocation function, with a signature similar to the standard realloc
  146. * @param free the memory allocation function, with a signature similar to the standard free
  147. * @param putchar a function to print to the output character by character
  148. * @return a pointer to a context allocated within the malloc function itself.
  149. */
  150. struct context* ink_make_context(void *(*malloc)(size_t), void *(*realloc)(void *, size_t), void(*free)(void *), int(*putchar)(int));
  151. #ifndef NOSTDLIB
  152. /**
  153. * Creates a context that includes the standard library of ink, as well as uses the C standard library to operate
  154. * @return a pointer to a context allocated with malloc and with predefined functions added
  155. */
  156. struct context* ink_make_default_context();
  157. #endif
  158. /**
  159. * Steps the current routine by one execution step
  160. * @param pContext The context of the routine to advance
  161. * @return A negative value in case of error, 0 if execution is finished, a positive value if more steps are required to execute
  162. */
  163. int ink_step(struct context *pContext);
  164. /**
  165. * Examine the context to see if any routines can execute
  166. * @param pContext the context
  167. * @return 0 if no coroutines are available, 1 otherwise
  168. */
  169. int ink_can_run(struct context *pContext);
  170. /**
  171. * Step every routine that can be executed.
  172. * @param pContext The context, the `routine_current` value may be modified.
  173. * @return 0
  174. */
  175. int ink_step_everyone(struct context *pContext);
  176. /**
  177. * Compiles the code and starts a main routine to execute it
  178. * @param pContext The context to execute the code in
  179. * @param buffer The buffer that contains the source as a NULL terminated string
  180. */
  181. void ink_compile(struct context *pContext, char *buffer);
  182. /**
  183. * Includes the standard library in the specified context
  184. * @param ctx The context
  185. * @return 0
  186. */
  187. int ink_std_library(struct context *ctx);
  188. /**
  189. * Removes the top element of the function stack of the current routine of the specified context
  190. * @param ctx the context
  191. */
  192. void ink_pop_fn(struct context *ctx);
  193. /**
  194. * Removes the top element of the stack of the current routine of the specified context
  195. * @param ctx the context
  196. */
  197. void ink_pop(struct context *ctx);
  198. #ifdef __cplusplus
  199. };
  200. #endif