start adding assignment (currently only identifier assignment works)
This commit is contained in:
51
src/translator/assignment/assignment.c
Normal file
51
src/translator/assignment/assignment.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 William Bell
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include "assignment.h"
|
||||
#include "../../hash_data/hash_data.h"
|
||||
#include "../../parser/assignable/identifier/identifier.h"
|
||||
#include "../translator.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t translate_parsed_assignment(Translated *translated,
|
||||
ParsedAssign *assignment, ArErr *err) {
|
||||
printf("bruh\n");
|
||||
set_registers(translated, 1);
|
||||
DArray *old_return_jumps = translated->return_jumps;
|
||||
translated->return_jumps = NULL;
|
||||
size_t first = translate_parsed(translated, assignment->from, err);
|
||||
if (err->exists)
|
||||
return first;
|
||||
switch (assignment->to->type) {
|
||||
case AST_IDENTIFIER:;
|
||||
ParsedIdentifier *identifier = assignment->to->data;
|
||||
size_t length = strlen(identifier->name);
|
||||
size_t offset =
|
||||
arena_push(&translated->constants, identifier->name, length);
|
||||
|
||||
push_instruction_byte(translated, OP_SOURCE_LOCATION);
|
||||
push_instruction_code(translated, identifier->line);
|
||||
push_instruction_code(translated, identifier->column);
|
||||
push_instruction_code(translated, length);
|
||||
|
||||
push_instruction_byte(translated, OP_ASSIGN);
|
||||
push_instruction_code(translated, length);
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated,
|
||||
siphash64_bytes(identifier->name, length,
|
||||
siphash_key_fixed_for_translator));
|
||||
push_instruction_byte(translated, 0);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "panic: freeing NULL pointer\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
translated->return_jumps = old_return_jumps;
|
||||
return first;
|
||||
}
|
||||
15
src/translator/assignment/assignment.h
Normal file
15
src/translator/assignment/assignment.h
Normal file
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 William Bell
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#ifndef BYTECODE_ASSIGNMENT_H
|
||||
#define BYTECODE_ASSIGNMENT_H
|
||||
#include "../translator.h"
|
||||
#include "../../parser/assignable/assign/assign.h"
|
||||
|
||||
size_t translate_parsed_assignment(Translated *translated,
|
||||
ParsedAssign *assignment, ArErr *err);
|
||||
|
||||
#endif
|
||||
@@ -29,6 +29,17 @@ this operation takes 3 operands.
|
||||
1. the fixed hash of the variable name.
|
||||
1. the register of the given value (*)
|
||||
|
||||
## OP_ASSIGN
|
||||
|
||||
assigns to a variable in the stack. if the variable doesnt exist on the stack, it is automatically declared on the current scope.
|
||||
|
||||
this operation takes 3 operands.
|
||||
|
||||
1. the length of the variable name.
|
||||
1. the offset in the constant buffer of the variable name.
|
||||
1. the fixed hash of the variable name.
|
||||
1. the register of the given value (*)
|
||||
|
||||
## OP_LOAD_NULL
|
||||
|
||||
sets a given register to null.
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "access/access.h"
|
||||
#include "call/call.h"
|
||||
#include "declaration/declaration.h"
|
||||
#include "assignment/assignment.h"
|
||||
#include "dowrap/dowrap.h"
|
||||
#include "function/function.h"
|
||||
#include "identifier/identifier.h"
|
||||
@@ -165,6 +166,9 @@ size_t translate_parsed(Translated *translated, ParsedValue *parsedValue,
|
||||
case AST_OPERATION:
|
||||
return translate_operation(translated, (ParsedOperation *)parsedValue->data,
|
||||
err);
|
||||
case AST_ASSIGN:
|
||||
return translate_parsed_assignment(translated,
|
||||
(ParsedAssign *)parsedValue->data, err);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,8 @@ typedef enum {
|
||||
OP_LOAD_ADDITION_FUNCTION,
|
||||
OP_LOAD_SUBTRACTION_FUNCTION,
|
||||
OP_LOAD_MULTIPLY_FUNCTION,
|
||||
OP_LOAD_DIVISION_FUNCTION
|
||||
OP_LOAD_DIVISION_FUNCTION,
|
||||
OP_ASSIGN
|
||||
} OperationType;
|
||||
|
||||
void arena_resize(ConstantArena *arena, size_t new_size);
|
||||
|
||||
Reference in New Issue
Block a user