diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/ast.h | 5 | ||||
-rw-r--r-- | include/driver.h | 13 | ||||
-rw-r--r-- | include/hashmap.h | 63 | ||||
-rw-r--r-- | include/interpreter.h | 38 | ||||
-rw-r--r-- | include/interpreter_context.h | 143 | ||||
-rw-r--r-- | include/repl.h | 8 |
6 files changed, 183 insertions, 87 deletions
diff --git a/include/ast.h b/include/ast.h index 068372e..a687a9b 100644 --- a/include/ast.h +++ b/include/ast.h @@ -8,7 +8,6 @@ * Provides data structures, enums, and functions for creating, managing, cloning, and freeing AST nodes. * * @author Flavian Kaufmann - * @date 2025-05-23 */ @@ -142,7 +141,7 @@ IMP_ASTNode *imp_ast_clone(const IMP_ASTNode *node); * * @param node Node to free. */ -void imp_ast_free(IMP_ASTNode *node); +void imp_ast_destroy(IMP_ASTNode *node); /** * Creates a new linked list of AST nodes. @@ -160,6 +159,6 @@ IMP_ASTNodeList *imp_ast_list(IMP_ASTNode *node, IMP_ASTNodeList *list); * * @param list List to free. */ -void imp_ast_list_free(IMP_ASTNodeList *list); +void imp_ast_list_destroy(IMP_ASTNodeList *list); #endif /* IMP_AST_H */
\ No newline at end of file diff --git a/include/driver.h b/include/driver.h new file mode 100644 index 0000000..40cfc58 --- /dev/null +++ b/include/driver.h @@ -0,0 +1,13 @@ +#ifndef IMP_DRIVER_H +#define IMP_DRIVER_H + +#include "interpreter_context.h" + +int imp_driver_interpret_file (IMP_InterpreterContext *context, const char *path); +int imp_driver_interpret_str (IMP_InterpreterContext *context, const char *str); +int imp_driver_print_ast_file (const char *path); + +void imp_driver_print_var_table(IMP_InterpreterContext *context); +void imp_driver_print_proc_table(IMP_InterpreterContext *context); + +#endif /* IMP_DRIVER_H */
\ No newline at end of file diff --git a/include/hashmap.h b/include/hashmap.h deleted file mode 100644 index 7cc20b1..0000000 --- a/include/hashmap.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef HASHMAP_H -#define HASHMAP_H - -/* Hashmap Handle */ -typedef struct HashMap *hashmap_t; - -/* Hashmap Key Iterator Handle */ -typedef struct HashMapKeysIter *hashmap_keys_iter_t; - - -/* - Creates hashmap that maps char* to void*. - Keys are copied, and will be freed when the hashmap is freed. - Elements are not copied, and must be freed by the caller. - Caller is responsible for freeing the hashmap using hashmap_free. -*/ -hashmap_t hashmap_create(void); - -/* - Frees the hashmap and all keys. - Elements are not freed, and must be freed by the caller. -*/ -void hashmap_free(hashmap_t map); - -/* - Looks up element with given key. - Returns pointer to element, or NULL if not found. -*/ -void **hashmap_get(hashmap_t map, const char *key); - -/* - Inserts element with given key. - If key already exists, the old element is replaced (caller is responsible for freeing beforehand). -*/ -void hashmap_insert(hashmap_t map, const char *key, void *element); - -/* - Deletes element with given key. - Caller is responsible for freeing the element. - Returns 0 on success, -1 when not found. -*/ -int hashmap_delete(hashmap_t map, const char *key); - - -/* - Creates iterator for keys. - Caller is responsible for freeing the iterator using hashmap_keys_iter_free. - Iterator is invalid when the hashmap is modified and must be freed. -*/ -hashmap_keys_iter_t hashmap_keys_iter_create(hashmap_t map); - -/* - Returns next key in the iterator. - Returns NULL when there are no more keys. - Keys are not copied, and must not be freed. -*/ -const char *hashmap_keys_iter_next(hashmap_keys_iter_t iter); - -/* Frees the iterator. */ -void hashmap_keys_iter_free(hashmap_keys_iter_t iter); - - -#endif
\ No newline at end of file diff --git a/include/interpreter.h b/include/interpreter.h index 5eff23f..d5a1e7a 100644 --- a/include/interpreter.h +++ b/include/interpreter.h @@ -1,26 +1,30 @@ -#ifndef INTERPRETER_H -#define INTERPRETER_H +#ifndef IMP_INTERPRETER_H +#define IMP_INTERPRETER_H -#include "ast.h" - +/** + * @file interpreter.h + * @brief Interpreter for evaluating IMP language programs. + * + * @author Flavian Kaufmann + */ -typedef struct Context *context_t; +#include "ast.h" +#include "interpreter_context.h" -context_t context_create(void); -void context_free(context_t context); - -int context_get_var(context_t context, const char *name); -void context_set_var(context_t context, const char *name, int value); -void context_print_var_table(context_t context); -void context_print_proc_table(context_t context); -int interp_ast(context_t context, IMP_ASTNode *node); -int interp_file (context_t context, const char *path); -int interp_str (context_t context, const char *str); +/** + * Evaluates an AST node within a given context. + * + * @param context The interpreter context. + * @param node AST node to evaluate. + * @return Status code or result of evaluation. (0 for success, non-zero for error). + * + * @note The interpreter does neither take ownership of the context nor the AST node. + */ +int imp_interpreter_interpret_ast(IMP_InterpreterContext *context, const IMP_ASTNode *node); -int print_ast_file (const char *path); -#endif
\ No newline at end of file +#endif /* IMP_INTERPRETER_H */
\ No newline at end of file diff --git a/include/interpreter_context.h b/include/interpreter_context.h new file mode 100644 index 0000000..acc63c1 --- /dev/null +++ b/include/interpreter_context.h @@ -0,0 +1,143 @@ +#ifndef IMP_INTERPRETER_CONTEXT_H +#define IMP_INTERPRETER_CONTEXT_H + +/** + * @file interpreter_context.h + * @brief Defines the context structure for the IMP interpreter. + * + * Provides the interface for managing the interpreter state, specifically + * the variable and procedure environments. + * + * Author: Flavian Kaufmann + */ + +#include "ast.h" + +/** + * @brief Opaque type representing the interpreter context. + */ +typedef struct IMP_InterpreterContext IMP_InterpreterContext; + +/** + * @brief Opaque type for iterating over variable entries in the context. + */ +typedef struct IMP_InterpreterContextVarIter IMP_InterpreterContextVarIter; + +/** + * @brief Opaque type for iterating over procedure entries in the context. + */ +typedef struct IMP_InterpreterContextProcIter IMP_InterpreterContextProcIter; + +/** + * @brief A single variable entry in the interpreter's environment. + */ +typedef struct IMP_InterpreterContextVarTableEntry { + const char *key; /**< The variable name (identifier). */ + int value; /**< The value associated with the variable. */ +} IMP_InterpreterContextVarTableEntry; + +/** + * @brief A single procedure entry in the interpreter's environment. + */ +typedef struct IMP_InterpreterContextProcTableEntry { + const char *key; /**< The procedure name (identifier). */ + const IMP_ASTNode *value; /**< The AST node representing the procedure declaration. */ +} IMP_InterpreterContextProcTableEntry; + +/** + * @brief Creates and initializes a new interpreter context. + * + * @return A pointer to the newly created context. + */ +IMP_InterpreterContext *imp_interpreter_context_create(void); + +/** + * @brief Frees all memory associated with the interpreter context. + * + * @param context The context to destroy. + */ +void imp_interpreter_context_destroy(IMP_InterpreterContext *context); + +/** + * @brief Retrieves the value of a variable from the context. + * + * @param context The interpreter context. + * @param name The name of the variable. (Is copied internally.) + * @return The value of the variable. Returns 0 if the variable is not found. + */ +int imp_interpreter_context_var_get(IMP_InterpreterContext *context, const char *name); + +/** + * @brief Sets or updates the value of a variable in the context. + * + * @param context The interpreter context. + * @param name The name of the variable. (Is copied internally.) + * @param value The value to assign. (If value is 0, the variable is removed from the context.) + */ +void imp_interpreter_context_var_set(IMP_InterpreterContext *context, const char *name, int value); + +/** + * @brief Retrieves the AST node for a procedure from the context. + * + * @param context The interpreter context. + * @param name The name of the procedure. (Is copied internally.) + * @return A pointer to the procedure's AST node, or NULL if not found. + */ +const IMP_ASTNode *imp_interpreter_context_proc_get(IMP_InterpreterContext *context, const char *name); + +/** + * @brief Adds or updates a procedure in the context. + * + * @param context The interpreter context. + * @param name The name of the procedure. (Is copied internally.) + * @param proc The AST node representing the procedure body. (Is cloned internally.) + */ +void imp_interpreter_context_proc_set(IMP_InterpreterContext *context, const char *name, const IMP_ASTNode *proc); + +/** + * @brief Creates an iterator over the variables in the context. (Is invalid if the variable table is modified.) + * + * @param context The interpreter context. + * @return A pointer to the variable iterator. + */ +IMP_InterpreterContextVarIter *imp_interpreter_context_var_iter_create(IMP_InterpreterContext *context); + +/** + * @brief Destroys a variable iterator. + * + * @param iter The iterator to destroy. + */ +void imp_interpreter_context_var_iter_destroy(IMP_InterpreterContextVarIter *iter); + +/** + * @brief Retrieves the next variable entry from the iterator. + * + * @param iter The variable iterator. + * @return A pointer to the next variable entry, or NULL if end is reached. + */ +IMP_InterpreterContextVarTableEntry *imp_interpreter_context_var_iter_next(IMP_InterpreterContextVarIter *iter); + +/** + * @brief Creates an iterator over the procedures in the context. (Is invalid if the procedure table is modified.) + * + * @param context The interpreter context. + * @return A pointer to the procedure iterator. + */ +IMP_InterpreterContextProcIter *imp_interpreter_context_proc_iter_create(IMP_InterpreterContext *context); + +/** + * @brief Destroys a procedure iterator. + * + * @param iter The iterator to destroy. + */ +void imp_interpreter_context_proc_iter_destroy(IMP_InterpreterContextProcIter *iter); + +/** + * @brief Retrieves the next procedure entry from the iterator. + * + * @param iter The procedure iterator. + * @return A pointer to the next procedure entry, or NULL if end is reached. + */ +IMP_InterpreterContextProcTableEntry *imp_interpreter_context_proc_iter_next(IMP_InterpreterContextProcIter *iter); + +#endif /* IMP_INTERPRETER_CONTEXT_H */
\ No newline at end of file diff --git a/include/repl.h b/include/repl.h index 4b833ad..5e6e168 100644 --- a/include/repl.h +++ b/include/repl.h @@ -1,8 +1,8 @@ -#ifndef REPL_H -#define REPL_H +#ifndef IMP_REPL_H +#define IMP_REPL_H -void repl(void); +void imp_repl(void); -#endif
\ No newline at end of file +#endif /* IMP_REPL_H */
\ No newline at end of file |