Reduce number of range and slice operators #239

Replace .. and ... with : and ..
This commit is contained in:
gingerBill
2018-08-01 21:34:59 +01:00
parent a6fe656f21
commit 0718f14774
31 changed files with 204 additions and 219 deletions
+9 -9
View File
@@ -20,29 +20,29 @@ decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string {
// TODO(bill): make this work with a buffer that's not big enough // TODO(bill): make this work with a buffer that's not big enough
assert(len(buf) >= n); assert(len(buf) >= n);
buf = buf[0..n]; buf = buf[0:n];
if a.count == 0 { if a.count == 0 {
buf[0] = '0'; buf[0] = '0';
return string(buf[0..1]); return string(buf[0:1]);
} }
w := 0; w := 0;
if a.decimal_point <= 0 { if a.decimal_point <= 0 {
buf[w] = '0'; w += 1; buf[w] = '0'; w += 1;
buf[w] = '.'; w += 1; buf[w] = '.'; w += 1;
w += digit_zero(buf[w .. w-a.decimal_point]); w += digit_zero(buf[w : w-a.decimal_point]);
w += copy(buf[w..], a.digits[0..a.count]); w += copy(buf[w:], a.digits[0:a.count]);
} else if a.decimal_point < a.count { } else if a.decimal_point < a.count {
w += copy(buf[w..], a.digits[0..a.decimal_point]); w += copy(buf[w:], a.digits[0:a.decimal_point]);
buf[w] = '.'; w += 1; buf[w] = '.'; w += 1;
w += copy(buf[w..], a.digits[a.decimal_point .. a.count]); w += copy(buf[w:], a.digits[a.decimal_point : a.count]);
} else { } else {
w += copy(buf[w..], a.digits[0..a.count]); w += copy(buf[w:], a.digits[0:a.count]);
w += digit_zero(buf[w .. w+a.decimal_point-a.count]); w += digit_zero(buf[w : w+a.decimal_point-a.count]);
} }
return string(buf[0..w]); return string(buf[0:w]);
} }
// trim trailing zeros // trim trailing zeros
+59 -59
View File
@@ -45,7 +45,7 @@ string_buffer_from_slice :: proc(backing: []byte) -> String_Buffer {
to_string :: proc(buf: String_Buffer) -> string { to_string :: proc(buf: String_Buffer) -> string {
return string(buf[..]); return string(buf[:]);
} }
@@ -53,7 +53,7 @@ write_string :: proc(buf: ^String_Buffer, s: string) {
append_string(buf, s); append_string(buf, s);
} }
write_bytes :: proc(buf: ^String_Buffer, data: []byte) { write_bytes :: proc(buf: ^String_Buffer, data: []byte) {
append(buf, ...data); append(buf, ..data);
} }
write_byte :: proc(buf: ^String_Buffer, data: byte) { write_byte :: proc(buf: ^String_Buffer, data: byte) {
append(buf, data); append(buf, data);
@@ -65,79 +65,79 @@ write_rune :: proc(buf: ^String_Buffer, r: rune) {
} }
b, n := utf8.encode_rune(r); b, n := utf8.encode_rune(r);
write_bytes(buf, b[..n]); write_bytes(buf, b[:n]);
} }
write_i64 :: proc(buf: ^String_Buffer, i: i64, base: int) { write_i64 :: proc(buf: ^String_Buffer, i: i64, base: int) {
b: [129]byte; b: [129]byte;
s := strconv.append_bits(b[..], u64(i), base, true, 64, strconv.digits, 0); s := strconv.append_bits(b[:], u64(i), base, true, 64, strconv.digits, 0);
write_string(buf, s); write_string(buf, s);
} }
fprint :: proc(fd: os.Handle, args: ...any) -> int { fprint :: proc(fd: os.Handle, args: ..any) -> int {
data: [_BUFFER_SIZE]byte; data: [_BUFFER_SIZE]byte;
buf := string_buffer_from_slice(data[..]); buf := string_buffer_from_slice(data[:]);
res := sbprint(&buf, ...args); res := sbprint(&buf, ..args);
os.write_string(fd, res); os.write_string(fd, res);
return len(res); return len(res);
} }
fprintln :: proc(fd: os.Handle, args: ...any) -> int { fprintln :: proc(fd: os.Handle, args: ..any) -> int {
data: [_BUFFER_SIZE]byte; data: [_BUFFER_SIZE]byte;
buf := string_buffer_from_slice(data[..]); buf := string_buffer_from_slice(data[:]);
res := sbprintln(&buf, ...args); res := sbprintln(&buf, ..args);
os.write_string(fd, res); os.write_string(fd, res);
return len(res); return len(res);
} }
fprintf :: proc(fd: os.Handle, fmt: string, args: ...any) -> int { fprintf :: proc(fd: os.Handle, fmt: string, args: ..any) -> int {
data: [_BUFFER_SIZE]byte; data: [_BUFFER_SIZE]byte;
buf := string_buffer_from_slice(data[..]); buf := string_buffer_from_slice(data[:]);
res := sbprintf(&buf, fmt, ...args); res := sbprintf(&buf, fmt, ..args);
os.write_string(fd, res); os.write_string(fd, res);
return len(res); return len(res);
} }
// print* procedures return the number of bytes written // print* procedures return the number of bytes written
print :: proc(args: ...any) -> int { return fprint(os.stdout, ...args); } print :: proc(args: ..any) -> int { return fprint(os.stdout, ..args); }
print_err :: proc(args: ...any) -> int { return fprint(os.stderr, ...args); } print_err :: proc(args: ..any) -> int { return fprint(os.stderr, ..args); }
println :: proc(args: ...any) -> int { return fprintln(os.stdout, ...args); } println :: proc(args: ..any) -> int { return fprintln(os.stdout, ..args); }
println_err :: proc(args: ...any) -> int { return fprintln(os.stderr, ...args); } println_err :: proc(args: ..any) -> int { return fprintln(os.stderr, ..args); }
printf :: proc(fmt: string, args: ...any) -> int { return fprintf(os.stdout, fmt, ...args); } printf :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stdout, fmt, ..args); }
printf_err :: proc(fmt: string, args: ...any) -> int { return fprintf(os.stderr, fmt, ...args); } printf_err :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stderr, fmt, ..args); }
// aprint* procedures return a string that was allocated with the current context // aprint* procedures return a string that was allocated with the current context
// They must be freed accordingly // They must be freed accordingly
aprint :: proc(args: ...any) -> string { aprint :: proc(args: ..any) -> string {
buf := String_Buffer(make([dynamic]byte)); buf := String_Buffer(make([dynamic]byte));
sbprint(&buf, ...args); sbprint(&buf, ..args);
return to_string(buf); return to_string(buf);
} }
aprintln :: proc(args: ...any) -> string { aprintln :: proc(args: ..any) -> string {
buf := String_Buffer(make([dynamic]byte)); buf := String_Buffer(make([dynamic]byte));
sbprintln(&buf, ...args); sbprintln(&buf, ..args);
return to_string(buf); return to_string(buf);
} }
aprintf :: proc(fmt: string, args: ...any) -> string { aprintf :: proc(fmt: string, args: ..any) -> string {
buf := String_Buffer(make([dynamic]byte)); buf := String_Buffer(make([dynamic]byte));
sbprintf(&buf, fmt, ...args); sbprintf(&buf, fmt, ..args);
return to_string(buf); return to_string(buf);
} }
// bprint* procedures return a string using a buffer from an array // bprint* procedures return a string using a buffer from an array
bprint :: proc(buf: []byte, args: ...any) -> string { bprint :: proc(buf: []byte, args: ..any) -> string {
sb := string_buffer_from_slice(buf[0..len(buf)]); sb := string_buffer_from_slice(buf[0:len(buf)]);
return sbprint(&sb, ...args); return sbprint(&sb, ..args);
} }
bprintln :: proc(buf: []byte, args: ...any) -> string { bprintln :: proc(buf: []byte, args: ..any) -> string {
sb := string_buffer_from_slice(buf[0..len(buf)]); sb := string_buffer_from_slice(buf[0:len(buf)]);
return sbprintln(&sb, ...args); return sbprintln(&sb, ..args);
} }
bprintf :: proc(buf: []byte, fmt: string, args: ...any) -> string { bprintf :: proc(buf: []byte, fmt: string, args: ..any) -> string {
sb := string_buffer_from_slice(buf[0..len(buf)]); sb := string_buffer_from_slice(buf[0:len(buf)]);
return sbprintf(&sb, fmt, ...args); return sbprintf(&sb, fmt, ..args);
} }
@@ -147,9 +147,9 @@ bprintf :: proc(buf: []byte, fmt: string, args: ...any) -> string {
fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info) { fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info) {
data: [_BUFFER_SIZE]byte; data: [_BUFFER_SIZE]byte;
buf := string_buffer_from_slice(data[..]); buf := string_buffer_from_slice(data[:]);
write_type(&buf, info); write_type(&buf, info);
os.write(fd, buf[..]); os.write(fd, buf[:]);
} }
write_typeid :: proc(buf: ^String_Buffer, id: typeid) { write_typeid :: proc(buf: ^String_Buffer, id: typeid) {
@@ -336,7 +336,7 @@ _arg_number :: proc(fi: ^Fmt_Info, arg_index: int, format: string, offset, arg_c
parse_arg_number :: proc(format: string) -> (int, int, bool) { parse_arg_number :: proc(format: string) -> (int, int, bool) {
if len(format) < 3 do return 0, 1, false; if len(format) < 3 do return 0, 1, false;
for i in 1...len(format) { for i in 1..len(format)-1 {
if format[i] == ']' { if format[i] == ']' {
width, new_index, ok := _parse_int(format, 1); width, new_index, ok := _parse_int(format, 1);
if !ok || new_index != i { if !ok || new_index != i {
@@ -356,7 +356,7 @@ _arg_number :: proc(fi: ^Fmt_Info, arg_index: int, format: string, offset, arg_c
fi.reordered = true; fi.reordered = true;
width: int; width: int;
index, width, ok = parse_arg_number(format[offset..]); index, width, ok = parse_arg_number(format[offset:]);
if ok && 0 <= index && index < arg_count { if ok && 0 <= index && index < arg_count {
return index, offset+width, true; return index, offset+width, true;
} }
@@ -421,7 +421,7 @@ fmt_write_padding :: proc(fi: ^Fmt_Info, width: int) {
pad_byte: byte = '0'; pad_byte: byte = '0';
if fi.space do pad_byte = ' '; if fi.space do pad_byte = ' ';
for _ in 0..width { for _ in 0..width-1 {
write_byte(fi.buf, pad_byte); write_byte(fi.buf, pad_byte);
} }
} }
@@ -470,7 +470,7 @@ _fmt_int :: proc(fi: ^Fmt_Info, u: u64, base: int, is_signed: bool, bit_size: in
if fi.hash && !fi.zero do flags |= strconv.Int_Flag.Prefix; if fi.hash && !fi.zero do flags |= strconv.Int_Flag.Prefix;
if fi.plus do flags |= strconv.Int_Flag.Plus; if fi.plus do flags |= strconv.Int_Flag.Plus;
if fi.space do flags |= strconv.Int_Flag.Space; if fi.space do flags |= strconv.Int_Flag.Space;
s := strconv.append_bits(buf[start..], u, base, is_signed, bit_size, digits, flags); s := strconv.append_bits(buf[start:], u, base, is_signed, bit_size, digits, flags);
if fi.hash && fi.zero { if fi.hash && fi.zero {
c: byte = 0; c: byte = 0;
@@ -557,10 +557,10 @@ fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
if fi.prec_set do prec = fi.prec; if fi.prec_set do prec = fi.prec;
buf: [386]byte; buf: [386]byte;
str := strconv.append_float(buf[1..], v, 'f', prec, bit_size); str := strconv.append_float(buf[1:], v, 'f', prec, bit_size);
str = string(buf[...len(str)]); str = string(buf[:len(str)+1]);
if str[1] == '+' || str[1] == '-' { if str[1] == '+' || str[1] == '-' {
str = str[1..]; str = str[1:];
} else { } else {
str[0] = '+'; str[0] = '+';
} }
@@ -578,12 +578,12 @@ fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
if fi.zero && fi.width_set && fi.width > len(str) { if fi.zero && fi.width_set && fi.width > len(str) {
write_byte(fi.buf, str[0]); write_byte(fi.buf, str[0]);
fmt_write_padding(fi, fi.width - len(str)); fmt_write_padding(fi, fi.width - len(str));
write_string(fi.buf, str[1..]); write_string(fi.buf, str[1:]);
} else { } else {
_pad(fi, str); _pad(fi, str);
} }
} else { } else {
_pad(fi, str[1..]); _pad(fi, str[1:]);
} }
case: case:
@@ -600,7 +600,7 @@ fmt_string :: proc(fi: ^Fmt_Info, s: string, verb: rune) {
fi.space = false; fi.space = false;
defer fi.space = space; defer fi.space = space;
for i in 0..len(s) { for i in 0..len(s)-1 {
if i > 0 && space do write_byte(fi.buf, ' '); if i > 0 && space do write_byte(fi.buf, ' ');
char_set := __DIGITS_UPPER; char_set := __DIGITS_UPPER;
if verb == 'x' do char_set = __DIGITS_LOWER; if verb == 'x' do char_set = __DIGITS_LOWER;
@@ -747,7 +747,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
for _, i in b.names { for _, i in b.names {
if !hash && i > 0 do write_string(fi.buf, ", "); if !hash && i > 0 do write_string(fi.buf, ", ");
if hash do for in 0..fi.indent do write_byte(fi.buf, '\t'); if hash do for in 0..fi.indent-1 do write_byte(fi.buf, '\t');
write_string(fi.buf, b.names[i]); write_string(fi.buf, b.names[i]);
write_string(fi.buf, " = "); write_string(fi.buf, " = ");
@@ -762,7 +762,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
if hash do write_string(fi.buf, ",\n"); if hash do write_string(fi.buf, ",\n");
} }
if hash do for in 0..indent do write_byte(fi.buf, '\t'); if hash do for in 0..indent-1 do write_byte(fi.buf, '\t');
write_byte(fi.buf, '}'); write_byte(fi.buf, '}');
case: case:
@@ -786,7 +786,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
case runtime.Type_Info_Array: case runtime.Type_Info_Array:
write_byte(fi.buf, '['); write_byte(fi.buf, '[');
defer write_byte(fi.buf, ']'); defer write_byte(fi.buf, ']');
for i in 0..info.count { for i in 0..info.count-1 {
if i > 0 do write_string(fi.buf, ", "); if i > 0 do write_string(fi.buf, ", ");
data := uintptr(v.data) + uintptr(i*info.elem_size); data := uintptr(v.data) + uintptr(i*info.elem_size);
@@ -797,7 +797,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
write_byte(fi.buf, '['); write_byte(fi.buf, '[');
defer write_byte(fi.buf, ']'); defer write_byte(fi.buf, ']');
array := cast(^mem.Raw_Dynamic_Array)v.data; array := cast(^mem.Raw_Dynamic_Array)v.data;
for i in 0..array.len { for i in 0..array.len-1 {
if i > 0 do write_string(fi.buf, ", "); if i > 0 do write_string(fi.buf, ", ");
data := uintptr(array.data) + uintptr(i*info.elem_size); data := uintptr(array.data) + uintptr(i*info.elem_size);
@@ -808,7 +808,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
write_byte(fi.buf, '['); write_byte(fi.buf, '[');
defer write_byte(fi.buf, ']'); defer write_byte(fi.buf, ']');
slice := cast(^mem.Raw_Slice)v.data; slice := cast(^mem.Raw_Slice)v.data;
for i in 0..slice.len { for i in 0..slice.len-1 {
if i > 0 do write_string(fi.buf, ", "); if i > 0 do write_string(fi.buf, ", ");
data := uintptr(slice.data) + uintptr(i*info.elem_size); data := uintptr(slice.data) + uintptr(i*info.elem_size);
@@ -833,7 +833,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
entry_type := ed.elem.variant.(runtime.Type_Info_Struct); entry_type := ed.elem.variant.(runtime.Type_Info_Struct);
entry_size := ed.elem_size; entry_size := ed.elem_size;
for i in 0..entries.len { for i in 0..entries.len-1 {
if i > 0 do write_string(fi.buf, ", "); if i > 0 do write_string(fi.buf, ", ");
data := uintptr(entries.data) + uintptr(i*entry_size); data := uintptr(entries.data) + uintptr(i*entry_size);
@@ -872,7 +872,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
for _, i in info.names { for _, i in info.names {
if !hash && i > 0 do write_string(fi.buf, ", "); if !hash && i > 0 do write_string(fi.buf, ", ");
if hash { if hash {
for in 0..fi.indent { for in 0..fi.indent-1 {
write_byte(fi.buf, '\t'); write_byte(fi.buf, '\t');
} }
} }
@@ -1007,7 +1007,7 @@ fmt_arg :: proc(fi: ^Fmt_Info, arg: any, verb: rune) {
sbprint :: proc(buf: ^String_Buffer, args: ...any) -> string { sbprint :: proc(buf: ^String_Buffer, args: ..any) -> string {
fi: Fmt_Info; fi: Fmt_Info;
prev_string := false; prev_string := false;
@@ -1024,7 +1024,7 @@ sbprint :: proc(buf: ^String_Buffer, args: ...any) -> string {
return to_string(buf^); return to_string(buf^);
} }
sbprintln :: proc(buf: ^String_Buffer, args: ...any) -> string { sbprintln :: proc(buf: ^String_Buffer, args: ..any) -> string {
fi: Fmt_Info; fi: Fmt_Info;
fi.buf = buf; fi.buf = buf;
@@ -1037,7 +1037,7 @@ sbprintln :: proc(buf: ^String_Buffer, args: ...any) -> string {
return to_string(buf^); return to_string(buf^);
} }
sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string { sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ..any) -> string {
fi: Fmt_Info; fi: Fmt_Info;
arg_index: int = 0; arg_index: int = 0;
end := len(fmt); end := len(fmt);
@@ -1052,7 +1052,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
i += 1; i += 1;
} }
if i > prev_i { if i > prev_i {
write_string(b, fmt[prev_i..i]); write_string(b, fmt[prev_i:i]);
} }
if i >= end { if i >= end {
break loop; break loop;
@@ -1138,7 +1138,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
break loop; break loop;
} }
verb, w := utf8.decode_rune_from_string(fmt[i..]); verb, w := utf8.decode_rune_from_string(fmt[i:]);
i += w; i += w;
switch { switch {
@@ -1156,7 +1156,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
if !fi.reordered && arg_index < len(args) { if !fi.reordered && arg_index < len(args) {
write_string(b, "%!(EXTRA "); write_string(b, "%!(EXTRA ");
for arg, index in args[arg_index..] { for arg, index in args[arg_index:] {
if index > 0 do write_string(b, ", "); if index > 0 do write_string(b, ", ");
if arg == nil do write_string(b, "<nil>"); if arg == nil do write_string(b, "<nil>");
+2 -2
View File
@@ -80,7 +80,7 @@ murmur32 :: proc(data: []byte) -> u32 {
h1 = h1*5 + 0xe6546b64; h1 = h1*5 + 0xe6546b64;
} }
tail := data[nblocks*4 ..]; tail := data[nblocks*4:];
k1: u32; k1: u32;
switch len(tail)&3 { switch len(tail)&3 {
case 3: case 3:
@@ -187,7 +187,7 @@ murmur64 :: proc(data: []byte) -> u64 {
} }
// TODO(bill): Fix this // TODO(bill): Fix this
#no_bounds_check data8 := mem.slice_to_bytes(data32[i..])[..3]; #no_bounds_check data8 := mem.slice_to_bytes(data32[i:])[:3];
switch len { switch len {
case 3: case 3:
h2 ~= u32(data8[2]) << 16; h2 ~= u32(data8[2]) << 16;
+8 -8
View File
@@ -164,7 +164,7 @@ cross :: proc[cross2, cross3];
vec_dot :: proc(a, b: $T/[$N]$E) -> E { vec_dot :: proc(a, b: $T/[$N]$E) -> E {
res: E; res: E;
for i in 0..N { for i in 0..N-1 {
res += a[i] * b[i]; res += a[i] * b[i];
} }
return res; return res;
@@ -194,13 +194,13 @@ norm0 :: proc(v: $T/[$N]$E) -> T {
identity :: proc(T: type/[$N][N]$E) -> T { identity :: proc(T: type/[$N][N]$E) -> T {
m: T; m: T;
for i in 0..N do m[i][i] = E(1); for i in 0..N-1 do m[i][i] = E(1);
return m; return m;
} }
transpose :: proc(m: $M/[$N][N]f32) -> M { transpose :: proc(m: $M/[$N][N]f32) -> M {
for j in 0..N { for j in 0..N-1 {
for i in 0..N { for i in 0..N-1 {
m[i][j], m[j][i] = m[j][i], m[i][j]; m[i][j], m[j][i] = m[j][i], m[i][j];
} }
} }
@@ -209,8 +209,8 @@ transpose :: proc(m: $M/[$N][N]f32) -> M {
mat3_mul :: proc(a, b: Mat3) -> Mat3 { mat3_mul :: proc(a, b: Mat3) -> Mat3 {
c: Mat3; c: Mat3;
for j in 0..3 { for j in 0..2 {
for i in 0..3 { for i in 0..2 {
c[j][i] = a[0][i]*b[j][0] + c[j][i] = a[0][i]*b[j][0] +
a[1][i]*b[j][1] + a[1][i]*b[j][1] +
a[2][i]*b[j][2]; a[2][i]*b[j][2];
@@ -221,8 +221,8 @@ mat3_mul :: proc(a, b: Mat3) -> Mat3 {
mat4_mul :: proc(a, b: Mat4) -> Mat4 { mat4_mul :: proc(a, b: Mat4) -> Mat4 {
c: Mat4; c: Mat4;
for j in 0..4 { for j in 0..3 {
for i in 0..4 { for i in 0..3 {
c[j][i] = a[0][i]*b[j][0] + c[j][i] = a[0][i]*b[j][0] +
a[1][i]*b[j][1] + a[1][i]*b[j][1] +
a[2][i]*b[j][2] + a[2][i]*b[j][2] +
+2 -2
View File
@@ -62,7 +62,7 @@ compare :: proc "contextless" (a, b: []byte) -> int {
} }
compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int { compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int {
pa :: ptr_offset; pa :: ptr_offset;
for i in 0..n do switch { for i in 0..n-1 do switch {
case pa(a, i)^ < pa(b, i)^: return -1; case pa(a, i)^ < pa(b, i)^: return -1;
case pa(a, i)^ > pa(b, i)^: return +1; case pa(a, i)^ > pa(b, i)^: return +1;
} }
@@ -147,7 +147,7 @@ allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: in
ptr := cast(^uint)(ptr_offset(header, 1)); ptr := cast(^uint)(ptr_offset(header, 1));
n := ptr_sub(cast(^uint)data, ptr); n := ptr_sub(cast(^uint)data, ptr);
for i in 0..n { for i in 0..n-1 {
ptr_offset(ptr, i)^ = ~uint(0); ptr_offset(ptr, i)^ = ~uint(0);
} }
} }
+1 -1
View File
@@ -46,7 +46,7 @@ _libgl := win32.load_library_a("opengl32.dll");
get_gl_proc_address :: proc(name: string) -> rawptr { get_gl_proc_address :: proc(name: string) -> rawptr {
if name[len(name)-1] == 0 { if name[len(name)-1] == 0 {
name = name[..len(name)-1]; name = name[:len(name)-1];
} }
// NOTE(bill): null terminated // NOTE(bill): null terminated
assert(mem.ptr_offset(&name[0], cast(uintptr)len(name))^ == 0); assert(mem.ptr_offset(&name[0], cast(uintptr)len(name))^ == 0);
+1 -1
View File
@@ -37,7 +37,7 @@ read_entire_file :: proc(name: string) -> (data: []byte, success: bool) {
delete(data); delete(data);
return nil, false; return nil, false;
} }
return data[0..bytes_read], true; return data[0:bytes_read], true;
} }
write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) { write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
+1 -1
View File
@@ -49,7 +49,7 @@ OS_Node_Information :: struct {
ntype: OS_Node_Type, ntype: OS_Node_Type,
size: i64, size: i64,
// Our additions... // Our additions..
position: i64, position: i64,
} }
+1 -1
View File
@@ -74,7 +74,7 @@ Stat :: struct {
_reserve1, _reserve1,
_reserve2, _reserve2,
_reserve3: i64, _reserve3: i64,
serial_numbe: u64, // File serial number...? Maybe. serial_numbe: u64, // File serial number..? Maybe.
_reserve4: i64, _reserve4: i64,
}; };
+1 -1
View File
@@ -73,7 +73,7 @@ Stat :: struct {
blocks: i64, // Number of blocks allocated for the file blocks: i64, // Number of blocks allocated for the file
block_size: i32, // Optimal blocksize for I/O block_size: i32, // Optimal blocksize for I/O
flags: u32, // User-defined flags for the file flags: u32, // User-defined flags for the file
gen_num: u32, // File generation number ...? gen_num: u32, // File generation number ..?
_spare: i32, // RESERVED _spare: i32, // RESERVED
_reserve1, _reserve1,
_reserve2: i64, // RESERVED _reserve2: i64, // RESERVED
+3 -3
View File
@@ -100,7 +100,7 @@ open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errn
} }
buf: [300]byte; buf: [300]byte;
copy(buf[..], cast([]byte)path); copy(buf[:], cast([]byte)path);
handle := Handle(win32.create_file_a(cstring(&buf[0]), access, share_mode, sa, create_mode, win32.FILE_ATTRIBUTE_NORMAL, nil)); handle := Handle(win32.create_file_a(cstring(&buf[0]), access, share_mode, sa, create_mode, win32.FILE_ATTRIBUTE_NORMAL, nil));
if handle != INVALID_HANDLE do return handle, ERROR_NONE; if handle != INVALID_HANDLE do return handle, ERROR_NONE;
@@ -221,7 +221,7 @@ last_write_time_by_name :: proc(name: string) -> File_Time {
assert(len(buf) > len(name)); assert(len(buf) > len(name));
copy(buf[..], cast([]byte)name); copy(buf[:], cast([]byte)name);
if win32.get_file_attributes_ex_a(cstring(&buf[0]), win32.GetFileExInfoStandard, &data) { if win32.get_file_attributes_ex_a(cstring(&buf[0]), win32.GetFileExInfoStandard, &data) {
last_write_time = data.last_write_time; last_write_time = data.last_write_time;
@@ -279,7 +279,7 @@ _alloc_command_line_arguments :: proc() -> []string {
if n > 0 { if n > 0 {
n -= 1; n -= 1;
} }
arg_list[i] = string(buf[..n]); arg_list[i] = string(buf[:n]);
} }
return arg_list; return arg_list;
+4 -4
View File
@@ -350,7 +350,7 @@ delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
@(builtin) @(builtin)
append :: proc(array: ^$T/[dynamic]$E, args: ...E, loc := #caller_location) -> int { append :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> int {
if array == nil do return 0; if array == nil do return 0;
arg_len := len(args); arg_len := len(args);
@@ -373,7 +373,7 @@ append :: proc(array: ^$T/[dynamic]$E, args: ...E, loc := #caller_location) -> i
} }
@(builtin) @(builtin)
append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ...string, loc := #caller_location) -> int { append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) -> int {
for arg in args { for arg in args {
append(array = array, args = ([]E)(arg), loc = loc); append(array = array, args = ([]E)(arg), loc = loc);
} }
@@ -616,9 +616,9 @@ __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #c
__dynamic_array_resize(nm_hashes, size_of(int), align_of(int), new_count, loc); __dynamic_array_resize(nm_hashes, size_of(int), align_of(int), new_count, loc);
__dynamic_array_reserve(&nm.entries, entry_size, entry_align, m.entries.len, loc); __dynamic_array_reserve(&nm.entries, entry_size, entry_align, m.entries.len, loc);
for i in 0..new_count do nm.hashes[i] = -1; for i in 0..new_count-1 do nm.hashes[i] = -1;
for i in 0..m.entries.len { for i in 0..m.entries.len-1 {
if len(nm.hashes) == 0 do __dynamic_map_grow(new_header, loc); if len(nm.hashes) == 0 do __dynamic_map_grow(new_header, loc);
entry_header := __dynamic_map_get_entry(header, i); entry_header := __dynamic_map_get_entry(header, i);
+7 -7
View File
@@ -17,7 +17,7 @@ __print_u64 :: proc(fd: os.Handle, u: u64) {
} }
i -= 1; a[i] = digits[u % b]; i -= 1; a[i] = digits[u % b];
os.write(fd, a[i..]); os.write(fd, a[i:]);
} }
__print_i64 :: proc(fd: os.Handle, u: i64) { __print_i64 :: proc(fd: os.Handle, u: i64) {
@@ -38,7 +38,7 @@ __print_i64 :: proc(fd: os.Handle, u: i64) {
i -= 1; a[i] = '-'; i -= 1; a[i] = '-';
} }
os.write(fd, a[i..]); os.write(fd, a[i:]);
} }
__print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) { __print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) {
@@ -256,7 +256,7 @@ bounds_check_error :: proc "contextless" (file: string, line, column: int, index
__print_caller_location(fd, Source_Code_Location{file, line, column, ""}); __print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Index "); os.write_string(fd, " Index ");
__print_i64(fd, i64(index)); __print_i64(fd, i64(index));
os.write_string(fd, " is out of bounds range 0.."); os.write_string(fd, " is out of bounds range 0:");
__print_i64(fd, i64(count)); __print_i64(fd, i64(count));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); debug_trap();
@@ -270,9 +270,9 @@ slice_expr_error :: proc "contextless" (file: string, line, column: int, lo, hi:
__print_caller_location(fd, Source_Code_Location{file, line, column, ""}); __print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Invalid slice indices: "); os.write_string(fd, " Invalid slice indices: ");
__print_i64(fd, i64(lo)); __print_i64(fd, i64(lo));
os.write_string(fd, ".."); os.write_string(fd, ":");
__print_i64(fd, i64(hi)); __print_i64(fd, i64(hi));
os.write_string(fd, ".."); os.write_string(fd, ":");
__print_i64(fd, i64(len)); __print_i64(fd, i64(len));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); debug_trap();
@@ -285,9 +285,9 @@ dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int,
__print_caller_location(fd, Source_Code_Location{file, line, column, ""}); __print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Invalid dynamic array values: "); os.write_string(fd, " Invalid dynamic array values: ");
__print_i64(fd, i64(low)); __print_i64(fd, i64(low));
os.write_string(fd, ".."); os.write_string(fd, ":");
__print_i64(fd, i64(high)); __print_i64(fd, i64(high));
os.write_string(fd, ".."); os.write_string(fd, ":");
__print_i64(fd, i64(max)); __print_i64(fd, i64(max));
os.write_byte(fd, '\n'); os.write_byte(fd, '\n');
debug_trap(); debug_trap();
+18 -18
View File
@@ -11,7 +11,7 @@ bubble_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
for { for {
init_swap, prev_swap := -1, -1; init_swap, prev_swap := -1, -1;
for j in init_j..last_j { for j in init_j..last_j-1 {
if f(array[j], array[j+1]) > 0 { if f(array[j], array[j+1]) > 0 {
array[j], array[j+1] = array[j+1], array[j]; array[j], array[j+1] = array[j+1], array[j];
prev_swap = j; prev_swap = j;
@@ -34,7 +34,7 @@ bubble_sort :: proc(array: $A/[]$T) {
for { for {
init_swap, prev_swap := -1, -1; init_swap, prev_swap := -1, -1;
for j in init_j..last_j { for j in init_j..last_j-1 {
if array[j] > array[j+1] { if array[j] > array[j+1] {
array[j], array[j+1] = array[j+1], array[j]; array[j], array[j+1] = array[j+1], array[j];
prev_swap = j; prev_swap = j;
@@ -69,8 +69,8 @@ quick_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
j -= 1; j -= 1;
} }
quick_sort_proc(a[0..i], f); quick_sort_proc(a[0:i], f);
quick_sort_proc(a[i..n], f); quick_sort_proc(a[i:n], f);
} }
quick_sort :: proc(array: $A/[]$T) { quick_sort :: proc(array: $A/[]$T) {
@@ -92,8 +92,8 @@ quick_sort :: proc(array: $A/[]$T) {
j -= 1; j -= 1;
} }
quick_sort(a[0..i]); quick_sort(a[0:i]);
quick_sort(a[i..n]); quick_sort(a[i:n]);
} }
_log2 :: proc(n: int) -> int { _log2 :: proc(n: int) -> int {
@@ -106,7 +106,7 @@ merge_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
merge_slices :: proc(arr1, arr2, out: A, f: proc(T, T) -> int) { merge_slices :: proc(arr1, arr2, out: A, f: proc(T, T) -> int) {
N1, N2 := len(arr1), len(arr2); N1, N2 := len(arr1), len(arr2);
i, j := 0, 0; i, j := 0, 0;
for k in 0..N1+N2 { for k in 0..N1+N2-1 {
if j == N2 || i < N1 && j < N2 && f(arr1[i], arr2[j]) < 0 { if j == N2 || i < N1 && j < N2 && f(arr1[i], arr2[j]) < 0 {
out[k] = arr1[i]; out[k] = arr1[i];
i += 1; i += 1;
@@ -126,16 +126,16 @@ merge_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
a, b, m, M := N/2, N, 1, _log2(N); a, b, m, M := N/2, N, 1, _log2(N);
for i in 0..M+1 { for i in 0..M {
for j in 0..a { for j in 0..a-1 {
k := 2*j*m; k := 2*j*m;
merge_slices(arr1[k..k+m], arr1[k+m..k+m+m], arr2[k..], f); merge_slices(arr1[k:k+m], arr1[k+m:k+m+m], arr2[k:], f);
} }
if N-b > m { if N-b > m {
k := 2*a*m; k := 2*a*m;
merge_slices(arr1[k..k+m], arr1[k+m..k+m+(N-b)&(m-1)], arr2[k..], f); merge_slices(arr1[k:k+m], arr1[k+m : k+m+(N-b)&(m-1)], arr2[k:], f);
} else { } else {
copy(arr2[b..N], arr1[b..N]); copy(arr2[b:N], arr1[b:N]);
} }
arr1, arr2 = arr2, arr1; arr1, arr2 = arr2, arr1;
m <<= 1; m <<= 1;
@@ -150,7 +150,7 @@ merge_sort :: proc(array: $A/[]$T) {
merge_slices :: proc(arr1, arr2, out: A) { merge_slices :: proc(arr1, arr2, out: A) {
N1, N2 := len(arr1), len(arr2); N1, N2 := len(arr1), len(arr2);
i, j := 0, 0; i, j := 0, 0;
for k in 0..N1+N2 { for k in 0..N1+N2-1 {
if j == N2 || i < N1 && j < N2 && arr1[i] < arr2[j] { if j == N2 || i < N1 && j < N2 && arr1[i] < arr2[j] {
out[k] = arr1[i]; out[k] = arr1[i];
i += 1; i += 1;
@@ -168,16 +168,16 @@ merge_sort :: proc(array: $A/[]$T) {
a, b, m, M := N/2, N, 1, _log2(N); a, b, m, M := N/2, N, 1, _log2(N);
for i in 0..M+1 { for i in 0..M {
for j in 0..a { for j in 0..a-1 {
k := 2*j*m; k := 2*j*m;
merge_slices(arr1[k..k+m], arr1[k+m..k+m+m], arr2[k..]); merge_slices(arr1[k:k+m], arr1[k+m:k+m+m], arr2[k:]);
} }
if N-b > m { if N-b > m {
k := 2*a*m; k := 2*a*m;
merge_slices(arr1[k..k+m], arr1[k+m..k+m+(N-b)&(m-1)], arr2[k..]); merge_slices(arr1[k:k+m], arr1[k+m : k+m+(N-b)&(m-1)], arr2[k:]);
} else { } else {
copy(arr2[b..N], arr1[b..N]); copy(arr2[b:N], arr1[b:N]);
} }
arr1, arr2 = arr2, arr1; arr1, arr2 = arr2, arr1;
m <<= 1; m <<= 1;
+28 -28
View File
@@ -23,9 +23,9 @@ _digit_value :: proc(r: rune) -> int {
ri := int(r); ri := int(r);
v: int = 16; v: int = 16;
switch r { switch r {
case '0'...'9': v = ri-'0'; case '0'..'9': v = ri-'0';
case 'a'...'z': v = ri-'a'+10; case 'a'..'z': v = ri-'a'+10;
case 'A'...'Z': v = ri-'A'+10; case 'A'..'Z': v = ri-'A'+10;
} }
return v; return v;
} }
@@ -36,9 +36,9 @@ parse_i64 :: proc(s: string) -> i64 {
switch s[0] { switch s[0] {
case '-': case '-':
neg = true; neg = true;
s = s[1..]; s = s[1:];
case '+': case '+':
s = s[1..]; s = s[1:];
} }
} }
@@ -46,11 +46,11 @@ parse_i64 :: proc(s: string) -> i64 {
base: i64 = 10; base: i64 = 10;
if len(s) > 2 && s[0] == '0' { if len(s) > 2 && s[0] == '0' {
switch s[1] { switch s[1] {
case 'b': base = 2; s = s[2..]; case 'b': base = 2; s = s[2:];
case 'o': base = 8; s = s[2..]; case 'o': base = 8; s = s[2:];
case 'd': base = 10; s = s[2..]; case 'd': base = 10; s = s[2:];
case 'z': base = 12; s = s[2..]; case 'z': base = 12; s = s[2:];
case 'x': base = 16; s = s[2..]; case 'x': base = 16; s = s[2:];
} }
} }
@@ -76,18 +76,18 @@ parse_i64 :: proc(s: string) -> i64 {
parse_u64 :: proc(s: string) -> u64 { parse_u64 :: proc(s: string) -> u64 {
neg := false; neg := false;
if len(s) > 1 && s[0] == '+' { if len(s) > 1 && s[0] == '+' {
s = s[1..]; s = s[1:];
} }
base := u64(10); base := u64(10);
if len(s) > 2 && s[0] == '0' { if len(s) > 2 && s[0] == '0' {
switch s[1] { switch s[1] {
case 'b': base = 2; s = s[2..]; case 'b': base = 2; s = s[2:];
case 'o': base = 8; s = s[2..]; case 'o': base = 8; s = s[2:];
case 'd': base = 10; s = s[2..]; case 'd': base = 10; s = s[2:];
case 'z': base = 12; s = s[2..]; case 'z': base = 12; s = s[2:];
case 'x': base = 16; s = s[2..]; case 'x': base = 16; s = s[2:];
} }
} }
@@ -194,7 +194,7 @@ append_bool :: proc(buf: []byte, b: bool) -> string {
n := 0; n := 0;
if b do n = copy(buf, cast([]byte)"true"); if b do n = copy(buf, cast([]byte)"true");
else do n = copy(buf, cast([]byte)"false"); else do n = copy(buf, cast([]byte)"false");
return string(buf[..n]); return string(buf[:n]);
} }
append_uint :: proc(buf: []byte, u: u64, base: int) -> string { append_uint :: proc(buf: []byte, u: u64, base: int) -> string {
@@ -260,7 +260,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
s = "+Inf"; s = "+Inf";
} }
n := copy(buf, cast([]byte)s); n := copy(buf, cast([]byte)s);
return buf[..n]; return buf[:n];
case 0: // denormalized case 0: // denormalized
exp += 1; exp += 1;
@@ -279,7 +279,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
shortest := prec < 0; shortest := prec < 0;
if shortest { if shortest {
round_shortest(d, mant, exp, flt); round_shortest(d, mant, exp, flt);
digs = DecimalSlice{digits = d.digits[..], count = d.count, decimal_point = d.decimal_point}; digs = DecimalSlice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point};
switch fmt { switch fmt {
case 'e', 'E': prec = digs.count-1; case 'e', 'E': prec = digs.count-1;
case 'f', 'F': prec = max(digs.count-digs.decimal_point, 0); case 'f', 'F': prec = max(digs.count-digs.decimal_point, 0);
@@ -296,7 +296,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
round(d, prec); round(d, prec);
} }
digs = DecimalSlice{digits = d.digits[..], count = d.count, decimal_point = d.decimal_point}; digs = DecimalSlice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point};
} }
return format_digits(buf, shortest, neg, digs, prec, fmt); return format_digits(buf, shortest, neg, digs, prec, fmt);
} }
@@ -309,9 +309,9 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: DecimalSlice
n: int, n: int,
} }
to_bytes :: proc(b: Buffer) -> []byte do return b.b[..b.n]; to_bytes :: proc(b: Buffer) -> []byte do return b.b[:b.n];
add_bytes :: proc(buf: ^Buffer, bytes: ...byte) { add_bytes :: proc(buf: ^Buffer, bytes: ..byte) {
buf.n += copy(buf.b[buf.n..], bytes); buf.n += copy(buf.b[buf.n:], bytes);
} }
b := Buffer{b = buf}; b := Buffer{b = buf};
@@ -323,7 +323,7 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: DecimalSlice
// integer, padded with zeros when needed // integer, padded with zeros when needed
if digs.decimal_point > 0 { if digs.decimal_point > 0 {
m := min(digs.count, digs.decimal_point); m := min(digs.count, digs.decimal_point);
add_bytes(&b, ...digs.digits[0..m]); add_bytes(&b, ..digs.digits[0:m]);
for ; m < digs.decimal_point; m += 1 { for ; m < digs.decimal_point; m += 1 {
add_bytes(&b, '0'); add_bytes(&b, '0');
} }
@@ -335,7 +335,7 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: DecimalSlice
// fractional part // fractional part
if prec > 0 { if prec > 0 {
add_bytes(&b, '.'); add_bytes(&b, '.');
for i in 0..prec { for i in 0..prec-1 {
c: byte = '0'; c: byte = '0';
if j := digs.decimal_point + i; 0 <= j && j < digs.count { if j := digs.decimal_point + i; 0 <= j && j < digs.count {
c = digs.digits[j]; c = digs.digits[j];
@@ -398,7 +398,7 @@ round_shortest :: proc(d: ^Decimal, mant: u64, exp: int, flt: ^FloatInfo) {
inclusive := mant%2 == 0; inclusive := mant%2 == 0;
for i in 0..d.count { for i in 0..d.count-1 {
l: byte = '0'; // lower digit l: byte = '0'; // lower digit
if i < lower.count { if i < lower.count {
l = lower.digits[i]; l = lower.digits[i];
@@ -498,8 +498,8 @@ append_bits :: proc(buf: []byte, u: u64, base: int, is_signed: bool, bit_size: i
i-=1; a[i] = ' '; i-=1; a[i] = ' ';
} }
out := a[i..]; out := a[i:];
copy(buf, out); copy(buf, out);
return string(buf[0..len(out)]); return string(buf[0:len(out)]);
} }
+1 -1
View File
@@ -6,7 +6,7 @@ new_string :: proc(s: string) -> string {
c := make([]byte, len(s)+1); c := make([]byte, len(s)+1);
copy(c, cast([]byte)s); copy(c, cast([]byte)s);
c[len(s)] = 0; c[len(s)] = 0;
return string(c[..len(s)]); return string(c[:len(s)]);
} }
new_cstring :: proc(s: string) -> cstring { new_cstring :: proc(s: string) -> cstring {
+2 -2
View File
@@ -33,7 +33,7 @@ encode :: proc(d: []u16, s: []rune) -> int {
n, m := 0, len(d); n, m := 0, len(d);
loop: for r in s { loop: for r in s {
switch r { switch r {
case 0.._surr1, _surr3 .. _surr_self: case 0.._surr1-1, _surr3 .. _surr_self-1:
if m+1 < n do break loop; if m+1 < n do break loop;
d[n] = u16(r); d[n] = u16(r);
n += 1; n += 1;
@@ -59,7 +59,7 @@ encode_string :: proc(d: []u16, s: string) -> int {
n, m := 0, len(d); n, m := 0, len(d);
loop: for r in s { loop: for r in s {
switch r { switch r {
case 0.._surr1, _surr3 .. _surr_self: case 0.._surr1-1, _surr3 .. _surr_self-1:
if m+1 < n do break loop; if m+1 < n do break loop;
d[n] = u16(r); d[n] = u16(r);
n += 1; n += 1;
+1 -1
View File
@@ -158,7 +158,7 @@ decode_last_rune :: proc(s: []u8) -> (rune, int) {
} }
start = max(start, 0); start = max(start, 0);
r, size = decode_rune(s[start..end]); r, size = decode_rune(s[start:end]);
if start+size != end { if start+size != end {
return RUNE_ERROR, 1; return RUNE_ERROR, 1;
} }
+8 -10
View File
@@ -74,11 +74,9 @@ general_stuff :: proc() {
} }
{ {
// .. half-closed range // .. open range
// ... open range
for in 0..2 {} // 0, 1 for in 0..2 {} // 0, 1, 2
for in 0...2 {} // 0, 1, 2
} }
{ // Multiple sized booleans { // Multiple sized booleans
@@ -267,17 +265,17 @@ union_type :: proc() {
/* /*
Entity :: struct { Entity :: struct {
... ..
derived: union{^Frog, ^Monster}, derived: union{^Frog, ^Monster},
} }
Frog :: struct { Frog :: struct {
using entity: Entity, using entity: Entity,
... ..
} }
Monster :: struct { Monster :: struct {
using entity: Entity, using entity: Entity,
... ..
} }
new_entity :: proc(T: type) -> ^Entity { new_entity :: proc(T: type) -> ^Entity {
@@ -451,7 +449,7 @@ parametric_polymorphism :: proc() {
get_hash :: proc(s: string) -> u32 { // fnv32a get_hash :: proc(s: string) -> u32 { // fnv32a
h: u32 = 0x811c9dc5; h: u32 = 0x811c9dc5;
for i in 0..len(s) { for i in 0..len(s)-1 {
h = (h ~ u32(s[i])) * 0x01000193; h = (h ~ u32(s[i])) * 0x01000193;
} }
return h; return h;
@@ -500,12 +498,12 @@ threading_example :: proc() {
} }
ordered_remove :: proc(array: ^[dynamic]$T, index: int, loc := #caller_location) { ordered_remove :: proc(array: ^[dynamic]$T, index: int, loc := #caller_location) {
runtime.bounds_check_error_loc(loc, index, len(array)); runtime.bounds_check_error_loc(loc, index, len(array));
copy(array[index..], array[index+1..]); copy(array[index:], array[index+1:]);
pop(array); pop(array);
} }
worker_proc :: proc(t: ^thread.Thread) -> int { worker_proc :: proc(t: ^thread.Thread) -> int {
for iteration in 1...5 { for iteration in 1..5 {
fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration); fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration);
fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration); fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration);
// win32.sleep(1); // win32.sleep(1);
+3 -3
View File
@@ -95,7 +95,7 @@ enumerations :: proc() {
} }
variadic_procedures :: proc() { variadic_procedures :: proc() {
print_ints :: proc(args: ...int) { print_ints :: proc(args: ..int) {
for arg, i in args { for arg, i in args {
if i > 0 do print(", "); if i > 0 do print(", ");
print(arg); print(arg);
@@ -106,7 +106,7 @@ variadic_procedures :: proc() {
print_ints(1); nl(); print_ints(1); nl();
print_ints(1, 2, 3); nl(); print_ints(1, 2, 3); nl();
print_prefix_f32s :: proc(prefix: string, args: ...f32) { print_prefix_f32s :: proc(prefix: string, args: ..f32) {
print(prefix); print(prefix);
print(": "); print(": ");
for arg, i in args { for arg, i in args {
@@ -323,7 +323,7 @@ match_statement :: proc() {
Vector3 :: struct {x, y, z: f32} Vector3 :: struct {x, y, z: f32}
print_floats :: proc(args: ...f32) { print_floats :: proc(args: ..f32) {
for arg, i in args { for arg, i in args {
if i > 0 do print(", "); if i > 0 do print(", ");
print(arg); print(arg);
+2 -2
View File
@@ -32,7 +32,7 @@ main :: proc() {
/* /*
push_allocator x { push_allocator x {
... ..
} }
is equivalent to: is equivalent to:
@@ -42,7 +42,7 @@ main :: proc() {
__context.allocator = x __context.allocator = x
defer __context.allocator = prev_allocator defer __context.allocator = prev_allocator
... ..
} }
*/ */
+2 -2
View File
@@ -21,7 +21,7 @@ when true {
Removed: Removed:
* Maybe/option types * Maybe/option types
* Remove `type` keyword and other "reserved" keywords * Remove `type` keyword and other "reserved" keywords
* ..< and ... removed and replace with .. (half-closed range) * ..< and .. removed and replace with .. (half-closed range)
Changed: Changed:
* `#assert` and `assert` return the value of the condition for semantic reasons * `#assert` and `assert` return the value of the condition for semantic reasons
@@ -51,7 +51,7 @@ when true {
} }
{ {
// Removal of ..< and ... // Removal of ..< and ..
for i in 0..16 { for i in 0..16 {
} }
// Is similar to // Is similar to
+6 -6
View File
@@ -69,10 +69,10 @@ general_stuff :: proc() {
{ {
// .. half-closed range // .. half-closed range
// ... open range // .. open range
for in 0..2 {} // 0, 1 for in 0..2 {} // 0, 1
for in 0...2 {} // 0, 1, 2 for in 0..2 {} // 0, 1, 2
} }
} }
@@ -287,17 +287,17 @@ union_type :: proc() {
/* /*
Entity :: struct { Entity :: struct {
... ..
derived: union{^Frog, ^Monster}; derived: union{^Frog, ^Monster};
} }
Frog :: struct { Frog :: struct {
using entity: Entity; using entity: Entity;
... ..
} }
Monster :: struct { Monster :: struct {
using entity: Entity; using entity: Entity;
... ..
} }
new_entity :: proc(T: type) -> ^Entity { new_entity :: proc(T: type) -> ^Entity {
@@ -522,7 +522,7 @@ threading_example :: proc() {
} }
worker_proc :: proc(t: ^thread.Thread) -> int { worker_proc :: proc(t: ^thread.Thread) -> int {
for iteration in 1...5 { for iteration in 1..5 {
fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration); fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration);
fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration); fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration);
// win32.sleep(1); // win32.sleep(1);
+6 -6
View File
@@ -72,10 +72,10 @@ general_stuff :: proc() {
{ {
// .. half-closed range // .. half-closed range
// ... open range // .. open range
for in 0..2 {} // 0, 1 for in 0..2 {} // 0, 1
for in 0...2 {} // 0, 1, 2 for in 0..2 {} // 0, 1, 2
} }
{ // Multiple sized booleans { // Multiple sized booleans
@@ -324,17 +324,17 @@ union_type :: proc() {
/* /*
Entity :: struct { Entity :: struct {
... ..
derived: union{^Frog, ^Monster}, derived: union{^Frog, ^Monster},
} }
Frog :: struct { Frog :: struct {
using entity: Entity, using entity: Entity,
... ..
} }
Monster :: struct { Monster :: struct {
using entity: Entity, using entity: Entity,
... ..
} }
new_entity :: proc(T: type) -> ^Entity { new_entity :: proc(T: type) -> ^Entity {
@@ -559,7 +559,7 @@ threading_example :: proc() {
} }
worker_proc :: proc(t: ^thread.Thread) -> int { worker_proc :: proc(t: ^thread.Thread) -> int {
for iteration in 1...5 { for iteration in 1..5 {
fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration); fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration);
fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration); fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration);
// win32.sleep(1); // win32.sleep(1);
+4 -4
View File
@@ -36,7 +36,7 @@ general_stuff :: proc() {
// C-style variadic procedures // C-style variadic procedures
foreign __llvm_core { foreign __llvm_core {
// The variadic part allows for extra type checking too which C does not provide // The variadic part allows for extra type checking too which C does not provide
c_printf :: proc(fmt: ^u8, #c_vararg args: ...any) -> i32 #link_name "printf" ---; c_printf :: proc(fmt: ^u8, #c_vararg args: ..any) -> i32 #link_name "printf" ---;
} }
str := "%d\n\x00"; str := "%d\n\x00";
// c_printf(&str[0], i32(789456123)); // c_printf(&str[0], i32(789456123));
@@ -154,8 +154,8 @@ default_return_values :: proc() {
match x { match x {
case 0: return; case 0: return;
case 1: return "Goodbye"; case 1: return "Goodbye";
case 2: return "Goodbye", "cruel world..."; case 2: return "Goodbye", "cruel world..";
case 3: return second = "cruel world...", first = "Goodbye"; case 3: return second = "cruel world..", first = "Goodbye";
} }
return second = "my old friend."; return second = "my old friend.";
@@ -231,7 +231,7 @@ explicit_parametric_polymorphic_procedures :: proc() {
defer free(another_ptr); defer free(another_ptr);
add :: proc(T: type, args: ...T) -> T { add :: proc(T: type, args: ..T) -> T {
res: T; res: T;
for arg in args do res += arg; for arg in args do res += arg;
return res; return res;
+1 -3
View File
@@ -6110,8 +6110,6 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
// It is okay to continue as it will assume the 1st index is zero // It is okay to continue as it will assume the 1st index is zero
} }
TokenKind interval_kind = se->interval.kind;
i64 indices[2] = {}; i64 indices[2] = {};
Ast *nodes[2] = {se->low, se->high}; Ast *nodes[2] = {se->low, se->high};
for (isize i = 0; i < gb_count_of(nodes); i++) { for (isize i = 0; i < gb_count_of(nodes); i++) {
@@ -6122,7 +6120,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
capacity = max_count; capacity = max_count;
} }
i64 j = 0; i64 j = 0;
if (check_index_value(c, interval_kind == Token_Ellipsis, nodes[i], capacity, &j)) { if (check_index_value(c, false, nodes[i], capacity, &j)) {
index = j; index = j;
} }
} else if (i == 0) { } else if (i == 0) {
+3 -6
View File
@@ -715,8 +715,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
continue; continue;
} }
switch (ie->op.kind) { switch (ie->op.kind) {
case Token_Ellipsis: op = Token_GtEq; break; case Token_Ellipsis: op = Token_GtEq; break;
case Token_HalfClosed: op = Token_Gt; break;
default: error(ie->op, "Invalid interval operator"); continue; default: error(ie->op, "Invalid interval operator"); continue;
} }
@@ -726,8 +725,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
} }
switch (ie->op.kind) { switch (ie->op.kind) {
case Token_Ellipsis: op = Token_LtEq; break; case Token_Ellipsis: op = Token_LtEq; break;
case Token_HalfClosed: op = Token_Lt; break;
default: error(ie->op, "Invalid interval operator"); continue; default: error(ie->op, "Invalid interval operator"); continue;
} }
@@ -1362,8 +1360,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
TokenKind op = Token_Lt; TokenKind op = Token_Lt;
switch (ie->op.kind) { switch (ie->op.kind) {
case Token_Ellipsis: op = Token_LtEq; break; case Token_Ellipsis: op = Token_LtEq; break;
case Token_HalfClosed: op = Token_Lt; break;
default: error(ie->op, "Invalid range operator"); break; default: error(ie->op, "Invalid range operator"); break;
} }
bool ok = compare_exact_values(op, a, b); bool ok = compare_exact_values(op, a, b);
+1 -1
View File
@@ -1548,7 +1548,7 @@ i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
return 0; return 0;
} }
if (e->kind == Ast_UnaryExpr && if (e->kind == Ast_UnaryExpr &&
e->UnaryExpr.op.kind == Token_Ellipsis) { e->UnaryExpr.op.kind == Token_Question) {
return -1; return -1;
} }
+2 -8
View File
@@ -5820,10 +5820,6 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) {
if (se->low != nullptr) low = ir_build_expr(proc, se->low); if (se->low != nullptr) low = ir_build_expr(proc, se->low);
if (se->high != nullptr) high = ir_build_expr(proc, se->high); if (se->high != nullptr) high = ir_build_expr(proc, se->high);
if (high != nullptr && se->interval.kind == Token_Ellipsis) {
high = ir_emit_arith(proc, Token_Add, high, v_one, t_int);
}
irValue *addr = ir_build_addr_ptr(proc, se->expr); irValue *addr = ir_build_addr_ptr(proc, se->expr);
irValue *base = ir_emit_load(proc, addr); irValue *base = ir_emit_load(proc, addr);
Type *type = base_type(ir_type(base)); Type *type = base_type(ir_type(base));
@@ -6625,8 +6621,7 @@ void ir_build_range_interval(irProcedure *proc, AstBinaryExpr *node, Type *val_t
TokenKind op = Token_Lt; TokenKind op = Token_Lt;
switch (node->op.kind) { switch (node->op.kind) {
case Token_Ellipsis: op = Token_LtEq; break; case Token_Ellipsis: op = Token_LtEq; break;
case Token_HalfClosed: op = Token_Lt; break;
default: GB_PANIC("Invalid interval operator"); break; default: GB_PANIC("Invalid interval operator"); break;
} }
@@ -7224,8 +7219,7 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) {
ast_node(ie, BinaryExpr, expr); ast_node(ie, BinaryExpr, expr);
TokenKind op = Token_Invalid; TokenKind op = Token_Invalid;
switch (ie->op.kind) { switch (ie->op.kind) {
case Token_Ellipsis: op = Token_LtEq; break; case Token_Ellipsis: op = Token_LtEq; break;
case Token_HalfClosed: op = Token_Lt; break;
default: GB_PANIC("Invalid interval operator"); break; default: GB_PANIC("Invalid interval operator"); break;
} }
irValue *lhs = ir_build_expr(proc, ie->left); irValue *lhs = ir_build_expr(proc, ie->left);
+6 -12
View File
@@ -1142,7 +1142,7 @@ Token expect_operator(AstFile *f) {
if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) { if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) {
syntax_error(f->curr_token, "Expected an operator, got '%.*s'", syntax_error(f->curr_token, "Expected an operator, got '%.*s'",
LIT(token_strings[prev.kind])); LIT(token_strings[prev.kind]));
} else if (!f->allow_range && (prev.kind == Token_Ellipsis || prev.kind == Token_HalfClosed)) { } else if (!f->allow_range && (prev.kind == Token_Ellipsis)) {
syntax_error(f->curr_token, "Expected an non-range operator, got '%.*s'", syntax_error(f->curr_token, "Expected an non-range operator, got '%.*s'",
LIT(token_strings[prev.kind])); LIT(token_strings[prev.kind]));
} }
@@ -2041,7 +2041,7 @@ Ast *parse_call_expr(AstFile *f, Ast *operand) {
Token eq = expect_token(f, Token_Eq); Token eq = expect_token(f, Token_Eq);
if (prefix_ellipsis) { if (prefix_ellipsis) {
syntax_error(ellipsis, "'...' must be applied to value rather than the field name"); syntax_error(ellipsis, "'..' must be applied to value rather than the field name");
} }
Ast *value = parse_value(f); Ast *value = parse_value(f);
@@ -2118,18 +2118,14 @@ Ast *parse_atom_expr(AstFile *f, Ast *operand, bool lhs) {
f->expr_level++; f->expr_level++;
open = expect_token(f, Token_OpenBracket); open = expect_token(f, Token_OpenBracket);
if (f->curr_token.kind != Token_Ellipsis && if (f->curr_token.kind != Token_Colon) {
f->curr_token.kind != Token_HalfClosed) {
indices[0] = parse_expr(f, false); indices[0] = parse_expr(f, false);
} }
if ((f->curr_token.kind == Token_Ellipsis || if (f->curr_token.kind == Token_Colon) {
f->curr_token.kind == Token_HalfClosed)) {
ellipsis = advance_token(f); ellipsis = advance_token(f);
is_ellipsis = true; is_ellipsis = true;
if (f->curr_token.kind != Token_Ellipsis && if (f->curr_token.kind != Token_CloseBracket &&
f->curr_token.kind != Token_HalfClosed &&
f->curr_token.kind != Token_CloseBracket &&
f->curr_token.kind != Token_EOF) { f->curr_token.kind != Token_EOF) {
indices[1] = parse_expr(f, false); indices[1] = parse_expr(f, false);
} }
@@ -2214,7 +2210,6 @@ bool is_ast_range(Ast *expr) {
TokenKind op = expr->BinaryExpr.op.kind; TokenKind op = expr->BinaryExpr.op.kind;
switch (op) { switch (op) {
case Token_Ellipsis: case Token_Ellipsis:
case Token_HalfClosed:
return true; return true;
} }
return false; return false;
@@ -2226,7 +2221,6 @@ i32 token_precedence(AstFile *f, TokenKind t) {
case Token_Question: case Token_Question:
return 1; return 1;
case Token_Ellipsis: case Token_Ellipsis:
case Token_HalfClosed:
if (!f->allow_range) { if (!f->allow_range) {
return 0; return 0;
} }
@@ -2690,7 +2684,7 @@ Ast *parse_var_type(AstFile *f, bool allow_ellipsis, bool allow_type_token) {
Token tok = advance_token(f); Token tok = advance_token(f);
Ast *type = parse_type_or_ident(f); Ast *type = parse_type_or_ident(f);
if (type == nullptr) { if (type == nullptr) {
syntax_error(tok, "variadic field missing type after '...'"); syntax_error(tok, "variadic field missing type after '..'");
type = ast_bad_expr(f, tok, f->curr_token); type = ast_bad_expr(f, tok, f->curr_token);
} }
return ast_ellipsis(f, tok, type); return ast_ellipsis(f, tok, type);
+11 -7
View File
@@ -76,8 +76,7 @@ TOKEN_KIND(Token__ComparisonEnd, ""), \
TOKEN_KIND(Token_Semicolon, ";"), \ TOKEN_KIND(Token_Semicolon, ";"), \
TOKEN_KIND(Token_Period, "."), \ TOKEN_KIND(Token_Period, "."), \
TOKEN_KIND(Token_Comma, ","), \ TOKEN_KIND(Token_Comma, ","), \
TOKEN_KIND(Token_Ellipsis, "..."), \ TOKEN_KIND(Token_Ellipsis, ".."), \
TOKEN_KIND(Token_HalfClosed, ".."), \
TOKEN_KIND(Token_BackSlash, "\\"), \ TOKEN_KIND(Token_BackSlash, "\\"), \
TOKEN_KIND(Token__OperatorEnd, ""), \ TOKEN_KIND(Token__OperatorEnd, ""), \
\ \
@@ -226,6 +225,9 @@ void error_va(Token token, char *fmt, va_list va) {
gb_bprintf_va(fmt, va)); gb_bprintf_va(fmt, va));
} }
gb_mutex_unlock(&global_error_collector.mutex); gb_mutex_unlock(&global_error_collector.mutex);
if (global_error_collector.count > 20) {
gb_exit(1);
}
} }
void error_no_newline_va(Token token, char *fmt, va_list va) { void error_no_newline_va(Token token, char *fmt, va_list va) {
@@ -241,6 +243,9 @@ void error_no_newline_va(Token token, char *fmt, va_list va) {
gb_bprintf_va(fmt, va)); gb_bprintf_va(fmt, va));
} }
gb_mutex_unlock(&global_error_collector.mutex); gb_mutex_unlock(&global_error_collector.mutex);
if (global_error_collector.count > 20) {
gb_exit(1);
}
} }
@@ -258,6 +263,9 @@ void syntax_error_va(Token token, char *fmt, va_list va) {
} }
gb_mutex_unlock(&global_error_collector.mutex); gb_mutex_unlock(&global_error_collector.mutex);
if (global_error_collector.count > 20) {
gb_exit(1);
}
} }
void syntax_warning_va(Token token, char *fmt, va_list va) { void syntax_warning_va(Token token, char *fmt, va_list va) {
@@ -936,11 +944,7 @@ Token tokenizer_get_token(Tokenizer *t) {
case '.': case '.':
if (t->curr_rune == '.') { // Could be an ellipsis if (t->curr_rune == '.') { // Could be an ellipsis
advance_to_next_rune(t); advance_to_next_rune(t);
token.kind = Token_HalfClosed; token.kind = Token_Ellipsis;
if (t->curr_rune == '.') {
advance_to_next_rune(t);
token.kind = Token_Ellipsis;
}
} else if ('0' <= t->curr_rune && t->curr_rune <= '9') { } else if ('0' <= t->curr_rune && t->curr_rune <= '9') {
token = scan_number_to_token(t, true); token = scan_number_to_token(t, true);
} else { } else {