Rename AST builders to use uniform naming scheme. Also add a few more.

This commit is contained in:
Kevin Atkinson 2012-01-26 16:34:05 -07:00
parent 5ef53382ae
commit 98450d0dad
3 changed files with 68 additions and 46 deletions

View File

@ -6,42 +6,78 @@ import syntax::ext::base::ext_ctxt;
// NOTE: Moved from fmt.rs which had this fixme:
// FIXME: Cleanup the naming of these functions
fn make_new_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr {
fn mk_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr {
let sp_lit = @{node: lit, span: sp};
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
}
fn make_new_str(cx: ext_ctxt, sp: span, s: str) -> @ast::expr {
fn mk_str(cx: ext_ctxt, sp: span, s: str) -> @ast::expr {
let lit = ast::lit_str(s);
ret make_new_lit(cx, sp, lit);
ret mk_lit(cx, sp, lit);
}
fn make_new_int(cx: ext_ctxt, sp: span, i: int) -> @ast::expr {
fn mk_int(cx: ext_ctxt, sp: span, i: int) -> @ast::expr {
let lit = ast::lit_int(i as i64, ast::ty_i);
ret make_new_lit(cx, sp, lit);
ret mk_lit(cx, sp, lit);
}
fn make_new_uint(cx: ext_ctxt, sp: span, u: uint) -> @ast::expr {
fn mk_uint(cx: ext_ctxt, sp: span, u: uint) -> @ast::expr {
let lit = ast::lit_uint(u as u64, ast::ty_u);
ret make_new_lit(cx, sp, lit);
ret mk_lit(cx, sp, lit);
}
fn make_add_expr(cx: ext_ctxt, sp: span, lhs: @ast::expr, rhs: @ast::expr)
fn mk_binary(cx: ext_ctxt, sp: span, op: ast::binop,
lhs: @ast::expr, rhs: @ast::expr)
-> @ast::expr {
let binexpr = ast::expr_binary(ast::add, lhs, rhs);
let binexpr = ast::expr_binary(op, lhs, rhs);
ret @{id: cx.next_id(), node: binexpr, span: sp};
}
fn make_path_expr(cx: ext_ctxt, sp: span, idents: [ast::ident]) ->
@ast::expr {
fn mk_unary(cx: ext_ctxt, sp: span, op: ast::unop, e: @ast::expr)
-> @ast::expr {
let expr = ast::expr_unary(op, e);
ret @{id: cx.next_id(), node: expr, span: sp};
}
fn mk_path(cx: ext_ctxt, sp: span, idents: [ast::ident]) ->
@ast::expr {
let path = {global: false, idents: idents, types: []};
let sp_path = @{node: path, span: sp};
let pathexpr = ast::expr_path(sp_path);
ret @{id: cx.next_id(), node: pathexpr, span: sp};
}
fn make_vec_expr(cx: ext_ctxt, sp: span, exprs: [@ast::expr]) ->
fn mk_access_(cx: ext_ctxt, sp: span, p: @ast::expr, m: ast::ident)
-> @ast::expr {
let expr = ast::expr_field(p, m, []);
ret @{id: cx.next_id(), node: expr, span: sp};
}
fn mk_access(cx: ext_ctxt, sp: span, p: [ast::ident], m: ast::ident)
-> @ast::expr {
let pathexpr = mk_path(cx, sp, p);
ret mk_access_(cx, sp, pathexpr, m);
}
fn mk_call_(cx: ext_ctxt, sp: span, fn_expr: @ast::expr,
args: [@ast::expr]) -> @ast::expr {
let callexpr = ast::expr_call(fn_expr, args, false);
ret @{id: cx.next_id(), node: callexpr, span: sp};
}
fn mk_call(cx: ext_ctxt, sp: span, fn_path: [ast::ident],
args: [@ast::expr]) -> @ast::expr {
let pathexpr = mk_path(cx, sp, fn_path);
ret mk_call_(cx, sp, pathexpr, args);
}
// e = expr, t = type
fn mk_vec_e(cx: ext_ctxt, sp: span, exprs: [@ast::expr]) ->
@ast::expr {
let vecexpr = ast::expr_vec(exprs, ast::imm);
ret @{id: cx.next_id(), node: vecexpr, span: sp};
}
fn make_call(cx: ext_ctxt, sp: span, fn_path: [ast::ident],
args: [@ast::expr]) -> @ast::expr {
let pathexpr = make_path_expr(cx, sp, fn_path);
let callexpr = ast::expr_call(pathexpr, args, false);
ret @{id: cx.next_id(), node: callexpr, span: sp};
fn mk_rec_e(cx: ext_ctxt, sp: span,
fields: [{ident: ast::ident, ex: @ast::expr}]) ->
@ast::expr {
let astfields: [ast::field] = [];
for field: {ident: ast::ident, ex: @ast::expr} in fields {
let ident = field.ident;
let val = field.ex;
let astfield =
{node: {mut: ast::imm, ident: ident, expr: val}, span: sp};
astfields += [astfield];
}
let recexpr = ast::expr_rec(astfields, option::none::<@ast::expr>);
ret @{id: cx.next_id(), node: recexpr, span: sp};
}

View File

@ -56,7 +56,7 @@ fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt,
fn expand_qquote(cx: ext_ctxt, sp: span, e: @ast::expr) -> ast::expr_ {
import syntax::ext::build::*;
let str = codemap::span_to_snippet(sp, cx.session().parse_sess.cm);
let expr = make_new_str(cx, e.span, str);
let expr = mk_str(cx, e.span, str);
ret expr.node;
}

View File

@ -45,26 +45,12 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: @ast::expr,
// NOTE: Moved many of the common ones to build.rs --kevina
fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
-> @ast::expr {
fn make_rec_expr(cx: ext_ctxt, sp: span,
fields: [{ident: ast::ident, ex: @ast::expr}]) ->
@ast::expr {
let astfields: [ast::field] = [];
for field: {ident: ast::ident, ex: @ast::expr} in fields {
let ident = field.ident;
let val = field.ex;
let astfield =
{node: {mut: ast::imm, ident: ident, expr: val}, span: sp};
astfields += [astfield];
}
let recexpr = ast::expr_rec(astfields, option::none::<@ast::expr>);
ret @{id: cx.next_id(), node: recexpr, span: sp};
}
fn make_path_vec(_cx: ext_ctxt, ident: ast::ident) -> [ast::ident] {
ret ["extfmt", "rt", ident];
}
fn make_rt_path_expr(cx: ext_ctxt, sp: span, ident: str) -> @ast::expr {
let path = make_path_vec(cx, ident);
ret make_path_expr(cx, sp, path);
ret mk_path(cx, sp, path);
}
// Produces an AST expression that represents a RT::conv record,
// which tells the RT::conv* functions how to perform the conversion
@ -90,7 +76,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
if vec::len::<@ast::expr>(flagexprs) == 0u {
flagexprs += [make_rt_path_expr(cx, sp, "flag_none")];
}
ret make_vec_expr(cx, sp, flagexprs);
ret mk_vec_e(cx, sp, flagexprs);
}
fn make_count(cx: ext_ctxt, sp: span, cnt: count) -> @ast::expr {
alt cnt {
@ -98,10 +84,10 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
ret make_rt_path_expr(cx, sp, "count_implied");
}
count_is(c) {
let count_lit = make_new_int(cx, sp, c);
let count_lit = mk_int(cx, sp, c);
let count_is_path = make_path_vec(cx, "count_is");
let count_is_args = [count_lit];
ret make_call(cx, sp, count_is_path, count_is_args);
ret mk_call(cx, sp, count_is_path, count_is_args);
}
_ { cx.span_unimpl(sp, "unimplemented #fmt conversion"); }
}
@ -124,11 +110,11 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
fn make_conv_rec(cx: ext_ctxt, sp: span, flags_expr: @ast::expr,
width_expr: @ast::expr, precision_expr: @ast::expr,
ty_expr: @ast::expr) -> @ast::expr {
ret make_rec_expr(cx, sp,
[{ident: "flags", ex: flags_expr},
{ident: "width", ex: width_expr},
{ident: "precision", ex: precision_expr},
{ident: "ty", ex: ty_expr}]);
ret mk_rec_e(cx, sp,
[{ident: "flags", ex: flags_expr},
{ident: "width", ex: width_expr},
{ident: "precision", ex: precision_expr},
{ident: "ty", ex: ty_expr}]);
}
let rt_conv_flags = make_flags(cx, sp, cnv.flags);
let rt_conv_width = make_count(cx, sp, cnv.width);
@ -143,7 +129,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
let path = make_path_vec(cx, fname);
let cnv_expr = make_rt_conv_expr(cx, sp, cnv);
let args = [cnv_expr, arg];
ret make_call(cx, arg.span, path, args);
ret mk_call(cx, arg.span, path, args);
}
fn make_new_conv(cx: ext_ctxt, sp: span, cnv: conv, arg: @ast::expr) ->
@ast::expr {
@ -272,13 +258,13 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
}
let fmt_sp = args[0].span;
let n = 0u;
let tmp_expr = make_new_str(cx, sp, "");
let tmp_expr = mk_str(cx, sp, "");
let nargs = vec::len::<@ast::expr>(args);
for pc: piece in pieces {
alt pc {
piece_string(s) {
let s_expr = make_new_str(cx, fmt_sp, s);
tmp_expr = make_add_expr(cx, fmt_sp, tmp_expr, s_expr);
let s_expr = mk_str(cx, fmt_sp, s);
tmp_expr = mk_binary(cx, fmt_sp, ast::add, tmp_expr, s_expr);
}
piece_conv(conv) {
n += 1u;
@ -291,7 +277,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
log_conv(conv);
let arg_expr = args[n];
let c_expr = make_new_conv(cx, fmt_sp, conv, arg_expr);
tmp_expr = make_add_expr(cx, fmt_sp, tmp_expr, c_expr);
tmp_expr = mk_binary(cx, fmt_sp, ast::add, tmp_expr, c_expr);
}
}
}