2011-04-13 19:51:24 -05:00
|
|
|
/*
|
|
|
|
* The compiler code necessary to support the #fmt extension. Eventually this
|
2011-05-12 10:24:54 -05:00
|
|
|
* should all get sucked into either the standard library extfmt module or the
|
2011-04-13 19:51:24 -05:00
|
|
|
* compiler syntax extension plugin interface.
|
2011-02-23 22:48:01 -06:00
|
|
|
*/
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
import front::parser::parser;
|
|
|
|
import util::common;
|
|
|
|
|
2011-05-17 13:41:41 -05:00
|
|
|
import std::str;
|
|
|
|
import std::vec;
|
2011-05-12 10:24:54 -05:00
|
|
|
import std::option;
|
|
|
|
import std::option::none;
|
|
|
|
import std::option::some;
|
|
|
|
|
2011-06-02 20:58:23 -05:00
|
|
|
import std::extfmt::ct::*;
|
2011-02-23 22:48:01 -06:00
|
|
|
|
2011-06-04 14:41:45 -05:00
|
|
|
import ext::*;
|
|
|
|
|
2011-04-10 13:09:47 -05:00
|
|
|
export expand_syntax_ext;
|
2011-02-24 22:22:36 -06:00
|
|
|
|
2011-06-04 14:41:45 -05:00
|
|
|
fn expand_syntax_ext(&ext_ctxt cx,
|
|
|
|
&parser p, common::span sp,
|
2011-06-02 20:58:23 -05:00
|
|
|
&vec[@ast::expr] args,
|
2011-05-12 10:24:54 -05:00
|
|
|
option::t[str] body) -> @ast::expr {
|
2011-02-24 22:22:36 -06:00
|
|
|
|
2011-05-17 13:41:41 -05:00
|
|
|
if (vec::len[@ast::expr](args) == 0u) {
|
2011-06-04 15:04:40 -05:00
|
|
|
cx.span_err(sp, "#fmt requires a format string");
|
2011-02-24 22:22:36 -06:00
|
|
|
}
|
|
|
|
|
2011-06-04 15:04:40 -05:00
|
|
|
auto fmt = expr_to_str(cx, args.(0));
|
2011-03-01 21:43:19 -06:00
|
|
|
|
2011-03-02 15:53:08 -06:00
|
|
|
// log "Format string:";
|
|
|
|
// log fmt;
|
2011-03-01 21:43:19 -06:00
|
|
|
|
2011-02-24 22:22:36 -06:00
|
|
|
auto pieces = parse_fmt_string(fmt);
|
2011-05-17 13:41:41 -05:00
|
|
|
auto args_len = vec::len[@ast::expr](args);
|
|
|
|
auto fmt_args = vec::slice[@ast::expr](args, 1u, args_len - 1u);
|
2011-06-04 15:04:40 -05:00
|
|
|
ret pieces_to_expr(cx, p, sp, pieces, args);
|
2011-02-24 22:22:36 -06:00
|
|
|
}
|
|
|
|
|
2011-06-04 15:04:40 -05:00
|
|
|
fn expr_to_str(&ext_ctxt cx, @ast::expr expr) -> str {
|
|
|
|
auto err_msg = "first argument to #fmt must be a string literal";
|
2011-02-24 22:22:36 -06:00
|
|
|
alt (expr.node) {
|
2011-05-12 10:24:54 -05:00
|
|
|
case (ast::expr_lit(?l, _)) {
|
2011-02-24 22:22:36 -06:00
|
|
|
alt (l.node) {
|
2011-05-12 10:24:54 -05:00
|
|
|
case (ast::lit_str(?s)) {
|
2011-02-24 22:22:36 -06:00
|
|
|
ret s;
|
|
|
|
}
|
2011-06-04 15:04:40 -05:00
|
|
|
case (_) {
|
|
|
|
cx.span_err(l.span, err_msg);
|
|
|
|
}
|
2011-02-24 22:22:36 -06:00
|
|
|
}
|
|
|
|
}
|
2011-06-04 15:04:40 -05:00
|
|
|
case (_) {
|
|
|
|
cx.span_err(expr.span, err_msg);
|
|
|
|
}
|
2011-02-24 22:22:36 -06:00
|
|
|
}
|
2011-02-23 22:48:01 -06:00
|
|
|
}
|
|
|
|
|
2011-04-16 18:43:29 -05:00
|
|
|
// FIXME: A lot of these functions for producing expressions can probably
|
|
|
|
// be factored out in common with other code that builds expressions.
|
|
|
|
// FIXME: Probably should be using the parser's span functions
|
|
|
|
// FIXME: Cleanup the naming of these functions
|
2011-06-04 15:04:40 -05:00
|
|
|
fn pieces_to_expr(&ext_ctxt cx, parser p, common::span sp,
|
|
|
|
vec[piece] pieces, vec[@ast::expr] args) -> @ast::expr {
|
2011-02-27 17:23:16 -06:00
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_new_lit(&ext_ctxt cx,
|
|
|
|
common::span sp, ast::lit_ lit) -> @ast::expr {
|
2011-04-08 11:44:20 -05:00
|
|
|
auto sp_lit = @rec(node=lit, span=sp);
|
2011-06-04 16:39:55 -05:00
|
|
|
auto expr = ast::expr_lit(sp_lit, cx.next_ann());
|
2011-04-08 11:44:20 -05:00
|
|
|
ret @rec(node=expr, span=sp);
|
2011-02-27 17:23:16 -06:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_new_str(&ext_ctxt cx, common::span sp, str s) -> @ast::expr {
|
2011-05-12 10:24:54 -05:00
|
|
|
auto lit = ast::lit_str(s);
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_new_lit(cx, sp, lit);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_new_int(&ext_ctxt cx, common::span sp, int i) -> @ast::expr {
|
2011-05-12 10:24:54 -05:00
|
|
|
auto lit = ast::lit_int(i);
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_new_lit(cx, sp, lit);
|
2011-04-16 17:00:52 -05:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_new_uint(&ext_ctxt cx, common::span sp, uint u) -> @ast::expr {
|
2011-05-12 10:24:54 -05:00
|
|
|
auto lit = ast::lit_uint(u);
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_new_lit(cx, sp, lit);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_add_expr(&ext_ctxt cx, common::span sp,
|
2011-05-12 10:24:54 -05:00
|
|
|
@ast::expr lhs, @ast::expr rhs) -> @ast::expr {
|
2011-06-04 16:39:55 -05:00
|
|
|
auto binexpr = ast::expr_binary(ast::add, lhs, rhs, cx.next_ann());
|
2011-04-08 11:44:20 -05:00
|
|
|
ret @rec(node=binexpr, span=sp);
|
2011-02-27 17:23:16 -06:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_path_expr(&ext_ctxt cx, common::span sp, vec[ast::ident] idents)
|
2011-05-12 10:24:54 -05:00
|
|
|
-> @ast::expr {
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[@ast::ty] types = [];
|
2011-04-13 19:14:59 -05:00
|
|
|
auto path = rec(idents=idents, types=types);
|
2011-04-08 11:44:20 -05:00
|
|
|
auto sp_path = rec(node=path, span=sp);
|
2011-06-04 16:39:55 -05:00
|
|
|
auto pathexpr = ast::expr_path(sp_path, cx.next_ann());
|
2011-04-08 11:44:20 -05:00
|
|
|
auto sp_pathexpr = @rec(node=pathexpr, span=sp);
|
2011-04-13 19:14:59 -05:00
|
|
|
ret sp_pathexpr;
|
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_vec_expr(&ext_ctxt cx, common::span sp, vec[@ast::expr] exprs)
|
2011-05-12 10:24:54 -05:00
|
|
|
-> @ast::expr {
|
2011-06-04 16:39:55 -05:00
|
|
|
auto vecexpr = ast::expr_vec(exprs, ast::imm, cx.next_ann());
|
2011-04-16 18:43:29 -05:00
|
|
|
auto sp_vecexpr = @rec(node=vecexpr, span=sp);
|
|
|
|
ret sp_vecexpr;
|
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_call(&ext_ctxt cx, common::span sp, vec[ast::ident] fn_path,
|
2011-05-12 10:24:54 -05:00
|
|
|
vec[@ast::expr] args) -> @ast::expr {
|
2011-06-04 16:39:55 -05:00
|
|
|
auto pathexpr = make_path_expr(cx, sp, fn_path);
|
|
|
|
auto callexpr = ast::expr_call(pathexpr, args, cx.next_ann());
|
2011-04-08 11:44:20 -05:00
|
|
|
auto sp_callexpr = @rec(node=callexpr, span=sp);
|
2011-02-27 18:44:57 -06:00
|
|
|
ret sp_callexpr;
|
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_rec_expr(&ext_ctxt cx, common::span sp,
|
2011-05-12 10:24:54 -05:00
|
|
|
vec[tup(ast::ident, @ast::expr)] fields) -> @ast::expr {
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ast::field] astfields = [];
|
2011-05-12 10:24:54 -05:00
|
|
|
for (tup(ast::ident, @ast::expr) field in fields) {
|
2011-04-13 19:14:59 -05:00
|
|
|
auto ident = field._0;
|
|
|
|
auto val = field._1;
|
2011-05-30 17:56:01 -05:00
|
|
|
auto astfield = rec(node=rec(mut = ast::imm,
|
|
|
|
ident = ident,
|
|
|
|
expr = val), span=sp);
|
2011-05-16 20:21:22 -05:00
|
|
|
astfields += [astfield];
|
2011-04-13 19:14:59 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
auto recexpr = ast::expr_rec(astfields,
|
|
|
|
option::none[@ast::expr],
|
2011-06-04 16:39:55 -05:00
|
|
|
cx.next_ann());
|
2011-04-13 19:14:59 -05:00
|
|
|
auto sp_recexpr = @rec(node=recexpr, span=sp);
|
|
|
|
ret sp_recexpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_path_vec(str ident) -> vec[str] {
|
2011-04-13 19:51:24 -05:00
|
|
|
// FIXME: #fmt can't currently be used from within std
|
|
|
|
// because we're explicitly referencing the 'std' crate here
|
2011-05-16 20:21:22 -05:00
|
|
|
ret ["std", "extfmt", "rt", ident];
|
2011-04-13 19:14:59 -05:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_rt_path_expr(&ext_ctxt cx,
|
|
|
|
common::span sp, str ident) -> @ast::expr {
|
2011-04-16 18:43:29 -05:00
|
|
|
auto path = make_path_vec(ident);
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_path_expr(cx, sp, path);
|
2011-04-16 18:43:29 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
// Produces an AST expression that represents a RT::conv record,
|
|
|
|
// which tells the RT::conv* functions how to perform the conversion
|
2011-06-04 15:59:57 -05:00
|
|
|
fn make_rt_conv_expr(&ext_ctxt cx,
|
2011-06-04 16:39:55 -05:00
|
|
|
common::span sp, &conv cnv) -> @ast::expr {
|
2011-04-16 17:00:52 -05:00
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_flags(&ext_ctxt cx, common::span sp, vec[flag] flags)
|
2011-05-12 10:24:54 -05:00
|
|
|
-> @ast::expr {
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[@ast::expr] flagexprs = [];
|
2011-04-16 18:43:29 -05:00
|
|
|
for (flag f in flags) {
|
2011-04-18 19:58:45 -05:00
|
|
|
auto fstr;
|
2011-04-16 18:43:29 -05:00
|
|
|
alt (f) {
|
|
|
|
case (flag_left_justify) {
|
2011-04-18 19:58:45 -05:00
|
|
|
fstr = "flag_left_justify";
|
2011-04-16 18:43:29 -05:00
|
|
|
}
|
2011-04-18 19:58:45 -05:00
|
|
|
case (flag_left_zero_pad) {
|
|
|
|
fstr = "flag_left_zero_pad";
|
2011-04-17 17:19:26 -05:00
|
|
|
}
|
2011-04-17 18:50:58 -05:00
|
|
|
case (flag_space_for_sign) {
|
2011-04-18 19:58:45 -05:00
|
|
|
fstr = "flag_space_for_sign";
|
|
|
|
}
|
|
|
|
case (flag_sign_always) {
|
|
|
|
fstr = "flag_sign_always";
|
|
|
|
}
|
|
|
|
case (flag_alternate) {
|
|
|
|
fstr = "flag_alternate";
|
2011-04-17 18:50:58 -05:00
|
|
|
}
|
2011-04-16 18:43:29 -05:00
|
|
|
}
|
2011-06-04 16:39:55 -05:00
|
|
|
flagexprs += [make_rt_path_expr(cx, sp, fstr)];
|
2011-04-16 18:43:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: 0-length vectors can't have their type inferred
|
|
|
|
// through the rec that these flags are a member of, so
|
|
|
|
// this is a hack placeholder flag
|
2011-05-17 13:41:41 -05:00
|
|
|
if (vec::len[@ast::expr](flagexprs) == 0u) {
|
2011-06-04 16:39:55 -05:00
|
|
|
flagexprs += [make_rt_path_expr(cx, sp, "flag_none")];
|
2011-04-16 18:43:29 -05:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_vec_expr(cx, sp, flagexprs);
|
2011-04-16 18:43:29 -05:00
|
|
|
}
|
|
|
|
|
2011-06-04 15:59:57 -05:00
|
|
|
fn make_count(&ext_ctxt cx,
|
2011-06-04 16:39:55 -05:00
|
|
|
common::span sp, &count cnt) -> @ast::expr {
|
2011-04-16 17:00:52 -05:00
|
|
|
alt (cnt) {
|
|
|
|
case (count_implied) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_rt_path_expr(cx, sp, "count_implied");
|
2011-04-16 17:00:52 -05:00
|
|
|
}
|
|
|
|
case (count_is(?c)) {
|
2011-06-04 16:39:55 -05:00
|
|
|
auto count_lit = make_new_int(cx, sp, c);
|
2011-04-16 17:00:52 -05:00
|
|
|
auto count_is_path = make_path_vec("count_is");
|
2011-05-16 20:21:22 -05:00
|
|
|
auto count_is_args = [count_lit];
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_call(cx, sp, count_is_path, count_is_args);
|
2011-04-16 17:00:52 -05:00
|
|
|
}
|
|
|
|
case (_) {
|
2011-06-04 15:59:57 -05:00
|
|
|
cx.span_unimpl(sp, "unimplemented #fmt conversion");
|
2011-04-16 17:00:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_ty(&ext_ctxt cx, common::span sp, &ty t) -> @ast::expr {
|
2011-04-13 19:14:59 -05:00
|
|
|
auto rt_type;
|
|
|
|
alt (t) {
|
|
|
|
case (ty_hex(?c)) {
|
|
|
|
alt (c) {
|
|
|
|
case (case_upper) {
|
|
|
|
rt_type = "ty_hex_upper";
|
|
|
|
}
|
|
|
|
case (case_lower) {
|
|
|
|
rt_type = "ty_hex_lower";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (ty_bits) {
|
|
|
|
rt_type = "ty_bits";
|
|
|
|
}
|
2011-04-26 19:13:23 -05:00
|
|
|
case (ty_octal) {
|
|
|
|
rt_type = "ty_octal";
|
|
|
|
}
|
2011-04-13 19:14:59 -05:00
|
|
|
case (_) {
|
|
|
|
rt_type = "ty_default";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_rt_path_expr(cx, sp, rt_type);
|
2011-04-13 19:14:59 -05:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_conv_rec(&ext_ctxt cx,
|
2011-05-12 10:24:54 -05:00
|
|
|
common::span sp,
|
|
|
|
@ast::expr flags_expr,
|
|
|
|
@ast::expr width_expr,
|
|
|
|
@ast::expr precision_expr,
|
|
|
|
@ast::expr ty_expr) -> @ast::expr {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_rec_expr(cx, sp, [tup("flags", flags_expr),
|
2011-05-10 19:27:30 -05:00
|
|
|
tup("width", width_expr),
|
|
|
|
tup("precision", precision_expr),
|
2011-05-16 20:21:22 -05:00
|
|
|
tup("ty", ty_expr)]);
|
2011-04-13 19:14:59 -05:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
auto rt_conv_flags = make_flags(cx, sp, cnv.flags);
|
|
|
|
auto rt_conv_width = make_count(cx, sp, cnv.width);
|
|
|
|
auto rt_conv_precision = make_count(cx, sp, cnv.precision);
|
|
|
|
auto rt_conv_ty = make_ty(cx, sp, cnv.ty);
|
|
|
|
ret make_conv_rec(cx,
|
2011-05-10 19:27:30 -05:00
|
|
|
sp,
|
2011-04-16 18:43:29 -05:00
|
|
|
rt_conv_flags,
|
2011-04-16 17:00:52 -05:00
|
|
|
rt_conv_width,
|
2011-04-17 12:10:02 -05:00
|
|
|
rt_conv_precision,
|
2011-04-16 17:00:52 -05:00
|
|
|
rt_conv_ty);
|
2011-04-13 19:14:59 -05:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_conv_call(&ext_ctxt cx, common::span sp, str conv_type,
|
2011-05-12 10:24:54 -05:00
|
|
|
&conv cnv, @ast::expr arg) -> @ast::expr {
|
2011-04-11 20:55:03 -05:00
|
|
|
auto fname = "conv_" + conv_type;
|
2011-04-13 19:14:59 -05:00
|
|
|
auto path = make_path_vec(fname);
|
2011-06-04 16:39:55 -05:00
|
|
|
auto cnv_expr = make_rt_conv_expr(cx, sp, cnv);
|
2011-05-16 20:21:22 -05:00
|
|
|
auto args = [cnv_expr, arg];
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_call(cx, arg.span, path, args);
|
2011-04-11 20:55:03 -05:00
|
|
|
}
|
|
|
|
|
2011-06-04 16:39:55 -05:00
|
|
|
fn make_new_conv(&ext_ctxt cx, common::span sp,
|
2011-06-04 15:04:40 -05:00
|
|
|
conv cnv, @ast::expr arg) -> @ast::expr {
|
2011-02-27 18:44:57 -06:00
|
|
|
|
2011-05-12 18:51:22 -05:00
|
|
|
// FIXME: Extract all this validation into extfmt::ct
|
2011-04-17 18:50:58 -05:00
|
|
|
fn is_signed_type(conv cnv) -> bool {
|
|
|
|
alt (cnv.ty) {
|
|
|
|
case (ty_int(?s)) {
|
|
|
|
alt (s) {
|
|
|
|
case (signed) {
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
case (unsigned) {
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-27 18:44:57 -06:00
|
|
|
auto unsupported = "conversion not supported in #fmt string";
|
|
|
|
|
|
|
|
alt (cnv.param) {
|
2011-05-30 23:39:19 -05:00
|
|
|
case (option::none) {
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
case (_) {
|
2011-06-04 15:59:57 -05:00
|
|
|
cx.span_unimpl(sp, unsupported);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-16 18:43:29 -05:00
|
|
|
for (flag f in cnv.flags) {
|
|
|
|
alt (f) {
|
|
|
|
case (flag_left_justify) {
|
|
|
|
}
|
2011-04-17 17:19:26 -05:00
|
|
|
case (flag_sign_always) {
|
2011-04-17 18:50:58 -05:00
|
|
|
if (!is_signed_type(cnv)) {
|
2011-06-04 15:04:40 -05:00
|
|
|
cx.span_err(sp, "+ flag only valid in "
|
|
|
|
+ "signed #fmt conversion");
|
2011-04-17 18:50:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case (flag_space_for_sign) {
|
|
|
|
if (!is_signed_type(cnv)) {
|
2011-06-04 15:04:40 -05:00
|
|
|
cx.span_err(sp, "space flag only valid in "
|
|
|
|
+ "signed #fmt conversions");
|
2011-04-17 17:19:26 -05:00
|
|
|
}
|
|
|
|
}
|
2011-04-18 19:58:45 -05:00
|
|
|
case (flag_left_zero_pad) {
|
|
|
|
}
|
2011-04-16 18:43:29 -05:00
|
|
|
case (_) {
|
2011-06-04 15:59:57 -05:00
|
|
|
cx.span_unimpl(sp, unsupported);
|
2011-04-16 18:43:29 -05:00
|
|
|
}
|
|
|
|
}
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
alt (cnv.width) {
|
|
|
|
case (count_implied) {
|
|
|
|
}
|
2011-04-16 17:00:52 -05:00
|
|
|
case (count_is(_)) {
|
|
|
|
}
|
2011-02-27 18:44:57 -06:00
|
|
|
case (_) {
|
2011-06-04 15:59:57 -05:00
|
|
|
cx.span_unimpl(sp, unsupported);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alt (cnv.precision) {
|
|
|
|
case (count_implied) {
|
|
|
|
}
|
2011-04-17 12:10:02 -05:00
|
|
|
case (count_is(_)) {
|
|
|
|
}
|
2011-02-27 18:44:57 -06:00
|
|
|
case (_) {
|
2011-06-04 15:59:57 -05:00
|
|
|
cx.span_unimpl(sp, unsupported);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alt (cnv.ty) {
|
|
|
|
case (ty_str) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_conv_call(cx, arg.span, "str", cnv, arg);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
case (ty_int(?sign)) {
|
|
|
|
alt (sign) {
|
|
|
|
case (signed) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_conv_call(cx, arg.span, "int", cnv, arg);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
case (unsigned) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_conv_call(cx, arg.span, "uint", cnv, arg);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-11 20:36:10 -05:00
|
|
|
case (ty_bool) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_conv_call(cx, arg.span, "bool", cnv, arg);
|
2011-04-11 20:36:10 -05:00
|
|
|
}
|
|
|
|
case (ty_char) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_conv_call(cx, arg.span, "char", cnv, arg);
|
2011-04-13 19:14:59 -05:00
|
|
|
}
|
|
|
|
case (ty_hex(_)) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_conv_call(cx, arg.span, "uint", cnv, arg);
|
2011-04-11 20:36:10 -05:00
|
|
|
}
|
2011-04-13 20:36:32 -05:00
|
|
|
case (ty_bits) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_conv_call(cx, arg.span, "uint", cnv, arg);
|
2011-04-13 20:36:32 -05:00
|
|
|
}
|
2011-04-26 19:13:23 -05:00
|
|
|
case (ty_octal) {
|
2011-06-04 16:39:55 -05:00
|
|
|
ret make_conv_call(cx, arg.span, "uint", cnv, arg);
|
2011-04-26 19:13:23 -05:00
|
|
|
}
|
2011-02-27 18:44:57 -06:00
|
|
|
case (_) {
|
2011-06-04 15:59:57 -05:00
|
|
|
cx.span_unimpl(sp, unsupported);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 20:03:44 -06:00
|
|
|
fn log_conv(conv c) {
|
|
|
|
alt (c.param) {
|
2011-05-30 23:39:19 -05:00
|
|
|
case (some(?p)) {
|
2011-05-17 13:41:41 -05:00
|
|
|
log "param: " + std::int::to_str(p, 10u);
|
2011-03-01 20:03:44 -06:00
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
log "param: none";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (flag f in c.flags) {
|
|
|
|
alt (f) {
|
|
|
|
case (flag_left_justify) {
|
|
|
|
log "flag: left justify";
|
|
|
|
}
|
|
|
|
case (flag_left_zero_pad) {
|
|
|
|
log "flag: left zero pad";
|
|
|
|
}
|
2011-04-17 18:50:58 -05:00
|
|
|
case (flag_space_for_sign) {
|
2011-03-01 20:03:44 -06:00
|
|
|
log "flag: left space pad";
|
|
|
|
}
|
2011-04-17 17:19:26 -05:00
|
|
|
case (flag_sign_always) {
|
|
|
|
log "flag: sign always";
|
2011-03-01 20:03:44 -06:00
|
|
|
}
|
|
|
|
case (flag_alternate) {
|
|
|
|
log "flag: alternate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alt (c.width) {
|
|
|
|
case (count_is(?i)) {
|
2011-05-17 13:41:41 -05:00
|
|
|
log "width: count is " + std::int::to_str(i, 10u);
|
2011-03-01 20:03:44 -06:00
|
|
|
}
|
|
|
|
case (count_is_param(?i)) {
|
2011-05-17 13:41:41 -05:00
|
|
|
log "width: count is param " + std::int::to_str(i, 10u);
|
2011-03-01 20:03:44 -06:00
|
|
|
}
|
|
|
|
case (count_is_next_param) {
|
|
|
|
log "width: count is next param";
|
|
|
|
}
|
|
|
|
case (count_implied) {
|
|
|
|
log "width: count is implied";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alt (c.precision) {
|
|
|
|
case (count_is(?i)) {
|
2011-05-17 13:41:41 -05:00
|
|
|
log "prec: count is " + std::int::to_str(i, 10u);
|
2011-03-01 20:03:44 -06:00
|
|
|
}
|
|
|
|
case (count_is_param(?i)) {
|
2011-05-17 13:41:41 -05:00
|
|
|
log "prec: count is param " + std::int::to_str(i, 10u);
|
2011-03-01 20:03:44 -06:00
|
|
|
}
|
|
|
|
case (count_is_next_param) {
|
|
|
|
log "prec: count is next param";
|
|
|
|
}
|
|
|
|
case (count_implied) {
|
|
|
|
log "prec: count is implied";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alt (c.ty) {
|
|
|
|
case (ty_bool) {
|
|
|
|
log "type: bool";
|
|
|
|
}
|
|
|
|
case (ty_str) {
|
|
|
|
log "type: str";
|
|
|
|
}
|
|
|
|
case (ty_char) {
|
|
|
|
log "type: char";
|
|
|
|
}
|
|
|
|
case (ty_int(?s)) {
|
|
|
|
alt (s) {
|
|
|
|
case (signed) {
|
|
|
|
log "type: signed";
|
|
|
|
}
|
|
|
|
case (unsigned) {
|
|
|
|
log "type: unsigned";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (ty_bits) {
|
|
|
|
log "type: bits";
|
|
|
|
}
|
|
|
|
case (ty_hex(?cs)) {
|
|
|
|
alt (cs) {
|
|
|
|
case (case_upper) {
|
|
|
|
log "type: uhex";
|
|
|
|
}
|
|
|
|
case (case_lower) {
|
|
|
|
log "type: lhex";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-04 15:04:40 -05:00
|
|
|
auto fmt_sp = args.(0).span;
|
2011-02-27 18:44:57 -06:00
|
|
|
auto n = 0u;
|
2011-06-04 16:39:55 -05:00
|
|
|
auto tmp_expr = make_new_str(cx, sp, "");
|
2011-05-25 22:42:50 -05:00
|
|
|
auto nargs = vec::len[@ast::expr](args);
|
2011-02-27 17:23:16 -06:00
|
|
|
|
2011-05-10 19:27:30 -05:00
|
|
|
for (piece pc in pieces) {
|
|
|
|
alt (pc) {
|
2011-02-27 17:23:16 -06:00
|
|
|
case (piece_string(?s)) {
|
2011-06-04 16:39:55 -05:00
|
|
|
auto s_expr = make_new_str(cx, fmt_sp, s);
|
|
|
|
tmp_expr = make_add_expr(cx, fmt_sp, tmp_expr, s_expr);
|
2011-02-27 17:23:16 -06:00
|
|
|
}
|
|
|
|
case (piece_conv(?conv)) {
|
2011-06-04 15:04:40 -05:00
|
|
|
n += 1u;
|
|
|
|
|
2011-05-25 22:42:50 -05:00
|
|
|
if (n >= nargs) {
|
2011-06-04 15:04:40 -05:00
|
|
|
cx.span_err(sp, "not enough arguments to #fmt "
|
|
|
|
+ "for the given format string");
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
|
2011-03-01 20:03:44 -06:00
|
|
|
// TODO: Remove debug logging
|
2011-04-17 12:10:02 -05:00
|
|
|
//log "Building conversion:";
|
|
|
|
//log_conv(conv);
|
2011-03-01 20:03:44 -06:00
|
|
|
|
2011-02-27 18:44:57 -06:00
|
|
|
auto arg_expr = args.(n);
|
2011-06-04 16:39:55 -05:00
|
|
|
auto c_expr = make_new_conv(cx, fmt_sp, conv, arg_expr);
|
|
|
|
tmp_expr = make_add_expr(cx, fmt_sp, tmp_expr, c_expr);
|
2011-02-27 17:23:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-25 22:42:50 -05:00
|
|
|
auto expected_nargs = n + 1u; // n conversions + the fmt string
|
|
|
|
if (expected_nargs < nargs) {
|
2011-06-04 15:04:40 -05:00
|
|
|
cx.span_err(sp,
|
|
|
|
#fmt("too many arguments to #fmt. found %u, expected %u",
|
|
|
|
nargs, expected_nargs));
|
2011-05-25 22:42:50 -05:00
|
|
|
}
|
|
|
|
|
2011-03-01 20:03:44 -06:00
|
|
|
// TODO: Remove this debug logging
|
2011-03-02 15:53:08 -06:00
|
|
|
// log "dumping expanded ast:";
|
2011-05-12 10:24:54 -05:00
|
|
|
// log pretty::print_expr(tmp_expr);
|
2011-03-01 20:03:44 -06:00
|
|
|
ret tmp_expr;
|
2011-02-24 22:22:36 -06:00
|
|
|
}
|
|
|
|
|
2011-02-23 22:48:01 -06:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-03-25 17:07:27 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-02-23 22:48:01 -06:00
|
|
|
// End:
|
|
|
|
//
|