From dddd7d8f447215a88aa87f438d4f495cf54be84d Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 9 Mar 2011 10:53:45 +0100 Subject: Add stdout_writer and string_writer to std.io For use by pretty-printer. string_writer API is a bit silly right now, feel free to suggest a cleaner way to do this. --- src/lib/io.rs | 71 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 23 deletions(-) (limited to 'src/lib') diff --git a/src/lib/io.rs b/src/lib/io.rs index 0c4eb39e1aa..71ebc1c7380 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -91,6 +91,7 @@ tag fileflag { truncate; } +// FIXME move into fd_buf_writer fn writefd(int fd, vec[u8] v) { auto len = _vec.len[u8](v); auto count = 0u; @@ -107,19 +108,17 @@ fn writefd(int fd, vec[u8] v) { } } -fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer { - - state obj fd_buf_writer(int fd) { - - fn write(vec[u8] v) { - writefd(fd, v); - } +state obj fd_buf_writer(int fd, bool must_close) { + fn write(vec[u8] v) { + writefd(fd, v); + } - drop { - os.libc.close(fd); - } + drop { + if (must_close) {os.libc.close(fd);} } +} +fn file_buf_writer(str path, vec[fileflag] flags) -> buf_writer { let int fflags = os.libc_constants.O_WRONLY() | os.libc_constants.O_BINARY(); @@ -142,26 +141,52 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer { log sys.rustrt.last_os_error(); fail; } - ret fd_buf_writer(fd); + ret fd_buf_writer(fd, true); } type writer = state obj { - fn write_str(str s); - fn write_int(int n); - fn write_uint(uint n); + impure fn write_str(str s); + impure fn write_int(int n); + impure fn write_uint(uint n); }; -fn file_writer(str path, - vec[fileflag] flags) - -> writer -{ - state obj fw(buf_writer out) { - fn write_str(str s) { out.write(_str.bytes(s)); } - fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); } - fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); } +state obj new_writer(buf_writer out) { + impure fn write_str(str s) { out.write(_str.bytes(s)); } + impure fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); } + impure fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); } +} + +fn file_writer(str path, vec[fileflag] flags) -> writer { + ret new_writer(file_buf_writer(path, flags)); +} + +// FIXME it would be great if this could be a const named stdout +fn stdout_writer() -> writer { + ret new_writer(fd_buf_writer(1, false)); +} + +type str_writer = + state obj { + fn get_writer() -> writer; + fn get_str() -> str; + }; + +type str_buf = @rec(mutable str buf); + +// TODO awkward! it's not possible to implement a writer with an extra method +fn string_writer() -> str_writer { + auto buf = @rec(mutable buf = ""); + state obj str_writer_writer(str_buf buf) { + impure fn write_str(str s) { buf.buf += s; } + impure fn write_int(int n) { buf.buf += _int.to_str(n, 10u); } + impure fn write_uint(uint n) { buf.buf += _uint.to_str(n, 10u); } + } + state obj str_writer_wrap(writer wr, str_buf buf) { + fn get_writer() -> writer {ret wr;} + fn get_str() -> str {ret buf.buf;} } - ret fw(new_buf_writer(path, flags)); + ret str_writer_wrap(str_writer_writer(buf), buf); } // -- cgit 1.4.1-3-g733a5 From aed40fbcd8e81cc1ef7a51b40b76b4631cba299e Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 9 Mar 2011 11:41:50 +0100 Subject: Have the pretty-printer take a writer stream as argument It now uses a string writer to also fill in for middle.ty.ast_ty_to_str --- src/comp/driver/rustc.rs | 2 +- src/comp/middle/ty.rs | 81 +---------------------------------------------- src/comp/pretty/pp.rs | 43 ++++++++++++++++--------- src/comp/pretty/pprust.rs | 10 ++++-- src/comp/rustc.rc | 1 + src/lib/io.rs | 43 +++++++++++++------------ 6 files changed, 61 insertions(+), 119 deletions(-) (limited to 'src/lib') diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs index 7ad0cdc74fd..6366bcafd2e 100644 --- a/src/comp/driver/rustc.rs +++ b/src/comp/driver/rustc.rs @@ -66,7 +66,7 @@ impure fn pretty_print_input(session.session sess, str input) { auto p = front.parser.new_parser(sess, env, 0, input); auto crate = front.parser.parse_crate_from_source_file(p); - pretty.pprust.print_ast(crate.node.module); + pretty.pprust.print_ast(crate.node.module, std.io.stdout_writer()); } fn warn_wrong_compiler() { diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index 05558da1311..958d0b78ef1 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -81,87 +81,10 @@ tag unify_result { // Stringification -fn ast_ty_to_str(&@ast.ty ty) -> str { - - fn ast_fn_input_to_str(&rec(ast.mode mode, @ast.ty ty) input) -> str { - auto s; - if (mode_is_alias(input.mode)) { - s = "&"; - } else { - s = ""; - } - - ret s + ast_ty_to_str(input.ty); - } - - fn ast_ty_field_to_str(&ast.ty_field f) -> str { - ret ast_ty_to_str(f.ty) + " " + f.ident; - } - - auto s; - alt (ty.node) { - case (ast.ty_nil) { s = "()"; } - case (ast.ty_bool) { s = "bool"; } - case (ast.ty_int) { s = "int"; } - case (ast.ty_uint) { s = "uint"; } - case (ast.ty_machine(?tm)) { s = common.ty_mach_to_str(tm); } - case (ast.ty_char) { s = "char"; } - case (ast.ty_str) { s = "str"; } - case (ast.ty_box(?t)) { s = "@" + ast_ty_to_str(t); } - case (ast.ty_vec(?t)) { s = "vec[" + ast_ty_to_str(t) + "]"; } - case (ast.ty_type) { s = "type"; } - - case (ast.ty_tup(?elts)) { - auto f = ast_ty_to_str; - s = "tup("; - s += _str.connect(_vec.map[@ast.ty,str](f, elts), ","); - s += ")"; - } - - case (ast.ty_rec(?fields)) { - auto f = ast_ty_field_to_str; - s = "rec("; - s += _str.connect(_vec.map[ast.ty_field,str](f, fields), ","); - s += ")"; - } - - case (ast.ty_fn(?proto, ?inputs, ?output)) { - auto f = ast_fn_input_to_str; - if (proto == ast.proto_fn) { - s = "fn("; - } else { - s = "iter("; - } - auto is = _vec.map[rec(ast.mode mode, @ast.ty ty),str](f, inputs); - s += _str.connect(is, ", "); - s += ")"; - - if (output.node != ast.ty_nil) { - s += " -> " + ast_ty_to_str(output); - } - } - - case (ast.ty_path(?path, _)) { - s = path_to_str(path); - } - - case (ast.ty_mutable(?t)) { - s = "mutable " + ast_ty_to_str(t); - } - - - case (_) { - fail; // FIXME: typestate bug - } - } - - ret s; -} - fn path_to_str(&ast.path pth) -> str { auto result = _str.connect(pth.node.idents, "."); if (_vec.len[@ast.ty](pth.node.types) > 0u) { - auto f = ast_ty_to_str; + auto f = pretty.pprust.ty_to_str; result += "["; result += _str.connect(_vec.map[@ast.ty,str](f, pth.node.types), ","); result += "]"; @@ -169,8 +92,6 @@ fn path_to_str(&ast.path pth) -> str { ret result; } -// FIXME use the pretty-printer for this once it has a concept of an -// abstract stream fn ty_to_str(&@t typ) -> str { fn fn_input_to_str(&rec(ast.mode mode, @t ty) input) -> str { diff --git a/src/comp/pretty/pp.rs b/src/comp/pretty/pp.rs index 43a9220f4e5..d3145180282 100644 --- a/src/comp/pretty/pp.rs +++ b/src/comp/pretty/pp.rs @@ -17,17 +17,19 @@ type context = rec(contexttype tp, uint indent); type ps = @rec(mutable vec[context] context, uint width, + io.writer out, mutable vec[token] buffered, mutable uint scandepth, mutable uint bufferedcol, mutable uint col, mutable bool start_of_line); -fn mkstate(uint width) -> ps { +fn mkstate(io.writer out, uint width) -> ps { let vec[context] stack = vec(rec(tp=cx_v, indent=0u)); let vec[token] buff = vec(); ret @rec(mutable context=stack, width=width, + out=out, mutable buffered=buff, mutable scandepth=0u, mutable bufferedcol=0u, @@ -46,10 +48,22 @@ impure fn pop_context(ps p) { } impure fn add_token(ps p, token tok) { - if (p.scandepth == 0u) {do_token(p, tok);} + if (p.width == 0u) {direct_token(p, tok);} + else if (p.scandepth == 0u) {do_token(p, tok);} else {buffer_token(p, tok);} } +impure fn direct_token(ps p, token tok) { + alt (tok) { + case (brk(?sz)) { + while (sz > 0u) {p.out.write_str(" "); sz -= 1u;} + } + case (word(?w)) {p.out.write_str(w);} + case (cword(?w)) {p.out.write_str(w);} + case (_) {} + } +} + impure fn buffer_token(ps p, token tok) { p.buffered += vec(tok); p.bufferedcol += token_size(tok); @@ -101,14 +115,13 @@ impure fn finish_block_scan(ps p, contexttype tp) { impure fn finish_break_scan(ps p) { if (p.bufferedcol > p.width) { - write_str("\n"); - p.col = 0u; + line_break(p); } else { auto width; alt (p.buffered.(0)) {case(brk(?w)) {width = w;}} auto i = 0u; - while (i < width) {write_str(" "); i+=1u;} + while (i < width) {p.out.write_str(" "); i+=1u;} p.col += width; } p.scandepth = 0u; @@ -142,20 +155,18 @@ impure fn do_token(ps p, token tok) { start_scan(p, tok); } case (cx_v) { - write_str("\n"); - p.col = 0u; - p.start_of_line = true; + line_break(p); } } } case (word(?w)) { before_print(p, false); - write_str(w); + p.out.write_str(w); p.col += _str.byte_len(w); // TODO char_len } case (cword(?w)) { before_print(p, true); - write_str(w); + p.out.write_str(w); p.col += _str.byte_len(w); // TODO char_len } case (open(?tp, ?indent)) { @@ -170,6 +181,12 @@ impure fn do_token(ps p, token tok) { } } +impure fn line_break(ps p) { + p.out.write_str("\n"); + p.col = 0u; + p.start_of_line = true; +} + impure fn before_print(ps p, bool closing) { if (p.start_of_line) { p.start_of_line = false; @@ -177,14 +194,10 @@ impure fn before_print(ps p, bool closing) { if (closing) {ind = base_indent(p);} else {ind = cur_context(p).indent;} p.col = ind; - while (ind > 0u) {write_str(" "); ind -= 1u;} + while (ind > 0u) {p.out.write_str(" "); ind -= 1u;} } } -fn write_str(str s) { - io.writefd(1, _str.bytes(s)); -} - fn token_size(token tok) -> uint { alt (tok) { case (brk(?sz)) {ret sz;} diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs index c54786fd5cf..e766cacda46 100644 --- a/src/comp/pretty/pprust.rs +++ b/src/comp/pretty/pprust.rs @@ -11,13 +11,19 @@ import foo = std.io; const uint indent_unit = 2u; const int as_prec = 5; -impure fn print_ast(ast._mod _mod) { - auto s = pp.mkstate(80u); +impure fn print_ast(ast._mod _mod, std.io.writer out) { + auto s = pp.mkstate(out, 80u); for (@ast.view_item vitem in _mod.view_items) {print_view_item(s, vitem);} line(s); for (@ast.item item in _mod.items) {print_item(s, item);} } +fn ty_to_str(&@ast.ty ty) -> str { + auto writer = std.io.string_writer(); + print_type(pp.mkstate(writer.get_writer(), 0u), ty); + ret writer.get_str(); +} + impure fn hbox(ps s) { pp.hbox(s, indent_unit); } diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc index 7e1d8fd9152..c7d3ff6b482 100644 --- a/src/comp/rustc.rc +++ b/src/comp/rustc.rc @@ -44,6 +44,7 @@ auth middle.trans = unsafe; auth middle.trans.copy_args_to_allocas = impure; auth middle.trans.trans_block = impure; auth lib.llvm = unsafe; +auth pretty.pprust = impure; mod lib { alt (target_os) { diff --git a/src/lib/io.rs b/src/lib/io.rs index 71ebc1c7380..dbe6de7a63b 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -91,26 +91,21 @@ tag fileflag { truncate; } -// FIXME move into fd_buf_writer -fn writefd(int fd, vec[u8] v) { - auto len = _vec.len[u8](v); - auto count = 0u; - auto vbuf; - while (count < len) { - vbuf = _vec.buf_off[u8](v, count); - auto nout = os.libc.write(fd, vbuf, len); - if (nout < 0) { - log "error dumping buffer"; - log sys.rustrt.last_os_error(); - fail; - } - count += nout as uint; - } -} - state obj fd_buf_writer(int fd, bool must_close) { fn write(vec[u8] v) { - writefd(fd, v); + auto len = _vec.len[u8](v); + auto count = 0u; + auto vbuf; + while (count < len) { + vbuf = _vec.buf_off[u8](v, count); + auto nout = os.libc.write(fd, vbuf, len); + if (nout < 0) { + log "error dumping buffer"; + log sys.rustrt.last_os_error(); + fail; + } + count += nout as uint; + } } drop { @@ -152,9 +147,15 @@ type writer = }; state obj new_writer(buf_writer out) { - impure fn write_str(str s) { out.write(_str.bytes(s)); } - impure fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); } - impure fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); } + impure fn write_str(str s) { + out.write(_str.bytes(s)); + } + impure fn write_int(int n) { + out.write(_str.bytes(_int.to_str(n, 10u))); + } + impure fn write_uint(uint n) { + out.write(_str.bytes(_uint.to_str(n, 10u))); + } } fn file_writer(str path, vec[fileflag] flags) -> writer { -- cgit 1.4.1-3-g733a5 From 11a10c7a7b86c7bf91a8adcd41573f9de9eeaf0e Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Wed, 9 Mar 2011 17:29:18 -0800 Subject: Remove redundant imports in lib (rustc doesn't like 'std' as a synonym for root within std.rc anyway) --- src/lib/_int.rs | 1 - src/lib/_str.rs | 2 +- src/lib/_uint.rs | 1 - src/lib/_vec.rs | 1 - src/lib/bitv.rs | 4 ---- src/lib/dbg.rs | 2 -- src/lib/deque.rs | 4 ---- src/lib/io.rs | 5 +---- src/lib/list.rs | 2 -- src/lib/map.rs | 6 ------ src/lib/sha1.rs | 3 --- 11 files changed, 2 insertions(+), 29 deletions(-) (limited to 'src/lib') diff --git a/src/lib/_int.rs b/src/lib/_int.rs index 38e161bcfa2..ee660f013b2 100644 --- a/src/lib/_int.rs +++ b/src/lib/_int.rs @@ -1,4 +1,3 @@ -import std.sys; fn add(int x, int y) -> int { ret x + y; } fn sub(int x, int y) -> int { ret x - y; } diff --git a/src/lib/_str.rs b/src/lib/_str.rs index 0e0e7650943..3f45334924d 100644 --- a/src/lib/_str.rs +++ b/src/lib/_str.rs @@ -1,6 +1,6 @@ import rustrt.sbuf; -import std._vec.rustrt.vbuf; +import _vec.rustrt.vbuf; native "rust" mod rustrt { type sbuf; diff --git a/src/lib/_uint.rs b/src/lib/_uint.rs index 79b5fa242b3..ceee9c77478 100644 --- a/src/lib/_uint.rs +++ b/src/lib/_uint.rs @@ -1,4 +1,3 @@ -import std.sys; fn add(uint x, uint y) -> uint { ret x + y; } fn sub(uint x, uint y) -> uint { ret x - y; } diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs index 2ffe8bad08b..680c8884f6d 100644 --- a/src/lib/_vec.rs +++ b/src/lib/_vec.rs @@ -1,5 +1,4 @@ import vbuf = rustrt.vbuf; -import std.option; type operator2[T,U,V] = fn(&T, &U) -> V; diff --git a/src/lib/bitv.rs b/src/lib/bitv.rs index adb3dc86279..2322c693b4d 100644 --- a/src/lib/bitv.rs +++ b/src/lib/bitv.rs @@ -1,7 +1,3 @@ -import std._uint; -import std._int; -import std._vec; - // FIXME: With recursive object types, we could implement binary methods like // union, intersection, and difference. At that point, we could write // an optimizing version of this module that produces a different obj diff --git a/src/lib/dbg.rs b/src/lib/dbg.rs index 774052dcaf5..b63f363e702 100644 --- a/src/lib/dbg.rs +++ b/src/lib/dbg.rs @@ -5,8 +5,6 @@ * logging. */ -import std._vec; - // FIXME: handle 64-bit case. const uint const_refcount = 0x7bad_face_u; diff --git a/src/lib/deque.rs b/src/lib/deque.rs index 1602345703d..776f82e99fe 100644 --- a/src/lib/deque.rs +++ b/src/lib/deque.rs @@ -2,10 +2,6 @@ * A deque, for fun. Untested as of yet. Likely buggy. */ -import std.option; -import std._vec; -import std._int; - type t[T] = obj { fn size() -> uint; diff --git a/src/lib/io.rs b/src/lib/io.rs index dbe6de7a63b..34c4a98d9ac 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -1,7 +1,4 @@ -import std.os.libc; -import std._str; -import std._vec; - +import os.libc; type stdio_reader = state obj { fn getc() -> int; diff --git a/src/lib/list.rs b/src/lib/list.rs index c4661940a5f..58c2cded6e6 100644 --- a/src/lib/list.rs +++ b/src/lib/list.rs @@ -1,5 +1,3 @@ - -import std.option; import option.some; import option.none; diff --git a/src/lib/map.rs b/src/lib/map.rs index 7f760b65967..29e9ba5d691 100644 --- a/src/lib/map.rs +++ b/src/lib/map.rs @@ -3,12 +3,6 @@ * use, but useful as a stress test for rustboot. */ -import std._int; -import std.sys; -import std.option; -import std._vec; - - type hashfn[K] = fn(&K) -> uint; type eqfn[K] = fn(&K, &K) -> bool; diff --git a/src/lib/sha1.rs b/src/lib/sha1.rs index 2a6b74d4ff3..a57ea894cdf 100644 --- a/src/lib/sha1.rs +++ b/src/lib/sha1.rs @@ -4,9 +4,6 @@ * point this will want to be rewritten. */ -import std._vec; -import std._str; - export sha1; export mk_sha1; -- cgit 1.4.1-3-g733a5