diff options
Diffstat (limited to 'code/lib')
-rw-r--r-- | code/lib/io.felan | 10 | ||||
-rw-r--r-- | code/lib/memory.felan | 26 | ||||
-rw-r--r-- | code/lib/operator.felan | 48 | ||||
-rw-r--r-- | code/lib/print.felan | 172 | ||||
-rw-r--r-- | code/lib/string.felan | 113 | ||||
-rw-r--r-- | code/lib/types.felan | 2 | ||||
-rw-r--r-- | code/lib/vector.felan | 47 |
7 files changed, 213 insertions, 205 deletions
diff --git a/code/lib/io.felan b/code/lib/io.felan index c486902..d470331 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)); }; @@ -80,7 +72,7 @@ _print_unsigned :: (comptime t:type, value:t) -> void { } {} while i < NUMBERS_SIZE - 1 { - @putc(numbers[i]); + print_char(numbers[i]); i += 1; } return; diff --git a/code/lib/memory.felan b/code/lib/memory.felan index f1f9898..0082a1e 100644 --- a/code/lib/memory.felan +++ b/code/lib/memory.felan @@ -1,11 +1,29 @@ +@import("operator.felan"); + 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:u64,comptime t:type) -> (*t) { + return @cast(malloc(@cast(size*@size_of(t),i64)),*t); +}; + +realloc :: (ptr:*anytype,size:u64) -> (@type_of(ptr)) { + return @cast( + realloc( + @cast(ptr,*void), + @cast(size*@size_of(@type_of(ptr.*)),i64), + ), + @type_of(ptr) + ); +}; -malloc :: (size:i64,comptime t:type) -> (*t) { - return @cast(malloc(size*@cast(@size_of(t),i64)),*t); +free :: (ptr:*anytype) -> void { + free(@cast(ptr,*void)); }; -free :: (a:*anytype) -> void { - free(@cast(a,*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 b7ac63a..5536343 100644 --- a/code/lib/operator.felan +++ b/code/lib/operator.felan @@ -578,7 +578,7 @@ __logical_not__ :: (value:bool) -> bool { return value == false; }; -__logical_and__ :: (left:bool,lazy right:bool) -> bool { +__logical_and__ :: (left:bool,right:bool) macro -> bool { if left == false { return false; } else if right == false { @@ -588,7 +588,7 @@ __logical_and__ :: (left:bool,lazy right:bool) -> bool { } }; -__logical_or__ :: (left:bool,lazy right:bool) -> bool { +__logical_or__ :: (left:bool,right:bool) macro -> bool { if left == true { return true; } else if right == true { @@ -800,28 +800,48 @@ __sub__ :: (left:*anytype,right:i64) -> (@type_of(left)) { return @sub(left,right); }; -__get_item__ :: (left:*anytype,index:i64) -> (@type_of(left.*)) { - return (left + index).*; +__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).*; }; -__set_item__ :: (left:*anytype,index:i64,item:@type_of(left.*)) -> (@type_of(left.*)) { - return (left + index).* = item; +__set_item__ :: (left:**anytype,index:i64,item:@type_of(left.*.*)) -> (@type_of(left.*.*)) { + return (left.* + index).* = item; }; -__get_item_address__ :: (left:*anytype,index:i64) -> (@type_of(left)) { - return (left + index); +__get_item_address__ :: (left:**anytype,index:i64) -> (@type_of(left.*)) { + return (left.* + index); }; //---------------------- Array ------------------------ -__get_item__ :: (left:[]anytype,index:i64) -> (@type_of(left.ptr.*)) { - return (left.ptr + index).*; +__get_item__ :: (left:*[]anytype,index:i64) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).*; +}; + +__set_item__ :: (left:*[]anytype,index:i64,item:@type_of(left.*.ptr.*)) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).* = item; +}; + +__get_item_address__ :: (left:*[]anytype,index:i64) -> (@type_of(left.*.ptr)) { + return (left.*.ptr + index); +}; + +__get_item__ :: (left:*[]anytype,index:u64) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).*; }; -__set_item__ :: (left:[]anytype,index:i64,item:@type_of(left.ptr.*)) -> (@type_of(left.ptr.*)) { - return (left.ptr + index).* = item; +__set_item__ :: (left:*[]anytype,index:u64,item:@type_of(left.*.ptr.*)) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).* = item; }; -__get_item_address__ :: (left:[]anytype,index:i64) -> (@type_of(left.ptr)) { - return (left.ptr + index); +__get_item_address__ :: (left:*[]anytype,index:u64) -> (@type_of(left.*.ptr)) { + return (left.*.ptr + index); }; diff --git a/code/lib/print.felan b/code/lib/print.felan deleted file mode 100644 index 2680921..0000000 --- a/code/lib/print.felan +++ /dev/null @@ -1,172 +0,0 @@ -print :: (value:bool)->void{ - if value print("true"); - else print("false"); -}; - -print :: (value:u8)->void{ - putc value; -}; - -print :: (value:[]u8)->void{ - i :u64= 0; - while i < value.length { - putc value[i]; - i += 1; - } -}; - -print_reverse :: (value:[]u8, size:u8)->void{ - size := size; - while size != 0 { - size -= 1; - putc value[size]; - } -}; - -print :: (value:i8)->void { - value := value; - if (value < 0) { - putc '-'; - if (value == -128) { - print("128"); - return; - } - value = -value; - } - - output :[3]u8= undefined; - i :u8= 0; - while { - output[i] = '0' + @cast(value % 10, u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(output, i - 1); -}; - -print :: (value:i16)->void { - value := value; - if (value < 0) { - putc '-'; - if (value == -32768) { - print("32768"); - return; - } - value = -value; - } - - output :[5]u8= undefined; - i :u8= 0; - while { - output[i] = '0' + @cast(value % 10, u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(output, i); -}; - -print :: (value:i32)->void { - value := value; - if (value < 0) { - putc '-'; - if (value == -2147483648) { - print("2147483648"); - return; - } - value = -value; - } - - output :[10]u8= undefined; - i :u8= 0; - while { - output[i] = '0' + @cast(value % 10, u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(output, i - 1); -}; - -print :: (value:i64)->void { - value := value; - if (value < 0) { - putc '-'; - if (value == -9223372036854775808) { - print("9223372036854775808"); - return; - } - value = -value; - } - - output :[19]u8= undefined; - i :u8= 0; - while { - output[i] = '0' + @cast(value % 10, u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(output, i - 1); -}; - -print :: (value:u8)->void{ - value := value; - result :[3]u8 = undefined; - i :u8= 0; - while { - result[i] = '0' + @cast(value % 10,u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(result, i - 1); -}; - -print :: (value:u16)->void{ - value := value; - result :[5]u8 = undefined; - i :u8= 0; - while { - result[i] = '0' + @cast(value % 10,u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(result, i - 1); -}; - -print :: (value:u32)->void{ - value := value; - result :[10]u8 = undefined; - i :u8= 0; - while { - result[i] = '0' + @cast(value % 10,u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(result, i - 1); -}; - -print :: (value:u64)->void{ - value := value; - result :[20]u8 = undefined; - i :u8= 0; - while { - result[i] = '0' + @cast(value % 10,u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(result, i - 1); -}; diff --git a/code/lib/string.felan b/code/lib/string.felan index c7047b3..789b0fb 100644 --- a/code/lib/string.felan +++ b/code/lib/string.felan @@ -1,9 +1,114 @@ @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); + + 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); + } +}; + +slice :: (this:String, begin:i64, end:i64) -> String { + result:String = undefined; + + result.ptr = this.ptr + begin; + result.size = @cast(end-begin,u64); + result.capacity = 0u64; -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; }; + +delete :: (this:String) -> void { + free(this.ptr); +}; + +print :: (this:String) -> void { + i := 0u64; + while i < this.size { + print_char(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 new file mode 100644 index 0000000..61fe77c --- /dev/null +++ b/code/lib/vector.felan @@ -0,0 +1,47 @@ +@import("operator.felan"); +@import("memory.felan"); + +vector :: (comptime t:type)->type{ + return struct { + ptr : *t; + size : u64; + capacity : u64; + }; +}; + +vector_new :: (comptime t:type) -> (vector(t)) { + v : vector(t) = undefined; + v.ptr = malloc(0u64,@type_of(v.ptr.*)); + v.size = 0u64; + v.capacity = 0u64; + return v; +}; + +__get_item__ :: (left:*vector(anytype),index:i64) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).*; +}; + +__set_item__ :: (left:*vector(anytype),index:i64,item:@type_of(left.*.ptr.*)) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).* = item; +}; + +__get_item_address__ :: (left:*vector(anytype),index:i64) -> (@type_of(left.*.ptr)) { + return (left.*.ptr + index); +}; + +push_back :: (vec:*vector(anytype),value:@type_of(vec.*[0])) -> void { + _grow_if_needed(vec); + vec.*[@cast(vec.*.size,i64)] = value; + vec.*.size += 1u64; +}; + +_grow_if_needed :: (vec:*vector(anytype)) -> void { + if vec.*.size == vec.*.capacity { + vec.*.capacity = vec.*.capacity + vec.*.capacity/2u64 + 1u64; + vec.*.ptr = realloc(vec.*.ptr,vec.*.capacity); + } +}; + +delete :: (vec:vector(anytype)) -> void { + free(vec.ptr); +}; |