write bytecode debugging script and start working on functions
This commit is contained in:
30
src/translator/bytecode_spec.md
Normal file
30
src/translator/bytecode_spec.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Bytecode Specification
|
||||
|
||||
## OP_LOAD_CONST
|
||||
|
||||
loads and initialises a value from the constant buffer into the provided register.
|
||||
|
||||
this operation 4 operands.
|
||||
|
||||
1. the register to write to.
|
||||
2. the type of data from the constant buffer.
|
||||
3. the length of the data in the constant buffer.
|
||||
4. the offset in the constant buffer.
|
||||
|
||||
## OP_DECLARE
|
||||
|
||||
initilises a variable on the current scope with a given value. errors if the variable is already initilises on the current scope.
|
||||
|
||||
this operation takes 3 operands.
|
||||
|
||||
1. the length of the variable name.
|
||||
2. the offset in the constant buffer of the variable name.
|
||||
3. the register of the given value
|
||||
|
||||
## OP_LOAD_NULL
|
||||
|
||||
sets a given register to null.
|
||||
|
||||
this operation takes 1 operand.
|
||||
|
||||
1. the register to set to null.
|
||||
@@ -1,33 +1,29 @@
|
||||
#include "../translator.h"
|
||||
#include "declaration.h"
|
||||
#include "../../parser/declaration/declaration.h"
|
||||
#include "../translator.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
size_t translate_parsed_declaration(Translated *translated,
|
||||
ParsedValue *parsedValue) {
|
||||
DArray *delcarations = (DArray *)parsedValue->data;
|
||||
set_registers(translated, 2);
|
||||
DArray delcarations) {
|
||||
set_registers(translated, 1);
|
||||
size_t first = 0;
|
||||
for (size_t i = 0; i < delcarations->size; i++) {
|
||||
for (size_t i = 0; i < delcarations.size; i++) {
|
||||
// TODO: add function delclaration
|
||||
ParsedSingleDeclaration*singleDeclaration = darray_get(delcarations, i);
|
||||
ParsedSingleDeclaration *singleDeclaration = darray_get(&delcarations, i);
|
||||
size_t temp = translate_parsed(translated, singleDeclaration->from);
|
||||
if (i==0) first = temp;
|
||||
if (i == 0)
|
||||
first = temp;
|
||||
size_t length = strlen(singleDeclaration->name);
|
||||
size_t offset = arena_push(&translated->constants, singleDeclaration->name, length);
|
||||
|
||||
push_instruction_code(translated, OP_LOAD_CONST);
|
||||
push_instruction_code(translated, 1);
|
||||
push_instruction_code(translated, TYPE_OP_STRING);
|
||||
push_instruction_code(translated,length);
|
||||
push_instruction_code(translated, offset);
|
||||
size_t offset =
|
||||
arena_push(&translated->constants, singleDeclaration->name, length);
|
||||
|
||||
push_instruction_code(translated, OP_DECLARE);
|
||||
push_instruction_code(translated, length);
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated, 0);
|
||||
push_instruction_code(translated, 1);
|
||||
}
|
||||
if (delcarations->size != 1) {
|
||||
if (delcarations.size != 1) {
|
||||
push_instruction_code(translated, OP_LOAD_NULL);
|
||||
push_instruction_code(translated, 0);
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
#define BYTECODE_DECLARATION_H
|
||||
#include "../translator.h"
|
||||
|
||||
size_t translate_parsed_string(Translated *translated, ParsedValue *parsedValue);
|
||||
|
||||
size_t translate_parsed_declaration(Translated *translated,
|
||||
ParsedValue *parsedValue);
|
||||
DArray delcarations);
|
||||
|
||||
#endif
|
||||
9
src/translator/function/function.c
Normal file
9
src/translator/function/function.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "function.h"
|
||||
#include "../translator.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
size_t translate_parsed_function(Translated *translated,
|
||||
ParsedValue *parsedValue) {
|
||||
return 0;
|
||||
}
|
||||
8
src/translator/function/function.h
Normal file
8
src/translator/function/function.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef BYTECODE_FUNCTION_H
|
||||
#define BYTECODE_FUNCTION_H
|
||||
#include "../translator.h"
|
||||
|
||||
size_t translate_parsed_function(Translated *translated,
|
||||
ParsedValue *parsedValue);
|
||||
|
||||
#endif
|
||||
@@ -5,14 +5,13 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t translate_parsed_number(Translated *translated, ParsedValue *parsedValue) {
|
||||
char *number_str = (char*)parsedValue->data;
|
||||
size_t translate_parsed_number(Translated *translated, char *number_str, size_t to_register) {
|
||||
size_t length = strlen(number_str);
|
||||
size_t number_pos = arena_push(&translated->constants, number_str, length);
|
||||
set_registers(translated, 1);
|
||||
set_registers(translated, to_register+1);
|
||||
|
||||
size_t start = push_instruction_code(translated, OP_LOAD_CONST);
|
||||
push_instruction_code(translated, 0);
|
||||
push_instruction_code(translated, to_register);
|
||||
|
||||
push_instruction_code(translated, TYPE_OP_NUMBER);
|
||||
push_instruction_code(translated,length);
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
#define BYTECODE_NUMBER_H
|
||||
#include "../translator.h"
|
||||
|
||||
size_t translate_parsed_number(Translated *translated, ParsedValue *parsedValue);
|
||||
size_t translate_parsed_number(Translated *translated, char *number_str, size_t to_register);
|
||||
|
||||
#endif
|
||||
@@ -1,18 +1,16 @@
|
||||
#include "../translator.h"
|
||||
#include "../../parser/string/string.h"
|
||||
#include "string.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t translate_parsed_string(Translated *translated, ParsedValue *parsedValue) {
|
||||
ParsedString *parsedString = (ParsedString*)parsedValue->data;
|
||||
size_t string_pos = arena_push(&translated->constants, parsedString->string, parsedString->length);
|
||||
set_registers(translated, 1);
|
||||
size_t translate_parsed_string(Translated *translated, ParsedString parsedString, size_t to_register) {
|
||||
size_t string_pos = arena_push(&translated->constants, parsedString.string, parsedString.length);
|
||||
set_registers(translated, to_register+1);
|
||||
size_t start = push_instruction_code(translated, OP_LOAD_CONST);
|
||||
push_instruction_code(translated, 0);
|
||||
push_instruction_code(translated, to_register);
|
||||
push_instruction_code(translated, TYPE_OP_STRING);
|
||||
push_instruction_code(translated,parsedString->length);
|
||||
push_instruction_code(translated,parsedString.length);
|
||||
push_instruction_code(translated, string_pos);
|
||||
return start;
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
#ifndef BYTECODE_STRING_H
|
||||
#define BYTECODE_STRING_H
|
||||
#include "../translator.h"
|
||||
#include "../../parser/string/string.h"
|
||||
|
||||
size_t translate_parsed_string(Translated *translated, ParsedValue *parsedValue);
|
||||
size_t translate_parsed_string(Translated *translated, ParsedString parsedString, size_t to_register);
|
||||
|
||||
#endif
|
||||
@@ -82,11 +82,11 @@ 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, parsedValue);
|
||||
return translate_parsed_string(translated, *((ParsedString*)parsedValue->data), 0);
|
||||
case AST_DECLARATION:
|
||||
return translate_parsed_declaration(translated, parsedValue);
|
||||
return translate_parsed_declaration(translated, *((DArray*)parsedValue->data));
|
||||
case AST_NUMBER:
|
||||
return translate_parsed_number(translated, parsedValue);
|
||||
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);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum { OP_LOAD_CONST = 255, OP_DECLARE, OP_LOAD_NULL, OP_JUMP } OperationType;
|
||||
typedef enum { OP_LOAD_CONST = 255, OP_DECLARE, OP_LOAD_NULL } OperationType;
|
||||
typedef enum { TYPE_OP_STRING = 255, TYPE_OP_NUMBER } types;
|
||||
|
||||
typedef struct {
|
||||
|
||||
Reference in New Issue
Block a user