start working on function calls
This commit is contained in:
@@ -87,4 +87,32 @@ creates a new stack
|
||||
|
||||
## OP_POP_SCOPE
|
||||
|
||||
pops the top scope off the current
|
||||
pops the top scope off the current
|
||||
|
||||
## OP_INIT_ARGS
|
||||
|
||||
initialises the arguments buffer with a fixed number of objects on the current state
|
||||
|
||||
1. the number of objects for the arguments buffer
|
||||
|
||||
## OP_INSERT_ARG
|
||||
|
||||
1. the register to take the object from. (*)
|
||||
1. index of the argument in the arguments buffer to write the object from the register into.
|
||||
|
||||
## OP_RESET_ARGS
|
||||
|
||||
resets the arguments buffer to NULL
|
||||
|
||||
## OP_CALL
|
||||
|
||||
call a function with args
|
||||
|
||||
1. the register containing the function to call. (*)
|
||||
|
||||
## OP_SWAP_REGISTERS
|
||||
|
||||
swap the contents in two registers
|
||||
|
||||
1. register a (*)
|
||||
2. register b (*)
|
||||
27
src/translator/call/call.c
Normal file
27
src/translator/call/call.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 William Bell
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
#include "call.h"
|
||||
|
||||
size_t translate_parsed_call(Translated *translated, ParsedCall* call, ArErr *err) {
|
||||
set_registers(translated, 1);
|
||||
size_t first = push_instruction_byte(translated, OP_INIT_ARGS);
|
||||
push_instruction_code(translated, call->args.size);
|
||||
for (size_t i = 0; i < call->args.size; i++) {
|
||||
translate_parsed(translated, darray_get(&call->args, i), err);
|
||||
if (err->exists)
|
||||
return first;
|
||||
push_instruction_byte(translated, OP_INSERT_ARG);
|
||||
push_instruction_byte(translated, 0);
|
||||
push_instruction_code(translated, i);
|
||||
}
|
||||
translate_parsed(translated, call->to_call, err);
|
||||
if (err->exists)
|
||||
return first;
|
||||
push_instruction_byte(translated, OP_CALL);
|
||||
push_instruction_byte(translated, 0);
|
||||
push_instruction_byte(translated, OP_RESET_ARGS);
|
||||
return first;
|
||||
}
|
||||
15
src/translator/call/call.h
Normal file
15
src/translator/call/call.h
Normal file
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 William Bell
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#ifndef BYTECODE_CALL_H
|
||||
#define BYTECODE_CALL_H
|
||||
#include "../../parser/assignable/call/call.h"
|
||||
#include "../translator.h"
|
||||
#include <stddef.h>
|
||||
|
||||
size_t translate_parsed_call(Translated *translated, ParsedCall* call, ArErr *err);
|
||||
|
||||
#endif
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "../hash_data/hash_data.h"
|
||||
#include "../hashmap/hashmap.h"
|
||||
#include "declaration/declaration.h"
|
||||
#include "call/call.h"
|
||||
#include "dowrap/dowrap.h"
|
||||
#include "function/function.h"
|
||||
#include "identifier/identifier.h"
|
||||
@@ -152,6 +153,8 @@ size_t translate_parsed(Translated *translated, ParsedValue *parsedValue, ArErr*
|
||||
return translate_parsed_dowrap(translated, (DArray *)parsedValue->data, err);
|
||||
case AST_RETURN:
|
||||
return translate_parsed_return(translated, (ParsedReturn *)parsedValue->data, err);
|
||||
case AST_CALL:
|
||||
return translate_parsed_call(translated, (ParsedCall*)parsedValue->data, err);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER, OP_BOOL, OP_JUMP_IF_FALSE, OP_JUMP, OP_NEW_SCOPE, OP_POP_SCOPE } OperationType;
|
||||
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER, OP_BOOL, OP_JUMP_IF_FALSE, OP_JUMP, OP_NEW_SCOPE, OP_POP_SCOPE, OP_INIT_ARGS, OP_INSERT_ARG, OP_RESET_ARGS, OP_CALL } OperationType;
|
||||
typedef enum { TYPE_OP_STRING, TYPE_OP_NUMBER } types;
|
||||
|
||||
typedef struct {
|
||||
|
||||
Reference in New Issue
Block a user