Remove the environment concept from front::eval

This is the old method of conditional compilation. It is going away.

Issue #489
This commit is contained in:
Brian Anderson 2011-06-30 17:29:54 -07:00
parent 70a28dc238
commit bca45720f7
3 changed files with 51 additions and 75 deletions

View File

@ -6,6 +6,7 @@ import front::parser;
import front::token;
import front::eval;
import front::ast;
import front::attr;
import middle::trans;
import middle::resolve;
import middle::ty;
@ -35,8 +36,8 @@ import back::link::output_type;
tag pp_mode { ppm_normal; ppm_typed; ppm_identified; }
fn default_environment(session::session sess, str argv0, str input) ->
eval::env {
fn default_configuration(session::session sess, str argv0, str input) ->
ast::crate_cfg {
auto libc =
alt (sess.get_targ_cfg().os) {
case (session::os_win32) { "msvcrt.dll" }
@ -44,13 +45,16 @@ fn default_environment(session::session sess, str argv0, str input) ->
case (session::os_linux) { "libc.so.6" }
case (_) { "libc.so" }
};
auto mk = attr::mk_name_value_item;
ret [ // Target bindings.
tup("target_os", eval::val_str(std::os::target_os())),
tup("target_arch", eval::val_str("x86")),
tup("target_libc", eval::val_str(libc)),
mk("target_os", std::os::target_os()),
mk("target_arch", "x86"),
mk("target_libc", libc),
// Build bindings.
tup("build_compiler", eval::val_str(argv0)),
tup("build_input", eval::val_str(input))];
mk("build_compiler", argv0),
mk("build_input", input)];
}
fn parse_input(session::session sess, parser::parser p, str input) ->
@ -73,10 +77,10 @@ fn time[T](bool do_it, str what, fn() -> T thunk) -> T {
ret rv;
}
fn compile_input(session::session sess, eval::env env, str input,
fn compile_input(session::session sess, ast::crate_cfg cfg, str input,
str output) {
auto time_passes = sess.get_opts().time_passes;
auto p = parser::new_parser(sess, env, input, 0u, 0);
auto p = parser::new_parser(sess, cfg, input, 0u, 0);
auto crate =
time(time_passes, "parsing", bind parse_input(sess, p, input));
if (sess.get_opts().output_type == link::output_type_none) { ret; }
@ -104,9 +108,9 @@ fn compile_input(session::session sess, eval::env env, str input,
bind link::write::run_passes(sess, llmod, output));
}
fn pretty_print_input(session::session sess, eval::env env, str input,
pp_mode ppm) {
auto p = front::parser::new_parser(sess, env, input, 0u, 0);
fn pretty_print_input(session::session sess, ast::crate_cfg cfg,
str input, pp_mode ppm) {
auto p = front::parser::new_parser(sess, cfg, input, 0u, 0);
auto crate = parse_input(sess, p, input);
auto mode;
alt (ppm) {
@ -337,7 +341,7 @@ fn main(vec[str] args) {
}
auto ifile = match.free.(0);
let str saved_out_filename = "";
auto env = default_environment(sess, binary, ifile);
auto cfg = default_configuration(sess, binary, ifile);
auto pretty =
option::map[str,
pp_mode](bind parse_pretty(sess, _),
@ -345,7 +349,7 @@ fn main(vec[str] args) {
auto ls = opt_present(match, "ls");
alt (pretty) {
case (some[pp_mode](?ppm)) {
pretty_print_input(sess, env, ifile, ppm);
pretty_print_input(sess, cfg, ifile, ppm);
ret;
}
case (none[pp_mode]) {/* continue */ }
@ -371,7 +375,7 @@ fn main(vec[str] args) {
case (link::output_type_exe) { parts += ["o"]; }
}
auto ofile = str::connect(parts, ".");
compile_input(sess, env, ifile, ofile);
compile_input(sess, cfg, ifile, ofile);
}
case (some(?ofile)) {
// FIXME: what about windows? This will create a foo.exe.o.
@ -386,7 +390,7 @@ fn main(vec[str] args) {
}
case (_) { temp_filename = ofile; }
}
compile_input(sess, env, ifile, temp_filename);
compile_input(sess, cfg, ifile, temp_filename);
}
}

View File

@ -23,17 +23,14 @@ tag val { val_bool(bool); val_int(int); val_str(str); }
tag eval_mode { mode_depend; mode_parse; }
type env = vec[tup(ident, val)];
type ctx =
@rec(parser p,
eval_mode mode,
mutable vec[str] deps,
session::session sess,
mutable uint chpos,
mutable int next_id);
fn mk_env() -> env { ret []; }
mutable int next_id,
ast::crate_cfg cfg);
fn val_is_bool(val v) -> bool {
alt (v) { case (val_bool(_)) { true } case (_) { false } }
@ -59,13 +56,6 @@ fn val_as_str(val v) -> str {
alt (v) { case (val_str(?s)) { s } case (_) { fail } }
}
fn lookup(session::session sess, env e, span sp, ident i) -> val {
for (tup(ident, val) pair in e) {
if (str::eq(i, pair._0)) { ret pair._1; }
}
sess.span_fatal(sp, "unknown variable: " + i)
}
fn eval_lit(ctx cx, span sp, @ast::lit lit) -> val {
alt (lit.node) {
case (ast::lit_bool(?b)) { val_bool(b) }
@ -75,18 +65,14 @@ fn eval_lit(ctx cx, span sp, @ast::lit lit) -> val {
}
}
fn eval_expr(ctx cx, env e, @ast::expr x) -> val {
fn eval_expr(ctx cx, @ast::expr x) -> val {
alt (x.node) {
case (ast::expr_path(?pth)) {
if (vec::len[ident](pth.node.idents) == 1u &&
vec::len[@ast::ty](pth.node.types) == 0u) {
ret lookup(cx.sess, e, x.span, pth.node.idents.(0));
}
cx.sess.span_fatal(x.span, "evaluating structured path-name");
}
case (ast::expr_lit(?lit)) { ret eval_lit(cx, x.span, lit); }
case (ast::expr_unary(?op, ?a)) {
auto av = eval_expr(cx, e, a);
auto av = eval_expr(cx, a);
alt (op) {
case (ast::not) {
if (val_is_bool(av)) { ret val_bool(!val_as_bool(av)); }
@ -98,8 +84,8 @@ fn eval_expr(ctx cx, env e, @ast::expr x) -> val {
}
}
case (ast::expr_binary(?op, ?a, ?b)) {
auto av = eval_expr(cx, e, a);
auto bv = eval_expr(cx, e, b);
auto av = eval_expr(cx, a);
auto bv = eval_expr(cx, b);
alt (op) {
case (ast::add) {
if (val_is_int(av) && val_is_int(bv)) {
@ -177,30 +163,30 @@ fn val_eq(session::session sess, span sp, val av, val bv) -> bool {
} else { sess.span_fatal(sp, "bad types in comparison") }
}
fn eval_crate_directives(ctx cx, env e, vec[@ast::crate_directive] cdirs,
fn eval_crate_directives(ctx cx, vec[@ast::crate_directive] cdirs,
str prefix, &mutable vec[@ast::view_item] view_items,
&mutable vec[@ast::item] items) {
for (@ast::crate_directive sub_cdir in cdirs) {
eval_crate_directive(cx, e, sub_cdir, prefix, view_items, items);
eval_crate_directive(cx, sub_cdir, prefix, view_items, items);
}
}
fn eval_crate_directives_to_mod(ctx cx, env e,
fn eval_crate_directives_to_mod(ctx cx,
vec[@ast::crate_directive] cdirs, str prefix)
-> ast::_mod {
let vec[@ast::view_item] view_items = [];
let vec[@ast::item] items = [];
eval_crate_directives(cx, e, cdirs, prefix, view_items, items);
eval_crate_directives(cx, cdirs, prefix, view_items, items);
ret rec(view_items=view_items, items=items);
}
fn eval_crate_directive_block(ctx cx, env e, &ast::block blk, str prefix,
fn eval_crate_directive_block(ctx cx, &ast::block blk, str prefix,
&mutable vec[@ast::view_item] view_items,
&mutable vec[@ast::item] items) {
for (@ast::stmt s in blk.node.stmts) {
alt (s.node) {
case (ast::stmt_crate_directive(?cdir)) {
eval_crate_directive(cx, e, cdir, prefix, view_items, items);
eval_crate_directive(cx, cdir, prefix, view_items, items);
}
case (_) {
cx.sess.span_fatal(s.span,
@ -210,22 +196,22 @@ fn eval_crate_directive_block(ctx cx, env e, &ast::block blk, str prefix,
}
}
fn eval_crate_directive_expr(ctx cx, env e, @ast::expr x, str prefix,
fn eval_crate_directive_expr(ctx cx, @ast::expr x, str prefix,
&mutable vec[@ast::view_item] view_items,
&mutable vec[@ast::item] items) {
alt (x.node) {
case (ast::expr_if(?cond, ?thn, ?elopt)) {
auto cv = eval_expr(cx, e, cond);
auto cv = eval_expr(cx, cond);
if (!val_is_bool(cv)) {
cx.sess.span_fatal(x.span, "bad cond type in 'if'");
}
if (val_as_bool(cv)) {
ret eval_crate_directive_block(cx, e, thn, prefix, view_items,
ret eval_crate_directive_block(cx, thn, prefix, view_items,
items);
}
alt (elopt) {
case (some(?els)) {
ret eval_crate_directive_expr(cx, e, els, prefix,
ret eval_crate_directive_expr(cx, els, prefix,
view_items, items);
}
case (_) {
@ -235,19 +221,19 @@ fn eval_crate_directive_expr(ctx cx, env e, @ast::expr x, str prefix,
}
}
case (ast::expr_alt(?v, ?arms)) {
auto vv = eval_expr(cx, e, v);
auto vv = eval_expr(cx, v);
for (ast::arm arm in arms) {
alt (arm.pat.node) {
case (ast::pat_lit(?lit, _)) {
auto pv = eval_lit(cx, arm.pat.span, lit);
if (val_eq(cx.sess, arm.pat.span, vv, pv)) {
ret eval_crate_directive_block(cx, e, arm.block,
ret eval_crate_directive_block(cx, arm.block,
prefix, view_items,
items);
}
}
case (ast::pat_wild(_)) {
ret eval_crate_directive_block(cx, e, arm.block,
ret eval_crate_directive_block(cx, arm.block,
prefix, view_items,
items);
}
@ -260,24 +246,23 @@ fn eval_crate_directive_expr(ctx cx, env e, @ast::expr x, str prefix,
cx.sess.span_fatal(x.span, "no cases matched in 'alt'");
}
case (ast::expr_block(?block)) {
ret eval_crate_directive_block(cx, e, block, prefix, view_items,
ret eval_crate_directive_block(cx, block, prefix, view_items,
items);
}
case (_) { cx.sess.span_fatal(x.span, "unsupported expr type"); }
}
}
fn eval_crate_directive(ctx cx, env e, @ast::crate_directive cdir, str prefix,
fn eval_crate_directive(ctx cx, @ast::crate_directive cdir, str prefix,
&mutable vec[@ast::view_item] view_items,
&mutable vec[@ast::item] items) {
alt (cdir.node) {
case (ast::cdir_let(?id, ?x, ?cdirs)) {
auto v = eval_expr(cx, e, x);
auto e0 = [tup(id, v)] + e;
eval_crate_directives(cx, e0, cdirs, prefix, view_items, items);
auto v = eval_expr(cx, x);
eval_crate_directives(cx, cdirs, prefix, view_items, items);
}
case (ast::cdir_expr(?x)) {
eval_crate_directive_expr(cx, e, x, prefix, view_items, items);
eval_crate_directive_expr(cx, x, prefix, view_items, items);
}
case (ast::cdir_src_mod(?id, ?file_opt, ?attrs)) {
auto file_path = id + ".rs";
@ -292,7 +277,7 @@ fn eval_crate_directive(ctx cx, env e, @ast::crate_directive cdir, str prefix,
};
if (cx.mode == mode_depend) { cx.deps += [full_path]; ret; }
auto p0 =
new_parser(cx.sess, e, full_path, cx.chpos,
new_parser(cx.sess, cx.cfg, full_path, cx.chpos,
cx.next_id);
auto inner_attrs = parse_inner_attrs_and_next(p0);
auto mod_attrs = attrs + inner_attrs._0;
@ -315,7 +300,7 @@ fn eval_crate_directive(ctx cx, env e, @ast::crate_directive cdir, str prefix,
} else {
prefix + std::fs::path_sep() + path
};
auto m0 = eval_crate_directives_to_mod(cx, e, cdirs, full_path);
auto m0 = eval_crate_directives_to_mod(cx, cdirs, full_path);
auto i = @rec(ident=id,
attrs=attrs,
id=cx.next_id,

View File

@ -32,7 +32,6 @@ type parser =
fn restrict(restriction) ;
fn get_restriction() -> restriction ;
fn get_file_type() -> file_type ;
fn get_env() -> eval::env ;
fn get_cfg() -> ast::crate_cfg;
fn get_session() -> session::session ;
fn get_span() -> common::span ;
@ -50,10 +49,9 @@ type parser =
fn next_id() -> ast::node_id ;
};
fn new_parser(session::session sess, eval::env env,
fn new_parser(session::session sess, ast::crate_cfg cfg,
str path, uint pos, ast::node_id next_id) -> parser {
obj stdio_parser(session::session sess,
eval::env env,
ast::crate_cfg cfg,
file_type ftype,
mutable token::token tok,
@ -85,7 +83,6 @@ fn new_parser(session::session sess, eval::env env,
fn get_hi_pos() -> uint { ret hi; }
fn get_last_lo_pos() -> uint { ret last_lo; }
fn get_file_type() -> file_type { ret ftype; }
fn get_env() -> eval::env { ret env; }
fn get_cfg() -> ast::crate_cfg { ret cfg; }
fn get_prec_table() -> vec[op_spec] { ret precs; }
fn get_str(token::str_num i) -> str {
@ -106,17 +103,6 @@ fn new_parser(session::session sess, eval::env env,
fn next_id() -> ast::node_id { ret next_id_var; }
}
auto cfg = {
fn m(&tup(ast::ident, eval::val) item) -> @ast::meta_item {
auto name = item._0;
auto value = eval::val_as_str(item._1);
auto meta_item_ = ast::meta_name_value(name, value);
ret @rec(node=meta_item_,
span=rec(lo=0u,hi=0u));
}
vec::map(m, env)
};
auto ftype = SOURCE_FILE;
if (str::ends_with(path, ".rc")) { ftype = CRATE_FILE; }
auto srdr = io::file_reader(path);
@ -128,7 +114,7 @@ fn new_parser(session::session sess, eval::env env,
lexer::consume_whitespace_and_comments(rdr);
auto npos = rdr.get_chpos();
ret stdio_parser(sess, env, cfg, ftype, lexer::next_token(rdr),
ret stdio_parser(sess, cfg, ftype, lexer::next_token(rdr),
npos, npos, npos, UNRESTRICTED, rdr,
prec_table(), next_id, bad_expr_word_table(),
ext::syntax_expander_table());
@ -2465,9 +2451,10 @@ fn parse_crate_from_crate_file(&parser p) -> @ast::crate {
mutable deps=deps,
sess=p.get_session(),
mutable chpos=p.get_chpos(),
mutable next_id=p.next_id());
mutable next_id=p.next_id(),
cfg = p.get_cfg());
auto m =
eval::eval_crate_directives_to_mod(cx, p.get_env(), cdirs, prefix);
eval::eval_crate_directives_to_mod(cx, cdirs, prefix);
auto hi = p.get_hi_pos();
expect(p, token::EOF);
ret @spanned(lo, hi, rec(directives=cdirs,