From d3b58a1d84b272abdb8a0bcb5c1a2775823c7a21 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Thu, 18 Jul 2024 04:53:49 +0200 Subject: [PATCH] added more control to ink_compile --- include/ink.h | 3 ++- lib.c | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/ink.h b/include/ink.h index b33ad9b..cf0711d 100644 --- a/include/ink.h +++ b/include/ink.h @@ -249,8 +249,9 @@ int ink_step_everyone(struct context *pContext); * Compiles the code and starts a main routine to execute it * @param pContext The context to execute the code in * @param buffer The buffer that contains the source as a NULL terminated string + * @return the routine that was created when compiling the code or -1 in case an error occured */ -void ink_compile(struct context *pContext, const char *buffer); +int ink_compile(struct context *pContext, const char *buffer); /** * Includes the standard library in the specified context diff --git a/lib.c b/lib.c index 3a13b35..54238b5 100644 --- a/lib.c +++ b/lib.c @@ -778,7 +778,7 @@ int ink_step(struct context *pContext) { return 1; } -void ink_compile(struct context *pContext, const char* buffer) { +int ink_compile(struct context *pContext, const char* buffer) { int routine, saved, executable_buffer_top; /* Main function has a size limit of 256 (need to know that for REPL */ struct elem executable_buffer[256]; @@ -799,13 +799,13 @@ void ink_compile(struct context *pContext, const char* buffer) { err = ink_lex(pContext, buffer); if(err < 0) { pContext->panic = 1; - return; + return -1; } executable_buffer_top = 0; err = ink_parse(pContext, executable_buffer, &executable_buffer_top); if(err < 0) { pContext->panic = 1; - return; + return -1; } if(executable_buffer_top != 0) { integer = ink_itoa(pContext, routine); @@ -816,7 +816,7 @@ void ink_compile(struct context *pContext, const char* buffer) { frame.executing.value = ink_add_indigenous(pContext, main_fn, executable_buffer, executable_buffer_top); if (frame.executing.value < 0) { pContext->panic = 1; - return; + return -1; } frame.executing.type = INK_FUNCTION; frame.index = 0; @@ -824,14 +824,14 @@ void ink_compile(struct context *pContext, const char* buffer) { pContext->routines[pContext->routine_current].top = 0; if (err < 0) { pContext->panic = 1; - return; + return -1; } } else { pContext->routines[pContext->routine_current].panic = INK_ROUTINE_SUCCESS; } pContext->routine_current = saved; - return; + return routine; } int ink_can_run(struct context* pContext) {