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.

235 lignes
6.2 KiB

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