Skip to content

Commit 546d144

Browse files
committed
Teach AST, parser, folder about iter items.
1 parent 3780551 commit 546d144

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/comp/front/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ tag ty_ {
200200

201201
type arg = rec(mode mode, @ty ty, ident ident, def_id id);
202202
type _fn = rec(effect effect,
203+
bool is_iter,
203204
vec[arg] inputs,
204205
@ty output,
205206
block body);

src/comp/front/parser.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ impure fn parse_ty_params(parser p) -> vec[ast.ty_param] {
14031403
ret ty_params;
14041404
}
14051405

1406-
impure fn parse_fn(parser p, ast.effect eff) -> ast._fn {
1406+
impure fn parse_fn(parser p, ast.effect eff, bool is_iter) -> ast._fn {
14071407
auto pf = parse_arg;
14081408
let util.common.spanned[vec[ast.arg]] inputs =
14091409
// 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 {
14251425
auto body = parse_block(p);
14261426

14271427
ret rec(effect = eff,
1428+
is_iter = is_iter,
14281429
inputs = inputs.node,
14291430
output = output,
14301431
body = body);
14311432
}
14321433

1433-
impure fn parse_item_fn(parser p, ast.effect eff) -> @ast.item {
1434+
impure fn parse_item_fn_or_iter(parser p, ast.effect eff,
1435+
bool is_iter) -> @ast.item {
14341436
auto lo = p.get_span();
1435-
expect(p, token.FN);
1437+
if (is_iter) {
1438+
expect(p, token.ITER);
1439+
} else {
1440+
expect(p, token.FN);
1441+
}
14361442
auto id = parse_ident(p);
14371443
auto ty_params = parse_ty_params(p);
1438-
auto f = parse_fn(p, eff);
1439-
auto item = ast.item_fn(id, f, ty_params, p.next_def_id(), ast.ann_none);
1444+
auto f = parse_fn(p, eff, is_iter);
1445+
auto item = ast.item_fn(id, f, ty_params,
1446+
p.next_def_id(), ast.ann_none);
14401447
ret @spanned(lo, f.body.span, item);
14411448
}
14421449

@@ -1450,9 +1457,14 @@ impure fn parse_obj_field(parser p) -> ast.obj_field {
14501457
impure fn parse_method(parser p) -> @ast.method {
14511458
auto lo = p.get_span();
14521459
auto eff = parse_effect(p);
1453-
expect(p, token.FN);
1460+
auto is_iter = false;
1461+
alt (p.peek()) {
1462+
case (token.FN) { p.bump(); }
1463+
case (token.ITER) { p.bump(); is_iter = true; }
1464+
case (?t) { unexpected(p, t); }
1465+
}
14541466
auto ident = parse_ident(p);
1455-
auto f = parse_fn(p, eff);
1467+
auto f = parse_fn(p, eff, is_iter);
14561468
auto meth = rec(ident=ident, meth=f,
14571469
id=p.next_def_id(), ann=ast.ann_none);
14581470
ret @spanned(lo, f.body.span, meth);
@@ -1655,7 +1667,11 @@ impure fn parse_item(parser p) -> @ast.item {
16551667

16561668
case (token.FN) {
16571669
check (lyr == ast.layer_value);
1658-
ret parse_item_fn(p, eff);
1670+
ret parse_item_fn_or_iter(p, eff, false);
1671+
}
1672+
case (token.ITER) {
1673+
check (lyr == ast.layer_value);
1674+
ret parse_item_fn_or_iter(p, eff, true);
16591675
}
16601676
case (token.MOD) {
16611677
check (eff == ast.eff_pure);

src/comp/middle/fold.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ type ast_fold[ENV] =
221221
&ast.block_) -> block) fold_block,
222222

223223
(fn(&ENV e, ast.effect effect,
224+
bool is_iter,
224225
vec[arg] inputs,
225226
@ty output, &block body) -> ast._fn) fold_fn,
226227

@@ -671,7 +672,7 @@ fn fold_fn[ENV](&ENV env, ast_fold[ENV] fld, &ast._fn f) -> ast._fn {
671672
auto output = fold_ty[ENV](env, fld, f.output);
672673
auto body = fold_block[ENV](env, fld, f.body);
673674

674-
ret fld.fold_fn(env, f.effect, inputs, output, body);
675+
ret fld.fold_fn(env, f.effect, f.is_iter, inputs, output, body);
675676
}
676677

677678

@@ -1129,10 +1130,12 @@ fn identity_fold_block[ENV](&ENV e, &span sp, &ast.block_ blk) -> block {
11291130

11301131
fn identity_fold_fn[ENV](&ENV e,
11311132
ast.effect effect,
1133+
bool is_iter,
11321134
vec[arg] inputs,
11331135
@ast.ty output,
11341136
&block body) -> ast._fn {
1135-
ret rec(effect=effect, inputs=inputs, output=output, body=body);
1137+
ret rec(effect=effect, is_iter=is_iter, inputs=inputs,
1138+
output=output, body=body);
11361139
}
11371140

11381141
fn identity_fold_mod[ENV](&ENV e, &ast._mod m) -> ast._mod {
@@ -1271,7 +1274,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
12711274
bind identity_fold_view_item_import[ENV](_,_,_,_,_),
12721275

12731276
fold_block = bind identity_fold_block[ENV](_,_,_),
1274-
fold_fn = bind identity_fold_fn[ENV](_,_,_,_,_),
1277+
fold_fn = bind identity_fold_fn[ENV](_,_,_,_,_,_),
12751278
fold_mod = bind identity_fold_mod[ENV](_,_),
12761279
fold_crate = bind identity_fold_crate[ENV](_,_,_),
12771280
fold_obj = bind identity_fold_obj[ENV](_,_,_),

src/comp/middle/typeck.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ fn check_const(&@crate_ctxt ccx, &span sp, ast.ident ident, @ast.ty t,
15901590
}
15911591

15921592
fn check_fn(&@crate_ctxt ccx, ast.effect effect,
1593-
vec[ast.arg] inputs,
1593+
bool is_iter, vec[ast.arg] inputs,
15941594
@ast.ty output, &ast.block body) -> ast._fn {
15951595
auto local_ty_table = @common.new_def_hash[@ty.t]();
15961596

@@ -1618,8 +1618,8 @@ fn check_fn(&@crate_ctxt ccx, ast.effect effect,
16181618
auto block_t = check_block(fcx, body);
16191619
auto block_wb = writeback(fcx, block_t);
16201620

1621-
auto fn_t = rec(effect=effect, inputs=inputs, output=output,
1622-
body=block_wb);
1621+
auto fn_t = rec(effect=effect, is_iter=is_iter,
1622+
inputs=inputs, output=output, body=block_wb);
16231623
ret fn_t;
16241624
}
16251625

@@ -1670,7 +1670,7 @@ fn check_crate(session.session sess, @ast.crate crate) -> @ast.crate {
16701670
auto fld = fold.new_identity_fold[@crate_ctxt]();
16711671

16721672
fld = @rec(update_env_for_item = bind update_obj_fields(_, _),
1673-
fold_fn = bind check_fn(_,_,_,_,_),
1673+
fold_fn = bind check_fn(_,_,_,_,_,_),
16741674
fold_item_fn = bind check_item_fn(_,_,_,_,_,_,_)
16751675
with *fld);
16761676
ret fold.fold_crate[@crate_ctxt](ccx, fld, result._0);

0 commit comments

Comments
 (0)