diff options
| author | Patrick Walton <[email protected]> | 2010-12-10 18:08:32 -0800 |
|---|---|---|
| committer | Patrick Walton <[email protected]> | 2010-12-10 18:08:32 -0800 |
| commit | de118d79b603b84e2911527a586f7716b750c5fa (patch) | |
| tree | 5ade03edc398491cd5a70a449fabf21832b46ab4 /src/comp/front | |
| parent | rustc: Add update_env_for_arm to fold; we'll need it to resolve pattern bindings (diff) | |
| download | rust-de118d79b603b84e2911527a586f7716b750c5fa.tar.xz rust-de118d79b603b84e2911527a586f7716b750c5fa.zip | |
rustc: Resolve pattern bindings
Diffstat (limited to 'src/comp/front')
| -rw-r--r-- | src/comp/front/ast.rs | 3 | ||||
| -rw-r--r-- | src/comp/front/parser.rs | 21 |
2 files changed, 22 insertions, 2 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index 71065150..11db3eed 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -33,6 +33,7 @@ tag def { def_variant(def_id /* tag */, def_id /* variant */); def_ty(def_id); def_ty_arg(def_id); + def_binding(def_id); } type crate = spanned[crate_]; @@ -124,7 +125,7 @@ tag decl_ { decl_item(@item); } -type arm = rec(@pat pat, block block); +type arm = rec(@pat pat, block block, hashmap[ident,def_id] index); type elt = rec(mutability mut, @expr expr); type field = rec(mutability mut, ident ident, @expr expr); diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index f8db2f71..decb28e1 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -817,8 +817,9 @@ impure fn parse_alt_expr(parser p) -> @ast.expr { expect(p, token.LPAREN); auto pat = parse_pat(p); expect(p, token.RPAREN); + auto index = index_arm(pat); auto block = parse_block(p); - arms += vec(rec(pat=pat, block=block)); + arms += vec(rec(pat=pat, block=block, index=index)); } case (token.RBRACE) { /* empty */ } case (?tok) { @@ -1082,6 +1083,24 @@ fn index_block(vec[@ast.stmt] stmts, option.t[@ast.expr] expr) -> ast.block_ { ret rec(stmts=stmts, expr=expr, index=index); } +fn index_arm(@ast.pat pat) -> hashmap[ast.ident,ast.def_id] { + fn do_index_arm(&hashmap[ast.ident,ast.def_id] index, @ast.pat pat) { + alt (pat.node) { + case (ast.pat_bind(?i, ?def_id, _)) { index.insert(i, def_id); } + case (ast.pat_wild(_)) { /* empty */ } + case (ast.pat_tag(_, ?pats, _)) { + for (@ast.pat p in pats) { + do_index_arm(index, p); + } + } + } + } + + auto index = new_str_hash[ast.def_id](); + do_index_arm(index, pat); + ret index; +} + fn stmt_to_expr(@ast.stmt stmt) -> option.t[@ast.expr] { alt (stmt.node) { case (ast.stmt_expr(?e)) { ret some[@ast.expr](e); } |