-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thinkin about how to format slices by writing reference implementatio…
…n in C and Penne. Revealed bug in slice pointer autoderef.
- Loading branch information
Showing
11 changed files
with
177 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
// Of course print_slice needs to be generic over the element type (here i32). | ||
fn print_slice(data: []i32) | ||
{ | ||
if |data| == 0 | ||
{ | ||
print!("[]\n"); | ||
goto end; | ||
} | ||
|
||
var buf: &&[]char8 = 0x0; | ||
var buflen: usize = 0; | ||
var real: usize = 0; | ||
|
||
{ | ||
var i: usize = 0; | ||
var writlen: usize = 0; | ||
{ | ||
var fmt0 = "[%d\0\0"; | ||
var fmt1 = ", %d\0"; | ||
var fmt: &[5]char8 = &fmt1; | ||
if i == 0 | ||
{ | ||
&fmt = &fmt0; | ||
} | ||
var head: &[...]char8 = add_offset(&buf, real * writlen); | ||
var len: usize = buflen - real * writlen; | ||
writlen = writlen + snprintf(&head, len, fmt, data[i]); | ||
|
||
if i == |data| | ||
{ | ||
goto terminator; | ||
} | ||
loop; | ||
} | ||
|
||
terminator: | ||
var head: &[...]char8 = add_offset(&buf, real * writlen); | ||
var len: usize = buflen - real * writlen; | ||
writlen = writlen + snprintf(&head, len, "]\0", 0); | ||
|
||
if real > 0 | ||
{ | ||
goto print; | ||
} | ||
|
||
buflen = writlen; | ||
&&buf = alloc(buflen); | ||
real = 1; | ||
loop; | ||
} | ||
|
||
print: | ||
print!(buf, "\n"); | ||
|
||
end: | ||
} | ||
|
||
// This is not the correct signature because variadics. | ||
extern fn snprintf(buf: &[]char8, buflen: usize, fmt: []char8, value: i32) -> usize; | ||
|
||
// This is not a real function. | ||
fn alloc(len: usize) -> &&[]char8; | ||
|
||
// This is not a real function. | ||
extern fn add_offset(buf: &[]char8, offset: usize) -> &[]char8; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
fn foo() | ||
{ | ||
var databuffer: [1024]u8; | ||
read_into_buffer(databuffer); | ||
read_into_fixed_buffer(databuffer); | ||
read_into_extern_buffer(databuffer); | ||
} | ||
|
||
fn bar(databuffer: &[1024]u8) | ||
{ | ||
read_into_buffer(databuffer); | ||
read_into_fixed_buffer(databuffer); | ||
read_into_extern_buffer(databuffer); | ||
} | ||
|
||
fn read_into_buffer(buffer: &[]u8); | ||
fn read_into_fixed_buffer(buffer: &[1024]u8); | ||
extern fn read_into_extern_buffer(buffer: &[]u8); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn use_slice_ptr(data: &[]i32) | ||
{ | ||
use_slice_ptr_2(data); | ||
use_ptr_to_endless(data); | ||
} | ||
|
||
fn use_slice_ptr_2(data: &[]i32); | ||
extern fn use_ptr_to_endless(data: &[]i32); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
fn all_uses_invalid() | ||
{ | ||
var five_zeroes: [5]i32 = [0, 0, 0, 0, 0]; | ||
var ptr_to_five_zeroes: &[5]i32 = &five_zeroes; | ||
// Invalid because use_ptr_to_ptr_to_endless might assign &[...]i32 to it. | ||
use_ptr_to_ptr_to_endless(&&ptr_to_five_zeroes); | ||
|
||
var ptr_to_ptr_to_five_zeroes: &&[5]i32 = &&ptr_to_five_zeroes; | ||
use_ptr_to_ptr_to_endless(&&ptr_to_ptr_to_five_zeroes); | ||
use_ptr_to_ptr_to_ptr_to_endless(&&&ptr_to_ptr_to_five_zeroes); | ||
} | ||
|
||
extern fn use_ptr_to_ptr_to_endless(data: &&[]i32); | ||
extern fn use_ptr_to_ptr_to_ptr_to_endless(data: &&&[]i32); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
fn all_valid() | ||
{ | ||
var five_zeroes: [5]i32 = [0, 0, 0, 0, 0]; | ||
use_ptr_to_endless(&five_zeroes); | ||
use_slice_ptr(&five_zeroes); | ||
var ptr_to_five_zeroes: &[5]i32 = &five_zeroes; | ||
use_ptr_to_endless(&ptr_to_five_zeroes); | ||
use_slice_ptr(&ptr_to_five_zeroes); | ||
var ptr_to_ptr_to_five_zeroes: &&[5]i32 = &&ptr_to_five_zeroes; | ||
use_ptr_to_endless(&ptr_to_ptr_to_five_zeroes); | ||
use_slice_ptr(&ptr_to_ptr_to_five_zeroes); | ||
} | ||
|
||
fn use_slice_ptr(data: &[]i32) | ||
{ | ||
use_slice(data); | ||
use_slice_ptr_2(&data); | ||
use_ptr_to_endless(&data); | ||
} | ||
|
||
fn use_slice(data: []i32); | ||
fn use_slice_ptr_2(data: &[]i32); | ||
|
||
extern fn use_ptr_to_endless(data: &[]i32); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters