add if statement and declaration
This commit is contained in:
@@ -21,7 +21,9 @@ 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 (*)
|
||||
3. the fixed hash of the variable name.
|
||||
4. the register of the given value (*)
|
||||
5. the index of the source location.
|
||||
|
||||
## OP_LOAD_NULL
|
||||
|
||||
@@ -50,4 +52,33 @@ initilises a function to a given register.
|
||||
|
||||
1. the length of the identifer.
|
||||
2. the offset of the identifier.
|
||||
3. the index of the source location.
|
||||
3. the fixed hash of the variable name.
|
||||
4. the index of the source location.
|
||||
|
||||
## OP_BOOL
|
||||
|
||||
converts a value in a given register into true or false depending on the result from \_\_bool\_\_
|
||||
|
||||
1. the register to read and write to. (*)
|
||||
|
||||
## OP_JUMP_IF_FALSE
|
||||
|
||||
jumps when a the value in the given register is false.
|
||||
|
||||
1. the register to read. (*)
|
||||
1. the index to jump to.
|
||||
|
||||
## OP_JUMP_IF_FALSE
|
||||
|
||||
jumps unconditionally to an index.
|
||||
|
||||
1. the index to jump to.
|
||||
|
||||
|
||||
## OP_NEW_SCOPE
|
||||
|
||||
creates a new stack
|
||||
|
||||
## OP_POP_SCOPE
|
||||
|
||||
pops the top scope off the current
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "declaration.h"
|
||||
#include "../../parser/declaration/declaration.h"
|
||||
#include "../translator.h"
|
||||
#include "../../hash_data/hash_data.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -20,11 +21,12 @@ size_t translate_parsed_declaration(Translated *translated,
|
||||
push_instruction_byte(translated, OP_DECLARE);
|
||||
push_instruction_code(translated, length);
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated, siphash64_bytes(singleDeclaration->name, length, siphash_key_fixed_for_translator));
|
||||
push_instruction_byte(translated, 0);
|
||||
}
|
||||
if (delcarations.size != 1) {
|
||||
push_instruction_byte(translated, OP_LOAD_NULL);
|
||||
push_instruction_byte(translated, 0);
|
||||
SourceLocation source_locations = {singleDeclaration->line,
|
||||
singleDeclaration->column, length};
|
||||
push_instruction_code(translated, translated->source_locations.size);
|
||||
darray_push(&translated->source_locations, &source_locations);
|
||||
}
|
||||
return first;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ size_t translate_parsed_function(Translated *translated,
|
||||
DArray main_bytecode = translated->bytecode;
|
||||
DArray _temp_bytecode;
|
||||
darray_init(&_temp_bytecode, sizeof(uint8_t));
|
||||
set_registers(translated, 1);
|
||||
translated->bytecode = _temp_bytecode;
|
||||
translate_parsed(translated, parsedFunction->body);
|
||||
size_t function_bytecode_offset =
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../../hash_data/hash_data.h"
|
||||
|
||||
size_t translate_parsed_identifier(Translated *translated, ParsedIdentifier *parsedIdentifier) {
|
||||
size_t length = strlen(parsedIdentifier->name);
|
||||
@@ -11,6 +12,7 @@ size_t translate_parsed_identifier(Translated *translated, ParsedIdentifier *par
|
||||
size_t start = push_instruction_byte(translated, OP_IDENTIFIER);
|
||||
push_instruction_code(translated,length);
|
||||
push_instruction_code(translated, identifier_pos);
|
||||
push_instruction_code(translated, siphash64_bytes(parsedIdentifier->name, length, siphash_key_fixed_for_translator));
|
||||
SourceLocation source_locations = {
|
||||
parsedIdentifier->line,
|
||||
parsedIdentifier->column,
|
||||
|
||||
45
src/translator/if/if.c
Normal file
45
src/translator/if/if.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "../../parser/if/if.h"
|
||||
#include "../translator.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
size_t translate_parsed_if(Translated *translated, DArray *parsedIf) {
|
||||
size_t *jump_after_body_positions =
|
||||
checked_malloc(parsedIf->size * sizeof(size_t));
|
||||
|
||||
size_t first = translated->bytecode.size;
|
||||
|
||||
set_registers(translated, 1);
|
||||
|
||||
for (uint64_t i = 0; i < parsedIf->size; i++) {
|
||||
ParsedConditional *condition = darray_get(parsedIf, i);
|
||||
push_instruction_byte(translated, OP_NEW_SCOPE);
|
||||
if (condition->condition) {
|
||||
translate_parsed(translated, condition->condition);
|
||||
push_instruction_byte(translated, OP_BOOL);
|
||||
push_instruction_byte(translated, 0);
|
||||
push_instruction_byte(translated, OP_JUMP_IF_FALSE);
|
||||
push_instruction_byte(translated, 0);
|
||||
uint64_t last_jump_index = push_instruction_code(translated, 0);
|
||||
translate_parsed(translated, condition->content);
|
||||
push_instruction_byte(translated, OP_POP_SCOPE);
|
||||
push_instruction_byte(translated, OP_JUMP);
|
||||
jump_after_body_positions[i] = push_instruction_code(translated, 0);
|
||||
set_instruction_code(translated, last_jump_index,
|
||||
translated->bytecode.size);
|
||||
push_instruction_byte(translated, OP_POP_SCOPE);
|
||||
} else {
|
||||
translate_parsed(translated, condition->content);
|
||||
push_instruction_byte(translated, OP_POP_SCOPE);
|
||||
jump_after_body_positions[i] = 0;
|
||||
}
|
||||
}
|
||||
for (uint64_t i = 0; i < parsedIf->size; i++) {
|
||||
if (jump_after_body_positions[i])
|
||||
set_instruction_code(translated, jump_after_body_positions[i],
|
||||
translated->bytecode.size);
|
||||
}
|
||||
free(jump_after_body_positions);
|
||||
return first;
|
||||
}
|
||||
7
src/translator/if/if.h
Normal file
7
src/translator/if/if.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef TRANSLATE_iF_H
|
||||
#define TRANSLATE_iF_H
|
||||
#include "../translator.h"
|
||||
|
||||
size_t translate_parsed_if(Translated *translated, DArray *parsedIf);
|
||||
|
||||
#endif // TRANSLATE_iF_H
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "declaration/declaration.h"
|
||||
#include "function/function.h"
|
||||
#include "identifier/identifier.h"
|
||||
#include "if/if.h"
|
||||
#include "number/number.h"
|
||||
#include "string/string.h"
|
||||
#include <stddef.h>
|
||||
@@ -91,6 +92,10 @@ size_t push_instruction_byte(Translated *translator, uint8_t byte) {
|
||||
return offset;
|
||||
}
|
||||
|
||||
void set_instruction_byte(Translated *translator, size_t index, uint8_t byte) {
|
||||
memcpy(translator->bytecode.data + index, &byte, sizeof(byte));
|
||||
}
|
||||
|
||||
size_t push_instruction_code(Translated *translator, uint64_t code) {
|
||||
size_t offset = translator->bytecode.size;
|
||||
uint8_t bytes[8];
|
||||
@@ -101,6 +106,10 @@ size_t push_instruction_code(Translated *translator, uint64_t code) {
|
||||
return offset;
|
||||
}
|
||||
|
||||
void set_instruction_code(Translated *translator, size_t index, uint64_t code) {
|
||||
memcpy(translator->bytecode.data + index, &code, sizeof(code));
|
||||
}
|
||||
|
||||
void set_registers(Translated *translator, uint8_t count) {
|
||||
if (count > translator->registerCount)
|
||||
translator->registerCount = count;
|
||||
@@ -125,7 +134,10 @@ size_t translate_parsed(Translated *translated, ParsedValue *parsedValue) {
|
||||
return translate_parsed_function(translated,
|
||||
(ParsedFunction *)parsedValue->data);
|
||||
case AST_IDENTIFIER:
|
||||
return translate_parsed_identifier(translated, (ParsedIdentifier *)parsedValue->data);
|
||||
return translate_parsed_identifier(translated,
|
||||
(ParsedIdentifier *)parsedValue->data);
|
||||
case AST_IF:
|
||||
return translate_parsed_if(translated, (DArray *)parsedValue->data);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER } 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 } OperationType;
|
||||
typedef enum { TYPE_OP_STRING, TYPE_OP_NUMBER } types;
|
||||
|
||||
typedef struct {
|
||||
@@ -39,8 +39,12 @@ size_t arena_push(ConstantArena *arena, const void *data, size_t length);
|
||||
|
||||
size_t push_instruction_byte(Translated *translator, uint8_t byte);
|
||||
|
||||
void set_instruction_byte(Translated *translator, size_t index, uint8_t byte);
|
||||
|
||||
size_t push_instruction_code(Translated *translator, uint64_t code);
|
||||
|
||||
void set_instruction_code(Translated *translator, size_t index, uint64_t code);
|
||||
|
||||
void set_registers(Translated *translator, uint8_t count);
|
||||
|
||||
Translated init_translator();
|
||||
|
||||
Reference in New Issue
Block a user