rustc: Parse lets

This commit is contained in:
Patrick Walton 2010-10-11 18:13:14 -07:00
parent a2c9879442
commit 8a7a0308e3
3 changed files with 33 additions and 6 deletions

View File

@ -76,7 +76,7 @@ tag stmt_ {
type decl = spanned[decl_];
tag decl_ {
decl_local(ident, bool, option[@ty]);
decl_local(ident, option[@ty], option[@expr]);
decl_item(name, @item);
}

View File

@ -541,6 +541,27 @@ io fn parse_expr(parser p) -> @ast.expr {
}
}
io fn parse_let(parser p) -> @ast.decl {
auto lo = p.get_span();
expect(p, token.LET);
auto ty = parse_ty(p);
auto id = parse_ident(p);
auto init;
if (p.peek() == token.EQ) {
p.bump();
init = some(parse_expr(p));
} else {
init = none[@ast.expr];
}
expect(p, token.SEMI);
auto hi = p.get_span();
ret @spanned(lo, hi, ast.decl_local(id, some(ty), init));
}
io fn parse_stmt(parser p) -> @ast.stmt {
auto lo = p.get_span();
alt (p.peek()) {
@ -553,6 +574,12 @@ io fn parse_stmt(parser p) -> @ast.stmt {
ret @spanned(lo, hi, ast.stmt_log(e));
}
case (token.LET) {
auto leht = parse_let(p);
auto hi = p.get_span();
ret @spanned(lo, hi, ast.stmt_decl(leht));
}
// Handle the (few) block-expr stmts first.
case (token.IF) {

View File

@ -85,8 +85,8 @@ type ast_fold[ENV] =
// Decl folds.
(fn(&ENV e, &span sp,
ident ident, bool infer,
&option[@ty] ty) -> @decl) fold_decl_local,
ident ident, &option[@ty] ty,
&option[@expr]) -> @decl) fold_decl_local,
(fn(&ENV e, &span sp,
&name name, @item item) -> @decl) fold_decl_item,
@ -483,9 +483,9 @@ fn identity_fold_expr_block[ENV](&ENV env, &span sp, block blk) -> @expr {
// Decl identities.
fn identity_fold_decl_local[ENV](&ENV e, &span sp,
ident i, bool infer,
&option[@ty] t) -> @decl {
ret @respan(sp, ast.decl_local(i, infer, t));
ident i, &option[@ty] t,
&option[@expr] init) -> @decl {
ret @respan(sp, ast.decl_local(i, t, init));
}
fn identity_fold_decl_item[ENV](&ENV e, &span sp,