Add fold, typecheck and trans for expr_ext

This commit is contained in:
Brian Anderson 2011-02-26 20:51:02 -05:00 committed by Graydon Hoare
parent 1c60399257
commit f1500e5872
5 changed files with 50 additions and 2 deletions

View File

@ -154,6 +154,12 @@ type ast_fold[ENV] =
&option.t[def] d,
ann a) -> @expr) fold_expr_path,
(fn(&ENV e, &span sp,
&path p, vec[@expr] args,
option.t[@expr] body,
option.t[@expr] expanded,
ann a) -> @expr) fold_expr_ext,
(fn(&ENV e, &span sp) -> @expr) fold_expr_fail,
(fn(&ENV e, &span sp,
@ -644,6 +650,15 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
ret fld.fold_expr_path(env_, e.span, p_, r, t);
}
case (ast.expr_ext(?p, ?args, ?body, ?expanded, ?t)) {
// Only fold the expanded expression, not the
// expressions involved in syntax extension
auto exp = option.get[@expr](expanded);
auto exp_ = fold_expr(env_, fld, exp);
ret fld.fold_expr_ext(env_, e.span, p, args, body,
some[@ast.expr](exp_), t);
}
case (ast.expr_fail) {
ret fld.fold_expr_fail(env_, e.span);
}
@ -1166,6 +1181,14 @@ fn identity_fold_expr_path[ENV](&ENV env, &span sp,
ret @respan(sp, ast.expr_path(p, d, a));
}
fn identity_fold_expr_ext[ENV](&ENV env, &span sp,
&path p, vec[@expr] args,
option.t[@expr] body,
option.t[@expr] expanded,
ann a) -> @expr {
ret @respan(sp, ast.expr_ext(p, args, body, expanded, a));
}
fn identity_fold_expr_fail[ENV](&ENV env, &span sp) -> @expr {
ret @respan(sp, ast.expr_fail);
}
@ -1447,6 +1470,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_expr_field = bind identity_fold_expr_field[ENV](_,_,_,_,_),
fold_expr_index = bind identity_fold_expr_index[ENV](_,_,_,_,_),
fold_expr_path = bind identity_fold_expr_path[ENV](_,_,_,_,_),
fold_expr_ext = bind identity_fold_expr_ext[ENV](_,_,_,_,_,_,_),
fold_expr_fail = bind identity_fold_expr_fail[ENV](_,_),
fold_expr_ret = bind identity_fold_expr_ret[ENV](_,_,_),
fold_expr_put = bind identity_fold_expr_put[ENV](_,_,_),

View File

@ -1957,6 +1957,8 @@ fn trans_lit(@crate_ctxt cx, &ast.lit lit, &ast.ann ann) -> ValueRef {
ret C_nil();
}
case (ast.lit_str(?s)) {
log "translating literal:";
log s;
ret C_str(cx, s);
}
}
@ -3599,6 +3601,7 @@ fn trans_rec(@block_ctxt cx, vec[ast.field] fields,
fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
alt (e.node) {
case (ast.expr_lit(?lit, ?ann)) {
log "translating literal";
ret res(cx, trans_lit(cx.fcx.ccx, *lit, ann));
}
@ -3693,6 +3696,11 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
ret trans_rec(cx, args, base, ann);
}
case (ast.expr_ext(_, _, _, ?expanded, _)) {
log "translating extension";
ret trans_expr(cx, option.get[@ast.expr](expanded));
}
case (ast.expr_fail) {
ret trans_fail(cx, e.span, "explicit failure");
}

View File

@ -749,6 +749,7 @@ fn expr_ty(@ast.expr expr) -> @t {
case (ast.expr_field(_, _, ?ann)) { ret ann_to_type(ann); }
case (ast.expr_index(_, _, ?ann)) { ret ann_to_type(ann); }
case (ast.expr_path(_, _, ?ann)) { ret ann_to_type(ann); }
case (ast.expr_ext(_, _, _, _, ?ann)) { ret ann_to_type(ann); }
case (ast.expr_fail) { ret plain_ty(ty_nil); }
case (ast.expr_log(_)) { ret plain_ty(ty_nil); }

View File

@ -1156,6 +1156,11 @@ fn demand_expr_full(&@fn_ctxt fcx, @ty.t expected, @ast.expr e,
ann_to_type(ann), adk);
e_1 = ast.expr_path(pth, d, ast.ann_type(t));
}
case (ast.expr_ext(?p, ?args, ?body, ?expanded, ?ann)) {
auto t = demand_full(fcx, e.span, expected,
ann_to_type(ann), adk);
e_1 = ast.expr_ext(p, args, body, expanded, ast.ann_type(t));
}
case (ast.expr_fail) { e_1 = e.node; }
case (ast.expr_log(_)) { e_1 = e.node; }
case (ast.expr_ret(_)) { e_1 = e.node; }
@ -1508,6 +1513,15 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
ast.ann_type(t)));
}
case (ast.expr_ext(?p, ?args, ?body, ?expanded, _)) {
auto exp_ = check_expr(fcx, option.get[@ast.expr](expanded));
auto t = expr_ty(exp_);
ret @fold.respan[ast.expr_](expr.span,
ast.expr_ext(p, args, body,
some[@ast.expr](exp_),
ast.ann_type(t)));
}
case (ast.expr_fail) {
ret expr;
}

View File

@ -8,6 +8,7 @@ fn test(str actual, str expected) {
}
fn main() {
test(#fmt("hello %d friends and %s things", 10, "formatted"),
"hello 10 friends and formatted things");
/*test(#fmt("hello %d friends and %s things", 10, "formatted"),
"hello 10 friends and formatted things");*/
log #fmt("test");
}