io.Writer 는 핸들 하나짜리 enum 이다. 참조도 컨텍스트 포인터도 담지 않으므로 Copy 이고 자유롭게 오간다 (SPEC 5 R8). fmt 는 sink 를 소유하지 않는다 -- 호출자가 버퍼를 주고 앞에서 몇 바이트가 쓰였는지 돌려받는다. lowering 에 추가: enum 변이 상수, match, 정수 캐스트, 문자열 이스케이프. 프론트엔드 정밀도 하나: 항상 빠져나가는 분기의 상태를 병합하지 않는다. 그 분기가 소비한 값이 그 분기를 지나지 않은 경로에서도 소비된 것처럼 보였다. fmt_i32 가 이것 때문에 못 쓰였다. extern "c" 이름은 유닛 접두사를 붙이지 않는다. 링커가 이미 아는 이름이라는 것이 그 선언의 요점이다. run.py 199/199, exec.py 11/11.
26 lines
761 B
Plaintext
26 lines
761 B
Plaintext
unit std.io;
|
|
import std.sys;
|
|
|
|
// A writer is a handle and nothing else: an integer the runtime understands.
|
|
// It stores no reference and no context pointer, so it is Copy and can be
|
|
// passed and returned freely (SPEC 5 R8).
|
|
pub enum Writer { Null, Stdout, Stderr }
|
|
|
|
pub fn write(w: Writer, bytes: []u8) -> usize {
|
|
if w == Writer.Null { return bytes.n; }
|
|
var handle: i32 = 1;
|
|
if w == Writer.Stderr { handle = 2; }
|
|
let done: i32 = sys.raw_write(handle, &bytes[0], bytes.n);
|
|
if done < 0 { return 0; }
|
|
return done as usize;
|
|
}
|
|
|
|
pub fn print(bytes: []u8) -> usize {
|
|
return write(Writer.Stdout, bytes);
|
|
}
|
|
|
|
pub fn println(bytes: []u8) -> usize {
|
|
let n: usize = write(Writer.Stdout, bytes);
|
|
return n + write(Writer.Stdout, "\n");
|
|
}
|