Populate default compilation environment as in rustboot.

This commit is contained in:
Graydon Hoare 2011-03-01 15:57:55 -08:00
parent 0761c5f462
commit 348c77c31b
3 changed files with 44 additions and 7 deletions

View File

@ -2,6 +2,7 @@
import front.parser;
import front.token;
import front.eval;
import middle.trans;
import middle.resolve;
import middle.typeck;
@ -13,6 +14,30 @@ import std.option.none;
import std._str;
import std._vec;
fn default_environment(session.session sess,
str argv0,
str input) -> eval.env {
auto libc = "libc.so";
alt (sess.get_targ_cfg().os) {
case (session.os_win32) { libc = "msvcrt.dll"; }
case (session.os_macos) { libc = "libc.dylib"; }
case (session.os_linux) { libc = "libc.so.6"; }
}
ret
vec(
// 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)),
// Build bindings.
tup("build_compiler", eval.val_str(argv0)),
tup("build_input", eval.val_str(input))
);
}
impure fn parse_input(session.session sess,
parser.parser p,
str input) -> @front.ast.crate {
@ -25,9 +50,11 @@ impure fn parse_input(session.session sess,
fail;
}
impure fn compile_input(session.session sess, str input, str output,
impure fn compile_input(session.session sess,
eval.env env,
str input, str output,
bool shared) {
auto p = parser.new_parser(sess, 0, input);
auto p = parser.new_parser(sess, env, 0, input);
auto crate = parse_input(sess, p, input);
crate = resolve.resolve_crate(sess, crate);
crate = typeck.check_crate(sess, crate);
@ -131,16 +158,19 @@ impure fn main(vec[str] args) {
sess.err("no input filename");
}
case (some[str](?ifile)) {
auto env = default_environment(sess, args.(0), ifile);
alt (output_file) {
case (none[str]) {
let vec[str] parts = _str.split(ifile, '.' as u8);
parts = _vec.pop[str](parts);
parts += ".bc";
auto ofile = _str.concat(parts);
compile_input(sess, ifile, ofile, shared);
compile_input(sess, env, ifile, ofile, shared);
}
case (some[str](?ofile)) {
compile_input(sess, ifile, ofile, shared);
compile_input(sess, env, ifile, ofile, shared);
}
}
}

View File

@ -393,7 +393,7 @@ impure fn eval_crate_directive(parser p,
auto full_path = prefix + std.os.path_sep() + file_path;
auto p0 = new_parser(p.get_session(), 0, full_path);
auto p0 = new_parser(p.get_session(), e, 0, full_path);
auto m0 = parse_mod_items(p0, token.EOF);
auto im = ast.item_mod(id, m0, p.next_def_id());
auto i = @spanned(cdir.span, cdir.span, im);

View File

@ -31,15 +31,18 @@ state type parser =
impure fn restrict(restriction r);
fn get_restriction() -> restriction;
fn get_file_type() -> file_type;
fn get_env() -> eval.env;
fn get_session() -> session.session;
fn get_span() -> common.span;
fn next_def_id() -> ast.def_id;
};
impure fn new_parser(session.session sess,
eval.env env,
ast.crate_num crate,
str path) -> parser {
state obj stdio_parser(session.session sess,
eval.env env,
file_type ftype,
mutable token.token tok,
mutable common.pos lo,
@ -93,6 +96,10 @@ impure fn new_parser(session.session sess,
ret ftype;
}
fn get_env() -> eval.env {
ret env;
}
}
auto ftype = SOURCE_FILE;
if (_str.ends_with(path, ".rc")) {
@ -101,7 +108,7 @@ impure fn new_parser(session.session sess,
auto srdr = io.new_stdio_reader(path);
auto rdr = lexer.new_reader(srdr, path);
auto npos = rdr.get_curr_pos();
ret stdio_parser(sess, ftype, lexer.next_token(rdr),
ret stdio_parser(sess, env, ftype, lexer.next_token(rdr),
npos, npos, 0, UNRESTRICTED, crate, rdr);
}
@ -2279,7 +2286,7 @@ impure fn parse_crate_from_crate_file(parser p) -> @ast.crate {
auto hi = lo;
auto prefix = std.path.dirname(lo.filename);
auto cdirs = parse_crate_directives(p, token.EOF);
auto m = eval.eval_crate_directives_to_mod(p, eval.mk_env(),
auto m = eval.eval_crate_directives_to_mod(p, p.get_env(),
cdirs, prefix);
hi = p.get_span();
expect(p, token.EOF);