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.

255 lignes
6.6 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
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. struct ink_collection_list {
  73. struct elem* elems;
  74. size_t size;
  75. };
  76. struct ink_type {
  77. const char* name;
  78. void* elements;
  79. void (*collect)(void*);
  80. struct ink_collection_list (*gc)(void*);
  81. };
  82. /**
  83. * Represents a complete execution context for the ink interpreter
  84. */
  85. struct context {
  86. int panic;
  87. void *(*inner_malloc)(size_t);
  88. void *(*inner_realloc)(void *, size_t);
  89. void (*inner_free)(void *);
  90. void *(*malloc)(size_t);
  91. void *(*realloc)(void *, size_t);
  92. void (*free)(void *);
  93. int (*putchar)(int);
  94. struct ink_routine *routines;
  95. int routines_capacity;
  96. int routines_top;
  97. struct ink_type *types;
  98. int types_capacity;
  99. int types_top;
  100. /**
  101. * Contains the id of the routine that is currently being manipulated
  102. */
  103. int routine_current;
  104. struct native_fn *native_words;
  105. int native_words_capacity;
  106. int native_words_top;
  107. struct fn *words;
  108. int words_capacity;
  109. int words_top;
  110. char **lex_reserved_words;
  111. int lex_reserved_words_capacity;
  112. int lex_reserved_words_top;
  113. unsigned int steps;
  114. /**
  115. * Can be set to any data that is convenient to the user to track and use within natively defined functions
  116. */
  117. void *persistent_userdata;
  118. };
  119. /**
  120. * Creates a routine to execute within the context
  121. * @param ctx The context in which to create the routine
  122. * @warning Does not set the `routine_current` of the context to the newly created routine
  123. * @return either a negative error value or the id of the created routine
  124. */
  125. int ink_make_routine(struct context *ctx);
  126. /**
  127. * Cleans the targeted routine id data from the context
  128. * @param ctx The context to operate in
  129. * @param routine The id of the routine to destroy
  130. * @return 0 if nothing could or needed to be performed, 1 otherwise
  131. */
  132. int ink_kill_routine(struct context *ctx, int routine);
  133. /**
  134. * Declares and defines a native function within the context
  135. * @param ctx The context tpo operate in
  136. * @param name The name to give to the newly declared word
  137. * @param value A pointer to a valid word-function
  138. * @return a negative value in case of error, 0 otherwise
  139. */
  140. int ink_add_native(struct context *ctx, const char *name, void(*value)(struct context *));
  141. /**
  142. * Pushes a value on the current routine's value stack
  143. * @param ctx The context to manipulate
  144. * @param value The value to push
  145. * @return 0 on success, a negative value on failure
  146. */
  147. int ink_push(struct context *ctx, struct elem value);
  148. /**
  149. * Pushes a function on the execution stack of the current routine of the context
  150. * @param ctx The context to operate in
  151. * @param value the function that will be pushed. in the state one wants it to run from
  152. * @return 0 on success, a negative value on failure
  153. */
  154. int ink_push_fn(struct context *ctx, struct stack_frame value);
  155. /**
  156. * Create a context to execute routines
  157. * @param malloc the memory allocation function, with a signature similar to the standard malloc
  158. * @param realloc the memory allocation function, with a signature similar to the standard realloc
  159. * @param free the memory allocation function, with a signature similar to the standard free
  160. * @param putchar a function to print to the output character by character
  161. * @return a pointer to a context allocated within the malloc function itself.
  162. */
  163. struct context* ink_make_context(void *(*malloc)(size_t), void *(*realloc)(void *, size_t), void(*free)(void *), int(*putchar)(int));
  164. #ifndef NOSTDLIB
  165. /**
  166. * Creates a context that includes the standard library of ink, as well as uses the C standard library to operate
  167. * @return a pointer to a context allocated with malloc and with predefined functions added
  168. */
  169. struct context* ink_make_default_context();
  170. #endif
  171. /**
  172. * Steps the current routine by one execution step
  173. * @param pContext The context of the routine to advance
  174. * @return A negative value in case of error, 0 if execution is finished, a positive value if more steps are required to execute
  175. */
  176. int ink_step(struct context *pContext);
  177. /**
  178. * Examine the context to see if any routines can execute
  179. * @param pContext the context
  180. * @return 0 if no coroutines are available, 1 otherwise
  181. */
  182. int ink_can_run(struct context *pContext);
  183. /**
  184. * Step every routine that can be executed.
  185. * @param pContext The context, the `routine_current` value may be modified.
  186. * @return 0
  187. */
  188. int ink_step_everyone(struct context *pContext);
  189. /**
  190. * Compiles the code and starts a main routine to execute it
  191. * @param pContext The context to execute the code in
  192. * @param buffer The buffer that contains the source as a NULL terminated string
  193. */
  194. void ink_compile(struct context *pContext, char *buffer);
  195. /**
  196. * Includes the standard library in the specified context
  197. * @param ctx The context
  198. * @return 0
  199. */
  200. int ink_std_library(struct context *ctx);
  201. /**
  202. * Removes the top element of the function stack of the current routine of the specified context
  203. * @param ctx the context
  204. */
  205. void ink_pop_fn(struct context *ctx);
  206. /**
  207. * Removes the top element of the stack of the current routine of the specified context
  208. * @param ctx the context
  209. */
  210. void ink_pop(struct context *ctx);
  211. #ifdef __cplusplus
  212. };
  213. #endif