diff options
Diffstat (limited to 'code/lib')
-rw-r--r-- | code/lib/io.felan | 8 | ||||
-rw-r--r-- | code/lib/memory.felan | 9 | ||||
-rw-r--r-- | code/lib/operator.felan | 8 | ||||
-rw-r--r-- | code/lib/string.felan | 103 | ||||
-rw-r--r-- | code/lib/types.felan | 2 | ||||
-rw-r--r-- | code/lib/vector.felan | 2 |
6 files changed, 115 insertions, 17 deletions
diff --git a/code/lib/io.felan b/code/lib/io.felan index c486902..413da8d 100644 --- a/code/lib/io.felan +++ b/code/lib/io.felan @@ -14,14 +14,6 @@ println :: (value:anytype) -> void { print_char('\n'); }; -print :: (value:string) -> void { - i := 0; - while i < @cast(value.length,i64) { - print_char(value[i]); - i += 1; - } -}; - print_char :: (value:u8) -> void { putchar(@cast(value,i32)); }; diff --git a/code/lib/memory.felan b/code/lib/memory.felan index 2710685..0082a1e 100644 --- a/code/lib/memory.felan +++ b/code/lib/memory.felan @@ -4,9 +4,10 @@ libc :: @c_library("/usr/lib/libc.so.6"); malloc :: @c_function(libc,"malloc",(i64)->(*void)); realloc :: @c_function(libc,"realloc",(*void,i64)->(*void)); free :: @c_function(libc,"free",(*void)->void); +memcpy :: @c_function(libc,"memcpy",(*void,*void,i64)->(*void)); -malloc :: (size:i64,comptime t:type) -> (*t) { - return @cast(malloc(size*@cast(@size_of(t),i64)),*t); +malloc :: (size:u64,comptime t:type) -> (*t) { + return @cast(malloc(@cast(size*@size_of(t),i64)),*t); }; realloc :: (ptr:*anytype,size:u64) -> (@type_of(ptr)) { @@ -22,3 +23,7 @@ realloc :: (ptr:*anytype,size:u64) -> (@type_of(ptr)) { free :: (ptr:*anytype) -> void { free(@cast(ptr,*void)); }; + +memcpy :: (dest:*anytype,src:@type_of(dest),n:u64) -> void { + memcpy(@cast(dest,*void),@cast(src,*void),@cast(n,i64)); +}; diff --git a/code/lib/operator.felan b/code/lib/operator.felan index 52f4a3a..0c72971 100644 --- a/code/lib/operator.felan +++ b/code/lib/operator.felan @@ -800,6 +800,14 @@ __sub__ :: (left:*anytype,right:i64) -> (@type_of(left)) { return @sub(left,right); }; +__sum__ :: (left:*anytype,right:u64) -> (@type_of(left)) { + return @add(left,right); +}; + +__sub__ :: (left:*anytype,right:u64) -> (@type_of(left)) { + return @sub(left,right); +}; + __get_item__ :: (left:**anytype,index:i64) -> (@type_of(left.*.*)) { return (left.* + index).*; }; diff --git a/code/lib/string.felan b/code/lib/string.felan index c7047b3..4b5452f 100644 --- a/code/lib/string.felan +++ b/code/lib/string.felan @@ -1,9 +1,104 @@ @import("operator.felan"); +@import("io.felan"); @import("types.felan"); +@import("memory.felan"); + +String :: struct { + ptr : *u8; + size : u64; + capacity : u64; +}; + +string_new :: () -> String { + str : String = undefined; + + str.ptr = malloc(0u64,u8); + str.size = 0u64; + str.capacity = 0u64; + + return str; +}; + +string_from :: (str:[]u8) -> String { + result : String = undefined; + + result.ptr = malloc(str.length,u8); + result.size = str.length; + result.capacity = str.length; + + memcpy(result.ptr,str.ptr,str.length); -sub_string :: (str:string, begin:i64, end:i64) -> string { - result :[0]u8 = undefined; - result.ptr = str.ptr + begin; - result.length = @cast(end-begin,u64); return result; }; + +append :: (this:*String, str:String) -> void { + _grow_if_needed(this, str.size); + + memcpy(this.*.ptr+this.*.size,str.ptr,str.size); + + this.*.size += str.size; +}; + +append :: (this:*String, str:[]u8) -> void { + _grow_if_needed(this, str.length); + + memcpy(this.*.ptr+this.*.size,str.ptr,str.length); + + this.*.size += str.length; +}; + +append :: (this:*String, char:u8) -> void { + _grow_if_needed(this, 1u64); + + this.*[this.*.size] = char; + + this.*.size += 1u64; +}; + +_grow_if_needed :: (this:*String, count:u64) -> void { + new_size := this.*.size + count; + if new_size >= this.*.capacity { + cap := this.*.capacity + this.*.capacity / 2u64 + 1u64; + if new_size >= cap { + cap = new_size; + } + this.*.capacity = cap; + this.*.ptr = realloc(this.*.ptr,this.*.capacity); + } +}; + +delete :: (this:String) -> void { + free(this.ptr); +}; + +print :: (this:String) -> void { + i := 0u64; + while i < this.size { + @putc(this[i]); + i += 1u64; + } +}; + +__get_item__ :: (left:*String,index:i64) -> u8 { + return (left.*.ptr + index).*; +}; + +__set_item__ :: (left:*String,index:i64,item:u8) -> u8 { + return (left.*.ptr + index).* = item; +}; + +__get_item_address__ :: (left:*String,index:i64) -> (*u8) { + return (left.*.ptr + index); +}; + +__get_item__ :: (left:*String,index:u64) -> u8 { + return (left.*.ptr + index).*; +}; + +__set_item__ :: (left:*String,index:u64,item:u8) -> u8 { + return (left.*.ptr + index).* = item; +}; + +__get_item_address__ :: (left:*String,index:u64) -> (*u8) { + return (left.*.ptr + index); +}; diff --git a/code/lib/types.felan b/code/lib/types.felan index df7f5c3..087f884 100644 --- a/code/lib/types.felan +++ b/code/lib/types.felan @@ -1,5 +1,3 @@ -string :: []u8; - array :: (comptime size:u64, comptime t:type) -> type { return struct { ptr : *t; diff --git a/code/lib/vector.felan b/code/lib/vector.felan index c4f2698..61fe77c 100644 --- a/code/lib/vector.felan +++ b/code/lib/vector.felan @@ -11,7 +11,7 @@ vector :: (comptime t:type)->type{ vector_new :: (comptime t:type) -> (vector(t)) { v : vector(t) = undefined; - v.ptr = malloc(0,@type_of(v.ptr.*)); + v.ptr = malloc(0u64,@type_of(v.ptr.*)); v.size = 0u64; v.capacity = 0u64; return v; |