A minimalistic programming language written in C89.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

322 Zeilen
9.3 KiB

vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 5 Monaten
vor 5 Monaten
vor 5 Monaten
vor 5 Monaten
vor 5 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
vor 6 Monaten
  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. * Contains a list of element on which garbage collection is to not be performed
  74. */
  75. struct ink_collection_list {
  76. struct elem* elements;
  77. int count;
  78. };
  79. struct element_slab {
  80. void* data;
  81. int uses;
  82. int in_use;
  83. };
  84. /**
  85. * Contains all the data for every element of any type and its garbage collection information.
  86. */
  87. struct ink_type {
  88. const char* name; /**< The name of the type */
  89. int element_size; /**< The size of individual elements of the type, 0 for int adjacent, negative for unmanaged size */
  90. struct element_slab* elements; /**< The elements that are still live */
  91. int elements_top; /**< The top of the elements list */
  92. int elements_capacity; /**< The allocated capacity of the elements list */
  93. void (*collect)(struct context*,void*); /**< The "destructor" of the object */
  94. struct ink_collection_list (*gc)(struct context*,void*); /**< A function that returns an in-interpreter allocated list of elem references within the object */
  95. };
  96. /**
  97. * Represents a complete execution context for the ink interpreter
  98. */
  99. struct context {
  100. int panic;
  101. void *(*inner_malloc)(size_t);
  102. void *(*inner_realloc)(void *, size_t);
  103. void (*inner_free)(void *);
  104. void *(*malloc)(size_t);
  105. void *(*realloc)(void *, size_t);
  106. void (*free)(void *);
  107. int (*putchar)(int);
  108. struct ink_routine *routines;
  109. int routines_capacity;
  110. int routines_top;
  111. struct ink_type *types;
  112. int types_capacity;
  113. int types_top;
  114. /**
  115. * Contains the id of the routine that is currently being manipulated
  116. */
  117. int routine_current;
  118. struct native_fn *native_words;
  119. int native_words_capacity;
  120. int native_words_top;
  121. struct fn *words;
  122. int words_capacity;
  123. int words_top;
  124. char **lex_reserved_words;
  125. int lex_reserved_words_capacity;
  126. int lex_reserved_words_top;
  127. unsigned int steps;
  128. unsigned int collections;
  129. /**
  130. * Can be set to any data that is convenient to the user to track and use within natively defined functions
  131. */
  132. void *persistent_userdata;
  133. };
  134. /**
  135. * Creates a routine to execute within the context
  136. * @param ctx The context in which to create the routine
  137. * @warning Does not set the `routine_current` of the context to the newly created routine
  138. * @return either a negative error value or the id of the created routine
  139. */
  140. int ink_make_routine(struct context *ctx);
  141. /**
  142. * Cleans the targeted routine id data from the context
  143. * @param ctx The context to operate in
  144. * @param routine The id of the routine to destroy
  145. * @return 0 if nothing could or needed to be performed, 1 otherwise
  146. */
  147. int ink_kill_routine(struct context *ctx, int routine);
  148. /**
  149. * Declares and defines a native function within the context
  150. * @param ctx The context tpo operate in
  151. * @param name The name to give to the newly declared word
  152. * @param value A pointer to a valid word-function
  153. * @return a negative value in case of error, 0 otherwise
  154. */
  155. int ink_add_native(struct context *ctx, const char *name, void(*value)(struct context *));
  156. /**
  157. * Pushes a value on the current routine's value stack
  158. * @param ctx The context to manipulate
  159. * @param value The value to push
  160. * @return 0 on success, a negative value on failure
  161. */
  162. int ink_push(struct context *ctx, struct elem value);
  163. /**
  164. * Pushes a function on the execution stack of the current routine of the context
  165. * @param ctx The context to operate in
  166. * @param value the function that will be pushed. in the state one wants it to run from
  167. * @return 0 on success, a negative value on failure
  168. */
  169. int ink_push_fn(struct context *ctx, struct stack_frame value);
  170. /**
  171. * Create a context to execute routines
  172. * @param malloc the memory allocation function, with a signature similar to the standard malloc
  173. * @param realloc the memory allocation function, with a signature similar to the standard realloc
  174. * @param free the memory allocation function, with a signature similar to the standard free
  175. * @param putchar a function to print to the output character by character
  176. * @return a pointer to a context allocated within the malloc function itself.
  177. */
  178. struct context* ink_make_context(void *(*malloc)(size_t), void *(*realloc)(void *, size_t), void(*free)(void *), int(*putchar)(int));
  179. #ifndef NOSTDLIB
  180. /**
  181. * Creates a context that includes the standard library of ink, as well as uses the C standard library to operate
  182. * @return a pointer to a context allocated with malloc and with predefined functions added
  183. */
  184. struct context* ink_make_default_context(void);
  185. #endif
  186. /**
  187. * Steps the current routine by one execution step
  188. * @param pContext The context of the routine to advance
  189. * @return A negative value in case of error, 0 if execution is finished, a positive value if more steps are required to execute
  190. */
  191. int ink_step(struct context *pContext);
  192. /**
  193. * Examine the context to see if any routines can execute
  194. * @param pContext the context
  195. * @return 0 if no coroutines are available, 1 otherwise
  196. */
  197. int ink_can_run(struct context *pContext);
  198. /**
  199. * Step every routine that can be executed.
  200. * @param pContext The context, the `routine_current` value may be modified.
  201. * @return 0
  202. */
  203. int ink_step_everyone(struct context *pContext);
  204. /**
  205. * Compiles the code and starts a main routine to execute it
  206. * @param pContext The context to execute the code in
  207. * @param buffer The buffer that contains the source as a NULL terminated string
  208. */
  209. void ink_compile(struct context *pContext, char *buffer);
  210. /**
  211. * Includes the standard library in the specified context
  212. * @param ctx The context
  213. * @return 0
  214. */
  215. int ink_std_library(struct context *ctx);
  216. /**
  217. * Removes the top element of the function stack of the current routine of the specified context
  218. * @param ctx the context
  219. */
  220. void ink_pop_fn(struct context *ctx);
  221. /**
  222. * Removes the top element of the stack of the current routine of the specified context
  223. * @param ctx the context
  224. */
  225. void ink_pop(struct context *ctx);
  226. /**
  227. * Declares a new type that can be stored within the interpreter
  228. * @param ctx The context in which to add the file
  229. * @param type_name The name of the type we want to add
  230. * @param size The size in bytes of the type to add, size of 0 mean no size, in which case the type is adjacent to C int, negative size means that the memory is not managed by the interpreter.
  231. * @param collect A "destructor" function for the data
  232. * @param gc A function that returns a list (allocated with the `inner_malloc`) of all the elements this element holds references to
  233. * @return if positive, a new type id, if negative an error value
  234. * @internal user defined type ids minimal value is 15, we keep the first 16 types as reserved, just like negative type ids
  235. */
  236. int ink_new_type(
  237. struct context* ctx,
  238. const char* type_name,
  239. int size,
  240. void (*collect)(struct context*,void*),
  241. struct ink_collection_list (*gc)(struct context*,void*)
  242. );
  243. /**
  244. *
  245. * @param ctx The context of the interpreter
  246. * @param ref The in-interpreter reference
  247. * @return A pointer to the created value or NULL if it can't be found or has a size of 0
  248. */
  249. void* ink_get_value(struct context* ctx, struct elem ref);
  250. /**
  251. * Builds a native type from the provided memory by copying it using memcpy
  252. * @param ctx The context in which we operate
  253. * @param type_id The type_id to use
  254. * @param ptr The pointer from which to copy
  255. * @return The in-interpreter reference of the newly created element
  256. */
  257. struct elem ink_make_native(struct context* ctx, int type_id, void* ptr);
  258. /**
  259. * Builds a transparent type from the provided pointer
  260. * @param ctx The context in which we operate
  261. * @param type_id The type_id to use
  262. * @param ptr The pointer
  263. * @return The in-interpreter reference of the newly created element
  264. */
  265. struct elem ink_make_transparent(struct context* ctx, int type_id, void* ptr);
  266. /**
  267. * Launch the mark and sweep garbage collection
  268. * @param ctx The context to clean
  269. */
  270. void ink_gc(struct context* ctx);
  271. #ifdef __cplusplus
  272. };
  273. #endif