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

@@ -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 {