diff --git a/lang/bytecode.c b/lang/bytecode.c new file mode 100644 index 0000000..3af1285 --- /dev/null +++ b/lang/bytecode.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2023 John Watts and the LuminaSensum contributors + +#include "bytecode.h" +#include "error.h" +#include "number.h" + +enum opcodes { + OP_RET = 0x00, + OP_NUM = 0x01, +}; + +// clang-format off +static const unsigned char bytecode[] = { + OP_NUM, 0xd2, 0x04, 0x00, 0x00, + OP_RET +}; +// clang-format on + +void bytecode_run(struct object **stack) { + abort_if(!stack, "bytecode_run has no stack"); + const unsigned char *pos_code = &bytecode[0]; + struct object **pos_stack = stack; + unsigned char op = OP_RET; + while ((op = *pos_code++) != OP_RET) { + switch (op) { + case OP_NUM: { + int num = 0; + num |= *pos_code++ << 0; + num |= *pos_code++ << 8; + num |= *pos_code++ << 16; + num |= *pos_code++ << 24; + *pos_stack++ = number_create(num); + break; + } + } + } +} diff --git a/lang/bytecode.h b/lang/bytecode.h new file mode 100644 index 0000000..d39bdae --- /dev/null +++ b/lang/bytecode.h @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2023 John Watts and the LuminaSensum contributors + +#ifndef BYTECODE_H +#define BYTECODE_H + +#include "object.h" + +// Runs bytecode on a stack +void bytecode_run(struct object **stack); + +#endif diff --git a/lang/func.c b/lang/func.c index d2d6250..5d41a56 100644 --- a/lang/func.c +++ b/lang/func.c @@ -1,8 +1,8 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2023 John Watts and the LuminaSensum contributors +#include "bytecode.h" #include "error.h" -#include "number.h" #include "object.h" #include @@ -30,7 +30,7 @@ static void func_call(struct object *obj, int arg_count, struct object **args) { abort_if(arg_count != 0, "func_add called with more than 0 arguments"); abort_if(obj->class_data != &func_class, "func_call obj is not a func"); - args[0] = number_create(1234); + bytecode_run(args); } static struct object_call calls[] = {