A minimalistic programming language written in C89.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

354 linhas
10 KiB

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