about summary refs log tree commit diff
path: root/src/comp/syntax/ext/env.rs
blob: 8c02d2e1e2f9c5a118a704604a63d2bb5289243e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


/*
 * The compiler code necessary to support the #env extension.  Eventually this
 * should all get sucked into either the compiler syntax extension plugin
 * interface.
 */
import std::ivec;
import std::str;
import std::option;
import std::generic_os;
import base::*;
export expand_syntax_ext;

fn expand_syntax_ext(&ext_ctxt cx, codemap::span sp, &(@ast::expr)[] args,
                     option::t[str] body) -> @ast::expr {
    if (ivec::len[@ast::expr](args) != 1u) {
        cx.span_fatal(sp, "malformed #env call");
    }
    // FIXME: if this was more thorough it would manufacture an
    // option::t[str] rather than just an maybe-empty string.

    auto var = expr_to_str(cx, args.(0), "#env requires a string");
    alt (generic_os::getenv(var)) {
        case (option::none) { ret make_new_str(cx, sp, ""); }
        case (option::some(?s)) { ret make_new_str(cx, sp, s); }
    }
}

fn make_new_lit(&ext_ctxt cx, codemap::span sp, ast::lit_ lit) -> @ast::expr {
    auto sp_lit = @rec(node=lit, span=sp);
    ret @rec(id=cx.next_id(), node=ast::expr_lit(sp_lit), span=sp);
}

fn make_new_str(&ext_ctxt cx, codemap::span sp, str s) -> @ast::expr {
    ret make_new_lit(cx, sp, ast::lit_str(s, ast::sk_rc));
}
//
// 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:
//