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.
type RustClosure = (int,int);
struct Handler<T, U:Copy> {
pub struct Handler<T, U> {
handle: RustClosure,
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>>
}
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> {
unsafe {
@ -32,7 +33,9 @@ impl<T, U: Copy> Condition<T,U> {
fn raise(t:&T) -> U {
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>,
handler: @Handler<T, U>
}
impl<T, U: Copy> Trap<T,U> {
fn in<V: Copy>(&self, inner: &self/fn() -> V) -> V {
impl<T, U> Trap<T,U> {
fn in<V>(&self, inner: &self/fn() -> V) -> V {
unsafe {
let _g = Guard { cond: self.cond };
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>,
drop {
unsafe {
@ -105,13 +108,13 @@ struct Guard<T, U:Copy> {
#[cfg(test)]
mod test {
fn sadness_key(_x: @Handler<int,int>) { }
const sadness_condition : Condition<int,int> =
Condition { key: sadness_key };
condition! {
sadness: int -> int;
}
fn trouble(i: int) {
debug!("trouble: raising conition");
let j = sadness_condition.raise(&i);
let j = sadness::cond.raise(&i);
debug!("trouble: handler recovered with %d", j);
}
@ -119,7 +122,7 @@ mod test {
let mut inner_trapped = false;
do sadness_condition.trap(|_j| {
do sadness::cond.trap(|_j| {
debug!("nested_trap_test_inner: in handler");
inner_trapped = true;
0
@ -136,7 +139,7 @@ mod test {
let mut outer_trapped = false;
do sadness_condition.trap(|_j| {
do sadness::cond.trap(|_j| {
debug!("nested_trap_test_outer: in handler");
outer_trapped = true; 0
}).in {
@ -152,12 +155,12 @@ mod test {
let mut inner_trapped = false;
do sadness_condition.trap(|_j| {
do sadness::cond.trap(|_j| {
debug!("nested_reraise_trap_test_inner: in handler");
inner_trapped = true;
let i = 10;
debug!("nested_reraise_trap_test_inner: handler re-raising");
sadness_condition.raise(&i)
sadness::cond.raise(&i)
}).in {
debug!("nested_reraise_trap_test_inner: in protected block");
trouble(1);
@ -171,7 +174,7 @@ mod test {
let mut outer_trapped = false;
do sadness_condition.trap(|_j| {
do sadness::cond.trap(|_j| {
debug!("nested_reraise_trap_test_outer: in handler");
outer_trapped = true; 0
}).in {
@ -187,9 +190,9 @@ mod test {
let mut trapped = false;
do sadness_condition.trap(|j| {
do sadness::cond.trap(|j| {
debug!("test_default: in handler");
sadness_condition.raise_default(j, || {trapped=true; 5})
sadness::cond.raise_default(j, || {trapped=true; 5})
}).in {
debug!("test_default: in protected block");
trouble(1);

View File

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

View File

@ -17,7 +17,7 @@ export inject_intrinsic;
fn inject_intrinsic(sess: Session,
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>",
intrinsic_module,

View File

@ -138,7 +138,7 @@ fn run(repl: Repl, input: ~str) -> Repl {
};
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}",
repl.view_items, repl.stmts, input);
let wrapped = driver::str_input(head + foot);

View File

@ -286,12 +286,30 @@ fn core_macros() -> ~str {
macro_rules! die(
($msg: expr) => (
core::sys::begin_unwind($msg, file!(), line!())
core::sys::begin_unwind($msg,
file!().to_owned(), line!())
);
() => (
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 codemap::{span, Loc, FileMap};
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_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!");
let Loc { file: @FileMap { name: filename, _ }, _ } =
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])
-> base::mac_result {
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])
-> base::mac_result {
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(
|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])

View File

@ -19,7 +19,7 @@ use std::map::{Map, HashMap};
use io::{Reader, ReaderUtil};
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() {

View File

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

View File

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