nitron

commit 177929bd033822f0cc40237a4cfee39229daed2a

Author: mesyeti <mesyeti@mesyeti.uk>

StringBuilder + compiler fix

 basic-std/Makefile | 2 
 basic-std/examples/stringBuilder.bas | 17 +++++++++++++++
 basic-std/examples/stringView.bas | 13 +++++++++++
 basic-std/std/stringBuilder.bas | 12 +++++++---
 basic-std/std/stringView.bas | 2 
 basic-std/test.bas | 12 +++++++---
 basic/source/frontend/c89.c | 2 
 basic/source/import.c | 33 ++++++++++++++++++++++++++++-
 basic/source/import.h | 2 +
 basic/source/parser.c | 3 ++


diff --git a/basic/source/frontend/c89.c b/basic/source/frontend/c89.c
index 06ab49faf963955502a6cc27a8fae157d1247b84..ea72761a00f4075850d86229296ce8f7de90d326 100644
--- a/basic/source/frontend/c89.c
+++ b/basic/source/frontend/c89.c
@@ -26,7 +26,7 @@
 	char cmd[512];
 
 	snprintf(
-		cmd, 512, "gcc %s %s.c -o %s -fno-builtin",
+		cmd, 512, "cc %s %s.c -o %s -fno-builtin -g",
 		options.obj? "-c" : "", options.out, options.out
 	);
 




diff --git a/basic/source/import.c b/basic/source/import.c
index 27f5a8bd2e8be5dc46d9442b968de533aa28a604..77fbd1ff2610935d5208f60246554ef0fb01a027 100644
--- a/basic/source/import.c
+++ b/basic/source/import.c
@@ -1,3 +1,4 @@
+#include <string.h>
 #include "mem.h"
 #include "util.h"
 #include "state.h"
@@ -5,16 +6,23 @@ #include "import.h"
 
 static char** paths;
 static size_t pathsLen;
+static char** imported;
+static size_t importedLen;
 
 void Import_Init(void) {
-	paths    = NULL;
-	pathsLen = 0;
+	paths       = NULL;
+	pathsLen    = 0;
+	imported    = NULL;
+	importedLen = 0;
 }
 
 void Import_Free(void) {
 	if (paths) {
 		free(paths);
 	}
+	if (imported) {
+		free(imported);
+	}
 }
 
 void Import_AddPath(const char* path) {
@@ -24,6 +32,21 @@ 	paths               = SafeRealloc(paths, pathsLen * sizeof(char*));
 	paths[pathsLen - 1] = NewString(path);
 }
 
+void Import_AddImported(const char* mod) {
+	++ importedLen;
+
+	imported                  = SafeRealloc(imported, importedLen * sizeof(char*));
+	imported[importedLen - 1] = NewString(mod);
+}
+
+bool Import_BeenImported(const char* mod) {
+	for (size_t i = 0; i < importedLen; ++ i) {
+		if (strcmp(imported[i], mod) == 0) return true;
+	}
+
+	return false;
+}
+
 const char* Import_Search(const char* name) {
 	for (size_t i = 0; i < pathsLen; ++ i) {
 		// TODO: improve using some string builder thingy
@@ -46,6 +69,10 @@
 void Import_Run(Node* nodes, size_t len) {
 	for (size_t i = 0; i < len; ++ i) {
 		if (nodes[i].i.type == NODE_IMPORT) {
+			if (Import_BeenImported(nodes[i].import.name)) {
+				continue;
+			}
+
 			const char* path = Import_Search(nodes[i].import.name);
 
 			if (!path) {
@@ -54,6 +81,8 @@ 			}
 			if (!State_ImportFile(path)) {
 				PrintError(nodes[i].i.err, "Failed to import '%s'", nodes[i].import.name);
 			}
+
+			Import_AddImported(nodes[i].import.name);
 		}
 	}
 }




diff --git a/basic/source/import.h b/basic/source/import.h
index 28b8f2ec4160bb17d64470699ab60b1b8f09f2e1..aa296e2cc0e4acbc71e3f37bf0fadaf6536c47ed 100644
--- a/basic/source/import.h
+++ b/basic/source/import.h
@@ -7,6 +7,8 @@
 void        Import_Init(void);
 void        Import_Free(void);
 void        Import_AddPath(const char* path);
+void        Import_AddImported(const char* mod);
+bool        Import_BeenImported(const char* mod);
 const char* Import_Search(const char* name);
 void        Import_Run(Node* nodes, size_t len);
 




diff --git a/basic/source/parser.c b/basic/source/parser.c
index 54968bc0ca55a294511b4c0183103ef4d3df004a..f68afe0621cb7df79272cada18f20b54507b7502 100644
--- a/basic/source/parser.c
+++ b/basic/source/parser.c
@@ -744,6 +744,9 @@ 	if (p->tokens[p->i].type == TOKEN_UNION) {
 		ret.isUnion = true;
 		Advance(p);
 	}
+	else {
+		ret.isUnion = false;
+	}
 
 	Expect(p, TOKEN_LINE);
 	Advance(p);




diff --git a/basic-std/Makefile b/basic-std/Makefile
index 5eb68946d5d081ba2edffc0c064d161e117e2e02..e4112da3fbf4df21b0306ce1f379352fe74c02e8 100644
--- a/basic-std/Makefile
+++ b/basic-std/Makefile
@@ -9,7 +9,7 @@ ./bin:
 	mkdir -p bin
 
 bin/%.o: std/%.bas $(SRC)
-	nitron-basic --obj $< -o $@
+	nitron-basic --obj $< -o $@ -i .
 
 clean:
 	rm bin/*.o $(OUT)




diff --git a/basic-std/examples/stringBuilder.bas b/basic-std/examples/stringBuilder.bas
new file mode 100644
index 0000000000000000000000000000000000000000..c228dbd92b7ccd117fbf8e2fc8a8c90bcabfda20
--- /dev/null
+++ b/basic-std/examples/stringBuilder.bas
@@ -0,0 +1,17 @@
+'NITRON
+
+program test
+
+import std.io
+import std.stringView
+import std.stringBuilder
+
+dim sb as StringBuilder
+sb.Init()
+sb.AddNTStr("hello ")
+sb.AddNTStr("world!")
+
+dim sv as StringView
+sb.ToStringView(&sv)
+sv.PrintLn()
+sb.Free()




diff --git a/basic-std/examples/stringView.bas b/basic-std/examples/stringView.bas
new file mode 100644
index 0000000000000000000000000000000000000000..41a5412c33f1ca031c03aa550641dcfa4fdd8110
--- /dev/null
+++ b/basic-std/examples/stringView.bas
@@ -0,0 +1,13 @@
+'NITRON
+
+program test
+
+import std.io
+import std.file
+import std.heap
+import std.stringView
+
+dim sv as StringView
+sv.FromNTStr("Hello World")
+sv.Shorten(sv.FindChar(32))
+sv.PrintLn()




diff --git a/basic-std/std/stringBuilder.bas b/basic-std/std/stringBuilder.bas
index 1f9000e5f832f416c157a39c26c6001495bd5599..d68dae10192f0db7f34a00486b80e5bc59c080bc 100644
--- a/basic-std/std/stringBuilder.bas
+++ b/basic-std/std/stringBuilder.bas
@@ -1,8 +1,8 @@
 'NITRON
 
-import mem
-import heap
-import stringView
+import std.mem
+import std.heap
+import std.stringView
 
 type StringBuilder
 	string   as ptr(char)
@@ -24,7 +24,7 @@ 	this.capacity = 0
 end
 
 private sub IncreaseCapacity(this as ptr(StringBuilder))
-	while this.length <= this.capacity do
+	while this.capacity <= this.length do
 		this.capacity = this.capacity * 2
 	end
 
@@ -47,3 +47,7 @@ 	dim sv as StringView
 	sv.FromNTStr(string)
 	this.AddSV(&sv)
 end
+
+sub StringBuilder_ToStringView(this as ptr(StringBuilder), sv as ptr(StringView))
+	sv.Init(this.string, this.length)
+end




diff --git a/basic-std/std/stringView.bas b/basic-std/std/stringView.bas
index 07aacfac3b3aa797a47e8e8ff0f089727b7e82fe..d242e21e3eac6683a340631575c7e9db57434c28 100644
--- a/basic-std/std/stringView.bas
+++ b/basic-std/std/stringView.bas
@@ -1,6 +1,6 @@
 'NITRON
 
-import io
+import std.io
 
 type Rune = u32
 




diff --git a/basic-std/test.bas b/basic-std/test.bas
index 41a5412c33f1ca031c03aa550641dcfa4fdd8110..c228dbd92b7ccd117fbf8e2fc8a8c90bcabfda20 100644
--- a/basic-std/test.bas
+++ b/basic-std/test.bas
@@ -3,11 +3,15 @@
 program test
 
 import std.io
-import std.file
-import std.heap
 import std.stringView
+import std.stringBuilder
+
+dim sb as StringBuilder
+sb.Init()
+sb.AddNTStr("hello ")
+sb.AddNTStr("world!")
 
 dim sv as StringView
-sv.FromNTStr("Hello World")
-sv.Shorten(sv.FindChar(32))
+sb.ToStringView(&sv)
 sv.PrintLn()
+sb.Free()