2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-04 19:05:16 -07:00
|
|
|
/*
|
|
|
|
* The compiler code necessary to support the #env extension. Eventually this
|
|
|
|
* should all get sucked into either the compiler syntax extension plugin
|
|
|
|
* interface.
|
|
|
|
*/
|
2011-08-15 16:38:23 -07:00
|
|
|
import std::vec;
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::option;
|
|
|
|
import std::generic_os;
|
2011-07-05 11:48:19 +02:00
|
|
|
import base::*;
|
2011-05-04 19:05:16 -07:00
|
|
|
export expand_syntax_ext;
|
|
|
|
|
2011-07-27 17:36:37 -07:00
|
|
|
fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr,
|
2011-08-18 10:01:39 +02:00
|
|
|
_body: option::t<str>) -> @ast::expr {
|
2011-08-04 16:20:09 -07:00
|
|
|
let args: [@ast::expr] = alt arg.node {
|
2011-08-18 11:37:19 -07:00
|
|
|
ast::expr_vec(elts, _) { elts }
|
2011-07-27 17:36:37 -07:00
|
|
|
_ { cx.span_fatal(sp, "#env requires arguments of the form `[...]`.") }
|
|
|
|
};
|
2011-08-13 00:09:25 -07:00
|
|
|
if vec::len::<@ast::expr>(args) != 1u {
|
2011-06-18 22:41:20 -07:00
|
|
|
cx.span_fatal(sp, "malformed #env call");
|
2011-05-04 19:05:16 -07:00
|
|
|
}
|
2011-05-06 16:30:39 -07:00
|
|
|
// FIXME: if this was more thorough it would manufacture an
|
2011-08-12 07:15:18 -07:00
|
|
|
// option::t<str> rather than just an maybe-empty string.
|
2011-05-06 16:30:39 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let var = expr_to_str(cx, args.(0), "#env requires a string");
|
|
|
|
alt generic_os::getenv(var) {
|
|
|
|
option::none. { ret make_new_str(cx, sp, ""); }
|
|
|
|
option::some(s) { ret make_new_str(cx, sp, s); }
|
2011-05-06 16:30:39 -07:00
|
|
|
}
|
2011-05-04 19:05:16 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn make_new_str(cx: &ext_ctxt, sp: codemap::span, s: str) -> @ast::expr {
|
2011-06-09 17:11:21 -07:00
|
|
|
ret make_new_lit(cx, sp, ast::lit_str(s, ast::sk_rc));
|
2011-05-04 19:05:16 -07:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|