Teach AST, parser, folder about iter items.

This commit is contained in:
Graydon Hoare 2011-01-21 17:06:20 -08:00
parent 3780551878
commit 546d144009
4 changed files with 35 additions and 15 deletions

View File

@ -200,6 +200,7 @@ tag ty_ {
type arg = rec(mode mode, @ty ty, ident ident, def_id id);
type _fn = rec(effect effect,
bool is_iter,
vec[arg] inputs,
@ty output,
block body);

View File

@ -1403,7 +1403,7 @@ impure fn parse_ty_params(parser p) -> vec[ast.ty_param] {
ret ty_params;
}
impure fn parse_fn(parser p, ast.effect eff) -> ast._fn {
impure fn parse_fn(parser p, ast.effect eff, bool is_iter) -> ast._fn {
auto pf = parse_arg;
let util.common.spanned[vec[ast.arg]] inputs =
// FIXME: passing parse_arg as an lval doesn't work at the
@ -1425,18 +1425,25 @@ impure fn parse_fn(parser p, ast.effect eff) -> ast._fn {
auto body = parse_block(p);
ret rec(effect = eff,
is_iter = is_iter,
inputs = inputs.node,
output = output,
body = body);
}
impure fn parse_item_fn(parser p, ast.effect eff) -> @ast.item {
impure fn parse_item_fn_or_iter(parser p, ast.effect eff,
bool is_iter) -> @ast.item {
auto lo = p.get_span();
expect(p, token.FN);
if (is_iter) {
expect(p, token.ITER);
} else {
expect(p, token.FN);
}
auto id = parse_ident(p);
auto ty_params = parse_ty_params(p);
auto f = parse_fn(p, eff);
auto item = ast.item_fn(id, f, ty_params, p.next_def_id(), ast.ann_none);
auto f = parse_fn(p, eff, is_iter);
auto item = ast.item_fn(id, f, ty_params,
p.next_def_id(), ast.ann_none);
ret @spanned(lo, f.body.span, item);
}
@ -1450,9 +1457,14 @@ impure fn parse_obj_field(parser p) -> ast.obj_field {
impure fn parse_method(parser p) -> @ast.method {
auto lo = p.get_span();
auto eff = parse_effect(p);
expect(p, token.FN);
auto is_iter = false;
alt (p.peek()) {
case (token.FN) { p.bump(); }
case (token.ITER) { p.bump(); is_iter = true; }
case (?t) { unexpected(p, t); }
}
auto ident = parse_ident(p);
auto f = parse_fn(p, eff);
auto f = parse_fn(p, eff, is_iter);
auto meth = rec(ident=ident, meth=f,
id=p.next_def_id(), ann=ast.ann_none);
ret @spanned(lo, f.body.span, meth);
@ -1655,7 +1667,11 @@ impure fn parse_item(parser p) -> @ast.item {
case (token.FN) {
check (lyr == ast.layer_value);
ret parse_item_fn(p, eff);
ret parse_item_fn_or_iter(p, eff, false);
}
case (token.ITER) {
check (lyr == ast.layer_value);
ret parse_item_fn_or_iter(p, eff, true);
}
case (token.MOD) {
check (eff == ast.eff_pure);

View File

@ -221,6 +221,7 @@ type ast_fold[ENV] =
&ast.block_) -> block) fold_block,
(fn(&ENV e, ast.effect effect,
bool is_iter,
vec[arg] inputs,
@ty output, &block body) -> ast._fn) fold_fn,
@ -671,7 +672,7 @@ fn fold_fn[ENV](&ENV env, ast_fold[ENV] fld, &ast._fn f) -> ast._fn {
auto output = fold_ty[ENV](env, fld, f.output);
auto body = fold_block[ENV](env, fld, f.body);
ret fld.fold_fn(env, f.effect, inputs, output, body);
ret fld.fold_fn(env, f.effect, f.is_iter, inputs, output, body);
}
@ -1129,10 +1130,12 @@ fn identity_fold_block[ENV](&ENV e, &span sp, &ast.block_ blk) -> block {
fn identity_fold_fn[ENV](&ENV e,
ast.effect effect,
bool is_iter,
vec[arg] inputs,
@ast.ty output,
&block body) -> ast._fn {
ret rec(effect=effect, inputs=inputs, output=output, body=body);
ret rec(effect=effect, is_iter=is_iter, inputs=inputs,
output=output, body=body);
}
fn identity_fold_mod[ENV](&ENV e, &ast._mod m) -> ast._mod {
@ -1271,7 +1274,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
bind identity_fold_view_item_import[ENV](_,_,_,_,_),
fold_block = bind identity_fold_block[ENV](_,_,_),
fold_fn = bind identity_fold_fn[ENV](_,_,_,_,_),
fold_fn = bind identity_fold_fn[ENV](_,_,_,_,_,_),
fold_mod = bind identity_fold_mod[ENV](_,_),
fold_crate = bind identity_fold_crate[ENV](_,_,_),
fold_obj = bind identity_fold_obj[ENV](_,_,_),

View File

@ -1590,7 +1590,7 @@ fn check_const(&@crate_ctxt ccx, &span sp, ast.ident ident, @ast.ty t,
}
fn check_fn(&@crate_ctxt ccx, ast.effect effect,
vec[ast.arg] inputs,
bool is_iter, vec[ast.arg] inputs,
@ast.ty output, &ast.block body) -> ast._fn {
auto local_ty_table = @common.new_def_hash[@ty.t]();
@ -1618,8 +1618,8 @@ fn check_fn(&@crate_ctxt ccx, ast.effect effect,
auto block_t = check_block(fcx, body);
auto block_wb = writeback(fcx, block_t);
auto fn_t = rec(effect=effect, inputs=inputs, output=output,
body=block_wb);
auto fn_t = rec(effect=effect, is_iter=is_iter,
inputs=inputs, output=output, body=block_wb);
ret fn_t;
}
@ -1670,7 +1670,7 @@ fn check_crate(session.session sess, @ast.crate crate) -> @ast.crate {
auto fld = fold.new_identity_fold[@crate_ctxt]();
fld = @rec(update_env_for_item = bind update_obj_fields(_, _),
fold_fn = bind check_fn(_,_,_,_,_),
fold_fn = bind check_fn(_,_,_,_,_,_),
fold_item_fn = bind check_item_fn(_,_,_,_,_,_,_)
with *fld);
ret fold.fold_crate[@crate_ctxt](ccx, fld, result._0);