summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler.c19
-rwxr-xr-xtests/run.sh9
-rw-r--r--tests/test_controll.tnsl12
-rw-r--r--tests/test_method_2.tnsl2
-rw-r--r--tnslc/main.tnsl4
-rwxr-xr-xtnslc/tnslcbin0 -> 21440 bytes
-rw-r--r--tnslc/vector.tnsl59
7 files changed, 83 insertions, 22 deletions
diff --git a/compiler.c b/compiler.c
index 9d66258..8b67a1e 100644
--- a/compiler.c
+++ b/compiler.c
@@ -1027,7 +1027,7 @@ void _var_op_set_ptr(CompData *out, Variable *store, Variable *from) {
int *cur;
for (size_t i = store->ptr_chain.count - 1; i > 0; i--) {
cur = vect_get(&store->ptr_chain, i - 1);
- if (cur == PTYPE_REF) {
+ if (*cur == PTYPE_REF) {
vect_push_string(&out->text, "\tmov rdi, [rdi]\n");
} else {
break;
@@ -1054,7 +1054,7 @@ void _var_op_set_ptr(CompData *out, Variable *store, Variable *from) {
int *cur;
for (size_t i = from->ptr_chain.count - 1; i > 0; i--) {
cur = vect_get(&store->ptr_chain, i - 1);
- if (cur == PTYPE_REF) {
+ if (*cur == PTYPE_REF) {
vect_push_string(&out->text, "\tmov rsi, [rsi]\n");
} else {
break;
@@ -1544,18 +1544,23 @@ void var_op_mul(CompData *out, Variable *base, Variable *mul) {
if(base->type->name[0] == 'i') {
// Integer multiplication
if (base->location > 0 && mul->location != LOC_LITL) {
+ char *store = _var_get_store(out, base);
+ char *from = _var_get_from(out, base, mul);
+
vect_push_string(&out->text, "\timul ");
- vect_push_free_string(&out->text, _var_get_store(out, base));
+ vect_push_free_string(&out->text, store);
vect_push_string(&out->text, ", ");
- vect_push_free_string(&out->text, _var_get_from(out, base, mul));
+ vect_push_free_string(&out->text, from);
vect_push_string(&out->text, "; complete mul\n\n");
} else if (base->location > 0) {
vect_push_string(&out->text, "\tmov rcx, ");
vect_push_free_string(&out->text, int_to_str(mul->offset));
vect_push_string(&out->text, "; literal load\n");
+ char *store = _var_get_store(out, base);
+
vect_push_string(&out->text, "\timul ");
- vect_push_free_string(&out->text, _var_get_store(out, base));
+ vect_push_free_string(&out->text, store);
vect_push_string(&out->text, ", ");
vect_push_free_string(&out->text, _op_get_register(3, _var_size(base)));
vect_push_string(&out->text, "; complete mul\n\n");
@@ -4134,7 +4139,7 @@ Variable _eval_call(Scope *s, CompData *data, Vector *tokens, Function *f, Varia
Variable set = scope_mk_stmp(s, data, cur);
Variable from = _eval(s, data, tokens, pstart, pend);
// eval and set
- var_op_pure_set(data, &set, &from);
+ var_op_set(data, &set, &from);
scope_free_to(s, data, &set);
var_end(&from);
var_end(&set);
@@ -4186,7 +4191,7 @@ Variable _eval_call(Scope *s, CompData *data, Vector *tokens, Function *f, Varia
Variable set = scope_mk_stmp(s, data, cur);
// eval and set
Variable from = _eval(s, data, tokens, pstart, pend);
- var_op_pure_set(data, &set, &from);
+ var_op_set(data, &set, &from);
// cleanup
scope_free_to(s, data, &set);
var_end(&from);
diff --git a/tests/run.sh b/tests/run.sh
new file mode 100755
index 0000000..136dac1
--- /dev/null
+++ b/tests/run.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+mkdir -p ./out/artifact
+filename=$1
+filename="${filename%.*}"
+../ctc $1 out/artifact/$filename.asm
+nasm -f elf64 -o ./out/artifact/$filename.o ./out/artifact/$filename.asm
+gcc -o ./out/$filename ./out/artifact/$filename.o
+
diff --git a/tests/test_controll.tnsl b/tests/test_controll.tnsl
deleted file mode 100644
index 47ff241..0000000
--- a/tests/test_controll.tnsl
+++ /dev/null
@@ -1,12 +0,0 @@
-/; main [int]
-
- /; if (false)
- return 1
- ;; else if (1 == 0)
- return 2
- ;; else
- return 69
- ;/
-
- return 0
-;/
diff --git a/tests/test_method_2.tnsl b/tests/test_method_2.tnsl
index 899cdcb..56b3419 100644
--- a/tests/test_method_2.tnsl
+++ b/tests/test_method_2.tnsl
@@ -12,6 +12,6 @@ struct Vector {
/; main [int]
Vector a
- a.init(1)
+ a.init(69)
return a._elsz
;/
diff --git a/tnslc/main.tnsl b/tnslc/main.tnsl
index ac56836..c7e8c95 100644
--- a/tnslc/main.tnsl
+++ b/tnslc/main.tnsl
@@ -5,6 +5,10 @@
Vector a
a.init(1)
+
+
+
+ a.end()
return 0
;/
diff --git a/tnslc/tnslc b/tnslc/tnslc
new file mode 100755
index 0000000..e8d4c46
--- /dev/null
+++ b/tnslc/tnslc
Binary files differ
diff --git a/tnslc/vector.tnsl b/tnslc/vector.tnsl
index 1377938..9b89081 100644
--- a/tnslc/vector.tnsl
+++ b/tnslc/vector.tnsl
@@ -1,3 +1,4 @@
+# Define vector struct
struct Vector {
~void data,
int
@@ -6,21 +7,75 @@ struct Vector {
_elsz
}
+# Consts used in impl
int VECT_DEFAULT_SIZE = 4
+int VECT_MAX_GROW = 128
+# Methods on the struct
/; method Vector
+ # Initialize a new vector with elements
+ # 'elsz' bytes long
/; init (int elsz)
- self._elsz = elsz
self.size = VECT_DEFAULT_SIZE
- self.data = _alloc(elsz * self.size)
+ self._elsz = elsz
self.count = 0
+ self.data = _alloc(elsz * VECT_DEFAULT_SIZE)
+ ;/
+
+ # Grow the size of the vector by 'size' elements
+ /; _grow (int size)
+ /; if (size > VECT_MAX_GROW)
+ size = VECT_MAX_GROW
+ ;/
+
+ self.size = self.size + size
+ self.data = _realloc(self.data, self.size * self._elsz)
+ ;/
+
+ # Shrink the size of the vector by 'size' elements
+ /; _shrink (int size)
+ /; if (self.size - size < 0)
+ self.size = 1
+ ;; else
+ self.size = self.size - size
+ ;/
+
+ /; if (self.count < self.size)
+ self.count = self.size
+ ;/
+
+ self.data = _realloc(self.data, self.size * self._elsz)
+ ;/
+
+ # Push an element onto the end of the vector
+ /; push (~void data)
+ /; if (count == size - 1)
+ self._grow(self.size)
+ ;/
+
+ /; loop (int i = 0; i < self._elsz) [i++]
+ (self.data + i)` = (data + i)`
+ ;/
+
+ self.count++
+ ;/
+
+ # Pop an element off of the end of the vector
+ /; pop
+ self.count--
+
+ /; if (self.count < self.size / 2)
+ self._shrink(self.size / 3)
+ ;/
;/
+ # Get a pointer to the start of an element in the vector
/; get (int index) [~void]
return self.data
;/
+ # Free all memory associated with the vector
/; end
_delete(self.data)
self.size = 0