nitron

commit 0cd3f609e7304f86a903133ce00674a466794bec

Author: mesyeti <mesyeti@mesyeti.uk>

string view stuff

 activate | 2 +
 basic-std/compile_test.sh | 2 +
 basic-std/std/stringView.bas | 65 +++++++++++++++++++++++++++++++++-
 basic-std/test.bas | 21 +----------
 basic/source/frontend/c89.c | 11 ++++-
 basic/source/semanticAnalysis.c | 3 +
 basic/source/state.h | 1 
 forth/Makefile | 17 +++++++++
 forth/source/forth.bas | 34 ++++++++++++++++++
 forth/source/main.bas | 7 +++


diff --git a/activate b/activate
index a7802e82599a961eef759699a1e0d48325426249..071bbb7a91d4ae4d5ea51ed3f44566ef441ab03e 100644
--- a/activate
+++ b/activate
@@ -4,3 +4,5 @@ export NITRON_BIN="$(realpath out/bin)"
 export NITRON_LIB="$(realpath out/lib)"
 export NITRON_SRC="$(realpath out/src)"
 export NITRON_MAN="$(realpath out/man)"
+
+echo "Set up environment variables!"




diff --git a/basic/source/frontend/c89.c b/basic/source/frontend/c89.c
index 39aea7e1830a570bb71fce79e6911259adcf6743..06ab49faf963955502a6cc27a8fae157d1247b84 100644
--- a/basic/source/frontend/c89.c
+++ b/basic/source/frontend/c89.c
@@ -415,8 +415,13 @@ 	fprintf(out, "}\n");
 }
 
 static void CompileStruct(Type* type) {
-	// fprintf(out, "typedef %s {\n", node->isUnion? "union" : "struct");
-	fprintf(out, "typedef struct {\n");
+	fprintf(
+		out, "typedef %s nitron_%s nitron_%s;\n", type->isUnion? "union" : "struct",
+		type->name, type->name
+	);
+
+	fprintf(out, "%s nitron_%s {\n", type->isUnion? "union" : "struct", type->name);
+	// fprintf(out, "typedef struct {\n");
 
 	for (size_t i = 0; i < type->structLen; ++ i) {
 		fprintf(out, "    ");
@@ -424,7 +429,7 @@ 		CompileUsedType(type->structure[i].type);
 		fprintf(out, " nitron_%s;\n", type->structure[i].name);
 	}
 
-	fprintf(out, "} nitron_%s;", type->name);
+	fprintf(out, "};");
 }
 
 static void CompileFuncType(Type* type) {




diff --git a/basic/source/semanticAnalysis.c b/basic/source/semanticAnalysis.c
index c1f50cea692fc5f7310006ff9200896b1320c462..829d774545c05e90008852d459f4d5a893a3d441 100644
--- a/basic/source/semanticAnalysis.c
+++ b/basic/source/semanticAnalysis.c
@@ -751,6 +751,7 @@ 				newType.type    = TYPE_FUNC;
 				newType.name    = NewString(node->typeDef.name);
 				newType.private = external && node->typeDef.private;
 				newType.func    = DecToFunc(node->i.err, node->typeDef.type.dec);
+				newType.isUnion = false;
 			}
 			else {
 				const char* fromName = node->typeDef.type.node->ident.name;
@@ -763,6 +764,7 @@
 				newType         = *from;
 				newType.name    = NewString(node->typeDef.name);
 				newType.private = external && node->typeDef.private;
+				newType.isUnion = false;
 			}
 
 			State_AddType(newType);
@@ -781,6 +783,7 @@ 			newType.private = external && node->typeBlock.private;
 
 			newType.structure = SafeMalloc(node->typeBlock.fieldsNum * sizeof(TypeField));
 			newType.structLen = node->typeBlock.fieldsNum;
