core: add macro_rules! for "condition! { c: in -> out; }".

This commit is contained in:
Graydon Hoare 2012-12-14 11:52:11 -08:00
parent 263136d389
commit 8e28f23c60
9 changed files with 58 additions and 35 deletions

View file

@ -10,16 +10,17 @@
// helper for transmutation, shown below. // helper for transmutation, shown below.
type RustClosure = (int,int); type RustClosure = (int,int);
struct Handler<T, U:Copy> { pub struct Handler<T, U> {
handle: RustClosure, handle: RustClosure,
prev: Option<@Handler<T, U>>, prev: Option<@Handler<T, U>>,
} }
struct Condition<T, U:Copy> { pub struct Condition<T, U> {
name: &static/str,
key: task::local_data::LocalDataKey<Handler<T,U>> key: task::local_data::LocalDataKey<Handler<T,U>>
} }
impl<T, U: Copy> Condition<T,U> { impl<T, U> Condition<T,U> {
fn trap(&self, h: &self/fn(&T) ->U) -> Trap/&self<T,U> { fn trap(&self, h: &self/fn(&T) ->U) -> Trap/&self<T,U> {
unsafe { unsafe {
@ -32,7 +33,9 @@ impl<T, U: Copy> Condition<T,U> {
fn raise(t:&T) -> U { fn raise(t:&T) -> U {
do self.raise_default(t) { do self.raise_default(t) {
fail ~"Unhandled condition"; fail fmt!("Unhandled condition: %s: %?",
self.name,
t);
} }
} }
@ -65,13 +68,13 @@ impl<T, U: Copy> Condition<T,U> {
struct Trap<T, U:Copy> { struct Trap<T, U> {
cond: &Condition<T,U>, cond: &Condition<T,U>,
handler: @Handler<T, U> handler: @Handler<T, U>
} }
impl<T, U: Copy> Trap<T,U> { impl<T, U> Trap<T,U> {
fn in<V: Copy>(&self, inner: &self/fn() -> V) -> V { fn in<V>(&self, inner: &self/fn() -> V) -> V {
unsafe { unsafe {
let _g = Guard { cond: self.cond }; let _g = Guard { cond: self.cond };
debug!("Trap: pushing handler to TLS"); debug!("Trap: pushing handler to TLS");
@ -81,7 +84,7 @@ impl<T, U: Copy> Trap<T,U> {
} }
} }
struct Guard<T, U:Copy> { struct Guard<T, U> {
cond: &Condition<T,U>, cond: &Condition<T,U>,
drop { drop {
unsafe { unsafe {
@ -105,13 +108,13 @@ struct Guard<T, U:Copy> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
fn sadness_key(_x: @Handler<int,int>) { } condition! {
const sadness_condition : Condition<int,int> = sadness: int -> int;
Condition { key: sadness_key }; }
fn trouble(i: int) { fn trouble(i: int) {
debug!("trouble: raising conition"); debug!("trouble: raising conition");
let j = sadness_condition.raise(&i); let j = sadness::cond.raise(&i);
debug!("trouble: handler recovered with %d", j); debug!("trouble: handler recovered with %d", j);
} }
@ -119,7 +122,7 @@ mod test {
let mut inner_trapped = false; let mut inner_trapped = false;
do sadness_condition.trap(|_j| { do sadness::cond.trap(|_j| {
debug!("nested_trap_test_inner: in handler"); debug!("nested_trap_test_inner: in handler");
inner_trapped = true; inner_trapped = true;
0 0
@ -136,7 +139,7 @@ mod test {
let mut outer_trapped = false; let mut outer_trapped = false;
do sadness_condition.trap(|_j| { do sadness::cond.trap(|_j| {
debug!("nested_trap_test_outer: in handler"); debug!("nested_trap_test_outer: in handler");
outer_trapped = true; 0 outer_trapped = true; 0
}).in { }).in {
@ -152,12 +155,12 @@ mod test {
let mut inner_trapped = false; let mut inner_trapped = false;
do sadness_condition.trap(|_j| { do sadness::cond.trap(|_j| {
debug!("nested_reraise_trap_test_inner: in handler"); debug!("nested_reraise_trap_test_inner: in handler");
inner_trapped = true; inner_trapped = true;
let i = 10; let i = 10;
debug!("nested_reraise_trap_test_inner: handler re-raising"); debug!("nested_reraise_trap_test_inner: handler re-raising");
sadness_condition.raise(&i) sadness::cond.raise(&i)
}).in { }).in {
debug!("nested_reraise_trap_test_inner: in protected block"); debug!("nested_reraise_trap_test_inner: in protected block");
trouble(1); trouble(1);
@ -171,7 +174,7 @@ mod test {
let mut outer_trapped = false; let mut outer_trapped = false;
do sadness_condition.trap(|_j| { do sadness::cond.trap(|_j| {
debug!("nested_reraise_trap_test_outer: in handler"); debug!("nested_reraise_trap_test_outer: in handler");
outer_trapped = true; 0 outer_trapped = true; 0
}).in { }).in {
@ -187,9 +190,9 @@ mod test {
let mut trapped = false; let mut trapped = false;
do sadness_condition.trap(|j| { do sadness::cond.trap(|j| {
debug!("test_default: in handler"); debug!("test_default: in handler");
sadness_condition.raise_default(j, || {trapped=true; 5}) sadness::cond.raise_default(j, || {trapped=true; 5})
}).in { }).in {
debug!("test_default: in protected block"); debug!("test_default: in protected block");
trouble(1); trouble(1);

View file

@ -242,6 +242,7 @@ mod core {
pub const debug : u32 = 4_u32; pub const debug : u32 = 4_u32;
pub use cmp; pub use cmp;
pub use condition;
} }

View file

@ -17,7 +17,7 @@ export inject_intrinsic;
fn inject_intrinsic(sess: Session, fn inject_intrinsic(sess: Session,
crate: @ast::crate) -> @ast::crate { crate: @ast::crate) -> @ast::crate {
let intrinsic_module = @include_str!("intrinsic.rs"); let intrinsic_module = @(include_str!("intrinsic.rs").to_owned());
let item = parse::parse_item_from_source_str(~"<intrinsic>", let item = parse::parse_item_from_source_str(~"<intrinsic>",
intrinsic_module, intrinsic_module,

View file

@ -138,7 +138,7 @@ fn run(repl: Repl, input: ~str) -> Repl {
}; };
debug!("building driver input"); debug!("building driver input");
let head = include_str!("wrapper.rs"); let head = include_str!("wrapper.rs").to_owned();
let foot = fmt!("%s\nfn main() {\n%s\n\nprint({\n%s\n})\n}", let foot = fmt!("%s\nfn main() {\n%s\n\nprint({\n%s\n})\n}",
repl.view_items, repl.stmts, input); repl.view_items, repl.stmts, input);
let wrapped = driver::str_input(head + foot); let wrapped = driver::str_input(head + foot);

View file

@ -286,12 +286,30 @@ fn core_macros() -> ~str {
macro_rules! die( macro_rules! die(
($msg: expr) => ( ($msg: expr) => (
core::sys::begin_unwind($msg, file!(), line!()) core::sys::begin_unwind($msg,
file!().to_owned(), line!())
); );
() => ( () => (
die!(~\"explicit failure\") die!(~\"explicit failure\")
) )
) )
macro_rules! condition (
{ $c:ident: $in:ty -> $out:ty; } => {
mod $c {
fn key(_x: @core::condition::Handler<$in,$out>) { }
pub const cond : core::condition::Condition<$in,$out> =
core::condition::Condition {
name: stringify!(c),
key: key
};
}
}
)
}"; }";
} }

View file

@ -11,7 +11,7 @@
use ext::base::*; use ext::base::*;
use codemap::{span, Loc, FileMap}; use codemap::{span, Loc, FileMap};
use print::pprust; use print::pprust;
use ext::build::{mk_base_vec_e, mk_uint, mk_u8, mk_uniq_str}; use ext::build::{mk_base_vec_e, mk_uint, mk_u8, mk_base_str};
export expand_line; export expand_line;
export expand_col; export expand_col;
@ -46,19 +46,19 @@ fn expand_file(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
base::check_zero_tts(cx, sp, tts, "file!"); base::check_zero_tts(cx, sp, tts, "file!");
let Loc { file: @FileMap { name: filename, _ }, _ } = let Loc { file: @FileMap { name: filename, _ }, _ } =
cx.codemap().lookup_char_pos(sp.lo); cx.codemap().lookup_char_pos(sp.lo);
base::mr_expr(mk_uniq_str(cx, sp, filename)) base::mr_expr(mk_base_str(cx, sp, filename))
} }
fn expand_stringify(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree]) fn expand_stringify(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
-> base::mac_result { -> base::mac_result {
let s = pprust::tts_to_str(tts, cx.parse_sess().interner); let s = pprust::tts_to_str(tts, cx.parse_sess().interner);
base::mr_expr(mk_uniq_str(cx, sp, s)) base::mr_expr(mk_base_str(cx, sp, s))
} }
fn expand_mod(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree]) fn expand_mod(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
-> base::mac_result { -> base::mac_result {
base::check_zero_tts(cx, sp, tts, "module_path!"); base::check_zero_tts(cx, sp, tts, "module_path!");
base::mr_expr(mk_uniq_str(cx, sp, base::mr_expr(mk_base_str(cx, sp,
str::connect(cx.mod_path().map( str::connect(cx.mod_path().map(
|x| cx.str_of(*x)), ~"::"))) |x| cx.str_of(*x)), ~"::")))
} }
@ -83,7 +83,7 @@ fn expand_include_str(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
} }
} }
base::mr_expr(mk_uniq_str(cx, sp, result::unwrap(res))) base::mr_expr(mk_base_str(cx, sp, result::unwrap(res)))
} }
fn expand_include_bin(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree]) fn expand_include_bin(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])

View file

@ -19,7 +19,7 @@ use std::map::{Map, HashMap};
use io::{Reader, ReaderUtil}; use io::{Reader, ReaderUtil};
macro_rules! bench ( macro_rules! bench (
($id:ident) => (maybe_run_test(argv, stringify!($id), $id)) ($id:ident) => (maybe_run_test(argv, stringify!($id).to_owned(), $id))
) )
fn main() { fn main() {

View file

@ -39,7 +39,8 @@ macro_rules! parse_node (
) => ( ) => (
parse_node!( parse_node!(
[$(: $tags ($(:$tag_nodes),*))*]; [$(: $tags ($(:$tag_nodes),*))*];
[$(:$head_nodes,)* :tag(stringify!($head), ~[$($nodes),*])]; [$(:$head_nodes,)* :tag(stringify!($head).to_owned(),
~[$($nodes),*])];
$($rest)* $($rest)*
) )
); );
@ -75,7 +76,7 @@ macro_rules! parse_node (
) => ( ) => (
parse_node!( parse_node!(
[$(: $tags ($(:$tag_nodes),*))*]; [$(: $tags ($(:$tag_nodes),*))*];
[$(:$nodes,)* :text(stringify!($word))]; [$(:$nodes,)* :text(stringify!($word).to_owned())];
$($rest)* $($rest)*
) )
); );

View file

@ -16,20 +16,20 @@ mod m1 {
#[legacy_exports]; #[legacy_exports];
mod m2 { mod m2 {
#[legacy_exports]; #[legacy_exports];
fn where_am_i() -> ~str { module_path!() } fn where_am_i() -> ~str { (module_path!()).to_owned() }
} }
} }
fn main() { fn main() {
assert(line!() == 24); assert(line!() == 24);
assert(col!() == 11); assert(col!() == 11);
assert(file!().ends_with(~"syntax-extension-source-utils.rs")); assert(file!().to_owned().ends_with(~"syntax-extension-source-utils.rs"));
assert(stringify!((2*3) + 5) == ~"( 2 * 3 ) + 5"); assert(stringify!((2*3) + 5).to_owned() == ~"( 2 * 3 ) + 5");
assert(include!("syntax-extension-source-utils-files/includeme.fragment") assert(include!("syntax-extension-source-utils-files/includeme.fragment").to_owned()
== ~"victory robot 6"); == ~"victory robot 6");
assert( assert(
include_str!("syntax-extension-source-utils-files/includeme.fragment") include_str!("syntax-extension-source-utils-files/includeme.fragment").to_owned()
.starts_with(~"/* this is for ")); .starts_with(~"/* this is for "));
assert( assert(
include_bin!("syntax-extension-source-utils-files/includeme.fragment") include_bin!("syntax-extension-source-utils-files/includeme.fragment")