aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bytecode.c19
-rw-r--r--src/parser.c4
-rw-r--r--src/std/buffer.c42
3 files changed, 64 insertions, 1 deletions
diff --git a/src/bytecode.c b/src/bytecode.c
new file mode 100644
index 0000000..3e9a6e8
--- /dev/null
+++ b/src/bytecode.c
@@ -0,0 +1,19 @@
+#include "../include/bytecode.h"
+#include <assert.h>
+
+void tsl_bytecode_writer_init(TslBytecodeWriter *self) {
+ assert(sizeof(TslInstruction) == 4);
+ tsl_buffer_init(&self->buffer);
+}
+
+void tsl_bytecode_writer_deinit(TslBytecodeWriter *self) {
+ tsl_buffer_deinit(&self->buffer);
+}
+
+int tsl_bytecode_writer_assign(TslBytecodeWriter *self, TslRegister reg, double value) {
+ TslInstruction instruction;
+ instruction.opcode = TSL_OPCODE_ASSIGN;
+ instruction.type2.dst_reg = reg;
+ instruction.type2.value_index = value;
+ return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction));
+}
diff --git a/src/parser.c b/src/parser.c
index 1324305..176be63 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -1,8 +1,10 @@
#include "../include/parser.h"
+#include "../include/bytecode.h"
#include <stdio.h>
typedef struct {
TslTokenizer tokenizer;
+ TslBytecodeWriter bytecode_writer;
} TslParser;
static int tsl_parser_parse_rhs(TslParser *self);
@@ -10,6 +12,7 @@ static int tsl_parser_parse_expressions(TslParser *self, TslToken end_token);
static void tsl_parser_init(TslParser *self, const char *code, size_t code_size) {
tsl_tokenizer_init(&self->tokenizer, code, code_size);
+ tsl_bytecode_writer_init(&self->bytecode_writer);
}
static int tsl_parser_parse_map(TslParser *self) {
@@ -263,7 +266,6 @@ int tsl_parser_parse_expressions(TslParser *self, TslToken end_token) {
return 0;
}
-/* EXPRS */
int tsl_parse(const char *code, size_t code_size) {
TslParser parser;
tsl_parser_init(&parser, code, code_size);
diff --git a/src/std/buffer.c b/src/std/buffer.c
new file mode 100644
index 0000000..3fc5184
--- /dev/null
+++ b/src/std/buffer.c
@@ -0,0 +1,42 @@
+#include "../../include/std/buffer.h"
+#include <stdlib.h>
+#include <string.h>
+
+void tsl_buffer_init(TslBuffer *self) {
+ self->data = NULL;
+ self->size = 0;
+ self->capacity = 0;
+}
+
+void tsl_buffer_deinit(TslBuffer *self) {
+ free(self->data);
+}
+
+static int tsl_buffer_ensure_capacity(TslBuffer *self, size_t new_size) {
+ void *new_ptr;
+ if(new_size <= self->capacity)
+ return 1;
+
+ if(self->capacity != 0) {
+ size_t new_capacity = self->capacity;
+ while(new_capacity < new_size) {
+ new_capacity *= 2;
+ }
+ new_size = new_capacity;
+ }
+ new_ptr = realloc(self->data, new_size);
+ if(!new_ptr)
+ return 0;
+
+ self->data = new_ptr;
+ self->capacity = new_size;
+ return 1;
+}
+
+int tsl_buffer_append(TslBuffer *self, void *data, size_t size) {
+ if(!tsl_buffer_ensure_capacity(self, self->size + size))
+ return 1;
+ memcpy((char*)self->data + self->size, data, size);
+ self->size += size;
+ return 0;
+}