Change var decl syntax

`var x int;` from `x: int;`
This commit is contained in:
Ginger Bill
2016-12-18 22:32:18 +00:00
parent 4c10fbdcd4
commit 77e219d442
12 changed files with 342 additions and 342 deletions
-1
View File
@@ -10,5 +10,4 @@
proc main() { proc main() {
fmt.println("Here"); fmt.println("Here");
} }
+14 -14
View File
@@ -81,7 +81,7 @@ proc type_info_base(info ^Type_Info) -> ^Type_Info {
if info == nil { if info == nil {
return nil; return nil;
} }
base := info; var base = info;
match type i : base { match type i : base {
case Type_Info.Named: case Type_Info.Named:
base = i.base; base = i.base;
@@ -147,7 +147,7 @@ const DEFAULT_ALIGNMENT = align_of([vector 4]f32);
proc __check_context() { proc __check_context() {
c := ^__context; var c = ^__context;
if c.allocator.procedure == nil { if c.allocator.procedure == nil {
c.allocator = default_allocator(); c.allocator = default_allocator();
@@ -161,20 +161,20 @@ proc alloc(size int) -> rawptr #inline { return alloc_align(size, DEFAULT_ALIGNM
proc alloc_align(size, alignment int) -> rawptr #inline { proc alloc_align(size, alignment int) -> rawptr #inline {
__check_context(); __check_context();
a := context.allocator; var a = context.allocator;
return a.procedure(a.data, Allocator_Mode.ALLOC, size, alignment, nil, 0, 0); return a.procedure(a.data, Allocator_Mode.ALLOC, size, alignment, nil, 0, 0);
} }
proc free(ptr rawptr) #inline { proc free(ptr rawptr) #inline {
__check_context(); __check_context();
a := context.allocator; var a = context.allocator;
if ptr != nil { if ptr != nil {
a.procedure(a.data, Allocator_Mode.FREE, 0, 0, ptr, 0, 0); a.procedure(a.data, Allocator_Mode.FREE, 0, 0, ptr, 0, 0);
} }
} }
proc free_all() #inline { proc free_all() #inline {
__check_context(); __check_context();
a := context.allocator; var a = context.allocator;
a.procedure(a.data, Allocator_Mode.FREE_ALL, 0, 0, nil, 0, 0); a.procedure(a.data, Allocator_Mode.FREE_ALL, 0, 0, nil, 0, 0);
} }
@@ -182,7 +182,7 @@ proc free_all() #inline {
proc resize (ptr rawptr, old_size, new_size int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT); } proc resize (ptr rawptr, old_size, new_size int) -> rawptr #inline { return resize_align(ptr, old_size, new_size, DEFAULT_ALIGNMENT); }
proc resize_align(ptr rawptr, old_size, new_size, alignment int) -> rawptr #inline { proc resize_align(ptr rawptr, old_size, new_size, alignment int) -> rawptr #inline {
__check_context(); __check_context();
a := context.allocator; var a = context.allocator;
return a.procedure(a.data, Allocator_Mode.RESIZE, new_size, alignment, ptr, old_size, 0); return a.procedure(a.data, Allocator_Mode.RESIZE, new_size, alignment, ptr, old_size, 0);
} }
@@ -202,7 +202,7 @@ proc default_resize_align(old_memory rawptr, old_size, new_size, alignment int)
return old_memory; return old_memory;
} }
new_memory := alloc_align(new_size, alignment); var new_memory = alloc_align(new_size, alignment);
if new_memory == nil { if new_memory == nil {
return nil; return nil;
} }
@@ -220,9 +220,9 @@ proc default_allocator_proc(allocator_data rawptr, mode Allocator_Mode,
when false { when false {
match mode { match mode {
case ALLOC: case ALLOC:
total_size := size + alignment + size_of(mem.AllocationHeader); var total_size = size + alignment + size_of(mem.AllocationHeader);
ptr := os.heap_alloc(total_size); var ptr = os.heap_alloc(total_size);
header := ptr as ^mem.AllocationHeader; var header = ptr as ^mem.AllocationHeader;
ptr = mem.align_forward(header+1, alignment); ptr = mem.align_forward(header+1, alignment);
mem.allocation_header_fill(header, ptr, size); mem.allocation_header_fill(header, ptr, size);
return mem.zero(ptr, size); return mem.zero(ptr, size);
@@ -235,9 +235,9 @@ proc default_allocator_proc(allocator_data rawptr, mode Allocator_Mode,
// NOTE(bill): Does nothing // NOTE(bill): Does nothing
case RESIZE: case RESIZE:
total_size := size + alignment + size_of(mem.AllocationHeader); var total_size = size + alignment + size_of(mem.AllocationHeader);
ptr := os.heap_resize(mem.allocation_header(old_memory), total_size); var ptr = os.heap_resize(mem.allocation_header(old_memory), total_size);
header := ptr as ^mem.AllocationHeader; var header = ptr as ^mem.AllocationHeader;
ptr = mem.align_forward(header+1, alignment); ptr = mem.align_forward(header+1, alignment);
mem.allocation_header_fill(header, ptr, size); mem.allocation_header_fill(header, ptr, size);
return mem.zero(ptr, size); return mem.zero(ptr, size);
@@ -336,7 +336,7 @@ proc __enum_to_string(info ^Type_Info, value i64) -> string {
match type ti : type_info_base(info) { match type ti : type_info_base(info) {
case Type_Info.Enum: case Type_Info.Enum:
// TODO(bill): Search faster than linearly // TODO(bill): Search faster than linearly
for i := 0; i < ti.values.count; i++ { for var i = 0; i < ti.values.count; i++ {
if ti.values[i] == value { if ti.values[i] == value {
return ti.names[i]; return ti.names[i];
} }
+7 -7
View File
@@ -2,7 +2,7 @@
// Inline vs external file? // Inline vs external file?
#import win32 "sys/windows.odin" when ODIN_OS == "windows"; #import win32 "sys/windows.odin" when ODIN_OS == "windows";
_ := compile_assert(ODIN_ARCH == "amd64"); // TODO(bill): x86 version var _ = compile_assert(ODIN_ARCH == "amd64"); // TODO(bill): x86 version
proc yield_thread() { win32._mm_pause(); } proc yield_thread() { win32._mm_pause(); }
@@ -35,8 +35,8 @@ proc fetch_or32(a ^i32, operand i32) -> i32 {
return win32.InterlockedOr(a, operand); return win32.InterlockedOr(a, operand);
} }
proc spin_lock32(a ^i32, time_out int) -> bool { // NOTE(bill) time_out = -1 as default proc spin_lock32(a ^i32, time_out int) -> bool { // NOTE(bill) time_out = -1 as default
old_value := compare_exchange32(a, 1, 0); var old_value = compare_exchange32(a, 1, 0);
counter := 0; var counter = 0;
for old_value != 0 && (time_out < 0 || counter < time_out) { for old_value != 0 && (time_out < 0 || counter < time_out) {
counter++; counter++;
yield_thread(); yield_thread();
@@ -51,7 +51,7 @@ proc spin_unlock32(a ^i32) {
} }
proc try_acquire_lock32(a ^i32) -> bool { proc try_acquire_lock32(a ^i32) -> bool {
yield_thread(); yield_thread();
old_value := compare_exchange32(a, 1, 0); var old_value = compare_exchange32(a, 1, 0);
mfence(); mfence();
return old_value == 0; return old_value == 0;
} }
@@ -79,8 +79,8 @@ proc fetch_or64(a ^i64, operand i64) -> i64 {
return win32.InterlockedOr64(a, operand); return win32.InterlockedOr64(a, operand);
} }
proc spin_lock64(a ^i64, time_out int) -> bool { // NOTE(bill) time_out = -1 as default proc spin_lock64(a ^i64, time_out int) -> bool { // NOTE(bill) time_out = -1 as default
old_value := compare_exchange64(a, 1, 0); var old_value = compare_exchange64(a, 1, 0);
counter := 0; var counter = 0;
for old_value != 0 && (time_out < 0 || counter < time_out) { for old_value != 0 && (time_out < 0 || counter < time_out) {
counter++; counter++;
yield_thread(); yield_thread();
@@ -95,7 +95,7 @@ proc spin_unlock64(a ^i64) {
} }
proc try_acquire_lock64(a ^i64) -> bool { proc try_acquire_lock64(a ^i64) -> bool {
yield_thread(); yield_thread();
old_value := compare_exchange64(a, 1, 0); var old_value = compare_exchange64(a, 1, 0);
mfence(); mfence();
return old_value == 0; return old_value == 0;
} }
+60 -60
View File
@@ -5,23 +5,23 @@
const PRINT_BUF_SIZE = 1<<12; const PRINT_BUF_SIZE = 1<<12;
proc fprint(f ^os.File, args ..any) -> int { proc fprint(f ^os.File, args ..any) -> int {
data: [PRINT_BUF_SIZE]byte; var data [PRINT_BUF_SIZE]byte;
buf := data[:0]; var buf = data[:0];
bprint(^buf, ..args); bprint(^buf, ..args);
os.write(f, buf); os.write(f, buf);
return buf.count; return buf.count;
} }
proc fprintln(f ^os.File, args ..any) -> int { proc fprintln(f ^os.File, args ..any) -> int {
data: [PRINT_BUF_SIZE]byte; var data [PRINT_BUF_SIZE]byte;
buf := data[:0]; var buf = data[:0];
bprintln(^buf, ..args); bprintln(^buf, ..args);
os.write(f, buf); os.write(f, buf);
return buf.count; return buf.count;
} }
proc fprintf(f ^os.File, fmt string, args ..any) -> int { proc fprintf(f ^os.File, fmt string, args ..any) -> int {
data: [PRINT_BUF_SIZE]byte; var data [PRINT_BUF_SIZE]byte;
buf := data[:0]; var buf = data[:0];
bprintf(^buf, fmt, ..args); bprintf(^buf, fmt, ..args);
os.write(f, buf); os.write(f, buf);
return buf.count; return buf.count;
@@ -41,8 +41,8 @@ proc printf(fmt string, args ..any) -> int {
proc fprint_type(f ^os.File, info ^Type_Info) { proc fprint_type(f ^os.File, info ^Type_Info) {
data: [PRINT_BUF_SIZE]byte; var data [PRINT_BUF_SIZE]byte;
buf := data[:0]; var buf = data[:0];
bprint_type(^buf, info); bprint_type(^buf, info);
os.write(f, buf); os.write(f, buf);
} }
@@ -51,7 +51,7 @@ proc fprint_type(f ^os.File, info ^Type_Info) {
proc print_byte_buffer(buf ^[]byte, b []byte) { proc print_byte_buffer(buf ^[]byte, b []byte) {
if buf.count < buf.capacity { if buf.count < buf.capacity {
n := min(buf.capacity-buf.count, b.count); var n = min(buf.capacity-buf.count, b.count);
if n > 0 { if n > 0 {
mem.copy(buf.data + buf.count, b.data, n); mem.copy(buf.data + buf.count, b.data, n);
buf.count += n; buf.count += n;
@@ -65,21 +65,21 @@ proc bprint_string(buf ^[]byte, s string) {
proc byte_reverse(b []byte) { proc byte_reverse(b []byte) {
n := b.count; var n = b.count;
for i := 0; i < n/2; i++ { for var i = 0; i < n/2; i++ {
b[i], b[n-1-i] = b[n-1-i], b[i]; b[i], b[n-1-i] = b[n-1-i], b[i];
} }
} }
proc bprint_rune(buf ^[]byte, r rune) { proc bprint_rune(buf ^[]byte, r rune) {
b, n := utf8.encode_rune(r); var b, n = utf8.encode_rune(r);
bprint_string(buf, b[:n] as string); bprint_string(buf, b[:n] as string);
} }
proc bprint_space(buf ^[]byte) { bprint_rune(buf, ' '); } proc bprint_space(buf ^[]byte) { bprint_rune(buf, ' '); }
proc bprint_nl (buf ^[]byte) { bprint_rune(buf, '\n'); } proc bprint_nl (buf ^[]byte) { bprint_rune(buf, '\n'); }
__NUM_TO_CHAR_TABLE := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"; var __NUM_TO_CHAR_TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$";
proc bprint_bool(buffer ^[]byte, b bool) { proc bprint_bool(buffer ^[]byte, b bool) {
if b { if b {
@@ -98,9 +98,9 @@ proc bprint_f16 (buffer ^[]byte, f f32) #inline { print__f64(buffer, f as f64,
proc bprint_f32 (buffer ^[]byte, f f32) #inline { print__f64(buffer, f as f64, 7); } proc bprint_f32 (buffer ^[]byte, f f32) #inline { print__f64(buffer, f as f64, 7); }
proc bprint_f64 (buffer ^[]byte, f f64) #inline { print__f64(buffer, f as f64, 16); } proc bprint_f64 (buffer ^[]byte, f f64) #inline { print__f64(buffer, f as f64, 16); }
proc bprint_u64(buffer ^[]byte, value u64) { proc bprint_u64(buffer ^[]byte, value u64) {
i := value; var i = value;
buf: [20]byte; var buf [20]byte;
len := 0; var len = 0;
if i == 0 { if i == 0 {
buf[len] = '0'; buf[len] = '0';
len++; len++;
@@ -115,7 +115,7 @@ proc bprint_u64(buffer ^[]byte, value u64) {
} }
proc bprint_i64(buffer ^[]byte, value i64) { proc bprint_i64(buffer ^[]byte, value i64) {
// TODO(bill): Cleanup printing // TODO(bill): Cleanup printing
i := value; var i = value;
if i < 0 { if i < 0 {
i = -i; i = -i;
bprint_rune(buffer, '-'); bprint_rune(buffer, '-');
@@ -125,14 +125,14 @@ proc bprint_i64(buffer ^[]byte, value i64) {
/* /*
proc bprint_u128(buffer ^[]byte, value u128) { proc bprint_u128(buffer ^[]byte, value u128) {
a := value transmute [2]u64; var a = value transmute [2]u64;
if a[1] != 0 { if a[1] != 0 {
bprint_u64(buffer, a[1]); bprint_u64(buffer, a[1]);
} }
bprint_u64(buffer, a[0]); bprint_u64(buffer, a[0]);
} }
proc bprint_i128(buffer ^[]byte, value i128) { proc bprint_i128(buffer ^[]byte, value i128) {
i := value; var i = value;
if i < 0 { if i < 0 {
i = -i; i = -i;
bprint_rune(buffer, '-'); bprint_rune(buffer, '-');
@@ -143,7 +143,7 @@ proc bprint_i128(buffer ^[]byte, value i128) {
proc print__f64(buffer ^[]byte, value f64, decimal_places int) { proc print__f64(buffer ^[]byte, value f64, decimal_places int) {
f := value; var f = value;
if f == 0 { if f == 0 {
bprint_rune(buffer, '0'); bprint_rune(buffer, '0');
return; return;
@@ -153,13 +153,13 @@ proc print__f64(buffer ^[]byte, value f64, decimal_places int) {
f = -f; f = -f;
} }
i := f as u64; var i = f as u64;
bprint_u64(buffer, i); bprint_u64(buffer, i);
f -= i as f64; f -= i as f64;
bprint_rune(buffer, '.'); bprint_rune(buffer, '.');
mult: f64 = 10.0; var mult f64 = 10.0;
for ; decimal_places >= 0; decimal_places-- { for ; decimal_places >= 0; decimal_places-- {
i = (f * mult) as u64; i = (f * mult) as u64;
bprint_u64(buffer, i as u64); bprint_u64(buffer, i as u64);
@@ -212,7 +212,7 @@ proc bprint_type(buf ^[]byte, ti ^Type_Info) {
if info.params == nil { if info.params == nil {
bprint_string(buf, "()"); bprint_string(buf, "()");
} else { } else {
count := (info.params as ^Tuple).fields.count; var count = (info.params as ^Tuple).fields.count;
if count == 1 { bprint_string(buf, "("); } if count == 1 { bprint_string(buf, "("); }
bprint_type(buf, info.params); bprint_type(buf, info.params);
if count == 1 { bprint_string(buf, ")"); } if count == 1 { bprint_string(buf, ")"); }
@@ -222,12 +222,12 @@ proc bprint_type(buf ^[]byte, ti ^Type_Info) {
bprint_type(buf, info.results); bprint_type(buf, info.results);
} }
case Tuple: case Tuple:
count := info.fields.count; var count = info.fields.count;
if count != 1 { bprint_string(buf, "("); } if count != 1 { bprint_string(buf, "("); }
for i := 0; i < count; i++ { for var i = 0; i < count; i++ {
if i > 0 { bprint_string(buf, ", "); } if i > 0 { bprint_string(buf, ", "); }
f := info.fields[i]; var f = info.fields[i];
if f.name.count > 0 { if f.name.count > 0 {
bprint_string(buf, f.name); bprint_string(buf, f.name);
@@ -257,7 +257,7 @@ proc bprint_type(buf ^[]byte, ti ^Type_Info) {
if info.packed { bprint_string(buf, "#packed "); } if info.packed { bprint_string(buf, "#packed "); }
if info.ordered { bprint_string(buf, "#ordered "); } if info.ordered { bprint_string(buf, "#ordered "); }
bprint_string(buf, "{"); bprint_string(buf, "{");
for i := 0; i < info.fields.count; i++ { for var i = 0; i < info.fields.count; i++ {
if i > 0 { if i > 0 {
bprint_string(buf, ", "); bprint_string(buf, ", ");
} }
@@ -269,7 +269,7 @@ proc bprint_type(buf ^[]byte, ti ^Type_Info) {
case Union: case Union:
bprint_string(buf, "union {"); bprint_string(buf, "union {");
for i := 0; i < info.fields.count; i++ { for var i = 0; i < info.fields.count; i++ {
if i > 0 { if i > 0 {
bprint_string(buf, ", "); bprint_string(buf, ", ");
} }
@@ -281,7 +281,7 @@ proc bprint_type(buf ^[]byte, ti ^Type_Info) {
case Raw_Union: case Raw_Union:
bprint_string(buf, "raw_union {"); bprint_string(buf, "raw_union {");
for i := 0; i < info.fields.count; i++ { for var i = 0; i < info.fields.count; i++ {
if i > 0 { if i > 0 {
bprint_string(buf, ", "); bprint_string(buf, ", ");
} }
@@ -300,7 +300,7 @@ proc bprint_type(buf ^[]byte, ti ^Type_Info) {
proc make_any(type_info ^Type_Info, data rawptr) -> any { proc make_any(type_info ^Type_Info, data rawptr) -> any {
a: any; var a any;
a.type_info = type_info; a.type_info = type_info;
a.data = data; a.data = data;
return a; return a;
@@ -320,20 +320,20 @@ proc bprint_any(buf ^[]byte, arg any) {
using Type_Info; using Type_Info;
match type info : arg.type_info { match type info : arg.type_info {
case Named: case Named:
a := make_any(info.base, arg.data); var a = make_any(info.base, arg.data);
match type b : info.base { match type b : info.base {
case Struct: case Struct:
bprint_string(buf, info.name); bprint_string(buf, info.name);
bprint_string(buf, "{"); bprint_string(buf, "{");
for i := 0; i < b.fields.count; i++ { for var i = 0; i < b.fields.count; i++ {
f := b.fields[i]; var f = b.fields[i];
if i > 0 { if i > 0 {
bprint_string(buf, ", "); bprint_string(buf, ", ");
} }
bprint_string(buf, f.name); bprint_string(buf, f.name);
// bprint_any(buf, f.offset); // bprint_any(buf, f.offset);
bprint_string(buf, " = "); bprint_string(buf, " = ");
data := arg.data as ^byte + f.offset; var data = arg.data as ^byte + f.offset;
bprint_any(buf, make_any(f.type_info, data)); bprint_any(buf, make_any(f.type_info, data));
} }
bprint_string(buf, "}"); bprint_string(buf, "}");
@@ -384,8 +384,8 @@ proc bprint_any(buf ^[]byte, arg any) {
} }
case Maybe: case Maybe:
size := mem.size_of_type_info(info.elem); var size = mem.size_of_type_info(info.elem);
data := slice_ptr(arg.data as ^byte, size+1); var data = slice_ptr(arg.data as ^byte, size+1);
if data[size] != 0 { if data[size] != 0 {
bprint_any(buf, make_any(info.elem, arg.data)); bprint_any(buf, make_any(info.elem, arg.data));
} else { } else {
@@ -393,7 +393,7 @@ proc bprint_any(buf ^[]byte, arg any) {
} }
case Enum: case Enum:
value: i64 = 0; var value i64 = 0;
match type i : make_any(info.base, arg.data) { match type i : make_any(info.base, arg.data) {
case i8: value = i as i64; case i8: value = i as i64;
@@ -411,26 +411,26 @@ proc bprint_any(buf ^[]byte, arg any) {
bprintf(buf, "[%]%{", info.count, info.elem); bprintf(buf, "[%]%{", info.count, info.elem);
defer bprint_string(buf, "}"); defer bprint_string(buf, "}");
for i := 0; i < info.count; i++ { for var i = 0; i < info.count; i++ {
if i > 0 { if i > 0 {
bprint_string(buf, ", "); bprint_string(buf, ", ");
} }
data := arg.data as ^byte + i*info.elem_size; var data = arg.data as ^byte + i*info.elem_size;
bprint_any(buf, make_any(info.elem, data)); bprint_any(buf, make_any(info.elem, data));
} }
case Slice: case Slice:
slice := arg.data as ^[]byte; var slice = arg.data as ^[]byte;
bprintf(buf, "[]%{", info.elem); bprintf(buf, "[]%{", info.elem);
defer bprint_string(buf, "}"); defer bprint_string(buf, "}");
for i := 0; i < slice.count; i++ { for var i = 0; i < slice.count; i++ {
if i > 0 { if i > 0 {
bprint_string(buf, ", "); bprint_string(buf, ", ");
} }
data := slice.data + i*info.elem_size; var data = slice.data + i*info.elem_size;
bprint_any(buf, make_any(info.elem, data)); bprint_any(buf, make_any(info.elem, data));
} }
@@ -452,12 +452,12 @@ proc bprint_any(buf ^[]byte, arg any) {
return; return;
} }
for i := 0; i < info.count; i++ { for var i = 0; i < info.count; i++ {
if i > 0 { if i > 0 {
bprint_string(buf, ", "); bprint_string(buf, ", ");
} }
data := arg.data as ^byte + i*info.elem_size; var data = arg.data as ^byte + i*info.elem_size;
bprint_any(buf, make_any(info.elem, data)); bprint_any(buf, make_any(info.elem, data));
} }
@@ -466,14 +466,14 @@ proc bprint_any(buf ^[]byte, arg any) {
bprintf(buf, "%{", arg.type_info); bprintf(buf, "%{", arg.type_info);
defer bprint_string(buf, "}"); defer bprint_string(buf, "}");
for i := 0; i < info.fields.count; i++ { for var i = 0; i < info.fields.count; i++ {
if i > 0 { if i > 0 {
bprint_string(buf, ", "); bprint_string(buf, ", ");
} }
bprint_string(buf, info.fields[i].name); bprint_string(buf, info.fields[i].name);
bprint_string(buf, " = "); bprint_string(buf, " = ");
data := arg.data as ^byte + info.fields[i].offset; var data = arg.data as ^byte + info.fields[i].offset;
ti := info.fields[i].type_info; var ti = info.fields[i].type_info;
bprint_any(buf, make_any(ti, data)); bprint_any(buf, make_any(ti, data));
} }
@@ -495,10 +495,10 @@ proc bprintf(buf ^[]byte, fmt string, args ..any) -> int {
} }
proc parse_int(s string, offset int) -> (int, int) { proc parse_int(s string, offset int) -> (int, int) {
result := 0; var result = 0;
for ; offset < s.count; offset++ { for ; offset < s.count; offset++ {
c := s[offset] as rune; var c = s[offset] as rune;
if !is_digit(c) { if !is_digit(c) {
break; break;
} }
@@ -510,12 +510,12 @@ proc bprintf(buf ^[]byte, fmt string, args ..any) -> int {
return result, offset; return result, offset;
} }
prev := 0; var prev = 0;
implicit_index := 0; var implicit_index = 0;
for i := 0; i < fmt.count; i++ { for var i = 0; i < fmt.count; i++ {
r := fmt[i] as rune; var r = fmt[i] as rune;
index := implicit_index; var index = implicit_index;
if r != '%' { if r != '%' {
continue; continue;
@@ -524,7 +524,7 @@ proc bprintf(buf ^[]byte, fmt string, args ..any) -> int {
bprint_string(buf, fmt[prev:i]); bprint_string(buf, fmt[prev:i]);
i++; // Skip % i++; // Skip %
if i < fmt.count { if i < fmt.count {
next := fmt[i] as rune; var next = fmt[i] as rune;
if next == '%' { if next == '%' {
bprint_string(buf, "%"); bprint_string(buf, "%");
@@ -569,10 +569,10 @@ proc bprint(buf ^[]byte, args ..any) -> int {
} }
prev_string := false; var prev_string = false;
for i := 0; i < args.count; i++ { for var i = 0; i < args.count; i++ {
arg := args[i]; var arg = args[i];
is_string := arg.data != nil && is_type_string(arg.type_info); var is_string = arg.data != nil && is_type_string(arg.type_info);
if i > 0 && !is_string && !prev_string { if i > 0 && !is_string && !prev_string {
bprint_space(buf); bprint_space(buf);
} }
@@ -583,7 +583,7 @@ proc bprint(buf ^[]byte, args ..any) -> int {
} }
proc bprintln(buf ^[]byte, args ..any) -> int { proc bprintln(buf ^[]byte, args ..any) -> int {
for i := 0; i < args.count; i++ { for var i = 0; i < args.count; i++ {
if i > 0 { if i > 0 {
append(buf, ' '); append(buf, ' ');
} }
+35 -35
View File
@@ -1,57 +1,57 @@
proc crc32(data rawptr, len int) -> u32 { proc crc32(data rawptr, len int) -> u32 {
result := ~(0 as u32); var result = ~(0 as u32);
s := slice_ptr(data as ^u8, len); var s = slice_ptr(data as ^u8, len);
for i := 0; i < len; i++ { for var i = 0; i < len; i++ {
b := s[i] as u32; var b = s[i] as u32;
result = result>>8 ~ __CRC32_TABLE[(result ~ b) & 0xff]; result = result>>8 ~ __CRC32_TABLE[(result ~ b) & 0xff];
} }
return ~result; return ~result;
} }
proc crc64(data rawptr, len int) -> u64 { proc crc64(data rawptr, len int) -> u64 {
result := ~(0 as u64); var result = ~(0 as u64);
s := slice_ptr(data as ^u8, len); var s = slice_ptr(data as ^u8, len);
for i := 0; i < len; i++ { for var i = 0; i < len; i++ {
b := s[i] as u64; var b = s[i] as u64;
result = result>>8 ~ __CRC64_TABLE[(result ~ b) & 0xff]; result = result>>8 ~ __CRC64_TABLE[(result ~ b) & 0xff];
} }
return ~result; return ~result;
} }
proc fnv32(data rawptr, len int) -> u32 { proc fnv32(data rawptr, len int) -> u32 {
s := slice_ptr(data as ^u8, len); var s = slice_ptr(data as ^u8, len);
h: u32 = 0x811c9dc5; var h u32 = 0x811c9dc5;
for i := 0; i < len; i++ { for var i = 0; i < len; i++ {
h = (h * 0x01000193) ~ s[i] as u32; h = (h * 0x01000193) ~ s[i] as u32;
} }
return h; return h;
} }
proc fnv64(data rawptr, len int) -> u64 { proc fnv64(data rawptr, len int) -> u64 {
s := slice_ptr(data as ^u8, len); var s = slice_ptr(data as ^u8, len);
h: u64 = 0xcbf29ce484222325; var h u64 = 0xcbf29ce484222325;
for i := 0; i < len; i++ { for var i = 0; i < len; i++ {
h = (h * 0x100000001b3) ~ s[i] as u64; h = (h * 0x100000001b3) ~ s[i] as u64;
} }
return h; return h;
} }
proc fnv32a(data rawptr, len int) -> u32 { proc fnv32a(data rawptr, len int) -> u32 {
s := slice_ptr(data as ^u8, len); var s = slice_ptr(data as ^u8, len);
h: u32 = 0x811c9dc5; var h u32 = 0x811c9dc5;
for i := 0; i < len; i++ { for var i = 0; i < len; i++ {
h = (h ~ s[i] as u32) * 0x01000193; h = (h ~ s[i] as u32) * 0x01000193;
} }
return h; return h;
} }
proc fnv64a(data rawptr, len int) -> u64 { proc fnv64a(data rawptr, len int) -> u64 {
s := slice_ptr(data as ^u8, len); var s = slice_ptr(data as ^u8, len);
h: u64 = 0xcbf29ce484222325; var h u64 = 0xcbf29ce484222325;
for i := 0; i < len; i++ { for var i = 0; i < len; i++ {
h = (h ~ s[i] as u64) * 0x100000001b3; h = (h ~ s[i] as u64) * 0x100000001b3;
} }
return h; return h;
@@ -65,13 +65,13 @@ proc murmur64(data_ rawptr, len int) -> u64 {
const m = 0xc6a4a7935bd1e995; const m = 0xc6a4a7935bd1e995;
const r = 47; const r = 47;
h: u64 = SEED ~ (len as u64 * m); var h u64 = SEED ~ (len as u64 * m);
data := slice_ptr(data_ as ^u64, len/size_of(u64)); var data = slice_ptr(data_ as ^u64, len/size_of(u64));
data2 := slice_ptr(data_ as ^u8, len); var data2 = slice_ptr(data_ as ^u8, len);
for i := 0; i < data.count; i++ { for var i = 0; i < data.count; i++ {
k := data[i]; var k = data[i];
k *= m; k *= m;
k ~= k>>r; k ~= k>>r;
@@ -102,14 +102,14 @@ proc murmur64(data_ rawptr, len int) -> u64 {
const m = 0x5bd1e995; const m = 0x5bd1e995;
const r = 24; const r = 24;
h1: u32 = SEED as u32 ~ len as u32; var h1 u32 = SEED as u32 ~ len as u32;
h2: u32 = SEED >> 32; var h2 u32 = SEED >> 32;
data := slice_ptr(data_ as ^u32, len/size_of(u32)); var data = slice_ptr(data_ as ^u32, len/size_of(u32));
i := 0; var i = 0;
for len >= 8 { for len >= 8 {
k1, k2: u32; var k1, k2 u32;
k1 = data[i]; i++; k1 = data[i]; i++;
k1 *= m; k1 *= m;
k1 ~= k1>>r; k1 ~= k1>>r;
@@ -128,7 +128,7 @@ proc murmur64(data_ rawptr, len int) -> u64 {
} }
if (len >= 4) { if (len >= 4) {
k1: u32; var k1 u32;
k1 = data[i]; i++; k1 = data[i]; i++;
k1 *= m; k1 *= m;
k1 ~= k1>>r; k1 ~= k1>>r;
@@ -138,7 +138,7 @@ proc murmur64(data_ rawptr, len int) -> u64 {
len -= 4; len -= 4;
} }
data8 := slice_ptr((data.data+i) as ^u8, 3); // NOTE(bill): This is unsafe var data8 = slice_ptr((data.data+i) as ^u8, 3); // NOTE(bill): This is unsafe
match len { match len {
case 3: h2 ~= data8[2] as u32 << 16; fallthrough; case 3: h2 ~= data8[2] as u32 << 16; fallthrough;
@@ -157,14 +157,14 @@ proc murmur64(data_ rawptr, len int) -> u64 {
h2 ~= h1>>19; h2 ~= h1>>19;
h2 *= m; h2 *= m;
h := (h1 as u64)<<32 | h2 as u64; var h = (h1 as u64)<<32 | h2 as u64;
return h; return h;
} }
} }
__CRC32_TABLE := [256]u32{ var __CRC32_TABLE = [256]u32{
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
@@ -230,7 +230,7 @@ __CRC32_TABLE := [256]u32{
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
}; };
__CRC64_TABLE := [256]u64{ var __CRC64_TABLE = [256]u64{
0x0000000000000000, 0x42f0e1eba9ea3693, 0x85e1c3d753d46d26, 0xc711223cfa3e5bb5, 0x0000000000000000, 0x42f0e1eba9ea3693, 0x85e1c3d753d46d26, 0xc711223cfa3e5bb5,
0x493366450e42ecdf, 0x0bc387aea7a8da4c, 0xccd2a5925d9681f9, 0x8e224479f47cb76a, 0x493366450e42ecdf, 0x0bc387aea7a8da4c, 0xccd2a5925d9681f9, 0x8e224479f47cb76a,
0x9266cc8a1c85d9be, 0xd0962d61b56fef2d, 0x17870f5d4f51b498, 0x5577eeb6e6bb820b, 0x9266cc8a1c85d9be, 0xd0962d61b56fef2d, 0x17870f5d4f51b498, 0x5577eeb6e6bb820b,
+50 -50
View File
@@ -47,8 +47,8 @@ proc sign64(x f64) -> f64 { if x >= 0 { return +1; } return -1; }
proc copy_sign32(x, y f32) -> f32 { proc copy_sign32(x, y f32) -> f32 {
ix := x transmute u32; var ix = x transmute u32;
iy := y transmute u32; var iy = y transmute u32;
ix &= 0x7fffffff; ix &= 0x7fffffff;
ix |= iy & 0x80000000; ix |= iy & 0x80000000;
return ix transmute f32; return ix transmute f32;
@@ -78,7 +78,7 @@ proc remainder32(x, y f32) -> f32 {
proc fmod32(x, y f32) -> f32 { proc fmod32(x, y f32) -> f32 {
y = abs(y); y = abs(y);
result := remainder32(abs(x), y); var result = remainder32(abs(x), y);
if sign32(result) < 0 { if sign32(result) < 0 {
result += y; result += y;
} }
@@ -92,13 +92,13 @@ proc to_degrees(radians f32) -> f32 { return radians * 360 / TAU; }
proc dot2(a, b Vec2) -> f32 { c := a*b; return c.x + c.y; } proc dot2(a, b Vec2) -> f32 { var c = a*b; return c.x + c.y; }
proc dot3(a, b Vec3) -> f32 { c := a*b; return c.x + c.y + c.z; } proc dot3(a, b Vec3) -> f32 { var c = a*b; return c.x + c.y + c.z; }
proc dot4(a, b Vec4) -> f32 { c := a*b; return c.x + c.y + c.z + c.w; } proc dot4(a, b Vec4) -> f32 { var c = a*b; return c.x + c.y + c.z + c.w; }
proc cross3(x, y Vec3) -> Vec3 { proc cross3(x, y Vec3) -> Vec3 {
a := swizzle(x, 1, 2, 0) * swizzle(y, 2, 0, 1); var a = swizzle(x, 1, 2, 0) * swizzle(y, 2, 0, 1);
b := swizzle(x, 2, 0, 1) * swizzle(y, 1, 2, 0); var b = swizzle(x, 2, 0, 1) * swizzle(y, 1, 2, 0);
return a - b; return a - b;
} }
@@ -112,7 +112,7 @@ proc vec3_norm(v Vec3) -> Vec3 { return v / Vec3{vec3_mag(v)}; }
proc vec4_norm(v Vec4) -> Vec4 { return v / Vec4{vec4_mag(v)}; } proc vec4_norm(v Vec4) -> Vec4 { return v / Vec4{vec4_mag(v)}; }
proc vec2_norm0(v Vec2) -> Vec2 { proc vec2_norm0(v Vec2) -> Vec2 {
m := vec2_mag(v); var m = vec2_mag(v);
if m == 0 { if m == 0 {
return Vec2{0}; return Vec2{0};
} }
@@ -120,7 +120,7 @@ proc vec2_norm0(v Vec2) -> Vec2 {
} }
proc vec3_norm0(v Vec3) -> Vec3 { proc vec3_norm0(v Vec3) -> Vec3 {
m := vec3_mag(v); var m = vec3_mag(v);
if m == 0 { if m == 0 {
return Vec3{0}; return Vec3{0};
} }
@@ -128,7 +128,7 @@ proc vec3_norm0(v Vec3) -> Vec3 {
} }
proc vec4_norm0(v Vec4) -> Vec4 { proc vec4_norm0(v Vec4) -> Vec4 {
m := vec4_mag(v); var m = vec4_mag(v);
if m == 0 { if m == 0 {
return Vec4{0}; return Vec4{0};
} }
@@ -147,8 +147,8 @@ proc mat4_identity() -> Mat4 {
} }
proc mat4_transpose(m Mat4) -> Mat4 { proc mat4_transpose(m Mat4) -> Mat4 {
for j := 0; j < 4; j++ { for var j = 0; j < 4; j++ {
for i := 0; i < 4; i++ { for var i = 0; i < 4; i++ {
m[i][j], m[j][i] = m[j][i], m[i][j]; m[i][j], m[j][i] = m[j][i], m[i][j];
} }
} }
@@ -156,9 +156,9 @@ proc mat4_transpose(m Mat4) -> Mat4 {
} }
proc mat4_mul(a, b Mat4) -> Mat4 { proc mat4_mul(a, b Mat4) -> Mat4 {
c: Mat4; var c Mat4;
for j := 0; j < 4; j++ { for var j = 0; j < 4; j++ {
for i := 0; i < 4; i++ { for var i = 0; i < 4; i++ {
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] +
@@ -178,27 +178,27 @@ proc mat4_mul_vec4(m Mat4, v Vec4) -> Vec4 {
} }
proc mat4_inverse(m Mat4) -> Mat4 { proc mat4_inverse(m Mat4) -> Mat4 {
o: Mat4; var o Mat4;
sf00 := m[2][2] * m[3][3] - m[3][2] * m[2][3]; var sf00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
sf01 := m[2][1] * m[3][3] - m[3][1] * m[2][3]; var sf01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
sf02 := m[2][1] * m[3][2] - m[3][1] * m[2][2]; var sf02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
sf03 := m[2][0] * m[3][3] - m[3][0] * m[2][3]; var sf03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
sf04 := m[2][0] * m[3][2] - m[3][0] * m[2][2]; var sf04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
sf05 := m[2][0] * m[3][1] - m[3][0] * m[2][1]; var sf05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
sf06 := m[1][2] * m[3][3] - m[3][2] * m[1][3]; var sf06 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
sf07 := m[1][1] * m[3][3] - m[3][1] * m[1][3]; var sf07 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
sf08 := m[1][1] * m[3][2] - m[3][1] * m[1][2]; var sf08 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
sf09 := m[1][0] * m[3][3] - m[3][0] * m[1][3]; var sf09 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
sf10 := m[1][0] * m[3][2] - m[3][0] * m[1][2]; var sf10 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
sf11 := m[1][1] * m[3][3] - m[3][1] * m[1][3]; var sf11 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
sf12 := m[1][0] * m[3][1] - m[3][0] * m[1][1]; var sf12 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
sf13 := m[1][2] * m[2][3] - m[2][2] * m[1][3]; var sf13 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
sf14 := m[1][1] * m[2][3] - m[2][1] * m[1][3]; var sf14 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
sf15 := m[1][1] * m[2][2] - m[2][1] * m[1][2]; var sf15 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
sf16 := m[1][0] * m[2][3] - m[2][0] * m[1][3]; var sf16 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
sf17 := m[1][0] * m[2][2] - m[2][0] * m[1][2]; var sf17 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
sf18 := m[1][0] * m[2][1] - m[2][0] * m[1][1]; var sf18 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
o[0][0] = +(m[1][1] * sf00 - m[1][2] * sf01 + m[1][3] * sf02); o[0][0] = +(m[1][1] * sf00 - m[1][2] * sf01 + m[1][3] * sf02);
o[0][1] = -(m[1][0] * sf00 - m[1][2] * sf03 + m[1][3] * sf04); o[0][1] = -(m[1][0] * sf00 - m[1][2] * sf03 + m[1][3] * sf04);
@@ -220,7 +220,7 @@ proc mat4_inverse(m Mat4) -> Mat4 {
o[3][2] = -(m[0][0] * sf14 - m[0][1] * sf16 + m[0][3] * sf18); o[3][2] = -(m[0][0] * sf14 - m[0][1] * sf16 + m[0][3] * sf18);
o[3][3] = +(m[0][0] * sf15 - m[0][1] * sf17 + m[0][2] * sf18); o[3][3] = +(m[0][0] * sf15 - m[0][1] * sf17 + m[0][2] * sf18);
ood := 1.0 / (m[0][0] * o[0][0] + var ood = 1.0 / (m[0][0] * o[0][0] +
m[0][1] * o[0][1] + m[0][1] * o[0][1] +
m[0][2] * o[0][2] + m[0][2] * o[0][2] +
m[0][3] * o[0][3]); m[0][3] * o[0][3]);
@@ -247,7 +247,7 @@ proc mat4_inverse(m Mat4) -> Mat4 {
proc mat4_translate(v Vec3) -> Mat4 { proc mat4_translate(v Vec3) -> Mat4 {
m := mat4_identity(); var m = mat4_identity();
m[3][0] = v.x; m[3][0] = v.x;
m[3][1] = v.y; m[3][1] = v.y;
m[3][2] = v.z; m[3][2] = v.z;
@@ -256,13 +256,13 @@ proc mat4_translate(v Vec3) -> Mat4 {
} }
proc mat4_rotate(v Vec3, angle_radians f32) -> Mat4 { proc mat4_rotate(v Vec3, angle_radians f32) -> Mat4 {
c := cos32(angle_radians); var c = cos32(angle_radians);
s := sin32(angle_radians); var s = sin32(angle_radians);
a := vec3_norm(v); var a = vec3_norm(v);
t := a * Vec3{1-c}; var t = a * Vec3{1-c};
rot := mat4_identity(); var rot = mat4_identity();
rot[0][0] = c + t.x*a.x; rot[0][0] = c + t.x*a.x;
rot[0][1] = 0 + t.x*a.y + s*a.z; rot[0][1] = 0 + t.x*a.y + s*a.z;
@@ -298,11 +298,11 @@ proc mat4_scalef(m Mat4, s f32) -> Mat4 {
proc mat4_look_at(eye, centre, up Vec3) -> Mat4 { proc mat4_look_at(eye, centre, up Vec3) -> Mat4 {
f := vec3_norm(centre - eye); var f = vec3_norm(centre - eye);
s := vec3_norm(cross3(f, up)); var s = vec3_norm(cross3(f, up));
u := cross3(s, f); var u = cross3(s, f);
m: Mat4; var m Mat4;
m[0] = Vec4{+s.x, +s.y, +s.z, 0}; m[0] = Vec4{+s.x, +s.y, +s.z, 0};
m[1] = Vec4{+u.x, +u.y, +u.z, 0}; m[1] = Vec4{+u.x, +u.y, +u.z, 0};
@@ -312,8 +312,8 @@ proc mat4_look_at(eye, centre, up Vec3) -> Mat4 {
return m; return m;
} }
proc mat4_perspective(fovy, aspect, near, far f32) -> Mat4 { proc mat4_perspective(fovy, aspect, near, far f32) -> Mat4 {
m: Mat4; var m Mat4;
tan_half_fovy := tan32(0.5 * fovy); var tan_half_fovy = tan32(0.5 * fovy);
m[0][0] = 1.0 / (aspect*tan_half_fovy); m[0][0] = 1.0 / (aspect*tan_half_fovy);
m[1][1] = 1.0 / (tan_half_fovy); m[1][1] = 1.0 / (tan_half_fovy);
m[2][2] = -(far + near) / (far - near); m[2][2] = -(far + near) / (far - near);
@@ -324,7 +324,7 @@ proc mat4_perspective(fovy, aspect, near, far f32) -> Mat4 {
proc mat4_ortho3d(left, right, bottom, top, near, far f32) -> Mat4 { proc mat4_ortho3d(left, right, bottom, top, near, far f32) -> Mat4 {
m := mat4_identity(); var m = mat4_identity();
m[0][0] = +2.0 / (right - left); m[0][0] = +2.0 / (right - left);
m[1][1] = +2.0 / (top - bottom); m[1][1] = +2.0 / (top - bottom);
m[2][2] = -2.0 / (far - near); m[2][2] = -2.0 / (far - near);
+31 -31
View File
@@ -28,22 +28,22 @@ proc copy_non_overlapping(dst, src rawptr, len int) -> rawptr #link_name "__mem_
proc compare(dst, src rawptr, n int) -> int #link_name "__mem_compare" { proc compare(dst, src rawptr, n int) -> int #link_name "__mem_compare" {
// Translation of http://mgronhol.github.io/fast-strcmp/ // Translation of http://mgronhol.github.io/fast-strcmp/
a := slice_ptr(dst as ^byte, n); var a = slice_ptr(dst as ^byte, n);
b := slice_ptr(src as ^byte, n); var b = slice_ptr(src as ^byte, n);
fast := n/size_of(int) + 1; var fast = n/size_of(int) + 1;
offset := (fast-1)*size_of(int); var offset = (fast-1)*size_of(int);
curr_block := 0; var curr_block = 0;
if n <= size_of(int) { if n <= size_of(int) {
fast = 0; fast = 0;
} }
la := slice_ptr(^a[0] as ^int, fast); var la = slice_ptr(^a[0] as ^int, fast);
lb := slice_ptr(^b[0] as ^int, fast); var lb = slice_ptr(^b[0] as ^int, fast);
for ; curr_block < fast; curr_block++ { for ; curr_block < fast; curr_block++ {
if (la[curr_block] ~ lb[curr_block]) != 0 { if (la[curr_block] ~ lb[curr_block]) != 0 {
for pos := curr_block*size_of(int); pos < n; pos++ { for var pos = curr_block*size_of(int); pos < n; pos++ {
if (a[pos] ~ b[pos]) != 0 { if (a[pos] ~ b[pos]) != 0 {
return a[pos] as int - b[pos] as int; return a[pos] as int - b[pos] as int;
} }
@@ -78,9 +78,9 @@ proc is_power_of_two(x int) -> bool {
proc align_forward(ptr rawptr, align int) -> rawptr { proc align_forward(ptr rawptr, align int) -> rawptr {
assert(is_power_of_two(align)); assert(is_power_of_two(align));
a := align as uint; var a = align as uint;
p := ptr as uint; var p = ptr as uint;
modulo := p & (a-1); var modulo = p & (a-1);
if modulo != 0 { if modulo != 0 {
p += a - modulo; p += a - modulo;
} }
@@ -94,14 +94,14 @@ type Allocation_Header struct {
} }
proc allocation_header_fill(header ^Allocation_Header, data rawptr, size int) { proc allocation_header_fill(header ^Allocation_Header, data rawptr, size int) {
header.size = size; header.size = size;
ptr := (header+1) as ^int; var ptr = (header+1) as ^int;
for i := 0; ptr as rawptr < data; i++ { for var i = 0; ptr as rawptr < data; i++ {
(ptr+i)^ = -1; (ptr+i)^ = -1;
} }
} }
proc allocation_header(data rawptr) -> ^Allocation_Header { proc allocation_header(data rawptr) -> ^Allocation_Header {
p := data as ^int; var p = data as ^int;
for (p-1)^ == -1 { for (p-1)^ == -1 {
p = (p-1); p = (p-1);
} }
@@ -160,21 +160,21 @@ proc arena_allocator(arena ^Arena) -> Allocator {
proc arena_allocator_proc(allocator_data rawptr, mode Allocator_Mode, proc arena_allocator_proc(allocator_data rawptr, mode Allocator_Mode,
size, alignment int, size, alignment int,
old_memory rawptr, old_size int, flags u64) -> rawptr { old_memory rawptr, old_size int, flags u64) -> rawptr {
arena := allocator_data as ^Arena; var arena = allocator_data as ^Arena;
using Allocator_Mode; using Allocator_Mode;
match mode { match mode {
case ALLOC: case ALLOC:
total_size := size + alignment; var total_size = size + alignment;
if arena.memory.count + total_size > arena.memory.capacity { if arena.memory.count + total_size > arena.memory.capacity {
fmt.fprintln(os.stderr, "Arena out of memory"); fmt.fprintln(os.stderr, "Arena out of memory");
return nil; return nil;
} }
#no_bounds_check end := ^arena.memory[arena.memory.count]; #no_bounds_check var end = ^arena.memory[arena.memory.count];
ptr := align_forward(end, alignment); var ptr = align_forward(end, alignment);
arena.memory.count += total_size; arena.memory.count += total_size;
return zero(ptr, size); return zero(ptr, size);
@@ -193,7 +193,7 @@ proc arena_allocator_proc(allocator_data rawptr, mode Allocator_Mode,
} }
proc begin_arena_temp_memory(a ^Arena) -> Arena_Temp_Memory { proc begin_arena_temp_memory(a ^Arena) -> Arena_Temp_Memory {
tmp: Arena_Temp_Memory; var tmp Arena_Temp_Memory;
tmp.arena = a; tmp.arena = a;
tmp.original_count = a.memory.count; tmp.original_count = a.memory.count;
a.temp_count++; a.temp_count++;
@@ -253,9 +253,9 @@ proc align_of_type_info(type_info ^Type_Info) -> int {
case Slice: case Slice:
return WORD_SIZE; return WORD_SIZE;
case Vector: case Vector:
size := size_of_type_info(info.elem); var size = size_of_type_info(info.elem);
count := max(prev_pow2(info.count as i64), 1) as int; var count = max(prev_pow2(info.count as i64), 1) as int;
total := size * count; var total = size * count;
return clamp(total, 1, MAX_ALIGN); return clamp(total, 1, MAX_ALIGN);
case Struct: case Struct:
return info.align; return info.align;
@@ -271,7 +271,7 @@ proc align_of_type_info(type_info ^Type_Info) -> int {
} }
proc align_formula(size, align int) -> int { proc align_formula(size, align int) -> int {
result := size + align-1; var result = size + align-1;
return result - result%align; return result - result%align;
}; };
@@ -298,13 +298,13 @@ proc size_of_type_info(type_info ^Type_Info) -> int {
case Procedure: case Procedure:
return WORD_SIZE; return WORD_SIZE;
case Array: case Array:
count := info.count; var count = info.count;
if count == 0 { if count == 0 {
return 0; return 0;
} }
size := size_of_type_info(info.elem); var size = size_of_type_info(info.elem);
align := align_of_type_info(info.elem); var align = align_of_type_info(info.elem);
alignment := align_formula(size, align); var alignment = align_formula(size, align);
return alignment*(count-1) + size; return alignment*(count-1) + size;
case Slice: case Slice:
return 3*WORD_SIZE; return 3*WORD_SIZE;
@@ -319,18 +319,18 @@ proc size_of_type_info(type_info ^Type_Info) -> int {
return false; return false;
} }
count := info.count; var count = info.count;
if count == 0 { if count == 0 {
return 0; return 0;
} }
bit_size := 8*size_of_type_info(info.elem); var bit_size = 8*size_of_type_info(info.elem);
if is_bool(info.elem) { if is_bool(info.elem) {
// NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1` // NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1`
// Silly LLVM spec // Silly LLVM spec
bit_size = 1; bit_size = 1;
} }
total_size_in_bits := bit_size * count; var total_size_in_bits = bit_size * count;
total_size := (total_size_in_bits+7)/8; var total_size = (total_size_in_bits+7)/8;
return total_size; return total_size;
case Struct: case Struct:
+48 -48
View File
@@ -30,11 +30,11 @@ proc GetIntegerv(name i32, v ^i32) #foreign "glGetIntegerv"
_libgl := win32.LoadLibraryA(("opengl32.dll\x00" as string).data); var _libgl = win32.LoadLibraryA(("opengl32.dll\x00" as string).data);
proc GetProcAddress(name string) -> proc() { proc GetProcAddress(name string) -> proc() {
assert(name[name.count-1] == 0); assert(name[name.count-1] == 0);
res := win32.wglGetProcAddress(name.data); var res = win32.wglGetProcAddress(name.data);
if res == nil { if res == nil {
res = win32.GetProcAddress(_libgl, name.data); res = win32.GetProcAddress(_libgl, name.data);
} }
@@ -42,63 +42,63 @@ proc GetProcAddress(name string) -> proc() {
} }
GenBuffers: proc(count i32, buffers ^u32); var GenBuffers proc(count i32, buffers ^u32);
GenVertexArrays: proc(count i32, buffers ^u32); var GenVertexArrays proc(count i32, buffers ^u32);
GenSamplers: proc(count i32, buffers ^u32); var GenSamplers proc(count i32, buffers ^u32);
BindBuffer: proc(target i32, buffer u32); var BindBuffer proc(target i32, buffer u32);
BindVertexArray: proc(buffer u32); var BindVertexArray proc(buffer u32);
BindSampler: proc(position i32, sampler u32); var BindSampler proc(position i32, sampler u32);
BufferData: proc(target i32, size int, data rawptr, usage i32); var BufferData proc(target i32, size int, data rawptr, usage i32);
BufferSubData: proc(target i32, offset, size int, data rawptr); var BufferSubData proc(target i32, offset, size int, data rawptr);
DrawArrays: proc(mode, first i32, count u32); var DrawArrays proc(mode, first i32, count u32);
DrawElements: proc(mode i32, count u32, type_ i32, indices rawptr); var DrawElements proc(mode i32, count u32, type_ i32, indices rawptr);
MapBuffer: proc(target, access i32) -> rawptr; var MapBuffer proc(target, access i32) -> rawptr;
UnmapBuffer: proc(target i32); var UnmapBuffer proc(target i32);
VertexAttribPointer: proc(index u32, size, type_ i32, normalized i32, stride u32, pointer rawptr); var VertexAttribPointer proc(index u32, size, type_ i32, normalized i32, stride u32, pointer rawptr);
EnableVertexAttribArray: proc(index u32); var EnableVertexAttribArray proc(index u32);
CreateShader: proc(shader_type i32) -> u32; var CreateShader proc(shader_type i32) -> u32;
ShaderSource: proc(shader u32, count u32, str ^^byte, length ^i32); var ShaderSource proc(shader u32, count u32, str ^^byte, length ^i32);
CompileShader: proc(shader u32); var CompileShader proc(shader u32);
CreateProgram: proc() -> u32; var CreateProgram proc() -> u32;
AttachShader: proc(program, shader u32); var AttachShader proc(program, shader u32);
DetachShader: proc(program, shader u32); var DetachShader proc(program, shader u32);
DeleteShader: proc(shader u32); var DeleteShader proc(shader u32);
LinkProgram: proc(program u32); var LinkProgram proc(program u32);
UseProgram: proc(program u32); var UseProgram proc(program u32);
DeleteProgram: proc(program u32); var DeleteProgram proc(program u32);
GetShaderiv: proc(shader u32, pname i32, params ^i32); var GetShaderiv proc(shader u32, pname i32, params ^i32);
GetProgramiv: proc(program u32, pname i32, params ^i32); var GetProgramiv proc(program u32, pname i32, params ^i32);
GetShaderInfoLog: proc(shader u32, max_length u32, length ^u32, info_long ^byte); var GetShaderInfoLog proc(shader u32, max_length u32, length ^u32, info_long ^byte);
GetProgramInfoLog: proc(program u32, max_length u32, length ^u32, info_long ^byte); var GetProgramInfoLog proc(program u32, max_length u32, length ^u32, info_long ^byte);
ActiveTexture: proc(texture i32); var ActiveTexture proc(texture i32);
GenerateMipmap: proc(target i32); var GenerateMipmap proc(target i32);
SamplerParameteri: proc(sampler u32, pname i32, param i32); var SamplerParameteri proc(sampler u32, pname i32, param i32);
SamplerParameterf: proc(sampler u32, pname i32, param f32); var SamplerParameterf proc(sampler u32, pname i32, param f32);
SamplerParameteriv: proc(sampler u32, pname i32, params ^i32); var SamplerParameteriv proc(sampler u32, pname i32, params ^i32);
SamplerParameterfv: proc(sampler u32, pname i32, params ^f32); var SamplerParameterfv proc(sampler u32, pname i32, params ^f32);
SamplerParameterIiv: proc(sampler u32, pname i32, params ^i32); var SamplerParameterIiv proc(sampler u32, pname i32, params ^i32);
SamplerParameterIuiv: proc(sampler u32, pname i32, params ^u32); var SamplerParameterIuiv proc(sampler u32, pname i32, params ^u32);
Uniform1i: proc(loc i32, v0 i32); var Uniform1i proc(loc i32, v0 i32);
Uniform2i: proc(loc i32, v0, v1 i32); var Uniform2i proc(loc i32, v0, v1 i32);
Uniform3i: proc(loc i32, v0, v1, v2 i32); var Uniform3i proc(loc i32, v0, v1, v2 i32);
Uniform4i: proc(loc i32, v0, v1, v2, v3 i32); var Uniform4i proc(loc i32, v0, v1, v2, v3 i32);
Uniform1f: proc(loc i32, v0 f32); var Uniform1f proc(loc i32, v0 f32);
Uniform2f: proc(loc i32, v0, v1 f32); var Uniform2f proc(loc i32, v0, v1 f32);
Uniform3f: proc(loc i32, v0, v1, v2 f32); var Uniform3f proc(loc i32, v0, v1, v2 f32);
Uniform4f: proc(loc i32, v0, v1, v2, v3 f32); var Uniform4f proc(loc i32, v0, v1, v2, v3 f32);
UniformMatrix4fv: proc(loc i32, count u32, transpose i32, value ^f32); var UniformMatrix4fv proc(loc i32, count u32, transpose i32, value ^f32);
GetUniformLocation: proc(program u32, name ^byte) -> i32; var GetUniformLocation proc(program u32, name ^byte) -> i32;
proc init() { proc init() {
proc set_proc_address(p rawptr, name string) #inline { (p as ^proc())^ = GetProcAddress(name); } proc set_proc_address(p rawptr, name string) #inline { (p as ^proc())^ = GetProcAddress(name); }
+29 -29
View File
@@ -15,22 +15,22 @@ type File struct {
proc open(name string) -> (File, bool) { proc open(name string) -> (File, bool) {
using win32; using win32;
buf: [300]byte; var buf [300]byte;
var f File;
copy(buf[:], name as []byte); copy(buf[:], name as []byte);
f: File;
f.handle.p = CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, nil) as rawptr; f.handle.p = CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, nil) as rawptr;
success := f.handle.p != INVALID_HANDLE_VALUE; var success = f.handle.p != INVALID_HANDLE_VALUE;
f.last_write_time = last_write_time(^f); f.last_write_time = last_write_time(^f);
return f, success; return f, success;
} }
proc create(name string) -> (File, bool) { proc create(name string) -> (File, bool) {
using win32; using win32;
buf: [300]byte; var buf [300]byte;
var f File;
copy(buf[:], name as []byte); copy(buf[:], name as []byte);
f: File;
f.handle.p = CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, nil, CREATE_ALWAYS, 0, nil) as rawptr; f.handle.p = CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, nil, CREATE_ALWAYS, 0, nil) as rawptr;
success := f.handle.p != INVALID_HANDLE_VALUE; var success = f.handle.p != INVALID_HANDLE_VALUE;
f.last_write_time = last_write_time(^f); f.last_write_time = last_write_time(^f);
return f, success; return f, success;
} }
@@ -40,12 +40,12 @@ proc close(using f ^File) {
} }
proc write(using f ^File, buf []byte) -> bool { proc write(using f ^File, buf []byte) -> bool {
bytes_written: i32; var bytes_written i32;
return win32.WriteFile(handle.p as win32.HANDLE, buf.data, buf.count as i32, ^bytes_written, nil) != 0; return win32.WriteFile(handle.p as win32.HANDLE, buf.data, buf.count as i32, ^bytes_written, nil) != 0;
} }
proc file_has_changed(f ^File) -> bool { proc file_has_changed(f ^File) -> bool {
last_write_time := last_write_time(f); var last_write_time = last_write_time(f);
if f.last_write_time != last_write_time { if f.last_write_time != last_write_time {
f.last_write_time = last_write_time; f.last_write_time = last_write_time;
return true; return true;
@@ -56,17 +56,17 @@ proc file_has_changed(f ^File) -> bool {
proc last_write_time(f ^File) -> File_Time { proc last_write_time(f ^File) -> File_Time {
file_info: win32.BY_HANDLE_FILE_INFORMATION; var file_info win32.BY_HANDLE_FILE_INFORMATION;
win32.GetFileInformationByHandle(f.handle.p as win32.HANDLE, ^file_info); win32.GetFileInformationByHandle(f.handle.p as win32.HANDLE, ^file_info);
l := file_info.last_write_time.low_date_time as File_Time; var l = file_info.last_write_time.low_date_time as File_Time;
h := file_info.last_write_time.high_date_time as File_Time; var h = file_info.last_write_time.high_date_time as File_Time;
return l | h << 32; return l | h << 32;
} }
proc last_write_time_by_name(name string) -> File_Time { proc last_write_time_by_name(name string) -> File_Time {
last_write_time: win32.FILETIME; var last_write_time win32.FILETIME;
data: win32.WIN32_FILE_ATTRIBUTE_DATA; var data win32.WIN32_FILE_ATTRIBUTE_DATA;
buf: [1024]byte; var buf [1024]byte;
assert(buf.count > name.count); assert(buf.count > name.count);
@@ -76,8 +76,8 @@ proc last_write_time_by_name(name string) -> File_Time {
last_write_time = data.last_write_time; last_write_time = data.last_write_time;
} }
l := last_write_time.low_date_time as File_Time; var l = last_write_time.low_date_time as File_Time;
h := last_write_time.high_date_time as File_Time; var h = last_write_time.high_date_time as File_Time;
return l | h << 32; return l | h << 32;
} }
@@ -91,45 +91,45 @@ type File_Standard enum {
} }
// NOTE(bill): Uses startup to initialize it // NOTE(bill): Uses startup to initialize it
__std_files := [File_Standard.count]File{ var __std_files = [File_Standard.count]File{
{handle = win32.GetStdHandle(win32.STD_INPUT_HANDLE) transmute File_Handle }, {handle = win32.GetStdHandle(win32.STD_INPUT_HANDLE) transmute File_Handle },
{handle = win32.GetStdHandle(win32.STD_OUTPUT_HANDLE) transmute File_Handle }, {handle = win32.GetStdHandle(win32.STD_OUTPUT_HANDLE) transmute File_Handle },
{handle = win32.GetStdHandle(win32.STD_ERROR_HANDLE) transmute File_Handle }, {handle = win32.GetStdHandle(win32.STD_ERROR_HANDLE) transmute File_Handle },
}; };
stdin := ^__std_files[File_Standard.INPUT]; var stdin = ^__std_files[File_Standard.INPUT];
stdout := ^__std_files[File_Standard.OUTPUT]; var stdout = ^__std_files[File_Standard.OUTPUT];
stderr := ^__std_files[File_Standard.ERROR]; var stderr = ^__std_files[File_Standard.ERROR];
proc read_entire_file(name string) -> ([]byte, bool) { proc read_entire_file(name string) -> ([]byte, bool) {
buf: [300]byte; var buf [300]byte;
copy(buf[:], name as []byte); copy(buf[:], name as []byte);
f, file_ok := open(name); var f, file_ok = open(name);
if !file_ok { if !file_ok {
return nil, false; return nil, false;
} }
defer close(^f); defer close(^f);
length: i64; var length i64;
file_size_ok := win32.GetFileSizeEx(f.handle.p as win32.HANDLE, ^length) != 0; var file_size_ok = win32.GetFileSizeEx(f.handle.p as win32.HANDLE, ^length) != 0;
if !file_size_ok { if !file_size_ok {
return nil, false; return nil, false;
} }
data := new_slice(u8, length); var data = new_slice(u8, length);
if data.data == nil { if data.data == nil {
return nil, false; return nil, false;
} }
single_read_length: i32; var single_read_length i32;
total_read: i64; var total_read i64;
for total_read < length { for total_read < length {
remaining := length - total_read; var remaining = length - total_read;
to_read: u32; var to_read u32;
const MAX = 1<<32-1; const MAX = 1<<32-1;
if remaining <= MAX { if remaining <= MAX {
to_read = remaining as u32; to_read = remaining as u32;
+5 -5
View File
@@ -46,7 +46,7 @@ proc mutex_destroy(m ^Mutex) {
semaphore_destroy(^m.semaphore); semaphore_destroy(^m.semaphore);
} }
proc mutex_lock(m ^Mutex) { proc mutex_lock(m ^Mutex) {
thread_id := current_thread_id(); var thread_id = current_thread_id();
if atomic.fetch_add32(^m.counter, 1) > 0 { if atomic.fetch_add32(^m.counter, 1) > 0 {
if thread_id != atomic.load32(^m.owner) { if thread_id != atomic.load32(^m.owner) {
semaphore_wait(^m.semaphore); semaphore_wait(^m.semaphore);
@@ -56,11 +56,11 @@ proc mutex_lock(m ^Mutex) {
m.recursion++; m.recursion++;
} }
proc mutex_try_lock(m ^Mutex) -> bool { proc mutex_try_lock(m ^Mutex) -> bool {
thread_id := current_thread_id(); var thread_id = current_thread_id();
if atomic.load32(^m.owner) == thread_id { if atomic.load32(^m.owner) == thread_id {
atomic.fetch_add32(^m.counter, 1); atomic.fetch_add32(^m.counter, 1);
} else { } else {
expected: i32 = 0; var expected i32 = 0;
if atomic.load32(^m.counter) != 0 { if atomic.load32(^m.counter) != 0 {
return false; return false;
} }
@@ -73,8 +73,8 @@ proc mutex_try_lock(m ^Mutex) -> bool {
return true; return true;
} }
proc mutex_unlock(m ^Mutex) { proc mutex_unlock(m ^Mutex) {
recursion: i32; var recursion i32;
thread_id := current_thread_id(); var thread_id = current_thread_id();
assert(thread_id == atomic.load32(^m.owner)); assert(thread_id == atomic.load32(^m.owner));
m.recursion--; m.recursion--;
+32 -32
View File
@@ -14,7 +14,7 @@ type Accept_Range struct {
lo, hi u8; lo, hi u8;
}; };
accept_ranges := [5]Accept_Range{ var accept_ranges = [5]Accept_Range{
{0x80, 0xbf}, {0x80, 0xbf},
{0xa0, 0xbf}, {0xa0, 0xbf},
{0x80, 0x9f}, {0x80, 0x9f},
@@ -22,7 +22,7 @@ accept_ranges := [5]Accept_Range{
{0x80, 0x8f}, {0x80, 0x8f},
}; };
accept_sizes := [256]byte{ var accept_sizes = [256]byte{
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x00-0x0f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x00-0x0f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x10-0x1f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x10-0x1f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x20-0x2f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x20-0x2f
@@ -43,8 +43,8 @@ accept_sizes := [256]byte{
}; };
proc encode_rune(r rune) -> ([4]byte, int) { proc encode_rune(r rune) -> ([4]byte, int) {
buf: [4]byte; var buf [4]byte;
i := r as u32; var i = r as u32;
const mask = 0x3f as byte; const mask = 0x3f as byte;
if i <= 1<<7-1 { if i <= 1<<7-1 {
buf[0] = r as byte; buf[0] = r as byte;
@@ -77,22 +77,22 @@ proc encode_rune(r rune) -> ([4]byte, int) {
} }
proc decode_rune(s string) -> (rune, int) { proc decode_rune(s string) -> (rune, int) {
n := s.count; var n = s.count;
if n < 1 { if n < 1 {
return RUNE_ERROR, 0; return RUNE_ERROR, 0;
} }
b0 := s[0]; var b0 = s[0];
x := accept_sizes[b0]; var x = accept_sizes[b0];
if x >= 0xf0 { if x >= 0xf0 {
mask := (x as rune << 31) >> 31; // all zeros or all ones var mask = (x as rune << 31) >> 31; // all zeros or all ones
return (b0 as rune) &~ mask | RUNE_ERROR&mask, 1; return (b0 as rune) &~ mask | RUNE_ERROR&mask, 1;
} }
size := x & 7; var size = x & 7;
ar := accept_ranges[x>>4]; var ar = accept_ranges[x>>4];
if n < size as int { if n < size as int {
return RUNE_ERROR, 1; return RUNE_ERROR, 1;
} }
b1 := s[1]; var b1 = s[1];
if b1 < ar.lo || ar.hi < b1 { if b1 < ar.lo || ar.hi < b1 {
return RUNE_ERROR, 1; return RUNE_ERROR, 1;
} }
@@ -105,14 +105,14 @@ proc decode_rune(s string) -> (rune, int) {
if size == 2 { if size == 2 {
return (b0&MASK_2) as rune <<6 | (b1&MASK_X) as rune, 2; return (b0&MASK_2) as rune <<6 | (b1&MASK_X) as rune, 2;
} }
b2 := s[2]; var b2 = s[2];
if b2 < 0x80 || 0xbf < b2 { if b2 < 0x80 || 0xbf < b2 {
return RUNE_ERROR, 1; return RUNE_ERROR, 1;
} }
if size == 3 { if size == 3 {
return (b0&MASK_3) as rune <<12 | (b1&MASK_X) as rune <<6 | (b2&MASK_X) as rune, 3; return (b0&MASK_3) as rune <<12 | (b1&MASK_X) as rune <<6 | (b2&MASK_X) as rune, 3;
} }
b3 := s[3]; var b3 = s[3];
if b3 < 0x80 || 0xbf < b3 { if b3 < 0x80 || 0xbf < b3 {
return RUNE_ERROR, 1; return RUNE_ERROR, 1;
} }
@@ -133,31 +133,31 @@ proc valid_rune(r rune) -> bool {
} }
proc valid_string(s string) -> bool { proc valid_string(s string) -> bool {
n := s.count; var n = s.count;
for i := 0; i < n; { for var i = 0; i < n; {
si := s[i]; var si = s[i];
if si < RUNE_SELF { // ascii if si < RUNE_SELF { // ascii
i++; i++;
continue; continue;
} }
x := accept_sizes[si]; var x = accept_sizes[si];
if x == 0xf1 { if x == 0xf1 {
return false; return false;
} }
size := (x & 7) as int; var size = (x & 7) as int;
if i+size > n { if i+size > n {
return false; return false;
} }
ar := accept_ranges[x>>4]; var ar = accept_ranges[x>>4];
if b := s[i+1]; b < ar.lo || ar.hi < b { if var b = s[i+1]; b < ar.lo || ar.hi < b {
return false; return false;
} else if size == 2 { } else if size == 2 {
// Okay // Okay
} else if b := s[i+2]; b < 0x80 || 0xbf < b { } else if var b = s[i+2]; b < 0x80 || 0xbf < b {
return false; return false;
} else if size == 3 { } else if size == 3 {
// Okay // Okay
} else if b := s[i+3]; b < 0x80 || 0xbf < b { } else if var b = s[i+3]; b < 0x80 || 0xbf < b {
return false; return false;
} }
i += size; i += size;
@@ -166,34 +166,34 @@ proc valid_string(s string) -> bool {
} }
proc rune_count(s string) -> int { proc rune_count(s string) -> int {
count := 0; var count = 0;
n := s.count; var n = s.count;
for i := 0; i < n; count++ { for var i = 0; i < n; count++ {
si := s[i]; var si = s[i];
if si < RUNE_SELF { // ascii if si < RUNE_SELF { // ascii
i++; i++;
continue; continue;
} }
x := accept_sizes[si]; var x = accept_sizes[si];
if x == 0xf1 { if x == 0xf1 {
i++; i++;
continue; continue;
} }
size := (x & 7) as int; var size = (x & 7) as int;
if i+size > n { if i+size > n {
i++; i++;
continue; continue;
} }
ar := accept_ranges[x>>4]; var ar = accept_ranges[x>>4];
if b := s[i+1]; b < ar.lo || ar.hi < b { if var b = s[i+1]; b < ar.lo || ar.hi < b {
size = 1; size = 1;
} else if size == 2 { } else if size == 2 {
// Okay // Okay
} else if b := s[i+2]; b < 0x80 || 0xbf < b { } else if var b = s[i+2]; b < 0x80 || 0xbf < b {
size = 1; size = 1;
} else if size == 3 { } else if size == 3 {
// Okay // Okay
} else if b := s[i+3]; b < 0x80 || 0xbf < b { } else if var b = s[i+3]; b < 0x80 || 0xbf < b {
size = 1; size = 1;
} }
i += size; i += size;
+31 -30
View File
@@ -1889,33 +1889,33 @@ AstNode *parse_simple_stmt(AstFile *f) {
return make_assign_stmt(f, token, lhs, rhs); return make_assign_stmt(f, token, lhs, rhs);
} break; } break;
case Token_Colon: { // Declare // case Token_Colon: { // Declare
AstNodeArray names = lhs; // AstNodeArray names = lhs;
parse_check_name_list_for_reserves(f, names); // parse_check_name_list_for_reserves(f, names);
Token colon = expect_token(f, Token_Colon); // Token colon = expect_token(f, Token_Colon);
AstNode *type = parse_identifier_or_type(f); // AstNode *type = parse_identifier_or_type(f);
AstNodeArray values = {0}; // AstNodeArray values = {0};
if (allow_token(f, Token_Eq)) { // if (allow_token(f, Token_Eq)) {
values = parse_rhs_expr_list(f); // values = parse_rhs_expr_list(f);
if (values.count > names.count) { // if (values.count > names.count) {
syntax_error(f->curr_token, "Too many values on the right hand side of the declaration"); // syntax_error(f->curr_token, "Too many values on the right hand side of the declaration");
} else if (values.count == 0) { // } else if (values.count == 0) {
syntax_error(f->curr_token, "Expected an expression for this declaration"); // syntax_error(f->curr_token, "Expected an expression for this declaration");
} // }
if (type == NULL && values.count == 0) { // if (type == NULL && values.count == 0) {
syntax_error(f->curr_token, "Missing variable type or initialization"); // syntax_error(f->curr_token, "Missing variable type or initialization");
return make_bad_decl(f, f->curr_token, f->curr_token); // return make_bad_decl(f, f->curr_token, f->curr_token);
} // }
} // }
if (values.e == NULL) { // if (values.e == NULL) {
values = make_ast_node_array(f); // values = make_ast_node_array(f);
} // }
return make_var_decl(f, names, type, values); // return make_var_decl(f, names, type, values);
} break; // } break;
// case Token_ColonColon: { // case Token_ColonColon: {
// AstNodeArray names = lhs; // AstNodeArray names = lhs;
@@ -2695,9 +2695,7 @@ AstNode *parse_type_decl(AstFile *f) {
Token token = expect_token(f, Token_type); Token token = expect_token(f, Token_type);
AstNode *name = parse_identifier(f); AstNode *name = parse_identifier(f);
AstNode *type = parse_type(f); AstNode *type = parse_type(f);
AstNode *decl = make_type_decl(f, token, name, type); return make_type_decl(f, token, name, type);
expect_semicolon(f, decl);
return decl;
} }
AstNode *parse_value_decl(AstFile *f) { AstNode *parse_value_decl(AstFile *f) {
@@ -2714,7 +2712,7 @@ AstNode *parse_value_decl(AstFile *f) {
return make_bad_decl(f, token, f->curr_token); return make_bad_decl(f, token, f->curr_token);
} }
AstNodeArray names = parse_lhs_expr_list(f); AstNodeArray names = parse_identfier_list(f);
parse_check_name_list_for_reserves(f, names); parse_check_name_list_for_reserves(f, names);
AstNode *type = parse_type_attempt(f); AstNode *type = parse_type_attempt(f);
AstNodeArray values = {0}; AstNodeArray values = {0};
@@ -2754,7 +2752,6 @@ AstNode *parse_value_decl(AstFile *f) {
decl = make_const_decl(f, names, type, values); decl = make_const_decl(f, names, type, values);
break; break;
} }
expect_semicolon(f, decl);
return decl; return decl;
} }
@@ -2798,10 +2795,14 @@ AstNode *parse_stmt(AstFile *f) {
case Token_proc: case Token_proc:
return parse_proc_decl(f); return parse_proc_decl(f);
case Token_type: case Token_type:
return parse_type_decl(f); s = parse_type_decl(f);
expect_semicolon(f, s);
return s;
case Token_var: case Token_var:
case Token_const: case Token_const:
return parse_value_decl(f); s = parse_value_decl(f);
expect_semicolon(f, s);
return s;
case Token_using: { case Token_using: {