From 5cbf8889d0e8bc0e4b52c19c89e4578c0fc65eac Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 20 Dec 2010 18:58:18 -0800 Subject: [PATCH] rustc: Add bind expressions to the AST --- src/comp/front/ast.rs | 1 + src/comp/middle/fold.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index f4e5e5e1c80..8f1a9638959 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -139,6 +139,7 @@ tag expr_ { expr_tup(vec[elt], ann); expr_rec(vec[field], ann); expr_call(@expr, vec[@expr], ann); + expr_bind(@expr, vec[option.t[@expr]], ann); expr_binary(binop, @expr, @expr, ann); expr_unary(unop, @expr, ann); expr_lit(@lit, ann); diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index 90f6bed355f..d8efa25d165 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -74,6 +74,10 @@ type ast_fold[ENV] = @expr f, vec[@expr] args, ann a) -> @expr) fold_expr_call, + (fn(&ENV e, &span sp, + @expr f, vec[option.t[@expr]] args, + ann a) -> @expr) fold_expr_bind, + (fn(&ENV e, &span sp, ast.binop, @expr lhs, @expr rhs, @@ -417,6 +421,20 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr { ret fld.fold_expr_call(env_, e.span, ff, aargs, t); } + case (ast.expr_bind(?f, ?args_opt, ?t)) { + auto ff = fold_expr(env_, fld, f); + let vec[option.t[@ast.expr]] aargs_opt = vec(); + for (option.t[@ast.expr] t_opt in args_opt) { + alt (t_opt) { + case (some[@ast.expr](?e)) { + aargs_opt += vec(some(fold_expr(env_, fld, e))); + } + case (none[@ast.expr]) { /* empty */ } + } + } + ret fld.fold_expr_bind(env_, e.span, ff, aargs_opt, t); + } + case (ast.expr_binary(?op, ?a, ?b, ?t)) { auto aa = fold_expr(env_, fld, a); auto bb = fold_expr(env_, fld, b); @@ -807,6 +825,12 @@ fn identity_fold_expr_call[ENV](&ENV env, &span sp, @expr f, ret @respan(sp, ast.expr_call(f, args, a)); } +fn identity_fold_expr_bind[ENV](&ENV env, &span sp, @expr f, + vec[option.t[@expr]] args_opt, ann a) + -> @expr { + ret @respan(sp, ast.expr_bind(f, args_opt, a)); +} + fn identity_fold_expr_binary[ENV](&ENV env, &span sp, ast.binop b, @expr lhs, @expr rhs, ann a) -> @expr { @@ -1075,6 +1099,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] { fold_expr_tup = bind identity_fold_expr_tup[ENV](_,_,_,_), fold_expr_rec = bind identity_fold_expr_rec[ENV](_,_,_,_), fold_expr_call = bind identity_fold_expr_call[ENV](_,_,_,_,_), + fold_expr_bind = bind identity_fold_expr_bind[ENV](_,_,_,_,_), fold_expr_binary = bind identity_fold_expr_binary[ENV](_,_,_,_,_,_), fold_expr_unary = bind identity_fold_expr_unary[ENV](_,_,_,_,_), fold_expr_lit = bind identity_fold_expr_lit[ENV](_,_,_,_),