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-09-01 17:27:58 -07:00
|
|
|
import std::str;
|
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-09-02 15:34:58 -07:00
|
|
|
_body: &option::t<str>) -> @ast::expr {
|
2011-08-19 15:16:48 -07:00
|
|
|
let args: [@ast::expr] =
|
|
|
|
alt arg.node {
|
|
|
|
ast::expr_vec(elts, _) { elts }
|
|
|
|
_ {
|
2011-09-02 15:34:58 -07:00
|
|
|
cx.span_fatal(sp, "#env requires arguments of the form `[...]`.")
|
2011-08-19 15:16:48 -07:00
|
|
|
}
|
|
|
|
};
|
2011-08-13 00:09:25 -07:00
|
|
|
if vec::len::<@ast::expr>(args) != 1u {
|
2011-09-02 15:34:58 -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-09-02 15:34:58 -07:00
|
|
|
let var = expr_to_str(cx, args[0], "#env requires a string");
|
2011-08-27 16:52:00 -07:00
|
|
|
alt generic_os::getenv(var) {
|
2011-09-02 15:34:58 -07:00
|
|
|
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-09-02 15:34:58 -07:00
|
|
|
fn make_new_str(cx: &ext_ctxt, sp: codemap::span, s: &str) -> @ast::expr {
|
2011-09-01 22:08:59 -07:00
|
|
|
ret make_new_lit(cx, sp, ast::lit_str(s));
|
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:
|
|
|
|
//
|