From d8bddea53f1082342d981e4312bf265453450159 Mon Sep 17 00:00:00 2001 From: Marc Auberer Date: Sun, 17 Mar 2024 23:44:10 +0100 Subject: [PATCH 1/3] Make Result a builtin type --- .run/spicetest.run.xml | 2 +- .../global/runtime-module-manager.spice | 8 +- src-bootstrap/reader/reader.spice | 1 - src/ast/ASTBuilder.cpp | 2 +- src/global/RuntimeModuleManager.cpp | 2 + src/global/RuntimeModuleManager.h | 27 +- src/symboltablebuilder/SymbolType.h | 1 + std/data/doubly-linked-list.spice | 1 - std/data/hash-table.spice | 1 - std/data/linked-list.spice | 1 - std/data/map.spice | 1 - std/data/queue.spice | 1 - std/data/red-black-tree.spice | 1 - std/data/stack.spice | 1 - std/data/unordered-map.spice | 1 - std/data/vector.spice | 1 - std/io/file.spice | 1 - std/runtime/memory_rt.spice | 2 - .../result.spice => runtime/result_rt.spice} | 2 + .../standalone-lexer-test/cout.out | 581 +++++++++--------- .../standalone-lexer-test/test-file.spice | 2 - .../success-dbg-info-complex/ir-code.ll | 14 +- .../structs/success-default-dtor/source.spice | 2 - .../std/io/file-write-read/source.spice | 1 - 24 files changed, 329 insertions(+), 328 deletions(-) rename std/{type/result.spice => runtime/result_rt.spice} (96%) diff --git a/.run/spicetest.run.xml b/.run/spicetest.run.xml index b13e08ceb..4e6a6a964 100644 --- a/.run/spicetest.run.xml +++ b/.run/spicetest.run.xml @@ -1,5 +1,5 @@ - + diff --git a/src-bootstrap/global/runtime-module-manager.spice b/src-bootstrap/global/runtime-module-manager.spice index 5ca8eba66..e01e08528 100644 --- a/src-bootstrap/global/runtime-module-manager.spice +++ b/src-bootstrap/global/runtime-module-manager.spice @@ -1,12 +1,14 @@ const string STRING_RT_IMPORT_NAME = "__rt_string"; +const string RESULT_RT_IMPORT_NAME = "__rt_result"; const string MEMORY_RT_IMPORT_NAME = "__rt_memory"; const string RTTI_RT_IMPORT_NAME = "__rt_rtti"; public type RuntimeModule enum { STRING_RT = 1, - ARRAY_RT = 2, - OBJECT_RT = 4 + RESULT_RT = 2, + ARRAY_RT = 4, + OBJECT_RT = 8 } type ModuleNamePair struct { @@ -73,6 +75,8 @@ public f RuntimeModuleManager.resolveNamePair(RuntimeModule requ switch requestedModule { case RuntimeModule::STRING_RT: return ModuleNamePair{STRING_RT_IMPORT_NAME, "string_rt"}; + case RuntimeModule::RESULT_RT: + return ModuleNamePair{RESULT_RT_IMPORT_NAME, "result_rt"}; case RuntimeModule::ARRAY_RT: return ModuleNamePair{MEMORY_RT_IMPORT_NAME, "memory_rt"}; case RuntimeModule::OBJECT_RT: diff --git a/src-bootstrap/reader/reader.spice b/src-bootstrap/reader/reader.spice index 58af82f91..c152f2540 100644 --- a/src-bootstrap/reader/reader.spice +++ b/src-bootstrap/reader/reader.spice @@ -1,7 +1,6 @@ // Std imports import "std/io/filepath"; import "std/io/file"; -import "std/type/result"; import "std/type/error"; import "std/os/os"; diff --git a/src/ast/ASTBuilder.cpp b/src/ast/ASTBuilder.cpp index 57e1a0ef6..b7c7ba0af 100644 --- a/src/ast/ASTBuilder.cpp +++ b/src/ast/ASTBuilder.cpp @@ -1502,7 +1502,7 @@ std::string ASTBuilder::getIdentifier(TerminalNode *terminal) { std::string identifier = terminal->getText(); // Check if the identifier is 'String' and this is no std source file - bool isReserved = identifier == STROBJ_NAME && !sourceFile->stdFile; + bool isReserved = !sourceFile->stdFile && (identifier == STROBJ_NAME || identifier == RESULTOBJ_NAME); // Check if the list of reserved keywords contains the given identifier isReserved |= std::find(std::begin(RESERVED_KEYWORDS), std::end(RESERVED_KEYWORDS), identifier) != std::end(RESERVED_KEYWORDS); // Print error message diff --git a/src/global/RuntimeModuleManager.cpp b/src/global/RuntimeModuleManager.cpp index 223428538..d64757430 100644 --- a/src/global/RuntimeModuleManager.cpp +++ b/src/global/RuntimeModuleManager.cpp @@ -59,6 +59,8 @@ ModuleNamePair RuntimeModuleManager::resolveNamePair(RuntimeModule runtimeModule switch (runtimeModule) { case STRING_RT: return {STRING_RT_IMPORT_NAME, "string_rt"}; + case RESULT_RT: + return {RESULT_RT_IMPORT_NAME, "result_rt"}; case MEMORY_RT: return {MEMORY_RT_IMPORT_NAME, "memory_rt"}; case RTTI_RT: diff --git a/src/global/RuntimeModuleManager.h b/src/global/RuntimeModuleManager.h index 17e347aad..d437ca1b1 100644 --- a/src/global/RuntimeModuleManager.h +++ b/src/global/RuntimeModuleManager.h @@ -15,29 +15,42 @@ class SourceFile; class Scope; const char *const STRING_RT_IMPORT_NAME = "__rt_string"; +const char *const RESULT_RT_IMPORT_NAME = "__rt_result"; const char *const MEMORY_RT_IMPORT_NAME = "__rt_memory"; const char *const RTTI_RT_IMPORT_NAME = "__rt_rtti"; enum RuntimeModule : uint8_t { STRING_RT = 1 << 0, - MEMORY_RT = 1 << 1, - RTTI_RT = 1 << 2, + RESULT_RT = 1 << 1, + MEMORY_RT = 1 << 2, + RTTI_RT = 1 << 3, }; const std::unordered_map TYPE_NAME_TO_RT_MODULE_MAPPING = { {STROBJ_NAME, STRING_RT}, + {RESULTOBJ_NAME, RESULT_RT}, }; const std::unordered_map FCT_NAME_TO_RT_MODULE_MAPPING = { - {"sAlloc", MEMORY_RT}, {"sRealloc", MEMORY_RT}, {"sCopy", MEMORY_RT}, {"sDealloc", MEMORY_RT}, - {"sNew", MEMORY_RT}, {"sPlacementNew", MEMORY_RT}, {"sDelete", MEMORY_RT}, + // Memory RT + {"sAlloc", MEMORY_RT}, + {"sRealloc", MEMORY_RT}, + {"sCopy", MEMORY_RT}, + {"sDealloc", MEMORY_RT}, + {"sNew", MEMORY_RT}, + {"sPlacementNew", MEMORY_RT}, + {"sDelete", MEMORY_RT}, + // Result RT + {"ok", RESULT_RT}, + {"err", RESULT_RT}, }; // This serves for the compiler to detect if a source file is a specific runtime module const std::unordered_map IDENTIFYING_TOP_LEVEL_NAMES = { - {STRING_RT, STROBJ_NAME}, // String struct - {MEMORY_RT, "sAlloc"}, // sAlloc function - {RTTI_RT, TIOBJ_NAME}, // TypeInfo struct + {STRING_RT, STROBJ_NAME}, // String struct + {RESULT_RT, RESULTOBJ_NAME}, // Result struct + {MEMORY_RT, "sAlloc"}, // sAlloc function + {RTTI_RT, TIOBJ_NAME}, // TypeInfo struct }; struct ModuleNamePair { diff --git a/src/symboltablebuilder/SymbolType.h b/src/symboltablebuilder/SymbolType.h index f15289e59..003b59b2d 100644 --- a/src/symboltablebuilder/SymbolType.h +++ b/src/symboltablebuilder/SymbolType.h @@ -24,6 +24,7 @@ class Interface; // Constants const char *const STROBJ_NAME = "String"; +const char *const RESULTOBJ_NAME = "Result"; const char *const ERROBJ_NAME = "Error"; const char *const TIOBJ_NAME = "TypeInfo"; const char *const IITERATOR_NAME = "IIterator"; diff --git a/std/data/doubly-linked-list.spice b/std/data/doubly-linked-list.spice index 1a2a17069..295677aa1 100644 --- a/std/data/doubly-linked-list.spice +++ b/std/data/doubly-linked-list.spice @@ -1,4 +1,3 @@ -import "std/type/result"; import "std/type/error"; // Add generic type definitions diff --git a/std/data/hash-table.spice b/std/data/hash-table.spice index 823cb9b57..c7cdcc2aa 100644 --- a/std/data/hash-table.spice +++ b/std/data/hash-table.spice @@ -1,6 +1,5 @@ import "std/data/vector"; import "std/data/linked-list"; -import "std/type/result"; import "std/type/error"; import "std/math/hash"; diff --git a/std/data/linked-list.spice b/std/data/linked-list.spice index 6d57b6119..6524b699d 100644 --- a/std/data/linked-list.spice +++ b/std/data/linked-list.spice @@ -1,4 +1,3 @@ -import "std/type/result"; import "std/type/error"; import "std/iterator/iterable"; import "std/iterator/iterator"; diff --git a/std/data/map.spice b/std/data/map.spice index 2dbf0fe39..561f47bce 100644 --- a/std/data/map.spice +++ b/std/data/map.spice @@ -1,5 +1,4 @@ import "std/data/red-black-tree"; -import "std/type/result"; // Add generic type definitions type K dyn; diff --git a/std/data/queue.spice b/std/data/queue.spice index 8c8564818..53ac49cb7 100644 --- a/std/data/queue.spice +++ b/std/data/queue.spice @@ -1,4 +1,3 @@ -import "std/type/result"; import "std/type/error"; // Constants diff --git a/std/data/red-black-tree.spice b/std/data/red-black-tree.spice index 8d218c661..6c4a7a4cb 100644 --- a/std/data/red-black-tree.spice +++ b/std/data/red-black-tree.spice @@ -1,4 +1,3 @@ -import "std/type/result"; import "std/type/error"; // Add generic type definitions diff --git a/std/data/stack.spice b/std/data/stack.spice index f7512f460..245cf5d19 100644 --- a/std/data/stack.spice +++ b/std/data/stack.spice @@ -1,4 +1,3 @@ -import "std/type/result"; import "std/type/error"; // Constants diff --git a/std/data/unordered-map.spice b/std/data/unordered-map.spice index b76ffef9b..c16719713 100644 --- a/std/data/unordered-map.spice +++ b/std/data/unordered-map.spice @@ -1,5 +1,4 @@ import "std/data/hash-table"; -import "std/type/result"; // Add generic type definitions type K dyn; diff --git a/std/data/vector.spice b/std/data/vector.spice index 89041c772..4f9f4b551 100644 --- a/std/data/vector.spice +++ b/std/data/vector.spice @@ -1,4 +1,3 @@ -import "std/type/result"; import "std/type/error"; import "std/iterator/iterable"; import "std/iterator/iterator"; diff --git a/std/io/file.spice b/std/io/file.spice index 84c02438a..836fa8165 100644 --- a/std/io/file.spice +++ b/std/io/file.spice @@ -1,4 +1,3 @@ -import "std/type/result"; import "std/type/error"; // File open modes diff --git a/std/runtime/memory_rt.spice b/std/runtime/memory_rt.spice index 637c3a8ec..2ef7dd7ba 100644 --- a/std/runtime/memory_rt.spice +++ b/std/runtime/memory_rt.spice @@ -1,7 +1,5 @@ #![core.compiler.alwaysKeepOnNameCollision = true] -import "std/type/result"; - // Link external functions ext f malloc(unsigned long); ext f realloc(heap byte*, unsigned long); diff --git a/std/type/result.spice b/std/runtime/result_rt.spice similarity index 96% rename from std/type/result.spice rename to std/runtime/result_rt.spice index 0ad618aa6..8557564e1 100644 --- a/std/type/result.spice +++ b/std/runtime/result_rt.spice @@ -1,3 +1,5 @@ +#![core.compiler.alwaysKeepOnNameCollision = true] + import "std/type/error"; // Generic types diff --git a/test/test-files/bootstrap-compiler/standalone-lexer-test/cout.out b/test/test-files/bootstrap-compiler/standalone-lexer-test/cout.out index e02bced08..3d47ab13e 100644 --- a/test/test-files/bootstrap-compiler/standalone-lexer-test/cout.out +++ b/test/test-files/bootstrap-compiler/standalone-lexer-test/cout.out @@ -8,193 +8,190 @@ Token(type=107, text="alwaysKeepOnNameCollision", line=1, col: 18) Token(type=90, text="=", line=1, col: 44) Token(type=50, text="true", line=1, col: 46) Token(type=57, text="]", line=1, col: 50) -Token(type=29, text="import", line=3, col: 1) -Token(type=106, text="std/type/result", line=3, col: 8) -Token(type=92, text=";", line=3, col: 25) -Token(type=111, text="// Link external functions", line=5, col: 1) +Token(type=111, text="// Link external functions", line=3, col: 1) +Token(type=49, text="ext", line=4, col: 1) +Token(type=17, text="f", line=4, col: 5) +Token(type=83, text="<", line=4, col: 6) +Token(type=15, text="heap", line=4, col: 7) +Token(type=5, text="byte", line=4, col: 12) +Token(type=77, text="*", line=4, col: 16) +Token(type=82, text=">", line=4, col: 17) +Token(type=107, text="malloc", line=4, col: 19) +Token(type=54, text="(", line=4, col: 25) +Token(type=12, text="unsigned", line=4, col: 26) +Token(type=4, text="long", line=4, col: 35) +Token(type=55, text=")", line=4, col: 39) +Token(type=92, text=";", line=4, col: 40) +Token(type=49, text="ext", line=5, col: 1) +Token(type=17, text="f", line=5, col: 5) +Token(type=83, text="<", line=5, col: 6) +Token(type=15, text="heap", line=5, col: 7) +Token(type=5, text="byte", line=5, col: 12) +Token(type=77, text="*", line=5, col: 16) +Token(type=82, text=">", line=5, col: 17) +Token(type=107, text="realloc", line=5, col: 19) +Token(type=54, text="(", line=5, col: 26) +Token(type=15, text="heap", line=5, col: 27) +Token(type=5, text="byte", line=5, col: 32) +Token(type=77, text="*", line=5, col: 36) +Token(type=94, text=",", line=5, col: 37) +Token(type=12, text="unsigned", line=5, col: 39) +Token(type=4, text="long", line=5, col: 48) +Token(type=55, text=")", line=5, col: 52) +Token(type=92, text=";", line=5, col: 53) Token(type=49, text="ext", line=6, col: 1) -Token(type=17, text="f", line=6, col: 5) -Token(type=83, text="<", line=6, col: 6) -Token(type=15, text="heap", line=6, col: 7) -Token(type=5, text="byte", line=6, col: 12) -Token(type=77, text="*", line=6, col: 16) -Token(type=82, text=">", line=6, col: 17) -Token(type=107, text="malloc", line=6, col: 19) -Token(type=54, text="(", line=6, col: 25) -Token(type=12, text="unsigned", line=6, col: 26) -Token(type=4, text="long", line=6, col: 35) -Token(type=55, text=")", line=6, col: 39) -Token(type=92, text=";", line=6, col: 40) +Token(type=18, text="p", line=6, col: 5) +Token(type=107, text="free", line=6, col: 7) +Token(type=54, text="(", line=6, col: 11) +Token(type=15, text="heap", line=6, col: 12) +Token(type=5, text="byte", line=6, col: 17) +Token(type=77, text="*", line=6, col: 21) +Token(type=55, text=")", line=6, col: 22) +Token(type=92, text=";", line=6, col: 23) Token(type=49, text="ext", line=7, col: 1) -Token(type=17, text="f", line=7, col: 5) -Token(type=83, text="<", line=7, col: 6) -Token(type=15, text="heap", line=7, col: 7) -Token(type=5, text="byte", line=7, col: 12) -Token(type=77, text="*", line=7, col: 16) -Token(type=82, text=">", line=7, col: 17) -Token(type=107, text="realloc", line=7, col: 19) -Token(type=54, text="(", line=7, col: 26) -Token(type=15, text="heap", line=7, col: 27) -Token(type=5, text="byte", line=7, col: 32) -Token(type=77, text="*", line=7, col: 36) -Token(type=94, text=",", line=7, col: 37) -Token(type=12, text="unsigned", line=7, col: 39) -Token(type=4, text="long", line=7, col: 48) -Token(type=55, text=")", line=7, col: 52) -Token(type=92, text=";", line=7, col: 53) -Token(type=49, text="ext", line=8, col: 1) -Token(type=18, text="p", line=8, col: 5) -Token(type=107, text="free", line=8, col: 7) -Token(type=54, text="(", line=8, col: 11) -Token(type=15, text="heap", line=8, col: 12) -Token(type=5, text="byte", line=8, col: 17) -Token(type=77, text="*", line=8, col: 21) -Token(type=55, text=")", line=8, col: 22) -Token(type=92, text=";", line=8, col: 23) -Token(type=49, text="ext", line=9, col: 1) -Token(type=18, text="p", line=9, col: 5) -Token(type=107, text="memcpy", line=9, col: 7) -Token(type=54, text="(", line=9, col: 13) -Token(type=15, text="heap", line=9, col: 14) -Token(type=5, text="byte", line=9, col: 19) -Token(type=77, text="*", line=9, col: 23) -Token(type=94, text=",", line=9, col: 24) -Token(type=15, text="heap", line=9, col: 26) -Token(type=5, text="byte", line=9, col: 31) -Token(type=77, text="*", line=9, col: 35) -Token(type=94, text=",", line=9, col: 36) -Token(type=12, text="unsigned", line=9, col: 38) -Token(type=4, text="long", line=9, col: 47) -Token(type=55, text=")", line=9, col: 51) -Token(type=92, text=";", line=9, col: 52) +Token(type=18, text="p", line=7, col: 5) +Token(type=107, text="memcpy", line=7, col: 7) +Token(type=54, text="(", line=7, col: 13) +Token(type=15, text="heap", line=7, col: 14) +Token(type=5, text="byte", line=7, col: 19) +Token(type=77, text="*", line=7, col: 23) +Token(type=94, text=",", line=7, col: 24) +Token(type=15, text="heap", line=7, col: 26) +Token(type=5, text="byte", line=7, col: 31) +Token(type=77, text="*", line=7, col: 35) +Token(type=94, text=",", line=7, col: 36) +Token(type=12, text="unsigned", line=7, col: 38) +Token(type=4, text="long", line=7, col: 47) +Token(type=55, text=")", line=7, col: 51) +Token(type=92, text=";", line=7, col: 52) Token(type=109, text="/** * Allocates a new block of memory of the given size. * * @param size The size of the block to allocate. * @return A pointer to the allocated block, or an error if the allocation failed. - */", line=11, col: 1) -Token(type=14, text="public", line=17, col: 1) -Token(type=17, text="f", line=17, col: 8) -Token(type=83, text="<", line=17, col: 9) -Token(type=108, text="Result", line=17, col: 10) -Token(type=83, text="<", line=17, col: 16) -Token(type=15, text="heap", line=17, col: 17) -Token(type=5, text="byte", line=17, col: 22) -Token(type=77, text="*", line=17, col: 26) -Token(type=87, text=">>", line=17, col: 27) -Token(type=107, text="sAlloc", line=17, col: 30) -Token(type=54, text="(", line=17, col: 36) -Token(type=12, text="unsigned", line=17, col: 37) -Token(type=4, text="long", line=17, col: 46) -Token(type=107, text="size", line=17, col: 51) -Token(type=55, text=")", line=17, col: 55) -Token(type=52, text="{", line=17, col: 57) -Token(type=15, text="heap", line=18, col: 5) -Token(type=5, text="byte", line=18, col: 10) -Token(type=77, text="*", line=18, col: 14) -Token(type=107, text="ptr", line=18, col: 16) -Token(type=90, text="=", line=18, col: 20) -Token(type=107, text="malloc", line=18, col: 22) -Token(type=54, text="(", line=18, col: 28) -Token(type=107, text="size", line=18, col: 29) -Token(type=55, text=")", line=18, col: 33) -Token(type=92, text=";", line=18, col: 34) -Token(type=33, text="return", line=19, col: 5) -Token(type=107, text="ptr", line=19, col: 12) -Token(type=89, text="!=", line=19, col: 16) -Token(type=42, text="nil", line=19, col: 19) -Token(type=83, text="<", line=19, col: 22) -Token(type=15, text="heap", line=19, col: 23) -Token(type=5, text="byte", line=19, col: 28) -Token(type=77, text="*", line=19, col: 32) -Token(type=82, text=">", line=19, col: 33) -Token(type=91, text="?", line=19, col: 35) -Token(type=107, text="ok", line=19, col: 37) -Token(type=54, text="(", line=19, col: 39) -Token(type=107, text="ptr", line=19, col: 40) -Token(type=55, text=")", line=19, col: 43) -Token(type=93, text=":", line=19, col: 45) -Token(type=107, text="err", line=19, col: 47) -Token(type=54, text="(", line=19, col: 50) -Token(type=42, text="nil", line=19, col: 51) -Token(type=83, text="<", line=19, col: 54) -Token(type=15, text="heap", line=19, col: 55) -Token(type=5, text="byte", line=19, col: 60) -Token(type=77, text="*", line=19, col: 64) -Token(type=82, text=">", line=19, col: 65) -Token(type=94, text=",", line=19, col: 66) -Token(type=106, text="Out of memory occurred!", line=19, col: 68) -Token(type=55, text=")", line=19, col: 93) -Token(type=92, text=";", line=19, col: 94) -Token(type=53, text="}", line=20, col: 1) + */", line=9, col: 1) +Token(type=14, text="public", line=15, col: 1) +Token(type=17, text="f", line=15, col: 8) +Token(type=83, text="<", line=15, col: 9) +Token(type=108, text="Result", line=15, col: 10) +Token(type=83, text="<", line=15, col: 16) +Token(type=15, text="heap", line=15, col: 17) +Token(type=5, text="byte", line=15, col: 22) +Token(type=77, text="*", line=15, col: 26) +Token(type=87, text=">>", line=15, col: 27) +Token(type=107, text="sAlloc", line=15, col: 30) +Token(type=54, text="(", line=15, col: 36) +Token(type=12, text="unsigned", line=15, col: 37) +Token(type=4, text="long", line=15, col: 46) +Token(type=107, text="size", line=15, col: 51) +Token(type=55, text=")", line=15, col: 55) +Token(type=52, text="{", line=15, col: 57) +Token(type=15, text="heap", line=16, col: 5) +Token(type=5, text="byte", line=16, col: 10) +Token(type=77, text="*", line=16, col: 14) +Token(type=107, text="ptr", line=16, col: 16) +Token(type=90, text="=", line=16, col: 20) +Token(type=107, text="malloc", line=16, col: 22) +Token(type=54, text="(", line=16, col: 28) +Token(type=107, text="size", line=16, col: 29) +Token(type=55, text=")", line=16, col: 33) +Token(type=92, text=";", line=16, col: 34) +Token(type=33, text="return", line=17, col: 5) +Token(type=107, text="ptr", line=17, col: 12) +Token(type=89, text="!=", line=17, col: 16) +Token(type=42, text="nil", line=17, col: 19) +Token(type=83, text="<", line=17, col: 22) +Token(type=15, text="heap", line=17, col: 23) +Token(type=5, text="byte", line=17, col: 28) +Token(type=77, text="*", line=17, col: 32) +Token(type=82, text=">", line=17, col: 33) +Token(type=91, text="?", line=17, col: 35) +Token(type=107, text="ok", line=17, col: 37) +Token(type=54, text="(", line=17, col: 39) +Token(type=107, text="ptr", line=17, col: 40) +Token(type=55, text=")", line=17, col: 43) +Token(type=93, text=":", line=17, col: 45) +Token(type=107, text="err", line=17, col: 47) +Token(type=54, text="(", line=17, col: 50) +Token(type=42, text="nil", line=17, col: 51) +Token(type=83, text="<", line=17, col: 54) +Token(type=15, text="heap", line=17, col: 55) +Token(type=5, text="byte", line=17, col: 60) +Token(type=77, text="*", line=17, col: 64) +Token(type=82, text=">", line=17, col: 65) +Token(type=94, text=",", line=17, col: 66) +Token(type=106, text="Out of memory occurred!", line=17, col: 68) +Token(type=55, text=")", line=17, col: 93) +Token(type=92, text=";", line=17, col: 94) +Token(type=53, text="}", line=18, col: 1) Token(type=109, text="/** * Reallocates a block of memory to the given size. * * @param ptr The pointer to the block to reallocate. * @param size The new size of the block. * @return A pointer to the reallocated block, or an error if the reallocation failed. - */", line=22, col: 1) -Token(type=14, text="public", line=29, col: 1) -Token(type=17, text="f", line=29, col: 8) -Token(type=83, text="<", line=29, col: 9) -Token(type=108, text="Result", line=29, col: 10) -Token(type=83, text="<", line=29, col: 16) -Token(type=15, text="heap", line=29, col: 17) -Token(type=5, text="byte", line=29, col: 22) -Token(type=77, text="*", line=29, col: 26) -Token(type=87, text=">>", line=29, col: 27) -Token(type=107, text="sRealloc", line=29, col: 30) -Token(type=54, text="(", line=29, col: 38) -Token(type=15, text="heap", line=29, col: 39) -Token(type=5, text="byte", line=29, col: 44) -Token(type=77, text="*", line=29, col: 48) -Token(type=107, text="ptr", line=29, col: 50) -Token(type=94, text=",", line=29, col: 53) -Token(type=12, text="unsigned", line=29, col: 55) -Token(type=4, text="long", line=29, col: 64) -Token(type=107, text="size", line=29, col: 69) -Token(type=55, text=")", line=29, col: 73) -Token(type=52, text="{", line=29, col: 75) -Token(type=15, text="heap", line=30, col: 5) -Token(type=5, text="byte", line=30, col: 10) -Token(type=77, text="*", line=30, col: 14) -Token(type=107, text="newPtr", line=30, col: 16) -Token(type=90, text="=", line=30, col: 23) -Token(type=107, text="realloc", line=30, col: 25) -Token(type=54, text="(", line=30, col: 32) -Token(type=107, text="ptr", line=30, col: 33) -Token(type=94, text=",", line=30, col: 36) -Token(type=107, text="size", line=30, col: 38) -Token(type=55, text=")", line=30, col: 42) -Token(type=92, text=";", line=30, col: 43) -Token(type=33, text="return", line=31, col: 5) -Token(type=107, text="newPtr", line=31, col: 12) -Token(type=89, text="!=", line=31, col: 19) -Token(type=42, text="nil", line=31, col: 22) -Token(type=83, text="<", line=31, col: 25) -Token(type=15, text="heap", line=31, col: 26) -Token(type=5, text="byte", line=31, col: 31) -Token(type=77, text="*", line=31, col: 35) -Token(type=82, text=">", line=31, col: 36) -Token(type=91, text="?", line=31, col: 38) -Token(type=107, text="ok", line=31, col: 40) -Token(type=54, text="(", line=31, col: 42) -Token(type=107, text="newPtr", line=31, col: 43) -Token(type=55, text=")", line=31, col: 49) -Token(type=93, text=":", line=31, col: 51) -Token(type=107, text="err", line=31, col: 53) -Token(type=54, text="(", line=31, col: 56) -Token(type=42, text="nil", line=31, col: 57) -Token(type=83, text="<", line=31, col: 60) -Token(type=15, text="heap", line=31, col: 61) -Token(type=5, text="byte", line=31, col: 66) -Token(type=77, text="*", line=31, col: 70) -Token(type=82, text=">", line=31, col: 71) -Token(type=94, text=",", line=31, col: 72) -Token(type=106, text="Out of memory occurred!", line=31, col: 74) -Token(type=55, text=")", line=31, col: 99) -Token(type=92, text=";", line=31, col: 100) -Token(type=53, text="}", line=32, col: 1) + */", line=20, col: 1) +Token(type=14, text="public", line=27, col: 1) +Token(type=17, text="f", line=27, col: 8) +Token(type=83, text="<", line=27, col: 9) +Token(type=108, text="Result", line=27, col: 10) +Token(type=83, text="<", line=27, col: 16) +Token(type=15, text="heap", line=27, col: 17) +Token(type=5, text="byte", line=27, col: 22) +Token(type=77, text="*", line=27, col: 26) +Token(type=87, text=">>", line=27, col: 27) +Token(type=107, text="sRealloc", line=27, col: 30) +Token(type=54, text="(", line=27, col: 38) +Token(type=15, text="heap", line=27, col: 39) +Token(type=5, text="byte", line=27, col: 44) +Token(type=77, text="*", line=27, col: 48) +Token(type=107, text="ptr", line=27, col: 50) +Token(type=94, text=",", line=27, col: 53) +Token(type=12, text="unsigned", line=27, col: 55) +Token(type=4, text="long", line=27, col: 64) +Token(type=107, text="size", line=27, col: 69) +Token(type=55, text=")", line=27, col: 73) +Token(type=52, text="{", line=27, col: 75) +Token(type=15, text="heap", line=28, col: 5) +Token(type=5, text="byte", line=28, col: 10) +Token(type=77, text="*", line=28, col: 14) +Token(type=107, text="newPtr", line=28, col: 16) +Token(type=90, text="=", line=28, col: 23) +Token(type=107, text="realloc", line=28, col: 25) +Token(type=54, text="(", line=28, col: 32) +Token(type=107, text="ptr", line=28, col: 33) +Token(type=94, text=",", line=28, col: 36) +Token(type=107, text="size", line=28, col: 38) +Token(type=55, text=")", line=28, col: 42) +Token(type=92, text=";", line=28, col: 43) +Token(type=33, text="return", line=29, col: 5) +Token(type=107, text="newPtr", line=29, col: 12) +Token(type=89, text="!=", line=29, col: 19) +Token(type=42, text="nil", line=29, col: 22) +Token(type=83, text="<", line=29, col: 25) +Token(type=15, text="heap", line=29, col: 26) +Token(type=5, text="byte", line=29, col: 31) +Token(type=77, text="*", line=29, col: 35) +Token(type=82, text=">", line=29, col: 36) +Token(type=91, text="?", line=29, col: 38) +Token(type=107, text="ok", line=29, col: 40) +Token(type=54, text="(", line=29, col: 42) +Token(type=107, text="newPtr", line=29, col: 43) +Token(type=55, text=")", line=29, col: 49) +Token(type=93, text=":", line=29, col: 51) +Token(type=107, text="err", line=29, col: 53) +Token(type=54, text="(", line=29, col: 56) +Token(type=42, text="nil", line=29, col: 57) +Token(type=83, text="<", line=29, col: 60) +Token(type=15, text="heap", line=29, col: 61) +Token(type=5, text="byte", line=29, col: 66) +Token(type=77, text="*", line=29, col: 70) +Token(type=82, text=">", line=29, col: 71) +Token(type=94, text=",", line=29, col: 72) +Token(type=106, text="Out of memory occurred!", line=29, col: 74) +Token(type=55, text=")", line=29, col: 99) +Token(type=92, text=";", line=29, col: 100) +Token(type=53, text="}", line=30, col: 1) Token(type=109, text="/** * Copies a block of memory to a new block of memory. * @@ -202,127 +199,127 @@ Token(type=109, text="/** * @param newPtr The pointer to the new block to copy to. * @param size The size of the block to copy. * @return A pointer to the copied block, or an error if the copy failed. - */", line=34, col: 1) -Token(type=14, text="public", line=42, col: 1) -Token(type=17, text="f", line=42, col: 8) -Token(type=83, text="<", line=42, col: 9) -Token(type=108, text="Result", line=42, col: 10) -Token(type=83, text="<", line=42, col: 16) -Token(type=15, text="heap", line=42, col: 17) -Token(type=5, text="byte", line=42, col: 22) -Token(type=77, text="*", line=42, col: 26) -Token(type=87, text=">>", line=42, col: 27) -Token(type=107, text="sCopy", line=42, col: 30) -Token(type=54, text="(", line=42, col: 35) -Token(type=15, text="heap", line=42, col: 36) -Token(type=5, text="byte", line=42, col: 41) -Token(type=77, text="*", line=42, col: 45) -Token(type=107, text="oldPtr", line=42, col: 47) -Token(type=94, text=",", line=42, col: 53) -Token(type=15, text="heap", line=42, col: 55) -Token(type=5, text="byte", line=42, col: 60) -Token(type=77, text="*", line=42, col: 64) -Token(type=107, text="newPtr", line=42, col: 66) -Token(type=94, text=",", line=42, col: 72) -Token(type=12, text="unsigned", line=42, col: 74) -Token(type=4, text="long", line=42, col: 83) -Token(type=107, text="size", line=42, col: 88) -Token(type=55, text=")", line=42, col: 92) -Token(type=52, text="{", line=42, col: 94) -Token(type=19, text="if", line=43, col: 5) -Token(type=107, text="oldPtr", line=43, col: 8) -Token(type=88, text="==", line=43, col: 15) -Token(type=42, text="nil", line=43, col: 18) -Token(type=83, text="<", line=43, col: 21) -Token(type=15, text="heap", line=43, col: 22) -Token(type=5, text="byte", line=43, col: 27) -Token(type=77, text="*", line=43, col: 31) -Token(type=82, text=">", line=43, col: 32) -Token(type=60, text="||", line=43, col: 34) -Token(type=107, text="newPtr", line=43, col: 36) -Token(type=88, text="==", line=43, col: 43) -Token(type=42, text="nil", line=43, col: 46) -Token(type=83, text="<", line=43, col: 49) -Token(type=15, text="heap", line=43, col: 50) -Token(type=5, text="byte", line=43, col: 55) -Token(type=77, text="*", line=43, col: 59) -Token(type=82, text=">", line=43, col: 60) -Token(type=52, text="{", line=43, col: 62) -Token(type=33, text="return", line=44, col: 9) -Token(type=107, text="err", line=44, col: 16) -Token(type=54, text="(", line=44, col: 19) -Token(type=42, text="nil", line=44, col: 20) -Token(type=83, text="<", line=44, col: 23) -Token(type=15, text="heap", line=44, col: 24) -Token(type=5, text="byte", line=44, col: 29) -Token(type=77, text="*", line=44, col: 33) -Token(type=82, text=">", line=44, col: 34) -Token(type=94, text=",", line=44, col: 35) -Token(type=106, text="Cannot copy from or to nil pointer!", line=44, col: 37) -Token(type=55, text=")", line=44, col: 74) -Token(type=92, text=";", line=44, col: 75) -Token(type=53, text="}", line=45, col: 5) -Token(type=107, text="memcpy", line=46, col: 5) -Token(type=54, text="(", line=46, col: 11) -Token(type=107, text="newPtr", line=46, col: 12) -Token(type=94, text=",", line=46, col: 18) -Token(type=107, text="oldPtr", line=46, col: 20) -Token(type=94, text=",", line=46, col: 26) -Token(type=107, text="size", line=46, col: 28) -Token(type=55, text=")", line=46, col: 32) -Token(type=92, text=";", line=46, col: 33) -Token(type=33, text="return", line=47, col: 5) -Token(type=107, text="ok", line=47, col: 12) -Token(type=54, text="(", line=47, col: 14) -Token(type=107, text="newPtr", line=47, col: 15) -Token(type=55, text=")", line=47, col: 21) -Token(type=92, text=";", line=47, col: 22) -Token(type=53, text="}", line=48, col: 1) + */", line=32, col: 1) +Token(type=14, text="public", line=40, col: 1) +Token(type=17, text="f", line=40, col: 8) +Token(type=83, text="<", line=40, col: 9) +Token(type=108, text="Result", line=40, col: 10) +Token(type=83, text="<", line=40, col: 16) +Token(type=15, text="heap", line=40, col: 17) +Token(type=5, text="byte", line=40, col: 22) +Token(type=77, text="*", line=40, col: 26) +Token(type=87, text=">>", line=40, col: 27) +Token(type=107, text="sCopy", line=40, col: 30) +Token(type=54, text="(", line=40, col: 35) +Token(type=15, text="heap", line=40, col: 36) +Token(type=5, text="byte", line=40, col: 41) +Token(type=77, text="*", line=40, col: 45) +Token(type=107, text="oldPtr", line=40, col: 47) +Token(type=94, text=",", line=40, col: 53) +Token(type=15, text="heap", line=40, col: 55) +Token(type=5, text="byte", line=40, col: 60) +Token(type=77, text="*", line=40, col: 64) +Token(type=107, text="newPtr", line=40, col: 66) +Token(type=94, text=",", line=40, col: 72) +Token(type=12, text="unsigned", line=40, col: 74) +Token(type=4, text="long", line=40, col: 83) +Token(type=107, text="size", line=40, col: 88) +Token(type=55, text=")", line=40, col: 92) +Token(type=52, text="{", line=40, col: 94) +Token(type=19, text="if", line=41, col: 5) +Token(type=107, text="oldPtr", line=41, col: 8) +Token(type=88, text="==", line=41, col: 15) +Token(type=42, text="nil", line=41, col: 18) +Token(type=83, text="<", line=41, col: 21) +Token(type=15, text="heap", line=41, col: 22) +Token(type=5, text="byte", line=41, col: 27) +Token(type=77, text="*", line=41, col: 31) +Token(type=82, text=">", line=41, col: 32) +Token(type=60, text="||", line=41, col: 34) +Token(type=107, text="newPtr", line=41, col: 36) +Token(type=88, text="==", line=41, col: 43) +Token(type=42, text="nil", line=41, col: 46) +Token(type=83, text="<", line=41, col: 49) +Token(type=15, text="heap", line=41, col: 50) +Token(type=5, text="byte", line=41, col: 55) +Token(type=77, text="*", line=41, col: 59) +Token(type=82, text=">", line=41, col: 60) +Token(type=52, text="{", line=41, col: 62) +Token(type=33, text="return", line=42, col: 9) +Token(type=107, text="err", line=42, col: 16) +Token(type=54, text="(", line=42, col: 19) +Token(type=42, text="nil", line=42, col: 20) +Token(type=83, text="<", line=42, col: 23) +Token(type=15, text="heap", line=42, col: 24) +Token(type=5, text="byte", line=42, col: 29) +Token(type=77, text="*", line=42, col: 33) +Token(type=82, text=">", line=42, col: 34) +Token(type=94, text=",", line=42, col: 35) +Token(type=106, text="Cannot copy from or to nil pointer!", line=42, col: 37) +Token(type=55, text=")", line=42, col: 74) +Token(type=92, text=";", line=42, col: 75) +Token(type=53, text="}", line=43, col: 5) +Token(type=107, text="memcpy", line=44, col: 5) +Token(type=54, text="(", line=44, col: 11) +Token(type=107, text="newPtr", line=44, col: 12) +Token(type=94, text=",", line=44, col: 18) +Token(type=107, text="oldPtr", line=44, col: 20) +Token(type=94, text=",", line=44, col: 26) +Token(type=107, text="size", line=44, col: 28) +Token(type=55, text=")", line=44, col: 32) +Token(type=92, text=";", line=44, col: 33) +Token(type=33, text="return", line=45, col: 5) +Token(type=107, text="ok", line=45, col: 12) +Token(type=54, text="(", line=45, col: 14) +Token(type=107, text="newPtr", line=45, col: 15) +Token(type=55, text=")", line=45, col: 21) +Token(type=92, text=";", line=45, col: 22) +Token(type=53, text="}", line=46, col: 1) Token(type=109, text="/** * Frees a block of memory. * The pointer is zeroed out after freeing the memory to prevent accidental double frees. * * @param ptr The pointer to the block to free. - */", line=50, col: 1) -Token(type=14, text="public", line=56, col: 1) -Token(type=18, text="p", line=56, col: 8) -Token(type=107, text="sDealloc", line=56, col: 10) -Token(type=54, text="(", line=56, col: 18) -Token(type=15, text="heap", line=56, col: 19) -Token(type=5, text="byte", line=56, col: 24) -Token(type=77, text="*", line=56, col: 28) -Token(type=62, text="&&", line=56, col: 29) -Token(type=107, text="ptr", line=56, col: 31) -Token(type=55, text=")", line=56, col: 34) -Token(type=52, text="{", line=56, col: 36) -Token(type=19, text="if", line=57, col: 5) -Token(type=107, text="ptr", line=57, col: 8) -Token(type=88, text="==", line=57, col: 12) -Token(type=42, text="nil", line=57, col: 15) -Token(type=83, text="<", line=57, col: 18) -Token(type=15, text="heap", line=57, col: 19) -Token(type=5, text="byte", line=57, col: 24) -Token(type=77, text="*", line=57, col: 28) -Token(type=82, text=">", line=57, col: 29) -Token(type=52, text="{", line=57, col: 31) -Token(type=33, text="return", line=57, col: 33) -Token(type=92, text=";", line=57, col: 39) -Token(type=53, text="}", line=57, col: 41) -Token(type=107, text="free", line=58, col: 5) -Token(type=54, text="(", line=58, col: 9) -Token(type=107, text="ptr", line=58, col: 10) -Token(type=55, text=")", line=58, col: 13) -Token(type=92, text=";", line=58, col: 14) -Token(type=107, text="ptr", line=59, col: 5) -Token(type=90, text="=", line=59, col: 9) -Token(type=42, text="nil", line=59, col: 11) -Token(type=83, text="<", line=59, col: 14) -Token(type=15, text="heap", line=59, col: 15) -Token(type=5, text="byte", line=59, col: 20) -Token(type=77, text="*", line=59, col: 24) -Token(type=82, text=">", line=59, col: 25) -Token(type=92, text=";", line=59, col: 26) -Token(type=111, text="// Zero out to prevent accidental double frees", line=59, col: 28) -Token(type=53, text="}", line=60, col: 1) + */", line=48, col: 1) +Token(type=14, text="public", line=54, col: 1) +Token(type=18, text="p", line=54, col: 8) +Token(type=107, text="sDealloc", line=54, col: 10) +Token(type=54, text="(", line=54, col: 18) +Token(type=15, text="heap", line=54, col: 19) +Token(type=5, text="byte", line=54, col: 24) +Token(type=77, text="*", line=54, col: 28) +Token(type=62, text="&&", line=54, col: 29) +Token(type=107, text="ptr", line=54, col: 31) +Token(type=55, text=")", line=54, col: 34) +Token(type=52, text="{", line=54, col: 36) +Token(type=19, text="if", line=55, col: 5) +Token(type=107, text="ptr", line=55, col: 8) +Token(type=88, text="==", line=55, col: 12) +Token(type=42, text="nil", line=55, col: 15) +Token(type=83, text="<", line=55, col: 18) +Token(type=15, text="heap", line=55, col: 19) +Token(type=5, text="byte", line=55, col: 24) +Token(type=77, text="*", line=55, col: 28) +Token(type=82, text=">", line=55, col: 29) +Token(type=52, text="{", line=55, col: 31) +Token(type=33, text="return", line=55, col: 33) +Token(type=92, text=";", line=55, col: 39) +Token(type=53, text="}", line=55, col: 41) +Token(type=107, text="free", line=56, col: 5) +Token(type=54, text="(", line=56, col: 9) +Token(type=107, text="ptr", line=56, col: 10) +Token(type=55, text=")", line=56, col: 13) +Token(type=92, text=";", line=56, col: 14) +Token(type=107, text="ptr", line=57, col: 5) +Token(type=90, text="=", line=57, col: 9) +Token(type=42, text="nil", line=57, col: 11) +Token(type=83, text="<", line=57, col: 14) +Token(type=15, text="heap", line=57, col: 15) +Token(type=5, text="byte", line=57, col: 20) +Token(type=77, text="*", line=57, col: 24) +Token(type=82, text=">", line=57, col: 25) +Token(type=92, text=";", line=57, col: 26) +Token(type=111, text="// Zero out to prevent accidental double frees", line=57, col: 28) +Token(type=53, text="}", line=58, col: 1) -Lexed tokens: 303 +Lexed tokens: 300 diff --git a/test/test-files/bootstrap-compiler/standalone-lexer-test/test-file.spice b/test/test-files/bootstrap-compiler/standalone-lexer-test/test-file.spice index 845155845..0cbc272bb 100644 --- a/test/test-files/bootstrap-compiler/standalone-lexer-test/test-file.spice +++ b/test/test-files/bootstrap-compiler/standalone-lexer-test/test-file.spice @@ -1,7 +1,5 @@ #![core.compiler.alwaysKeepOnNameCollision = true] -import "std/type/result"; - // Link external functions ext f malloc(unsigned long); ext f realloc(heap byte*, unsigned long); diff --git a/test/test-files/irgenerator/debug-info/success-dbg-info-complex/ir-code.ll b/test/test-files/irgenerator/debug-info/success-dbg-info-complex/ir-code.ll index b01998cb1..fcd458a27 100644 --- a/test/test-files/irgenerator/debug-info/success-dbg-info-complex/ir-code.ll +++ b/test/test-files/irgenerator/debug-info/success-dbg-info-complex/ir-code.ll @@ -521,14 +521,14 @@ attributes #3 = { cold noreturn nounwind } !25 = !DILocalVariable(name: "_argc", arg: 1, scope: !15, file: !5, line: 4, type: !18) !26 = !DILocalVariable(name: "_argv", arg: 2, scope: !15, file: !5, line: 4, type: !19) !27 = !DILocalVariable(name: "vi", scope: !15, file: !5, line: 6, type: !28) -!28 = !DICompositeType(tag: DW_TAG_structure_type, name: "Vector", scope: !29, file: !29, line: 27, size: 256, align: 8, flags: DIFlagTypePassByReference | DIFlagNonTrivial, elements: !30, identifier: "struct.Vector") +!28 = !DICompositeType(tag: DW_TAG_structure_type, name: "Vector", scope: !29, file: !29, line: 26, size: 256, align: 8, flags: DIFlagTypePassByReference | DIFlagNonTrivial, elements: !30, identifier: "struct.Vector") !29 = !DIFile(filename: "vector.spice", directory: "C:\\Users\\Marc\\Documents\\JustForFunGitHubClonesFast\\spice\\std\\data") !30 = !{!31, !33, !35} -!31 = !DIDerivedType(tag: DW_TAG_member, name: "contents", scope: !28, file: !29, line: 28, baseType: !32, size: 64, offset: 64) +!31 = !DIDerivedType(tag: DW_TAG_member, name: "contents", scope: !28, file: !29, line: 27, baseType: !32, size: 64, offset: 64) !32 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !18, size: 64) -!33 = !DIDerivedType(tag: DW_TAG_member, name: "capacity", scope: !28, file: !29, line: 29, baseType: !34, size: 64, offset: 128) +!33 = !DIDerivedType(tag: DW_TAG_member, name: "capacity", scope: !28, file: !29, line: 28, baseType: !34, size: 64, offset: 128) !34 = !DIBasicType(name: "unsigned long", size: 64, encoding: DW_ATE_unsigned) -!35 = !DIDerivedType(tag: DW_TAG_member, name: "size", scope: !28, file: !29, line: 30, baseType: !34, size: 64, offset: 192) +!35 = !DIDerivedType(tag: DW_TAG_member, name: "size", scope: !28, file: !29, line: 29, baseType: !34, size: 64, offset: 192) !36 = !DILocation(line: 6, column: 5, scope: !15) !37 = !DILocation(line: 6, column: 22, scope: !15) !38 = !DILocation(line: 7, column: 17, scope: !15) @@ -539,11 +539,11 @@ attributes #3 = { cold noreturn nounwind } !43 = !{!"branch_weights", i32 2000, i32 1} !44 = !DILocation(line: 13, column: 14, scope: !15) !45 = !DILocalVariable(name: "it", scope: !15, file: !5, line: 13, type: !46) -!46 = !DICompositeType(tag: DW_TAG_structure_type, name: "VectorIterator", scope: !29, file: !29, line: 255, size: 192, align: 8, flags: DIFlagTypePassByReference | DIFlagNonTrivial, elements: !47, identifier: "struct.VectorIterator") +!46 = !DICompositeType(tag: DW_TAG_structure_type, name: "VectorIterator", scope: !29, file: !29, line: 254, size: 192, align: 8, flags: DIFlagTypePassByReference | DIFlagNonTrivial, elements: !47, identifier: "struct.VectorIterator") !47 = !{!48, !50} -!48 = !DIDerivedType(tag: DW_TAG_member, name: "vector", scope: !46, file: !29, line: 256, baseType: !49, size: 64, offset: 64) +!48 = !DIDerivedType(tag: DW_TAG_member, name: "vector", scope: !46, file: !29, line: 255, baseType: !49, size: 64, offset: 64) !49 = !DIDerivedType(tag: DW_TAG_reference_type, baseType: !28, size: 64) -!50 = !DIDerivedType(tag: DW_TAG_member, name: "cursor", scope: !46, file: !29, line: 257, baseType: !34, size: 64, offset: 128) +!50 = !DIDerivedType(tag: DW_TAG_member, name: "cursor", scope: !46, file: !29, line: 256, baseType: !34, size: 64, offset: 128) !51 = !DILocation(line: 13, column: 5, scope: !15) !52 = !DILocation(line: 14, column: 12, scope: !15) !53 = !DILocation(line: 15, column: 12, scope: !15) diff --git a/test/test-files/irgenerator/structs/success-default-dtor/source.spice b/test/test-files/irgenerator/structs/success-default-dtor/source.spice index 98e49670a..43ccb8019 100644 --- a/test/test-files/irgenerator/structs/success-default-dtor/source.spice +++ b/test/test-files/irgenerator/structs/success-default-dtor/source.spice @@ -1,5 +1,3 @@ -import "std/type/result"; - type StructWithHeapFields struct { heap int* data } diff --git a/test/test-files/std/io/file-write-read/source.spice b/test/test-files/std/io/file-write-read/source.spice index cda3a5bd5..3b685db18 100644 --- a/test/test-files/std/io/file-write-read/source.spice +++ b/test/test-files/std/io/file-write-read/source.spice @@ -1,5 +1,4 @@ import "std/io/file"; -import "std/type/result"; f main() { // Write file From 06dce5d4debee1e2a4b5c6fff6e1ae29cb8637f8 Mon Sep 17 00:00:00 2001 From: Marc Auberer Date: Mon, 18 Mar 2024 00:07:09 +0100 Subject: [PATCH 2/3] Add documentation for Result builtin type --- docs/docs/language/builtin-types.md | 101 ++++++++++++++++++++-------- std/runtime/string_rt.spice | 26 +++---- 2 files changed, 87 insertions(+), 40 deletions(-) diff --git a/docs/docs/language/builtin-types.md b/docs/docs/language/builtin-types.md index 55bf5e4e9..265bdaf4e 100644 --- a/docs/docs/language/builtin-types.md +++ b/docs/docs/language/builtin-types.md @@ -19,50 +19,97 @@ The `String` builtin type offers the following constructors: - `void String()`: Initialize empty - `void String(string)`: Initialize with a raw `string` as start value - `void String(char)`: Initialize with a single char -- `void String(String)`: Initialize by copying another `String` (copy constructor) +- `void String(const String&)`: Initialize by copying another `String` (copy constructor) +- `void String(int)`: Initialize with an initial size +- `void String(long)`: Initialize with an initial size ### Methods The `String` builtin type offers the following methods: - `void append(string)`: Appends a raw string -- `void append(String)`: Appends a string +- `void append(const String&)`: Appends a string - `void append(char)`: Appends a single char -- `char* getRaw()`: Returns a char* to the heap allocated value -- `long getLength()`: Returns the length of the string in chars +- `string getRaw()`: Returns a char* to the heap allocated value +- `unsigned long getLength()`: Returns the length of the string in chars - `bool isEmpty()`: Checks if the string has a length of 0 -- `long getCapacity()`: Returns the allocated space in bytes +- `unsigned long getCapacity()`: Returns the allocated space in bytes - `bool isFull()`: Checks if the length is equal with the capacity - `void clear()`: Clear the value of the string -- `long find(string, int)`: Returns the index, where a substring was found, starting from a start index -- `long find(string, long)`: Returns the index, where a substring was found, starting from a start index -- `long find(string, short)`: Returns the index, where a substring was found, starting from a start index +- `long find(string, unsigned int)`: Returns the index, where a substring was found, starting from a start index +- `long find(string, unsigned long)`: Returns the index, where a substring was found, starting from a start index +- `long find(char, unsigned long)`: Returns the index, where a subchar was found, starting from a start index +- `long rfind(string, unsigned int)`: Returns the index, where a substring was found, starting from reversed at a start index +- `long rfind(string, unsigned long)`: Returns the index, where a substring was found, starting from reversed at a start index +- `long rfind(char, unsigned long)`: Returns the index, where a subchar was found, starting from reversed at a start index - `bool contains(string)`: Checks if the string contains a substring +- `bool startsWith(string)`: Checks if the string starts with a substring +- `bool endsWith(string)`: Checks if the string ends with a substring - `void reverse()`: Reverses the value of the string -- `String substring(int, long)`: Returns the substring from start index `x` and length `y` -- `String substring(long, long)`: Returns the substring from start index `x` and length `y` -- `String substring(short, long)`: Returns the substring from start index `x` and length `y` -- `void reserve(int)`: Increase the capacity to the given number -- `void reserve(long)`: Increase the capacity to the given number -- `void reserve(short)`: Increase the capacity to the given number +- `void replace(string, string, unsigned long)`: Replaces a substring with another string, starting from a start index +- `void replaceAll(string, string)`: Replaces all occurrences of a substring with another string +- `void replaceAll(char, char)`: Replaces all occurrences of a subchar with another char +- `String getSubstring(unsigned int, long)`: Returns the substring from start index `x` and length `y` +- `String getSubstring(unsigned long, long)`: Returns the substring from start index `x` and length `y` +- `String getSubstring(unsigned short, long)`: Returns the substring from start index `x` and length `y` +- `void reserve(unsigned int)`: Increase the capacity to the given number +- `void reserve(unsigned long)`: Increase the capacity to the given number +- `void reserve(unsigned short)`: Increase the capacity to the given number + +### Static functions +The `String` builtin type offers the following static functions: + +- `getRawLength(string)`: Returns the length of a raw string +- `isRawEqual(string, string)`: Checks if two raw strings are equal in value ### Operators The `String` builtin type overrides the following operators: -- `String operator+(String, String)`: Concatenates two strings and returns the result -- `String operator+(String, string)`: Concatenates a string and a raw string and returns the result -- `String operator+(string, String)`: Concatenates a raw string and a string and returns the result -- `String operator+(string, string)`: Concatenates two raw strings and returns the result +- `String operator+(const String&, const String&)`: Concatenates two strings and returns the result +- `String operator+(const String&, const string&)`: Concatenates a string and a raw string and returns the result +- `String operator+(const String&, const char&)`: Concatenates a string and a raw string and returns the result +- `String operator+(const string&, const String&)`: Concatenates a raw string and a string and returns the result +- `String operator+(const string&, const string&)`: Concatenates two raw strings and returns the result +- `String operator+(const string&, const char&)`: Concatenates two raw strings and returns the result - `void operator+=(String&, String)`: Appends a string - `void operator+=(String&, string)`: Appends a raw string -- `void operator+=(String&, string)`: Appends a single char -- `String operator*(String, int)`: Concatenates a string with itself n times -- `String operator*(String, long)`: Concatenates a string with itself n times -- `String operator*(String, short)`: Concatenates a string with itself n times -- `String operator*(int, String)`: Concatenates a string with itself n times -- `String operator*(long, String)`: Concatenates a string with itself n times -- `String operator*(short, String)`: Concatenates a string with itself n times +- `void operator+=(String&, char)`: Appends a single char +- `String operator*(const String&, int)`: Concatenates a string with itself n times +- `String operator*(const String&, long)`: Concatenates a string with itself n times +- `String operator*(const String&, short)`: Concatenates a string with itself n times +- `String operator*(int, const String&)`: Concatenates a string with itself n times +- `String operator*(long, const String&)`: Concatenates a string with itself n times +- `String operator*(short, const String&)`: Concatenates a string with itself n times - `void operator*=(String&, int)`: Concatenates with itself n times - `void operator*=(String&, long)`: Concatenates with itself n times - `void operator*=(String&, short)`: Concatenates with itself n times -- `bool operator==(String, String)`: Checks if two strings are equal in value -- `bool operator!=(String, String)`: Checks if two strings are unequal in value \ No newline at end of file +- `bool operator==(const String&, const String&)`: Checks if two strings are equal in value +- `bool operator==(const String&, string)`: Checks if two strings are equal in value +- `bool operator==(string, const String&)`: Checks if two strings are equal in value +- `bool operator!=(const String&, const String&)`: Checks if two strings are unequal in value +- `bool operator!=(const String&, string)`: Checks if two strings are unequal in value +- `bool operator!=(string, const String&)`: Checks if two strings are unequal in value + +## The `Result` data type +The `Result` builtin type is a generic type, which is used to return a value or an error. It is used to handle errors + +### Constructors +The `Result` builtin type offers the following constructors: + +- `void Result(const T&)`: Initialize Result object with a value +- `void Result(const Error&)`: Initialize Result object with an error + +### Methods +The `Result` builtin type offers the following methods: + +- `T unwrap()`: Returns the value of the Result object. If the Result object contains an error, the program will panic +- `Error getErr()`: Returns the error of the Result object. If no error is present, an error object with error code 0 is returned. +- `bool isOk()`: Checks if the Result object contains a value +- `bool isErr()`: Checks if the Result object contains an error + +### Static functions +The `Result` builtin type offers the following static functions: + +- `Result ok(const T&)`: Returns a Result object with a value +- `Result err(const Error&)`: Returns a Result object with an error +- `Result err(int, string)`: Returns a Result object with an error, constructed with an error code and an error message +- `Result err(string)`: Returns a Result object with an error, constructed with an error message \ No newline at end of file diff --git a/std/runtime/string_rt.spice b/std/runtime/string_rt.spice index 61445c7bf..eabf6b22e 100644 --- a/std/runtime/string_rt.spice +++ b/std/runtime/string_rt.spice @@ -251,11 +251,11 @@ public f operator==(const String& a, const String& b) { return true; } -public f operator==(const String& a, const string& b) { +public f operator==(const String& a, string b) { return isRawEqual(a.getRaw(), b); } -public f operator==(const string& a, const String& b) { +public f operator==(string a, const String& b) { return isRawEqual(a, b.getRaw()); } @@ -294,7 +294,7 @@ public inline f String.getRaw() { * * @return Current length of the string */ -public inline f String.getLength() { +public inline f String.getLength() { return this.length; } @@ -310,7 +310,7 @@ public inline f String.isEmpty() { * * @return Current capacity of the string */ - public inline f String.getCapacity() { + public inline f String.getCapacity() { return this.capacity; } @@ -417,24 +417,24 @@ public f String.rfind(string needle, unsigned long startIndex = 0l) { } /** - * Searches for a subchar in a string from the back. Returns -1 if the char was not found. + * Searches for a substring in a string from the back. Returns -1 if the string was not found. * * @param startIndex Index where to start the search - * @return Index, where the subchar was found / -1 + * @return Index, where the substring was found / -1 */ -public f String.rfind(char needle, unsigned long startIndex = 0l) { - const String needleStr = String(needle); - return this.rfind(needleStr.getRaw(), startIndex); +public f String.rfind(string needle, unsigned int startIndex) { + return this.rfind(needle, (unsigned long) startIndex); } /** - * Searches for a substring in a string from the back. Returns -1 if the string was not found. + * Searches for a subchar in a string from the back. Returns -1 if the char was not found. * * @param startIndex Index where to start the search - * @return Index, where the substring was found / -1 + * @return Index, where the subchar was found / -1 */ -public f String.rfind(string needle, unsigned int startIndex) { - return this.rfind(needle, (unsigned long) startIndex); +public f String.rfind(char needle, unsigned long startIndex = 0l) { + const String needleStr = String(needle); + return this.rfind(needleStr.getRaw(), startIndex); } /** From f88161118db97914eb3d20901e20dd9e22b95a16 Mon Sep 17 00:00:00 2001 From: Marc Auberer Date: Mon, 18 Mar 2024 00:10:09 +0100 Subject: [PATCH 3/3] Fix dependency clash --- docs/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index e236a833e..11bc524ee 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,5 +1,5 @@ mkdocs-material==9.5.13 -mkdocs-material[imaging]==9.5.12 +mkdocs-material[imaging]==9.5.13 mkdocs-minify-plugin==0.8.0 mkdocs-git-revision-date-localized-plugin==1.2.4 mkdocs-git-committers-plugin-2==2.3.0