compiler/
lib/
examples/
std/
arch/
char/
collections/
graph/
lang/
sys/
arch.rad
68 B
char.rad
855 B
collections.rad
39 B
fmt.rad
8.3 KiB
graph.rad
4.3 KiB
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
276 B
mem.rad
2.3 KiB
sys.rad
179 B
testing.rad
2.4 KiB
tests.rad
15.7 KiB
vec.rad
3.2 KiB
std.rad
299 B
scripts/
seed/
sublime/
test/
vim/
.gitignore
336 B
.gitsigners
112 B
CELL_PERMISSIONS
6.8 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
5.4 KiB
README
2.5 KiB
STYLE
2.5 KiB
std.lib
1.5 KiB
std.lib.test
808 B
lib/std/io.rad
raw
| 1 | //! Input/output utilities. |
| 2 | use std::fmt; |
| 3 | use std::intrinsics; |
| 4 | |
| 5 | /// Write the bytes to standard output. |
| 6 | export fn print(str: &[u8]) { |
| 7 | unsafe { |
| 8 | intrinsics::ecall(64, 1, str.ptr as i64, str.len as i64, 0); |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | /// Write the bytes to standard error. |
| 13 | export fn printError(str: &[u8]) { |
| 14 | unsafe { |
| 15 | intrinsics::ecall(64, 2, str.ptr as i64, str.len as i64, 0); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /// Write the bytes and a newline to standard output. |
| 20 | export fn printLn(str: &[u8]) { |
| 21 | print(str); |
| 22 | print("\n"); |
| 23 | } |
| 24 | |
| 25 | /// Write a signed decimal integer to standard output. |
| 26 | export fn printI32(val: i32) { |
| 27 | let mut buffer: [u8; 11] = [0; 11]; |
| 28 | let start = fmt::formatI32(val, &mut buffer[..]); |
| 29 | print(&buffer[start..]); |
| 30 | } |
| 31 | |
| 32 | /// Write an unsigned decimal integer to standard output. |
| 33 | export fn printU32(val: u32) { |
| 34 | let mut buffer: [u8; 10] = [0; 10]; |
| 35 | let start = fmt::formatU32(val, &mut buffer[..]); |
| 36 | print(&buffer[start..]); |
| 37 | } |
| 38 | |
| 39 | /// Write a Boolean value to standard output. |
| 40 | export fn printBool(val: bool) { |
| 41 | let mut buffer: [u8; 5] = [0; 5]; |
| 42 | let start = fmt::formatBool(val, &mut buffer[..]); |
| 43 | print(&buffer[start..]); |
| 44 | } |
| 45 | |
| 46 | /// Read standard input into the buffer and return the system call result. |
| 47 | export fn read(buf: &mut [u8]) -> u32 { |
| 48 | unsafe { |
| 49 | return intrinsics::ecall(63, 0, buf.ptr as i64, buf.len as i64, 0) as u32; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | export fn readToEnd(buf: *mut [u8]) -> *[u8] { |
| 54 | let mut total: u32 = 0; |
| 55 | |
| 56 | while total < buf.len { |
| 57 | let chunk: *mut [u8] = &mut buf[total..]; |
| 58 | let n: u32 = read(chunk); |
| 59 | |
| 60 | if n == 0 { |
| 61 | break; |
| 62 | } |
| 63 | if n > chunk.len { |
| 64 | set total = buf.len; |
| 65 | break; |
| 66 | } |
| 67 | set total += n; |
| 68 | } |
| 69 | return &buf[..total]; |
| 70 | } |