+			newType.isUnion   = node->typeBlock.isUnion;
 
 			for (size_t i = 0; i < node->typeBlock.fieldsNum; ++ i) {
 				newType.structure[i] = (TypeField) {




diff --git a/basic/source/state.h b/basic/source/state.h
index a5991d02ce0172c20f09458bf1296cde3ab23917..bb23da40dd70579dbdacfc305c4fa9d3979972f8 100644
--- a/basic/source/state.h
+++ b/basic/source/state.h
@@ -54,6 +54,7 @@
 	// only for "struct" types
 	TypeField* structure;
 	size_t     structLen;
+	bool       isUnion;
 
 	// only for "primitive" types
 	int primType;




diff --git a/basic-std/compile_test.sh b/basic-std/compile_test.sh
index 9afd5d34305244941954d47518d8e9a4b212683b..209872670d899d2b15f7de2bc1e5084b52efbaba 100755
--- a/basic-std/compile_test.sh
+++ b/basic-std/compile_test.sh
@@ -9,3 +9,5 @@
 if [ $? -ne 0 ]; then
 	exit
 fi
+
+rm test.o.c test.o




diff --git a/basic-std/std/stringView.bas b/basic-std/std/stringView.bas
index adca26b4f16d034031c40cb4f58e85ab6fca70af..4277a4b9d3b316d3a347ee4c0eb5d70a1de3bd7d 100644
--- a/basic-std/std/stringView.bas
+++ b/basic-std/std/stringView.bas
@@ -2,7 +2,7 @@ 'NITRON
 
 import io
 
-type Char = u32
+type Rune = u32
 
 type StringView
 	string as ptr(char)
@@ -24,7 +24,7 @@ 		string      = string + 1
 	end
 end
 
-func StringView_Get(this as ptr(StringView), index as uint) Char
+func StringView_Get(this as ptr(StringView), index as uint) Rune
 	if index >= this.length then
 		' TODO: error
 		return 0
@@ -35,6 +35,67 @@ end
 
 func StringView_Length(this as ptr(StringView)) uint
 	return this.length
+end
+
+func StringView_ContainsChar(this as ptr(StringView), ch as Rune) bool
+	dim length as uint
+	dim i      as uint
+
+	length = this.Length()
+
+	while i < length do
+		if this.Get(i) == ch then
+			return true
+		end
+
+		i = i + 1
+	end
+
+	return false
+end
+
+func StringView_FindChar(this as ptr(StringView), ch as Rune) uint
+	dim length as uint
+	dim i      as uint
+
+	length = this.Length()
+
+	while i < length do
+		if this.Get(i) == ch then
+			return i
+		end
+
+		i = i + 1
+	end
+
+	return -1 as uint
+end
+
+func StringView_FindAnyChar(this as ptr(StringView), chars as ptr(char)) uint
+	dim length as uint
+	dim i      as uint
+	dim j      as uint
+
+	length = this.Length()
+
+	while i < length do
+		j = 0
+		while chars[j] != 0 do
+			if this.Get(i) == chars[j] then
+				return i
+			end
+
+			j = j + 1
+		end
+
+		i = i + 1
+	end
+
+	return -1 as uint
+end
+
+sub StringView_Shorten(this as ptr(StringView), newLength as uint)
+	this.length = newLength
 end
 
 sub StringView_Print(this as ptr(StringView))




diff --git a/basic-std/test.bas b/basic-std/test.bas
index 6a5c47f56fb403626c83230c795051a8c6892088..41a5412c33f1ca031c03aa550641dcfa4fdd8110 100644
--- a/basic-std/test.bas
+++ b/basic-std/test.bas
@@ -7,24 +7,7 @@ import std.file
 import std.heap
 import std.stringView
 
-dim file as File
-dim size as uint
-
-file.Open("test.bas", false)
-file.Seek(SEEK_END, 0)
-size = file.Peek()
-file.Seek(SEEK_SET, 0)
-
-dim contents as ptr(char)
-contents = Alloc(size) as ptr(char)
-
-if ((contents as ptr(unit)) == null) then
-	PrintStrLn("Failed to allocate contents")
-end
-
-file.Read(size, contents as ptr(unit))
-file.Close()
-
 dim sv as StringView
-sv.Init(contents, size)
+sv.FromNTStr("Hello World")
+sv.Shorten(sv.FindChar(32))
 sv.PrintLn()




diff --git a/forth/Makefile b/forth/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..e1f106295caabaafbddf75ad2217dd68580c5933
--- /dev/null
+++ b/forth/Makefile
@@ -0,0 +1,17 @@
+SRC   = $(wildcard source/*.bas)
+OBJ   = $(addsuffix .o,$(subst source/,bin/,$(basename $(SRC))))
+OUT   = nitron-forth
+LIB   = $(NITRON_LIB)/libstd.a
+FLAGS = -i $(NITRON_SRC)
+
+compile: ./bin $(OBJ) $(SRC)
+	gcc -o $(OUT) $(OBJ) $(LIB)
+
+./bin:
+	mkdir -p bin
+
+bin/%.o: source/%.bas $(SRC)
+	nitron-basic --obj $< -o $@ $(FLAGS)
+
+clean:
+	rm bin/*.o $(OUT)




diff --git a/forth/source/forth.bas b/forth/source/forth.bas
new file mode 100644
index 0000000000000000000000000000000000000000..dde17bb3eb31dad043f1dac8b4eea1629ed8280f
--- /dev/null
+++ b/forth/source/forth.bas
@@ -0,0 +1,34 @@
+'NITRON
+
+type NativeWord = sub()
+
+type Exec union
+	words  as ptr(ptr(unit))
+	native as NativeWord
+end
+
+type Word
+	next as ptr(Word)
+	name as ptr(char)
+
+	native  as bool
+	compile as Exec
+	runtime as Exec
+end
+
+type Interpreter
+	ip as ptr(ptr(Word))
+end
+
+sub Interpreter_Run(this as ptr(Interpreter))
+	while true do
+		dim word as ptr(Word)
+		word = ^(this.ip)
+
+		if word.native then
+			word.runtime.native()
+		else
+			this.ip = word.runtime.words
+		end
+	end
+end




diff --git a/forth/source/main.bas b/forth/source/main.bas
new file mode 100644
index 0000000000000000000000000000000000000000..9592083171875cada53c13ced182ddef3822a9dc
--- /dev/null
+++ b/forth/source/main.bas
@@ -0,0 +1,7 @@
+'NITRON
+
+program forth
+
+import std.io
+
+PrintStrLn("hello world!")