add functions to bytecode and continuing working on runtime objects
This commit is contained in:
@@ -28,3 +28,16 @@ sets a given register to null.
|
||||
this operation takes 1 operand.
|
||||
|
||||
1. the register to set to null.
|
||||
|
||||
## OP_LOAD_FUNCTION
|
||||
|
||||
initilises a function to a given register.
|
||||
|
||||
1. the offset of the name of the function.
|
||||
2. the length of the name of the function.
|
||||
3. the number of arguments.
|
||||
4. the offset of the name of the argument.
|
||||
5. the length of the name of the argument.
|
||||
6. instruction 4 and 5 loop for each argument.
|
||||
7. the offset of the bytecode of the function.
|
||||
8. the length of the bytecode of the function.
|
||||
|
||||
@@ -9,7 +9,6 @@ size_t translate_parsed_declaration(Translated *translated,
|
||||
set_registers(translated, 1);
|
||||
size_t first = 0;
|
||||
for (size_t i = 0; i < delcarations.size; i++) {
|
||||
// TODO: add function delclaration
|
||||
ParsedSingleDeclaration *singleDeclaration = darray_get(&delcarations, i);
|
||||
size_t temp = translate_parsed(translated, singleDeclaration->from);
|
||||
if (i == 0)
|
||||
|
||||
@@ -4,6 +4,32 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
size_t translate_parsed_function(Translated *translated,
|
||||
ParsedValue *parsedValue) {
|
||||
return 0;
|
||||
ParsedFunction *parsedFunction) {
|
||||
DArray temp_bytecode;
|
||||
darray_init(&temp_bytecode, sizeof(uint64_t));
|
||||
DArray main_bytecode = translated->bytecode;
|
||||
translated->bytecode = temp_bytecode;
|
||||
translate_parsed(translated, parsedFunction->body);
|
||||
size_t function_bytecode_offset =
|
||||
arena_push(&translated->constants, translated->bytecode.data,
|
||||
translated->bytecode.size);
|
||||
size_t function_bytecode_length = translated->bytecode.size;
|
||||
translated->bytecode = main_bytecode;
|
||||
darray_free(&temp_bytecode, NULL);
|
||||
size_t start = push_instruction_code(translated, OP_LOAD_FUNCTION);
|
||||
size_t offset = arena_push(&translated->constants, parsedFunction->name,
|
||||
strlen(parsedFunction->name));
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated, strlen(parsedFunction->name));
|
||||
push_instruction_code(translated, parsedFunction->parameters.size);
|
||||
for (size_t i = 0; i < parsedFunction->parameters.size; i++) {
|
||||
char **parameter_name = darray_get(&parsedFunction->parameters, i);
|
||||
offset = arena_push(&translated->constants, *parameter_name,
|
||||
strlen(*parameter_name));
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated, strlen(*parameter_name));
|
||||
}
|
||||
push_instruction_code(translated, function_bytecode_offset);
|
||||
push_instruction_code(translated, function_bytecode_length);
|
||||
return start;
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
#ifndef BYTECODE_FUNCTION_H
|
||||
#define BYTECODE_FUNCTION_H
|
||||
#include "../translator.h"
|
||||
#include "../../parser/function/function.h"
|
||||
|
||||
size_t translate_parsed_function(Translated *translated,
|
||||
ParsedValue *parsedValue);
|
||||
ParsedFunction *parsedFunction);
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "translator.h"
|
||||
#include "declaration/declaration.h"
|
||||
#include "function/function.h"
|
||||
#include "number/number.h"
|
||||
#include "string/string.h"
|
||||
#include <stddef.h>
|
||||
@@ -81,16 +82,21 @@ void set_registers(Translated *translator, size_t count) {
|
||||
size_t translate_parsed(Translated *translated, ParsedValue *parsedValue) {
|
||||
switch (parsedValue->type) {
|
||||
case AST_STRING:
|
||||
return translate_parsed_string(translated, *((ParsedString*)parsedValue->data));
|
||||
return translate_parsed_string(translated,
|
||||
*((ParsedString *)parsedValue->data));
|
||||
case AST_DECLARATION:
|
||||
return translate_parsed_declaration(translated, *((DArray*)parsedValue->data));
|
||||
return translate_parsed_declaration(translated,
|
||||
*((DArray *)parsedValue->data));
|
||||
case AST_NUMBER:
|
||||
return translate_parsed_number(translated, (char*)parsedValue->data, 0);
|
||||
return translate_parsed_number(translated, (char *)parsedValue->data, 0);
|
||||
case AST_NULL:
|
||||
set_registers(translated, 1);
|
||||
size_t output = push_instruction_code(translated, OP_LOAD_NULL);
|
||||
push_instruction_code(translated, 0);
|
||||
return output;
|
||||
case AST_FUNCTION:
|
||||
return translate_parsed_function(translated,
|
||||
(ParsedFunction *)parsedValue->data);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum { OP_LOAD_CONST = 255, OP_DECLARE, OP_LOAD_NULL } OperationType;
|
||||
typedef enum { OP_LOAD_CONST = 255, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION } OperationType;
|
||||
typedef enum { TYPE_OP_STRING = 255, TYPE_OP_NUMBER } types;
|
||||
|
||||
typedef struct {
|
||||
|
||||
Reference in New Issue
Block a user