|
| 1 | + |
| 2 | +Casts :: module { |
| 3 | + // Float casting |
| 4 | + cast:: float(x : int) #inline { |
| 5 | + return #cast x : float; |
| 6 | + } |
| 7 | + cast:: float(x : char) #inline { |
| 8 | + return #cast x : float; |
| 9 | + } |
| 10 | + cast:: float(x : float) #inline { |
| 11 | + return #cast x : float; |
| 12 | + } |
| 13 | + cast:: float(x : double) #inline { |
| 14 | + return #cast x : float; |
| 15 | + } |
| 16 | + cast:: float(x : bool) #inline { |
| 17 | + return #cast x : float; |
| 18 | + } |
| 19 | + // Double |
| 20 | + cast:: double(x : int) #inline { |
| 21 | + return #cast x : double; |
| 22 | + } |
| 23 | + cast:: double(x : char) #inline { |
| 24 | + return #cast x : double; |
| 25 | + } |
| 26 | + cast:: double(x : float) #inline { |
| 27 | + return #cast x : double; |
| 28 | + } |
| 29 | + cast:: double(x : double) #inline { |
| 30 | + return #cast x : double; |
| 31 | + } |
| 32 | + cast:: double(x : bool) #inline { |
| 33 | + return #cast x : double; |
| 34 | + } |
| 35 | + |
| 36 | + // Int casting |
| 37 | + cast:: int(x : float) #inline { |
| 38 | + return #cast x : int; |
| 39 | + } |
| 40 | + cast:: int(x : double) #inline { |
| 41 | + return #cast x : int; |
| 42 | + } |
| 43 | + cast:: int(x : int) #inline { |
| 44 | + return #cast x : int; |
| 45 | + } |
| 46 | + cast:: int(x : uint) #inline { |
| 47 | + return #cast x : int; |
| 48 | + } |
| 49 | + cast:: int(x : bool) #inline { |
| 50 | + return #cast x : int; |
| 51 | + } |
| 52 | + |
| 53 | + cast:: uint(x : float) #inline { |
| 54 | + return #cast x : uint; |
| 55 | + } |
| 56 | + cast:: uint(x : double) #inline { |
| 57 | + return #cast x : uint; |
| 58 | + } |
| 59 | + cast:: uint(x : int) #inline { |
| 60 | + return #cast x : uint; |
| 61 | + } |
| 62 | + cast:: uint(x : uint) #inline { |
| 63 | + return #cast x : uint; |
| 64 | + } |
| 65 | + cast:: uint(x : bool) #inline { |
| 66 | + return #cast x : uint; |
| 67 | + } |
| 68 | + |
| 69 | + cast:: uint64(x : float) #inline { |
| 70 | + return #cast x : uint64; |
| 71 | + } |
| 72 | + cast:: uint64(x : double) #inline { |
| 73 | + return #cast x : uint64; |
| 74 | + } |
| 75 | + cast:: uint64(x : int) #inline { |
| 76 | + return #cast x : uint64; |
| 77 | + } |
| 78 | + cast:: uint64(x : uint) #inline { |
| 79 | + return #cast x : uint64; |
| 80 | + } |
| 81 | + cast:: uint64(x : bool) #inline { |
| 82 | + return #cast x : uint64; |
| 83 | + } |
| 84 | + |
| 85 | + cast:: uint16(x : float) #inline { |
| 86 | + return #cast x : uint16; |
| 87 | + } |
| 88 | + cast:: uint16(x : double) #inline { |
| 89 | + return #cast x : uint16; |
| 90 | + } |
| 91 | + cast:: uint16(x : int) #inline { |
| 92 | + return #cast x : uint16; |
| 93 | + } |
| 94 | + cast:: uint16(x : uint) #inline { |
| 95 | + return #cast x : uint16; |
| 96 | + } |
| 97 | + cast:: uint16(x : bool) #inline { |
| 98 | + return #cast x : uint16; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +Operators::module{ |
| 103 | + unary++ :: int(i : ref int) #inline { |
| 104 | + i += 1; |
| 105 | + return i; |
| 106 | + } |
| 107 | + unary-- :: int(i : ref int) #inline { |
| 108 | + i -= 1; |
| 109 | + return i; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +Memory :: module{ |
| 115 | + #extern malloc::*int(b : int64); |
| 116 | + #extern free::(p : *int); |
| 117 | +} |
| 118 | + |
| 119 | +Strings :: module{ |
| 120 | + #import Builtin.Memory; |
| 121 | + #import Builtin.Operators; |
| 122 | + |
| 123 | + string :: struct { |
| 124 | + address : *char; |
| 125 | + length : uint32; |
| 126 | + |
| 127 | + size::uint32(){ |
| 128 | + return length; |
| 129 | + } |
| 130 | + |
| 131 | + cast::string(c : *char){ |
| 132 | + len = 0; |
| 133 | + for(i : 0..4_294_967_296){ |
| 134 | + if(c[i] == '\0') |
| 135 | + break; |
| 136 | + len++; |
| 137 | + } |
| 138 | + address = malloc(len); |
| 139 | + for(i : 0..len){ |
| 140 | + address[i] = c[i]; |
| 141 | + } |
| 142 | + length = len; |
| 143 | + } |
| 144 | + |
| 145 | + binary+ :: string(s : ref string, c : char) #inline { |
| 146 | + s.length += 1; |
| 147 | + newAddress : *char = malloc(s.length); |
| 148 | + for(i : 0..s.length-1){ |
| 149 | + newAddress[i] = s.address[i]; |
| 150 | + } |
| 151 | + free(s.address); |
| 152 | + newAddress[s.length-1] = c; |
| 153 | + s.address = newAddress; |
| 154 | + return s; |
| 155 | + } |
| 156 | + |
| 157 | + binary+ :: string(s : ref string, otherStr : string) #inline { |
| 158 | + oldLength = s.length; |
| 159 | + s.length += otherStr.length; |
| 160 | + newAddress : *char = malloc(s.length); |
| 161 | + for(i : 0..oldLength){ |
| 162 | + newAddress[i] = s.address[i]; |
| 163 | + } |
| 164 | + free(s.address); |
| 165 | + for(i : oldLength..otherStr.length){ |
| 166 | + newAddress[i] = otherStr.address[i-oldLength]; |
| 167 | + } |
| 168 | + s.address = newAddress; |
| 169 | + return s; |
| 170 | + } |
| 171 | + |
| 172 | + binary[] :: char(s : ref string, i : int) #inline { |
| 173 | + return s.address[i]; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments