Ellipsis is now just ..; Remove half-closed range operator and treat all of them as half-closed; slice expression uses ..;

This commit is contained in:
Ginger Bill
2017-02-26 14:19:03 +00:00
parent 18b3c0b2fc
commit 3c9143957c
14 changed files with 210 additions and 197 deletions
+4 -4
View File
@@ -9,14 +9,14 @@
main :: proc() { main :: proc() {
buf: [64]byte; buf: [64]byte;
// len := strconv.generic_ftoa(buf[:], 123.5431, 'f', 4, 64); // len := strconv.generic_ftoa(buf[..], 123.5431, 'f', 4, 64);
x := 624.123; x := 624.123;
s := strconv.format_float(buf[:], x, 'f', 6, 64); s := strconv.format_float(buf[..], x, 'f', 6, 64);
fmt.println(s); fmt.println(s);
fmt.printf("%3d\n", 102); fmt.printf("%3d\n", 102);
i := 123; a := [..]int{0, 1, 2, 3, 4, 5, 6};
fmt.println(123); fmt.println(a[0..4]);
when false { when false {
+3 -3
View File
@@ -316,7 +316,7 @@ __bounds_check_error :: proc(file: string, line, column: int, index, count: int)
if 0 <= index && index < count { if 0 <= index && index < count {
return; return;
} }
fmt.fprintf(os.stderr, "%s(%d:%d) Index %d is out of bounds range 0..<%d\n", fmt.fprintf(os.stderr, "%s(%d:%d) Index %d is out of bounds range 0..%d\n",
file, line, column, index, count); file, line, column, index, count);
__debug_trap(); __debug_trap();
} }
@@ -325,7 +325,7 @@ __slice_expr_error :: proc(file: string, line, column: int, low, high: int) {
if 0 <= low && low <= high { if 0 <= low && low <= high {
return; return;
} }
fmt.fprintf(os.stderr, "%s(%d:%d) Invalid slice indices: [%d:%d]\n", fmt.fprintf(os.stderr, "%s(%d:%d) Invalid slice indices: [%d..%d]\n",
file, line, column, low, high); file, line, column, low, high);
__debug_trap(); __debug_trap();
} }
@@ -333,7 +333,7 @@ __substring_expr_error :: proc(file: string, line, column: int, low, high: int)
if 0 <= low && low <= high { if 0 <= low && low <= high {
return; return;
} }
fmt.fprintf(os.stderr, "%s(%d:%d) Invalid substring indices: [%d:%d]\n", fmt.fprintf(os.stderr, "%s(%d:%d) Invalid substring indices: [%d..%d]\n",
file, line, column, low, high); file, line, column, low, high);
__debug_trap(); __debug_trap();
} }
+65 -66
View File
@@ -3,11 +3,10 @@
// NOTE: This is only for floating point printing and nothing else // NOTE: This is only for floating point printing and nothing else
Decimal :: struct { Decimal :: struct {
d: [384]byte, // big-endian digits digits: [384]byte, // big-endian digits
ndu: int, count: int,
dp: int, decimal_point: int,
neg: bool, neg, trunc: bool,
trunc: bool,
} }
decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string { decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string {
@@ -19,42 +18,42 @@ decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string {
} }
n := 10 + a.ndu + abs(a.dp); n := 10 + a.count + abs(a.decimal_point);
// 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(buf.count >= n); assert(buf.count >= n);
buf = buf[:n]; buf = buf[..n];
if a.ndu == 0 { if a.count == 0 {
buf[0] = '0'; buf[0] = '0';
return cast(string)buf[0:1]; return cast(string)buf[0..1];
} }
w := 0; w := 0;
if a.dp <= 0 { if a.decimal_point <= 0 {
buf[w] = '0'; w++; buf[w] = '0'; w++;
buf[w] = '.'; w++; buf[w] = '.'; w++;
w += digit_zero(buf[w: w-a.dp]); w += digit_zero(buf[w .. w-a.decimal_point]);
w += copy(buf[w:], a.d[0:a.ndu]); w += copy(buf[w..], a.digits[0..a.count]);
} else if a.dp < a.ndu { } else if a.decimal_point < a.count {
w += copy(buf[w:], a.d[0:a.dp]); w += copy(buf[w..], a.digits[0..a.decimal_point]);
buf[w] = '.'; w++; buf[w] = '.'; w++;
w += copy(buf[w:], a.d[a.dp:a.ndu]); w += copy(buf[w..], a.digits[a.decimal_point .. a.count]);
} else { } else {
w += copy(buf[w:], a.d[0:a.ndu]); w += copy(buf[w..], a.digits[0..a.count]);
w += digit_zero(buf[w : w+a.dp-a.ndu]); w += digit_zero(buf[w .. w+a.decimal_point-a.count]);
} }
return cast(string)buf[0:w]; return cast(string)buf[0..w];
} }
// trim trailing zeros // trim trailing zeros
trim :: proc(a: ^Decimal) { trim :: proc(a: ^Decimal) {
for a.ndu > 0 && a.d[a.ndu-1] == '0' { for a.count > 0 && a.digits[a.count-1] == '0' {
a.ndu--; a.count--;
} }
if a.ndu == 0 { if a.count == 0 {
a.dp = 0; a.decimal_point = 0;
} }
} }
@@ -70,12 +69,12 @@ assign :: proc(a: ^Decimal, i: u64) {
i = j; i = j;
} }
a.ndu = 0; a.count = 0;
for n--; n >= 0; n-- { for n--; n >= 0; n-- {
a.d[a.ndu] = buf[n]; a.digits[a.count] = buf[n];
a.ndu++; a.count++;
} }
a.dp = a.ndu; a.decimal_point = a.count;
trim(a); trim(a);
} }
@@ -88,10 +87,10 @@ shift_right :: proc(a: ^Decimal, k: uint) {
n: uint; n: uint;
for ; n>>k == 0; r++ { for ; n>>k == 0; r++ {
if r >= a.ndu { if r >= a.count {
if n == 0 { if n == 0 {
// Just in case // Just in case
a.ndu = 0; a.count = 0;
return; return;
} }
for n>>k == 0 { for n>>k == 0 {
@@ -100,18 +99,18 @@ shift_right :: proc(a: ^Decimal, k: uint) {
} }
break; break;
} }
c := cast(uint)a.d[r]; c := cast(uint)a.digits[r];
n = n*10 + c - '0'; n = n*10 + c - '0';
} }
a.dp -= r-1; a.decimal_point -= r-1;
mask: uint = (1<<k) - 1; mask: uint = (1<<k) - 1;
for ; r < a.ndu; r++ { for ; r < a.count; r++ {
c := cast(uint)a.d[r]; c := cast(uint)a.digits[r];
dig := n>>k; dig := n>>k;
n &= mask; n &= mask;
a.d[w] = cast(byte)('0' + dig); a.digits[w] = cast(byte)('0' + dig);
w++; w++;
n = n*10 + c - '0'; n = n*10 + c - '0';
} }
@@ -119,8 +118,8 @@ shift_right :: proc(a: ^Decimal, k: uint) {
for n > 0 { for n > 0 {
dig := n>>k; dig := n>>k;
n &= mask; n &= mask;
if w < a.d.count { if w < a.digits.count {
a.d[w] = cast(byte)('0' + dig); a.digits[w] = cast(byte)('0' + dig);
w++; w++;
} else if dig > 0 { } else if dig > 0 {
a.trunc = true; a.trunc = true;
@@ -129,24 +128,24 @@ shift_right :: proc(a: ^Decimal, k: uint) {
} }
a.ndu = w; a.count = w;
trim(a); trim(a);
} }
shift_left :: proc(a: ^Decimal, k: uint) { shift_left :: proc(a: ^Decimal, k: uint) {
delta := cast(int)(k/4); delta := cast(int)(k/4);
r := a.ndu; // read index r := a.count; // read index
w := a.ndu+delta; // write index w := a.count+delta; // write index
n: uint; n: uint;
for r--; r >= 0; r-- { for r--; r >= 0; r-- {
n += (cast(uint)a.d[r] - '0') << k; n += (cast(uint)a.digits[r] - '0') << k;
quo := n/10; quo := n/10;
rem := n - 10*quo; rem := n - 10*quo;
w--; w--;
if w < a.d.count { if w < a.digits.count {
a.d[w] = cast(byte)('0' + rem); a.digits[w] = cast(byte)('0' + rem);
} else if rem != 0 { } else if rem != 0 {
a.trunc = true; a.trunc = true;
} }
@@ -157,23 +156,23 @@ shift_left :: proc(a: ^Decimal, k: uint) {
quo := n/10; quo := n/10;
rem := n - 10*quo; rem := n - 10*quo;
w--; w--;
if w < a.d.count { if w < a.digits.count {
a.d[w] = cast(byte)('0' + rem); a.digits[w] = cast(byte)('0' + rem);
} else if rem != 0 { } else if rem != 0 {
a.trunc = true; a.trunc = true;
} }
n = quo; n = quo;
} }
a.ndu += delta; a.count += delta;
a.ndu = min(a.ndu, a.d.count); a.count = min(a.count, a.digits.count);
a.dp += delta; a.decimal_point += delta;
trim(a); trim(a);
} }
shift :: proc(a: ^Decimal, k: int) { shift :: proc(a: ^Decimal, k: int) {
match { match {
case a.ndu == 0: case a.count == 0:
// no need to update // no need to update
case k > 0: case k > 0:
for k > max_shift { for k > max_shift {
@@ -193,19 +192,19 @@ shift :: proc(a: ^Decimal, k: int) {
} }
can_round_up :: proc(a: ^Decimal, nd: int) -> bool { can_round_up :: proc(a: ^Decimal, nd: int) -> bool {
if nd < 0 || nd >= a.ndu { return false ; } if nd < 0 || nd >= a.count { return false ; }
if a.d[nd] == '5' && nd+1 == a.ndu { if a.digits[nd] == '5' && nd+1 == a.count {
if a.trunc { if a.trunc {
return true; return true;
} }
return nd > 0 && (a.d[nd-1]-'0')%2 != 0; return nd > 0 && (a.digits[nd-1]-'0')%2 != 0;
} }
return a.d[nd] >= '5'; return a.digits[nd] >= '5';
} }
round :: proc(a: ^Decimal, nd: int) { round :: proc(a: ^Decimal, nd: int) {
if nd < 0 || nd >= a.ndu { return; } if nd < 0 || nd >= a.count { return; }
if can_round_up(a, nd) { if can_round_up(a, nd) {
round_up(a, nd); round_up(a, nd);
} else { } else {
@@ -214,44 +213,44 @@ round :: proc(a: ^Decimal, nd: int) {
} }
round_up :: proc(a: ^Decimal, nd: int) { round_up :: proc(a: ^Decimal, nd: int) {
if nd < 0 || nd >= a.ndu { return; } if nd < 0 || nd >= a.count { return; }
for i := nd-1; i >= 0; i-- { for i := nd-1; i >= 0; i-- {
if c := a.d[i]; c < '9' { if c := a.digits[i]; c < '9' {
a.d[i]++; a.digits[i]++;
a.ndu = i+1; a.count = i+1;
return; return;
} }
} }
// Number is just 9s // Number is just 9s
a.d[0] = '1'; a.digits[0] = '1';
a.ndu = 1; a.count = 1;
a.dp++; a.decimal_point++;
} }
round_down :: proc(a: ^Decimal, nd: int) { round_down :: proc(a: ^Decimal, nd: int) {
if nd < 0 || nd >= a.ndu { return; } if nd < 0 || nd >= a.count { return; }
a.ndu = nd; a.count = nd;
trim(a); trim(a);
} }
// Extract integer part, rounded appropriately. There are no guarantees about overflow. // Extract integer part, rounded appropriately. There are no guarantees about overflow.
rounded_integer :: proc(a: ^Decimal) -> u64 { rounded_integer :: proc(a: ^Decimal) -> u64 {
if a.dp > 20 { if a.decimal_point > 20 {
return 0xffff_ffff_ffff_ffff; return 0xffff_ffff_ffff_ffff;
} }
i: int; i: int;
n: u64 = 0; n: u64 = 0;
m := min(a.dp, a.ndu); m := min(a.decimal_point, a.count);
for i = 0; i < m; i++ { for i = 0; i < m; i++ {
n = n*10 + cast(u64)(a.d[i]-'0'); n = n*10 + cast(u64)(a.digits[i]-'0');
} }
for ; i < a.dp; i++ { for ; i < a.decimal_point; i++ {
n *= 10; n *= 10;
} }
if can_round_up(a, a.dp) { if can_round_up(a, a.decimal_point) {
n++; n++;
} }
return n; return n;
+55 -55
View File
@@ -16,7 +16,7 @@ buffer_write :: proc(buf: ^Buffer, b: []byte) {
if buf.length < buf.data.count { if buf.length < buf.data.count {
n := min(buf.data.count-buf.length, b.count); n := min(buf.data.count-buf.length, b.count);
if n > 0 { if n > 0 {
copy(buf.data[buf.length:], b[:n]); copy(buf.data[buf.length..], b[..n]);
buf.length += n; buf.length += n;
} }
} }
@@ -37,7 +37,7 @@ buffer_write_rune :: proc(buf: ^Buffer, r: rune) {
} }
b, n := utf8.encode_rune(r); b, n := utf8.encode_rune(r);
buffer_write(buf, b[:n]); buffer_write(buf, b[..n]);
} }
Fmt_Info :: struct { Fmt_Info :: struct {
@@ -61,46 +61,46 @@ Fmt_Info :: struct {
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 := Buffer{data[:], 0}; buf := Buffer{data[..], 0};
bprint(^buf, ...args); bprint(^buf, ..args);
os.write(fd, buf.data[:buf.length]); os.write(fd, buf.data[..buf.length]);
return buf.length; return buf.length;
} }
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 := Buffer{data[:], 0}; buf := Buffer{data[..], 0};
bprintln(^buf, ...args); bprintln(^buf, ..args);
os.write(fd, buf.data[:buf.length]); os.write(fd, buf.data[..buf.length]);
return buf.length; return buf.length;
} }
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 := Buffer{data[:], 0}; buf := Buffer{data[..], 0};
bprintf(^buf, fmt, ...args); bprintf(^buf, fmt, ..args);
os.write(fd, buf.data[:buf.length]); os.write(fd, buf.data[..buf.length]);
return buf.length; return buf.length;
} }
print :: proc(args: ...any) -> int { print :: proc(args: ..any) -> int {
return fprint(os.stdout, ...args); return fprint(os.stdout, ..args);
} }
println :: proc(args: ...any) -> int { println :: proc(args: ..any) -> int {
return fprintln(os.stdout, ...args); return fprintln(os.stdout, ..args);
} }
printf :: proc(fmt: string, args: ...any) -> int { printf :: proc(fmt: string, args: ..any) -> int {
return fprintf(os.stdout, fmt, ...args); return fprintf(os.stdout, fmt, ..args);
} }
fprint_type :: proc(fd: os.Handle, info: ^Type_Info) { fprint_type :: proc(fd: os.Handle, info: ^Type_Info) {
data: [_BUFFER_SIZE]byte; data: [_BUFFER_SIZE]byte;
buf := Buffer{data[:], 0}; buf := Buffer{data[..], 0};
buffer_write_type(^buf, info); buffer_write_type(^buf, info);
os.write(fd, buf.data[:buf.length]); os.write(fd, buf.data[..buf.length]);
} }
buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) { buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) {
@@ -176,7 +176,7 @@ buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) {
buffer_write_string(buf, "]"); buffer_write_string(buf, "]");
buffer_write_type(buf, info.elem); buffer_write_type(buf, info.elem);
case Dynamic_Array: case Dynamic_Array:
buffer_write_string(buf, "[...]"); buffer_write_string(buf, "[..]");
buffer_write_type(buf, info.elem); buffer_write_type(buf, info.elem);
case Slice: case Slice:
buffer_write_string(buf, "["); buffer_write_string(buf, "[");
@@ -241,7 +241,7 @@ buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) {
variant := union_cast(^Struct)variant_type; variant := union_cast(^Struct)variant_type;
vc := variant.names.count-cf.names.count; vc := variant.names.count-cf.names.count;
for j in 0..<vc { for j in 0..vc {
if j > 0 { if j > 0 {
buffer_write_string(buf, ", "); buffer_write_string(buf, ", ");
} }
@@ -280,7 +280,7 @@ buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) {
} }
bprint :: proc(buf: ^Buffer, args: ...any) -> int { bprint :: proc(buf: ^Buffer, args: ..any) -> int {
fi: Fmt_Info; fi: Fmt_Info;
fi.buf = buf; fi.buf = buf;
@@ -296,7 +296,7 @@ bprint :: proc(buf: ^Buffer, args: ...any) -> int {
return buf.length; return buf.length;
} }
bprintln :: proc(buf: ^Buffer, args: ...any) -> int { bprintln :: proc(buf: ^Buffer, args: ..any) -> int {
fi: Fmt_Info; fi: Fmt_Info;
fi.buf = buf; fi.buf = buf;
@@ -310,23 +310,23 @@ bprintln :: proc(buf: ^Buffer, args: ...any) -> int {
return buf.length; return buf.length;
} }
sprint :: proc(buf: []byte, args: ...any) -> string { sprint :: proc(buf: []byte, args: ..any) -> string {
b: Buffer; b: Buffer;
b.data = buf; b.data = buf;
count := bprint(^b, ...args); count := bprint(^b, ..args);
return cast(string)b.data[:b.length]; return cast(string)b.data[..b.length];
} }
sprintln :: proc(buf: []byte, args: ...any) -> string { sprintln :: proc(buf: []byte, args: ..any) -> string {
b: Buffer; b: Buffer;
b.data = buf; b.data = buf;
count := bprintln(^b, ...args); count := bprintln(^b, ..args);
return cast(string)b.data[:b.length]; return cast(string)b.data[..b.length];
} }
sprintf :: proc(buf: []byte, fmt: string, args: ...any) -> string { sprintf :: proc(buf: []byte, fmt: string, args: ..any) -> string {
b: Buffer; b: Buffer;
b.data = buf; b.data = buf;
count := bprintf(^b, fmt, ...args); count := bprintf(^b, fmt, ..args);
return cast(string)b.data[:b.length]; return cast(string)b.data[..b.length];
} }
@@ -341,7 +341,7 @@ parse_int :: proc(s: string, offset: int) -> (result: int, offset: int, ok: bool
ok := true; ok := true;
i := 0; i := 0;
for o in offset..<s.count { for o in offset..s.count {
c := cast(rune)s[offset+i]; c := cast(rune)s[offset+i];
if !is_digit(c) { if !is_digit(c) {
break; break;
@@ -366,7 +366,7 @@ _arg_number :: proc(fi: ^Fmt_Info,
return 0, 1, false; return 0, 1, false;
} }
for i in 1..<format.count { for i in 1..format.count {
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 {
@@ -384,7 +384,7 @@ _arg_number :: proc(fi: ^Fmt_Info,
return arg_index, offset, false; return arg_index, offset, false;
} }
fi.reordered = true; fi.reordered = true;
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;
} }
@@ -454,7 +454,7 @@ fmt_write_padding :: proc(fi: ^Fmt_Info, width: int) {
count := min(width, fi.buf.data.count-fi.buf.length); count := min(width, fi.buf.data.count-fi.buf.length);
start := fi.buf.length; start := fi.buf.length;
for i in start..<count { for i in start..count {
fi.buf.data[i] = pad_byte; fi.buf.data[i] = pad_byte;
} }
fi.buf.length += count; fi.buf.length += count;
@@ -503,11 +503,11 @@ _write_int :: proc(fi: ^Fmt_Info, u: u64, base: int, neg: bool, digits: string)
if fi.hash { flags |= strconv.Int_Flag.PREFIX; } if fi.hash { flags |= strconv.Int_Flag.PREFIX; }
if fi.plus { flags |= strconv.Int_Flag.PLUS; } if fi.plus { flags |= strconv.Int_Flag.PLUS; }
if fi.space { flags |= strconv.Int_Flag.SPACE; } if fi.space { flags |= strconv.Int_Flag.SPACE; }
s := strconv.format_bits(buf[:], u, base, neg, digits, flags); s := strconv.format_bits(buf[..], u, base, neg, digits, flags);
prev_zero := fi.zero; prev_zero := fi.zero;
fi.zero = false;
defer fi.zero = prev_zero; defer fi.zero = prev_zero;
fi.zero = false;
_pad(fi, s); _pad(fi, s);
} }
@@ -570,10 +570,10 @@ fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
prec = fi.prec; prec = fi.prec;
} }
buf: [128]byte; buf: [128]byte;
str := strconv.format_float(buf[1:], v, 'f', prec, bit_size); str := strconv.format_float(buf[1..], v, 'f', prec, bit_size);
str = cast(string)buf[:str.count+1]; str = cast(string)buf[..str.count+1];
if str[1] == '+' || str[1] == '-' { if str[1] == '+' || str[1] == '-' {
str = str[1:]; str = str[1..];
} else { } else {
str[0] = '+'; str[0] = '+';
} }
@@ -591,12 +591,12 @@ fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
if fi.zero && fi.width_set && fi.width > str.count { if fi.zero && fi.width_set && fi.width > str.count {
buffer_write_byte(fi.buf, str[0]); buffer_write_byte(fi.buf, str[0]);
fmt_write_padding(fi, fi.width - str.count); fmt_write_padding(fi, fi.width - str.count);
buffer_write_string(fi.buf, str[1:]); buffer_write_string(fi.buf, str[1..]);
} else { } else {
_pad(fi, str); _pad(fi, str);
} }
} else { } else {
_pad(fi, str[1:]); _pad(fi, str[1..]);
} }
default: default:
@@ -747,7 +747,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
buffer_write_byte(fi.buf, '['); buffer_write_byte(fi.buf, '[');
defer buffer_write_byte(fi.buf, ']'); defer buffer_write_byte(fi.buf, ']');
for i in 0..<info.count { for i in 0..info.count {
if i > 0 { if i > 0 {
buffer_write_string(fi.buf, ", "); buffer_write_string(fi.buf, ", ");
} }
@@ -764,7 +764,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
buffer_write_byte(fi.buf, '['); buffer_write_byte(fi.buf, '[');
defer buffer_write_byte(fi.buf, ']'); defer buffer_write_byte(fi.buf, ']');
array := cast(^Raw_Dynamic_Array)v.data; array := cast(^Raw_Dynamic_Array)v.data;
for i in 0..<array.count { for i in 0..array.count {
if i > 0 { if i > 0 {
buffer_write_string(fi.buf, ", "); buffer_write_string(fi.buf, ", ");
} }
@@ -786,7 +786,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
entry_type := union_cast(^Struct)ed.elem; entry_type := union_cast(^Struct)ed.elem;
entry_size := ed.elem_size; entry_size := ed.elem_size;
for i in 0..<entries.count { for i in 0..entries.count {
if i > 0 { if i > 0 {
buffer_write_string(fi.buf, ", "); buffer_write_string(fi.buf, ", ");
} }
@@ -815,7 +815,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
buffer_write_byte(fi.buf, '['); buffer_write_byte(fi.buf, '[');
defer buffer_write_byte(fi.buf, ']'); defer buffer_write_byte(fi.buf, ']');
slice := cast(^[]byte)v.data; slice := cast(^[]byte)v.data;
for i in 0..<slice.count { for i in 0..slice.count {
if i > 0 { if i > 0 {
buffer_write_string(fi.buf, ", "); buffer_write_string(fi.buf, ", ");
} }
@@ -827,7 +827,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
buffer_write_byte(fi.buf, '<'); buffer_write_byte(fi.buf, '<');
defer buffer_write_byte(fi.buf, '>'); defer buffer_write_byte(fi.buf, '>');
for i in 0..<info.count { for i in 0..info.count {
if i > 0 { if i > 0 {
buffer_write_string(fi.buf, ", "); buffer_write_string(fi.buf, ", ");
} }
@@ -920,7 +920,7 @@ fmt_arg :: proc(fi: ^Fmt_Info, arg: any, verb: rune) {
} }
bprintf :: proc(b: ^Buffer, fmt: string, args: ...any) -> int { bprintf :: proc(b: ^Buffer, fmt: string, args: ..any) -> int {
fi := Fmt_Info{}; fi := Fmt_Info{};
end := fmt.count; end := fmt.count;
arg_index := 0; arg_index := 0;
@@ -933,7 +933,7 @@ bprintf :: proc(b: ^Buffer, fmt: string, args: ...any) -> int {
i++; i++;
} }
if i > prev_i { if i > prev_i {
buffer_write_string(b, fmt[prev_i:i]); buffer_write_string(b, fmt[prev_i..i]);
} }
if i >= end { if i >= end {
break; break;
@@ -1026,7 +1026,7 @@ bprintf :: proc(b: ^Buffer, fmt: string, args: ...any) -> int {
break; break;
} }
verb, w := utf8.decode_rune(fmt[i:]); verb, w := utf8.decode_rune(fmt[i..]);
i += w; i += w;
if verb == '%' { if verb == '%' {
@@ -1043,7 +1043,7 @@ bprintf :: proc(b: ^Buffer, fmt: string, args: ...any) -> int {
if !fi.reordered && arg_index < args.count { if !fi.reordered && arg_index < args.count {
buffer_write_string(b, "%!(EXTRA "); buffer_write_string(b, "%!(EXTRA ");
for arg, index in args[arg_index:] { for arg, index in args[arg_index..] {
if index > 0 { if index > 0 {
buffer_write_string(b, ", "); buffer_write_string(b, ", ");
} }
+2 -2
View File
@@ -66,7 +66,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;
match tail.count&3 { match tail.count&3 {
@@ -174,7 +174,7 @@ murmur64 :: proc(data: []byte) -> u64 {
len -= 4; len -= 4;
} }
data8 := slice_to_bytes(data32[i:])[:3]; data8 := slice_to_bytes(data32[i..])[..3];
match len { match len {
case 3: case 3:
h2 ~= cast(u32)data8[2] << 16; h2 ~= cast(u32)data8[2] << 16;
+4 -4
View File
@@ -151,8 +151,8 @@ mat4_identity :: proc() -> Mat4 {
} }
mat4_transpose :: proc(m: Mat4) -> Mat4 { mat4_transpose :: proc(m: Mat4) -> Mat4 {
for j in 0..<4 { for j in 0..4 {
for i in 0..<4 { for i in 0..4 {
m[i][j], m[j][i] = m[j][i], m[i][j]; m[i][j], m[j][i] = m[j][i], m[i][j];
} }
} }
@@ -161,8 +161,8 @@ mat4_transpose :: proc(m: Mat4) -> Mat4 {
mul :: proc(a, b: Mat4) -> Mat4 { mul :: proc(a, b: Mat4) -> Mat4 {
c: Mat4; c: Mat4;
for j in 0..<4 { for j in 0..4 {
for i in 0..<4 { for i in 0..4 {
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
@@ -32,7 +32,7 @@ copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr #link_name "_
compare :: proc(a, b: []byte) -> int #link_name "__mem_compare" { compare :: proc(a, b: []byte) -> int #link_name "__mem_compare" {
n := min(a.count, b.count); n := min(a.count, b.count);
for i in 0..<n { for i in 0..n {
match { match {
case a[i] < b[i]: case a[i] < b[i]:
return -1; return -1;
@@ -117,7 +117,7 @@ Arena_Temp_Memory :: struct {
init_arena_from_memory :: proc(using a: ^Arena, data: []byte) { init_arena_from_memory :: proc(using a: ^Arena, data: []byte) {
backing = Allocator{}; backing = Allocator{};
memory = data[:0]; memory = data[..0];
temp_count = 0; temp_count = 0;
} }
+3 -3
View File
@@ -95,7 +95,7 @@ open :: proc(path: string, mode: int, perm: u32) -> (Handle, Errno) {
} }
buf: [300]byte; buf: [300]byte;
copy(buf[:], cast([]byte)path); copy(buf[..], cast([]byte)path);
handle := cast(Handle)CreateFileA(^buf[0], access, share_mode, sa, create_mode, FILE_ATTRIBUTE_NORMAL, nil); handle := cast(Handle)CreateFileA(^buf[0], access, share_mode, sa, create_mode, FILE_ATTRIBUTE_NORMAL, nil);
if handle != INVALID_HANDLE { if handle != INVALID_HANDLE {
@@ -184,7 +184,7 @@ last_write_time_by_name :: proc(name: string) -> File_Time {
assert(buf.count > name.count); assert(buf.count > name.count);
copy(buf[:], cast([]byte)name); copy(buf[..], cast([]byte)name);
if win32.GetFileAttributesExA(^buf[0], win32.GetFileExInfoStandard, ^data) != 0 { if win32.GetFileAttributesExA(^buf[0], win32.GetFileExInfoStandard, ^data) != 0 {
last_write_time = data.last_write_time; last_write_time = data.last_write_time;
@@ -201,7 +201,7 @@ last_write_time_by_name :: proc(name: string) -> File_Time {
read_entire_file :: proc(name: string) -> ([]byte, bool) { read_entire_file :: proc(name: string) -> ([]byte, bool) {
buf: [300]byte; buf: [300]byte;
copy(buf[:], cast([]byte)name); copy(buf[..], cast([]byte)name);
fd, err := open(name, O_RDONLY, 0); fd, err := open(name, O_RDONLY, 0);
if err != ERROR_NONE { if err != ERROR_NONE {
+36 -37
View File
@@ -1,6 +1,5 @@
#import . "decimal.odin"; #import . "decimal.odin";
#import "math.odin"; #import "math.odin";
#import format "fmt.odin";
Int_Flag :: enum { Int_Flag :: enum {
PREFIX = 1<<0, PREFIX = 1<<0,
@@ -22,7 +21,7 @@ parse_bool :: proc(s: string) -> (result: bool, ok: bool) {
format_bool :: proc(buf: []byte, b: bool) -> string { format_bool :: proc(buf: []byte, b: bool) -> string {
s := b ? "true" : "false"; s := b ? "true" : "false";
len := copy(buf, cast([]byte)s); len := copy(buf, cast([]byte)s);
return cast(string)buf[:len]; return cast(string)buf[..len];
} }
format_uint :: proc(buf: []byte, u: u64, base: int) -> string { format_uint :: proc(buf: []byte, u: u64, base: int) -> string {
@@ -44,10 +43,10 @@ format_float :: proc(buf: []byte, f: f64, fmt: byte, prec, bit_size: int) -> str
Decimal_Slice :: struct { Decimal_Slice :: struct {
d: []byte, digits: []byte,
ndu: int, count: int,
dp: int, decimal_point: int,
neg: bool, neg: bool,
} }
Float_Info :: struct { Float_Info :: struct {
@@ -89,7 +88,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
s = "+Inf"; s = "+Inf";
} }
len := copy(buf, cast([]byte)s); len := copy(buf, cast([]byte)s);
return buf[:len]; return buf[..len];
case 0: // denormalized case 0: // denormalized
exp++; exp++;
@@ -108,19 +107,19 @@ 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 = Decimal_Slice{d = d.d[:], ndu = d.ndu, dp = d.dp}; digs = Decimal_Slice{digits = d.digits[..], count = d.count, decimal_point = d.decimal_point};
match fmt { match fmt {
case 'e', 'E': case 'e', 'E':
prec = digs.ndu-1; prec = digs.count-1;
case 'f', 'F': case 'f', 'F':
prec = max(digs.ndu-digs.dp, 0); prec = max(digs.count-digs.decimal_point, 0);
case 'g', 'G': case 'g', 'G':
prec = digs.ndu; prec = digs.count;
} }
} else { } else {
match fmt { match fmt {
case 'e', 'E': round(d, prec+1); case 'e', 'E': round(d, prec+1);
case 'f', 'F': round(d, d.dp+prec); case 'f', 'F': round(d, d.decimal_point+prec);
case 'g', 'G': case 'g', 'G':
if prec == 0 { if prec == 0 {
prec = 1; prec = 1;
@@ -128,7 +127,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
round(d, prec); round(d, prec);
} }
digs = Decimal_Slice{d = d.d[:], ndu = d.ndu, dp = d.dp}; digs = Decimal_Slice{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);
} }
@@ -138,7 +137,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slice, prec: int, fmt: byte) -> []byte { format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slice, prec: int, fmt: byte) -> []byte {
match fmt { match fmt {
case 'f', 'F': case 'f', 'F':
add_bytes :: proc(dst: ^[]byte, w: ^int, bytes: ...byte) { add_bytes :: proc(dst: ^[]byte, w: ^int, bytes: ..byte) {
for b in bytes { for b in bytes {
if dst.count <= w^ { if dst.count <= w^ {
break; break;
@@ -148,7 +147,7 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slic
} }
} }
dst := buf[:]; dst := buf[..];
w := 0; w := 0;
if neg { if neg {
add_bytes(^dst, ^w, '-'); add_bytes(^dst, ^w, '-');
@@ -157,10 +156,10 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slic
} }
// integer, padded with zeros when needed // integer, padded with zeros when needed
if digs.dp > 0 { if digs.decimal_point > 0 {
m := min(digs.ndu, digs.dp); m := min(digs.count, digs.decimal_point);
add_bytes(^dst, ^w, ...digs.d[:m]); add_bytes(^dst, ^w, ..digs.digits[..m]);
for ; m < digs.dp; m++ { for ; m < digs.decimal_point; m++ {
add_bytes(^dst, ^w, '0'); add_bytes(^dst, ^w, '0');
} }
} else { } else {
@@ -170,16 +169,16 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slic
// fractional part // fractional part
if prec > 0 { if prec > 0 {
add_bytes(^dst, ^w, '.'); add_bytes(^dst, ^w, '.');
for i in 0..<prec { for i in 0..prec {
c: byte = '0'; c: byte = '0';
if j := digs.dp + i; 0 <= j && j < digs.ndu { if j := digs.decimal_point + i; 0 <= j && j < digs.count {
c = digs.d[j]; c = digs.digits[j];
} }
add_bytes(^dst, ^w, c); add_bytes(^dst, ^w, c);
} }
} }
return buf[:w]; return buf[..w];
case 'e', 'E': case 'e', 'E':
return nil; // TODO return nil; // TODO
@@ -191,13 +190,13 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slic
c: [2]byte; c: [2]byte;
c[0] = '%'; c[0] = '%';
c[1] = fmt; c[1] = fmt;
len := copy(buf, ...c[:]); len := copy(buf, ..c[..]);
return buf[:len]; return buf[..len];
} }
round_shortest :: proc(d: ^Decimal, mant: u64, exp: int, flt: ^Float_Info) { round_shortest :: proc(d: ^Decimal, mant: u64, exp: int, flt: ^Float_Info) {
if mant == 0 { // If mantissa is zero, the number is zero if mant == 0 { // If mantissa is zero, the number is zero
d.ndu = 0; d.count = 0;
return; return;
} }
@@ -208,7 +207,7 @@ round_shortest :: proc(d: ^Decimal, mant: u64, exp: int, flt: ^Float_Info) {
332*(dp-nd) >= 100*(exp-mantbits) 332*(dp-nd) >= 100*(exp-mantbits)
*/ */
minexp := flt.bias+1; minexp := flt.bias+1;
if exp > minexp && 332*(d.dp-d.ndu) >= 100*(exp - cast(int)flt.mantbits) { if exp > minexp && 332*(d.decimal_point-d.count) >= 100*(exp - cast(int)flt.mantbits) {
// Number is already its shortest // Number is already its shortest
return; return;
} }
@@ -232,19 +231,19 @@ round_shortest :: proc(d: ^Decimal, mant: u64, exp: int, flt: ^Float_Info) {
inclusive := mant%2 == 0; inclusive := mant%2 == 0;
for i in 0..<d.ndu { for i in 0..d.count {
l: byte = '0'; // lower digit l: byte = '0'; // lower digit
if i < lower.ndu { if i < lower.count {
l = lower.d[i]; l = lower.digits[i];
} }
m := d.d[i]; // middle digit m := d.digits[i]; // middle digit
u: byte = '0'; // upper digit u: byte = '0'; // upper digit
if i < upper.ndu { if i < upper.count {
u = upper.d[i]; u = upper.digits[i];
} }
ok_round_down := l != m || inclusive && i+1 == lower.ndu; ok_round_down := l != m || inclusive && i+1 == lower.count;
ok_round_up := m != u && (inclusive || m+1 < u || i+1 < upper.ndu); ok_round_up := m != u && (inclusive || m+1 < u || i+1 < upper.count);
if (ok_round_down && ok_round_up) { if (ok_round_down && ok_round_up) {
round(d, i+1); round(d, i+1);
@@ -331,7 +330,7 @@ format_bits :: proc(buf: []byte, u: u64, base: int, neg: bool, digits: string, f
} }
len := copy(buf, ...a[i:]); len := copy(buf, ..a[i..]);
return cast(string)buf[:len]; return cast(string)buf[..len];
} }
+1 -1
View File
@@ -160,7 +160,7 @@ decode_last_rune :: proc(s: []byte) -> (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;
} }
+1 -2
View File
@@ -715,8 +715,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
TokenKind op = Token_Lt; TokenKind op = Token_Lt;
switch (ie->op.kind) { switch (ie->op.kind) {
case Token_HalfOpenRange: op = Token_Lt; break; case Token_Ellipsis: op = Token_Lt; break;
case Token_Ellipsis: op = Token_LtEq; break;
default: error(ie->op, "Invalid range operator"); break; default: error(ie->op, "Invalid range operator"); break;
} }
bool ok = compare_exact_values(Token_Lt, a, b); bool ok = compare_exact_values(Token_Lt, a, b);
+1 -2
View File
@@ -4742,8 +4742,7 @@ void ir_build_range_interval(irProcedure *proc, AstNodeIntervalExpr *node, Type
TokenKind op = Token_Lt; TokenKind op = Token_Lt;
switch (node->op.kind) { switch (node->op.kind) {
case Token_HalfOpenRange: op = Token_Lt; break; case Token_Ellipsis: op = Token_Lt; break;
case Token_Ellipsis: op = Token_LtEq; break;
default: GB_PANIC("Invalid interval operator"); break; default: GB_PANIC("Invalid interval operator"); break;
} }
irValue *cond = ir_emit_comp(proc, op, ir_emit_load(proc, value), upper); irValue *cond = ir_emit_comp(proc, op, ir_emit_load(proc, value), upper);
+2 -7
View File
@@ -1950,16 +1950,12 @@ AstNode *parse_atom_expr(AstFile *f, 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_Ellipsis) {
// f->curr_token.kind != Token_HalfOpenRange) {
if (f->curr_token.kind != Token_Colon) {
indices[0] = parse_expr(f, false); indices[0] = parse_expr(f, false);
} }
bool is_index = true; bool is_index = true;
// if (f->curr_token.kind == Token_Ellipsis || if (f->curr_token.kind == Token_Ellipsis) {
// f->curr_token.kind == Token_HalfOpenRange) {
if (f->curr_token.kind == Token_Colon) {
is_index = false; is_index = false;
interval = f->curr_token; interval = f->curr_token;
next_token(f); next_token(f);
@@ -2261,7 +2257,6 @@ AstNode *parse_simple_stmt(AstFile *f, bool in_stmt_ok) {
allow_token(f, Token_in); allow_token(f, Token_in);
AstNode *expr = parse_expr(f, false); AstNode *expr = parse_expr(f, false);
switch (f->curr_token.kind) { switch (f->curr_token.kind) {
case Token_HalfOpenRange:
case Token_Ellipsis: { case Token_Ellipsis: {
Token op = f->curr_token; Token op = f->curr_token;
next_token(f); next_token(f);
+31 -9
View File
@@ -73,8 +73,8 @@ TOKEN_KIND(Token__ComparisonEnd, "_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_HalfOpenRange, "..<"), \ /* TOKEN_KIND(Token_HalfOpenRange, "..<"), */ \
TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \ TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \
\ \
TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
@@ -225,6 +225,23 @@ 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);
} }
void syntax_warning_va(Token token, char *fmt, va_list va) {
gb_mutex_lock(&global_error_collector.mutex);
global_error_collector.warning_count++;
// NOTE(bill): Duplicate error, skip it
if (!token_pos_eq(global_error_collector.prev, token.pos)) {
global_error_collector.prev = token.pos;
gb_printf_err("%.*s(%td:%td) Syntax Warning: %s\n",
LIT(token.pos.file), token.pos.line, token.pos.column,
gb_bprintf_va(fmt, va));
} else if (token.pos.line == 0) {
gb_printf_err("Warning: %s\n", gb_bprintf_va(fmt, va));
}
gb_mutex_unlock(&global_error_collector.mutex);
}
void warning(Token token, char *fmt, ...) { void warning(Token token, char *fmt, ...) {
va_list va; va_list va;
@@ -247,6 +264,13 @@ void syntax_error(Token token, char *fmt, ...) {
va_end(va); va_end(va);
} }
void syntax_warning(Token token, char *fmt, ...) {
va_list va;
va_start(va, fmt);
syntax_warning_va(token, fmt, va);
va_end(va);
}
void compiler_error(char *fmt, ...) { void compiler_error(char *fmt, ...) {
va_list va; va_list va;
@@ -834,13 +858,11 @@ Token tokenizer_get_token(Tokenizer *t) {
token.kind = Token_Period; // Default token.kind = Token_Period; // Default
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);
if (t->curr_rune == '<') { token.kind = Token_Ellipsis;
advance_to_next_rune(t); // if (t->curr_rune == '<') {
token.kind = Token_HalfOpenRange; // advance_to_next_rune(t);
} else if (t->curr_rune == '.') { // token.kind = Token_HalfOpenRange;
advance_to_next_rune(t); // }
token.kind = Token_Ellipsis;
}
} }
break; break;