start supporting identifiers in bytecode

This commit is contained in:
2025-07-06 03:19:30 +01:00
parent 886599c9c5
commit 5c0ced5e45
8 changed files with 33 additions and 5 deletions

View File

@@ -86,15 +86,13 @@ int load_cache(Translated *translated_dest, char *joined_paths, uint64_t hash) {
FILE *bytecode_file = fopen(joined_paths, "rb");
if (!bytecode_file)
return 1;
char file_identifier_from_cache[sizeof(FILE_IDENTIFIER)];
file_identifier_from_cache[strlen(FILE_IDENTIFIER)] = '\0';
char file_identifier_from_cache[sizeof(FILE_IDENTIFIER)] = {0};
if (fread(&file_identifier_from_cache, 1,
sizeof(file_identifier_from_cache) - 1,
bytecode_file) != sizeof(file_identifier_from_cache) - 1 ||
memcmp(file_identifier_from_cache, FILE_IDENTIFIER,
sizeof(file_identifier_from_cache)) != 0) {
fclose(bytecode_file);
return 1;
goto FAILED;
}
uint32_t read_version;

View File

@@ -22,7 +22,9 @@ ParsedValue *parse_access(char *file, DArray *tokens, size_t *index,
ParsedValue *parsedString = parse_string(token, false);
darray_push(&parsedAccess->access, parsedString);
free(parsedString);
parsedAccess->access_fields = true;
} else {
parsedAccess->access_fields = false;
while (true) {
skip_newlines_and_indents(tokens, index);
error_if_finished(file, tokens, index);

View File

@@ -5,6 +5,7 @@
typedef struct {
ParsedValue to_access;
bool access_fields;
DArray access;
} ParsedAccess;

View File

@@ -0,0 +1,15 @@
#include "../translator.h"
#include "identifier.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
size_t translate_parsed_identifier(Translated *translated, char* identifier) {
size_t length = strlen(identifier);
size_t identifier_pos = arena_push(&translated->constants, identifier, length);
set_registers(translated, 1);
size_t start = push_instruction_byte(translated, OP_IDENTIFIER);
push_instruction_code(translated,length);
push_instruction_code(translated, identifier_pos);
return start;
}

View File

@@ -0,0 +1,8 @@
#ifndef BYTECODE_IDENTIFIER_H
#define BYTECODE_IDENTIFIER_H
#include "../translator.h"
#include "../../parser/assignable/identifier/identifier.h"
size_t translate_parsed_identifier(Translated *translated, char* identifier);
#endif

View File

@@ -3,6 +3,7 @@
#include "../hashmap/hashmap.h"
#include "declaration/declaration.h"
#include "function/function.h"
#include "identifier/identifier.h"
#include "number/number.h"
#include "string/string.h"
#include <stddef.h>
@@ -122,6 +123,8 @@ size_t translate_parsed(Translated *translated, ParsedValue *parsedValue) {
case AST_FUNCTION:
return translate_parsed_function(translated,
(ParsedFunction *)parsedValue->data);
case AST_IDENTIFIER:
return translate_parsed_identifier(translated, (char *)parsedValue->data);
}
return 0;
}

View File

@@ -8,7 +8,7 @@
#include <stddef.h>
#include <stdint.h>
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION } OperationType;
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER } OperationType;
typedef enum { TYPE_OP_STRING, TYPE_OP_NUMBER } types;
typedef struct {