Author: mesyeti <mesyeti@mesyeti.uk>
fix imports and add new std modules
basic-std/std/mem.bas | 10 +++++++ basic-std/std/stringBuilder.bas | 49 +++++++++++++++++++++++++++++++++++ basic-std/std/stringView.bas | 6 ++++ basic/source/state.c | 3 ++
diff --git a/basic/source/state.c b/basic/source/state.c index 55624aa6df580199d918e24fb0d8447f3707c3f2..5ff9b2f7f9561d45ce33f7bc492c275b9b584345 100644 --- a/basic/source/state.c +++ b/basic/source/state.c @@ -5,6 +5,7 @@ #include "util.h" #include "state.h" #include "parser.h" #include "string.h" +#include "import.h" #include "semanticAnalysis.h" State state; @@ -160,6 +161,8 @@ Parser parser = Parser_Init(file, path, false); size_t len; Node* nodes = Parser_Parse(&parser, &len); + + Import_Run(nodes, len); for (size_t i = 0; i < len; ++ i) { SemanticAnalysis_SetExternal(true); diff --git a/basic-std/std/mem.bas b/basic-std/std/mem.bas new file mode 100644 index 0000000000000000000000000000000000000000..25116bfde015fdf02583151d7a2615640968e141 --- /dev/null +++ b/basic-std/std/mem.bas @@ -0,0 +1,10 @@ +'NITRON + +sub CopyMem(dest as ptr(u8), src as ptr(u8), size as uint) + dim i as uint + + while i < size do + dest[i] = src[i] + i = i + 1 + end +end diff --git a/basic-std/std/stringBuilder.bas b/basic-std/std/stringBuilder.bas new file mode 100644 index 0000000000000000000000000000000000000000..1f9000e5f832f416c157a39c26c6001495bd5599 --- /dev/null +++ b/basic-std/std/stringBuilder.bas @@ -0,0 +1,49 @@ +'NITRON + +import mem +import heap +import stringView + +type StringBuilder + string as ptr(char) + length as uint + capacity as uint +end + +sub StringBuilder_Init(this as ptr(StringBuilder)) + this.string = Alloc(8) as ptr(char) + this.length = 0 + this.capacity = 8 +end + +sub StringBuilder_Free(this as ptr(StringBuilder)) + Free(this.string as ptr(unit)) + this.string = null as ptr(char) + this.length = 0 + this.capacity = 0 +end + +private sub IncreaseCapacity(this as ptr(StringBuilder)) + while this.length <= this.capacity do + this.capacity = this.capacity * 2 + end + + this.string = ReAlloc(this.string as ptr(unit), this.capacity) as ptr(char) +end + +sub StringBuilder_AddSV(this as ptr(StringBuilder), sv as ptr(StringView)) + dim dest as ptr(char) + + dest = &this.string[this.length] + this.length = this.length + sv.Length() + + IncreaseCapacity(this) + + CopyMem(dest as ptr(u8), sv.string as ptr(u8), sv.Size()) +end + +sub StringBuilder_AddNTStr(this as ptr(StringBuilder), string as ptr(char)) + dim sv as StringView + sv.FromNTStr(string) + this.AddSV(&sv) +end diff --git a/basic-std/std/stringView.bas b/basic-std/std/stringView.bas index 4277a4b9d3b316d3a347ee4c0eb5d70a1de3bd7d..07aacfac3b3aa797a47e8e8ff0f089727b7e82fe 100644 --- a/basic-std/std/stringView.bas +++ b/basic-std/std/stringView.bas @@ -33,7 +33,13 @@ return this.string[index] as u32 end +' Number of characters in string func StringView_Length(this as ptr(StringView)) uint + return this.length +end + +' Number of bytes in string +func StringView_Size(this as ptr(StringView)) uint return this.length end