2011-04-13 19:51:24 -05:00
|
|
|
/*
|
|
|
|
* The compiler code necessary to support the #fmt extension. Eventually this
|
|
|
|
* should all get sucked into either the standard library ExtFmt module or the
|
|
|
|
* compiler syntax extension plugin interface.
|
2011-02-23 22:48:01 -06:00
|
|
|
*/
|
|
|
|
|
2011-02-27 17:23:16 -06:00
|
|
|
import util.common;
|
2011-02-23 22:48:01 -06:00
|
|
|
|
2011-02-24 22:22:36 -06:00
|
|
|
import std._str;
|
|
|
|
import std._vec;
|
2011-02-23 22:48:01 -06:00
|
|
|
import std.option;
|
2011-02-27 13:30:44 -06:00
|
|
|
import std.option.none;
|
|
|
|
import std.option.some;
|
2011-02-23 22:48:01 -06:00
|
|
|
|
2011-04-13 17:59:40 -05:00
|
|
|
import std.ExtFmt.CT.signedness;
|
|
|
|
import std.ExtFmt.CT.signed;
|
|
|
|
import std.ExtFmt.CT.unsigned;
|
|
|
|
import std.ExtFmt.CT.caseness;
|
|
|
|
import std.ExtFmt.CT.case_upper;
|
|
|
|
import std.ExtFmt.CT.case_lower;
|
|
|
|
import std.ExtFmt.CT.ty;
|
|
|
|
import std.ExtFmt.CT.ty_bool;
|
|
|
|
import std.ExtFmt.CT.ty_str;
|
|
|
|
import std.ExtFmt.CT.ty_char;
|
|
|
|
import std.ExtFmt.CT.ty_int;
|
|
|
|
import std.ExtFmt.CT.ty_bits;
|
|
|
|
import std.ExtFmt.CT.ty_hex;
|
|
|
|
import std.ExtFmt.CT.flag;
|
|
|
|
import std.ExtFmt.CT.flag_left_justify;
|
|
|
|
import std.ExtFmt.CT.flag_left_zero_pad;
|
|
|
|
import std.ExtFmt.CT.flag_left_space_pad;
|
|
|
|
import std.ExtFmt.CT.flag_plus_if_positive;
|
|
|
|
import std.ExtFmt.CT.flag_alternate;
|
|
|
|
import std.ExtFmt.CT.count;
|
|
|
|
import std.ExtFmt.CT.count_is;
|
|
|
|
import std.ExtFmt.CT.count_is_param;
|
|
|
|
import std.ExtFmt.CT.count_is_next_param;
|
|
|
|
import std.ExtFmt.CT.count_implied;
|
|
|
|
import std.ExtFmt.CT.conv;
|
|
|
|
import std.ExtFmt.CT.piece;
|
|
|
|
import std.ExtFmt.CT.piece_string;
|
|
|
|
import std.ExtFmt.CT.piece_conv;
|
2011-04-11 19:22:27 -05:00
|
|
|
import std.ExtFmt.CT.parse_fmt_string;
|
2011-02-23 22:48:01 -06:00
|
|
|
|
2011-04-10 13:09:47 -05:00
|
|
|
export expand_syntax_ext;
|
2011-02-24 22:22:36 -06:00
|
|
|
|
2011-04-13 19:51:24 -05:00
|
|
|
// FIXME: Need to thread parser through here to handle errors correctly
|
2011-02-23 22:48:01 -06:00
|
|
|
fn expand_syntax_ext(vec[@ast.expr] args,
|
|
|
|
option.t[@ast.expr] body) -> @ast.expr {
|
2011-02-24 22:22:36 -06:00
|
|
|
|
|
|
|
if (_vec.len[@ast.expr](args) == 0u) {
|
2011-03-01 20:03:44 -06:00
|
|
|
log "malformed #fmt call";
|
|
|
|
fail;
|
2011-02-24 22:22:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
auto fmt = expr_to_str(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-02-27 18:44:57 -06:00
|
|
|
auto args_len = _vec.len[@ast.expr](args);
|
|
|
|
auto fmt_args = _vec.slice[@ast.expr](args, 1u, args_len - 1u);
|
2011-02-24 22:22:36 -06:00
|
|
|
ret pieces_to_expr(pieces, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_to_str(@ast.expr expr) -> str {
|
|
|
|
alt (expr.node) {
|
|
|
|
case (ast.expr_lit(?l, _)) {
|
|
|
|
alt (l.node) {
|
|
|
|
case (ast.lit_str(?s)) {
|
|
|
|
ret s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-01 20:03:44 -06:00
|
|
|
log "malformed #fmt call";
|
2011-02-23 22:48:01 -06:00
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-02-24 22:22:36 -06:00
|
|
|
fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
|
2011-02-27 17:23:16 -06:00
|
|
|
|
2011-02-27 18:44:57 -06:00
|
|
|
fn make_new_lit(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-02-27 18:44:57 -06:00
|
|
|
auto expr = ast.expr_lit(sp_lit, ast.ann_none);
|
2011-04-08 11:44:20 -05:00
|
|
|
ret @rec(node=expr, span=sp);
|
2011-02-27 17:23:16 -06:00
|
|
|
}
|
|
|
|
|
2011-02-27 18:44:57 -06:00
|
|
|
fn make_new_str(common.span sp, str s) -> @ast.expr {
|
|
|
|
auto lit = ast.lit_str(s);
|
|
|
|
ret make_new_lit(sp, lit);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_new_uint(common.span sp, uint u) -> @ast.expr {
|
|
|
|
auto lit = ast.lit_uint(u);
|
|
|
|
ret make_new_lit(sp, lit);
|
|
|
|
}
|
|
|
|
|
2011-02-27 17:23:16 -06:00
|
|
|
fn make_add_expr(common.span sp,
|
|
|
|
@ast.expr lhs, @ast.expr rhs) -> @ast.expr {
|
|
|
|
auto binexpr = ast.expr_binary(ast.add, lhs, rhs, ast.ann_none);
|
2011-04-08 11:44:20 -05:00
|
|
|
ret @rec(node=binexpr, span=sp);
|
2011-02-27 17:23:16 -06:00
|
|
|
}
|
|
|
|
|
2011-04-13 19:14:59 -05:00
|
|
|
fn make_path_expr(common.span sp, vec[ast.ident] idents) -> @ast.expr {
|
|
|
|
let vec[@ast.ty] types = vec();
|
|
|
|
auto path = rec(idents=idents, types=types);
|
2011-04-08 11:44:20 -05:00
|
|
|
auto sp_path = rec(node=path, span=sp);
|
2011-02-27 18:44:57 -06:00
|
|
|
auto pathexpr = ast.expr_path(sp_path, none[ast.def], ast.ann_none);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_call(common.span sp, vec[ast.ident] fn_path,
|
|
|
|
vec[@ast.expr] args) -> @ast.expr {
|
|
|
|
auto pathexpr = make_path_expr(sp, fn_path);
|
|
|
|
auto callexpr = ast.expr_call(pathexpr, args, ast.ann_none);
|
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-04-13 19:14:59 -05:00
|
|
|
fn make_rec_expr(common.span sp,
|
|
|
|
vec[tup(ast.ident, @ast.expr)] fields) -> @ast.expr {
|
|
|
|
let vec[ast.field] astfields = vec();
|
|
|
|
for (tup(ast.ident, @ast.expr) field in fields) {
|
|
|
|
auto ident = field._0;
|
|
|
|
auto val = field._1;
|
|
|
|
auto astfield = rec(mut = ast.imm,
|
|
|
|
ident = ident,
|
|
|
|
expr = val);
|
|
|
|
astfields += vec(astfield);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto recexpr = ast.expr_rec(astfields,
|
|
|
|
option.none[@ast.expr],
|
|
|
|
ast.ann_none);
|
|
|
|
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-04-13 19:14:59 -05:00
|
|
|
ret vec("std", "ExtFmt", "RT", ident);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_rt_conv_expr(common.span sp, &conv cnv) -> @ast.expr {
|
|
|
|
fn make_ty(common.span sp, &ty t) -> @ast.expr {
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
rt_type = "ty_default";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto idents = make_path_vec(rt_type);
|
|
|
|
ret make_path_expr(sp, idents);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_conv_rec(common.span sp, &@ast.expr ty_expr) -> @ast.expr {
|
|
|
|
ret make_rec_expr(sp, vec(tup("ty", ty_expr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rt_conv_ty = make_ty(sp, cnv.ty);
|
|
|
|
ret make_conv_rec(sp, rt_conv_ty);
|
|
|
|
}
|
|
|
|
|
2011-04-11 20:55:03 -05:00
|
|
|
fn make_conv_call(common.span sp, str conv_type,
|
2011-04-13 19:14:59 -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);
|
|
|
|
auto cnv_expr = make_rt_conv_expr(sp, cnv);
|
|
|
|
auto args = vec(cnv_expr, arg);
|
2011-04-11 20:55:03 -05:00
|
|
|
ret make_call(arg.span, path, args);
|
|
|
|
}
|
|
|
|
|
2011-02-27 18:44:57 -06:00
|
|
|
fn make_new_conv(conv cnv, @ast.expr arg) -> @ast.expr {
|
|
|
|
|
|
|
|
auto unsupported = "conversion not supported in #fmt string";
|
|
|
|
|
|
|
|
alt (cnv.param) {
|
|
|
|
case (option.none[int]) {
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
log unsupported;
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_vec.len[flag](cnv.flags) != 0u) {
|
|
|
|
log unsupported;
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
alt (cnv.width) {
|
|
|
|
case (count_implied) {
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
log unsupported;
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alt (cnv.precision) {
|
|
|
|
case (count_implied) {
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
log unsupported;
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alt (cnv.ty) {
|
|
|
|
case (ty_str) {
|
|
|
|
ret arg;
|
|
|
|
}
|
|
|
|
case (ty_int(?sign)) {
|
|
|
|
alt (sign) {
|
|
|
|
case (signed) {
|
2011-04-13 19:14:59 -05:00
|
|
|
ret make_conv_call(arg.span, "int", cnv, arg);
|
2011-02-27 18:44:57 -06:00
|
|
|
}
|
|
|
|
case (unsigned) {
|
2011-04-13 19:14:59 -05:00
|
|
|
ret make_conv_call(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-04-13 19:14:59 -05:00
|
|
|
ret make_conv_call(arg.span, "bool", cnv, arg);
|
2011-04-11 20:36:10 -05:00
|
|
|
}
|
|
|
|
case (ty_char) {
|
2011-04-13 19:14:59 -05:00
|
|
|
ret make_conv_call(arg.span, "char", cnv, arg);
|
|
|
|
}
|
|
|
|
case (ty_hex(_)) {
|
|
|
|
ret make_conv_call(arg.span, "uint", cnv, arg);
|
2011-04-11 20:36:10 -05:00
|
|
|
}
|
2011-04-13 20:36:32 -05:00
|
|
|
case (ty_bits) {
|
|
|
|
ret make_conv_call(arg.span, "uint", cnv, arg);
|
|
|
|
}
|
2011-02-27 18:44:57 -06:00
|
|
|
case (_) {
|
|
|
|
log unsupported;
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 20:03:44 -06:00
|
|
|
fn log_conv(conv c) {
|
|
|
|
alt (c.param) {
|
|
|
|
case (some[int](?p)) {
|
|
|
|
log "param: " + std._int.to_str(p, 10u);
|
|
|
|
}
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
case (flag_left_space_pad) {
|
|
|
|
log "flag: left space pad";
|
|
|
|
}
|
|
|
|
case (flag_plus_if_positive) {
|
|
|
|
log "flag: plus if positive";
|
|
|
|
}
|
|
|
|
case (flag_alternate) {
|
|
|
|
log "flag: alternate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alt (c.width) {
|
|
|
|
case (count_is(?i)) {
|
|
|
|
log "width: count is " + std._int.to_str(i, 10u);
|
|
|
|
}
|
|
|
|
case (count_is_param(?i)) {
|
|
|
|
log "width: count is param " + std._int.to_str(i, 10u);
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
log "prec: count is " + std._int.to_str(i, 10u);
|
|
|
|
}
|
|
|
|
case (count_is_param(?i)) {
|
|
|
|
log "prec: count is param " + std._int.to_str(i, 10u);
|
|
|
|
}
|
|
|
|
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-02-27 17:23:16 -06:00
|
|
|
auto sp = args.(0).span;
|
2011-02-27 18:44:57 -06:00
|
|
|
auto n = 0u;
|
|
|
|
auto tmp_expr = make_new_str(sp, "");
|
2011-02-27 17:23:16 -06:00
|
|
|
|
|
|
|
for (piece p in pieces) {
|
|
|
|
alt (p) {
|
|
|
|
case (piece_string(?s)) {
|
|
|
|
auto s_expr = make_new_str(sp, s);
|
|
|
|
tmp_expr = make_add_expr(sp, tmp_expr, s_expr);
|
|
|
|
}
|
|
|
|
case (piece_conv(?conv)) {
|
2011-02-27 18:44:57 -06:00
|
|
|
if (n >= _vec.len[@ast.expr](args)) {
|
|
|
|
log "too many conversions in #fmt string";
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-03-01 20:03:44 -06:00
|
|
|
// TODO: Remove debug logging
|
2011-03-02 15:53:08 -06:00
|
|
|
// log "Building conversion:";
|
|
|
|
// log_conv(conv);
|
2011-03-01 20:03:44 -06:00
|
|
|
|
2011-02-27 18:44:57 -06:00
|
|
|
n += 1u;
|
|
|
|
auto arg_expr = args.(n);
|
|
|
|
auto c_expr = make_new_conv(conv, arg_expr);
|
|
|
|
tmp_expr = make_add_expr(sp, tmp_expr, c_expr);
|
2011-02-27 17:23:16 -06: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:";
|
|
|
|
// 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:
|
|
|
|
//
|