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-05-12 17:24:54 +02:00
|
|
|
import util::common;
|
2011-05-17 20:41:41 +02:00
|
|
|
import std::str;
|
|
|
|
import std::vec;
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::option;
|
|
|
|
import std::generic_os;
|
2011-06-04 15:41:45 -04:00
|
|
|
import ext::*;
|
2011-05-04 19:05:16 -07:00
|
|
|
export expand_syntax_ext;
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn expand_syntax_ext(&ext_ctxt cx, common::span sp, &vec[@ast::expr] args,
|
2011-05-12 17:24:54 +02:00
|
|
|
option::t[str] body) -> @ast::expr {
|
2011-05-17 20:41:41 +02:00
|
|
|
if (vec::len[@ast::expr](args) != 1u) {
|
2011-06-19 03:24:42 -07:00
|
|
|
cx.span_err(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-05-12 17:24:54 +02:00
|
|
|
// option::t[str] rather than just an maybe-empty string.
|
2011-05-06 16:30:39 -07:00
|
|
|
|
2011-06-04 17:39:55 -04:00
|
|
|
auto var = expr_to_str(cx, args.(0));
|
2011-05-12 17:24:54 +02:00
|
|
|
alt (generic_os::getenv(var)) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (option::none) { ret make_new_str(cx, sp, ""); }
|
|
|
|
case (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-06-15 11:19:50 -07:00
|
|
|
// FIXME: duplicate code copied from extfmt:
|
2011-06-09 17:11:21 -07:00
|
|
|
fn expr_to_str(&ext_ctxt cx, @ast::expr expr) -> str {
|
2011-05-04 19:05:16 -07:00
|
|
|
alt (expr.node) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::expr_lit(?l, _)) {
|
2011-05-04 19:05:16 -07:00
|
|
|
alt (l.node) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (ast::lit_str(?s, _)) { ret s; }
|
2011-06-19 03:24:42 -07:00
|
|
|
case (_) { cx.span_err(l.span, "malformed #env call"); }
|
2011-05-04 19:05:16 -07:00
|
|
|
}
|
|
|
|
}
|
2011-06-19 03:24:42 -07:00
|
|
|
case (_) { cx.span_err(expr.span, "malformed #env call"); }
|
2011-05-04 19:05:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn make_new_lit(&ext_ctxt cx, common::span sp, ast::lit_ lit) -> @ast::expr {
|
2011-05-04 19:05:16 -07:00
|
|
|
auto sp_lit = @rec(node=lit, span=sp);
|
2011-06-04 17:39:55 -04:00
|
|
|
auto expr = ast::expr_lit(sp_lit, cx.next_ann());
|
2011-05-04 19:05:16 -07:00
|
|
|
ret @rec(node=expr, span=sp);
|
|
|
|
}
|
|
|
|
|
2011-06-04 17:39:55 -04:00
|
|
|
fn make_new_str(&ext_ctxt cx, common::span sp, str s) -> @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:
|
|
|
|
//
|