From d2ab53c625d386a4fbc6a9d5a5eb29faab1b3f0c Mon Sep 17 00:00:00 2001 From: A404M Date: Thu, 19 Sep 2024 15:53:13 +0330 Subject: removing print command and keyword added function call support cleaned up keyword and operator checking and speed it up a little bit cleaning includes added builtin functions --- src/vm/runner/runner.c | 64 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 10 deletions(-) (limited to 'src/vm/runner/runner.c') diff --git a/src/vm/runner/runner.c b/src/vm/runner/runner.c index 5e63395..de1a478 100644 --- a/src/vm/runner/runner.c +++ b/src/vm/runner/runner.c @@ -1,14 +1,25 @@ #include "runner.h" +#include #include #include #include #include +const BuiltinFunction BUILTIN_FUNCTIONS[] = { + print, +}; +const char *BUILTIN_FUNCTION_NAMES[] = { + "print", +}; +const size_t BUILTIN_FUNCTIONS_SIZE = + sizeof(BUILTIN_FUNCTIONS) / sizeof(BuiltinFunction); + bool runner(Instructions instructions) { size_t stack_size = 0; void **stack = a404m_malloc(stack_size * sizeof(void *)); size_t stack_inserted = 0; + for (size_t i = 0; i < instructions.size; ++i) { if (!runInstruction(instructions.instructions[i], &stack, &stack_size, &stack_inserted)) { @@ -24,15 +35,32 @@ RETURN_ERROR: return false; } +BuiltinFunction getBuiltinFunction(SizedString string) { + for (size_t i = 0; i < BUILTIN_FUNCTIONS_SIZE; ++i) { + const char *search = BUILTIN_FUNCTION_NAMES[i]; + // faster than strlen+strncpy + for (size_t j = 0;; ++j) { + const char searchChar = search[j]; + if (j == string.size) { + if (searchChar == '\0') { + return BUILTIN_FUNCTIONS[i]; + } else { + break; + } + } else if (searchChar == '\0') { + break; + } else if (searchChar != string.str[j]) { + break; + } + } + } + return NULL; +} + bool runInstruction(Instruction instruction, void ***restrict stack, size_t *restrict stack_size, size_t *restrict stack_inserted) { switch (instruction.command) { - case COMMAND_PRINT: { - const SizedString *string = (*stack)[*stack_inserted - 1]; - --*stack_inserted; - printf("%.*s", (int)string->size, string->str); - } break; case COMMAND_PUSH_STRING: { SizedString *string = instruction.operand; if (*stack_inserted == *stack_size) { @@ -41,10 +69,26 @@ bool runInstruction(Instruction instruction, void ***restrict stack, } (*stack)[*stack_inserted] = string; ++*stack_inserted; - } break; - default: - fprintf(stderr, "unknown command '%d'\n", instruction.command); - return false; + return true; + } + case COMMAND_CALL_FUNCTION: { + SizedString *functionName = instruction.operand; + BuiltinFunction function = getBuiltinFunction(*functionName); + if (function == NULL) { + fprintf(stderr, "function '%.*s' not found\n", (int)functionName->size, + functionName->str); + return false; + } + function(stack, stack_inserted); + return true; + } + case COMMAND_NONE: } - return true; + fprintf(stderr, "unknown command '%d'\n", instruction.command); + return false; +} + +void print(void ***restrict stack, size_t *restrict stack_inserted) { + const SizedString *string = (*stack)[--*stack_inserted]; + printf("%.*s", (int)string->size, string->str); } -- cgit v1.2.3