diff options
author | John Hodge (bugs) <tpg@mutabah.net> | 2017-05-14 12:01:45 +0800 |
---|---|---|
committer | John Hodge (bugs) <tpg@mutabah.net> | 2017-05-14 12:01:45 +0800 |
commit | 1d2fe7681219700998c8ecbdb8ed5acab66578df (patch) | |
tree | 0d38e8ab5fd80c890d3dec67a0596abfc7a4f0cd /src/expand/format_args.cpp | |
parent | c6fca061dd134068c831aefd88d9535a30f423ed (diff) | |
parent | fde22b3f03d802231985b8ded567cba16cb5aa00 (diff) | |
download | mrust-1d2fe7681219700998c8ecbdb8ed5acab66578df.tar.gz |
Merge branch 'master' of https://github.com/thepowersgang/mrustc
# Conflicts:
# src/common.hpp
# src/hir/deserialise.cpp
# src/hir_typeck/static.cpp
# src/mir/from_hir.cpp
# src/mir/from_hir.hpp
# src/mir/from_hir_match.cpp
# src/mir/helpers.hpp
# src/mir/mir_builder.cpp
Diffstat (limited to 'src/expand/format_args.cpp')
-rw-r--r-- | src/expand/format_args.cpp | 124 |
1 files changed, 118 insertions, 6 deletions
diff --git a/src/expand/format_args.cpp b/src/expand/format_args.cpp index deda6968..92afcbb2 100644 --- a/src/expand/format_args.cpp +++ b/src/expand/format_args.cpp @@ -140,6 +140,11 @@ namespace { if( *s != '{' ) { if( *s == '}' ) { + s ++; + if( *s != '}' ) { + // TODO: Error? Warning? + s --; // Step backwards, just in case + } // Doesn't need escaping cur_literal += '}'; } @@ -247,6 +252,7 @@ namespace { if( *s == '0' ) { args.zero_pad = true; + args.align_char = '0'; s ++; } else { @@ -407,6 +413,24 @@ namespace { toks.push_back( Token(TOK_IDENT, ent) ); } } + void push_toks(::std::vector<TokenTree>& toks, Token t1) { + toks.push_back( mv$(t1) ); + } + void push_toks(::std::vector<TokenTree>& toks, Token t1, Token t2) { + toks.push_back( mv$(t1) ); + toks.push_back( mv$(t2) ); + } + //void push_toks(::std::vector<TokenTree>& toks, Token t1, Token t2, Token t3) { + // toks.push_back( mv$(t1) ); + // toks.push_back( mv$(t2) ); + // toks.push_back( mv$(t3) ); + //} + void push_toks(::std::vector<TokenTree>& toks, Token t1, Token t2, Token t3, Token t4) { + toks.push_back( mv$(t1) ); + toks.push_back( mv$(t2) ); + toks.push_back( mv$(t3) ); + toks.push_back( mv$(t4) ); + } } class CFormatArgsExpander: @@ -576,10 +600,12 @@ class CFormatArgsExpander: } else // if(is_simple) { + // 1. Generate a set of arguments+formatters + // > Each combination of argument index and fragment type needs a unique entry in the `args` array + // Use new_v1_formatted // - requires creating more entries in the `args` list to cover multiple formatters for one value - //push_path(toks, crate, {"fmt", "Arguments", "new_v1_formatted"}); - push_path(toks, crate, {"fmt", "Arguments", "new_v1"}); + push_path(toks, crate, {"fmt", "Arguments", "new_v1_formatted"}); // ( toks.push_back( TokenTree(TOK_PAREN_OPEN) ); { @@ -587,14 +613,100 @@ class CFormatArgsExpander: toks.push_back( Token(TOK_IDENT, "FRAGMENTS") ); toks.push_back( TokenTree(TOK_COMMA) ); - // 1. Generate a set of arguments+formatters - // TODO: Fragments to format // - The format stored by mrustc doesn't quite work with how rustc (and fmt::rt::v1) works toks.push_back( TokenTree(TOK_AMP) ); toks.push_back( TokenTree(TOK_SQUARE_OPEN) ); - //for(const auto& frag : fragments ) { - //} + for(const auto& frag : fragments ) + { + push_path(toks, crate, {"fmt", "ArgumentV1", "new"}); + toks.push_back( Token(TOK_PAREN_OPEN) ); + toks.push_back( Token(TOK_IDENT, FMT("a" << frag.arg_index)) ); + + toks.push_back( TokenTree(TOK_COMMA) ); + + push_path(toks, crate, {"fmt", frag.trait_name, "fmt"}); + toks.push_back( TokenTree(TOK_PAREN_CLOSE) ); + toks.push_back( TokenTree(TOK_COMMA) ); + } + toks.push_back( TokenTree(TOK_SQUARE_CLOSE) ); + toks.push_back( TokenTree(TOK_COMMA) ); + + toks.push_back( TokenTree(TOK_AMP) ); + toks.push_back( TokenTree(TOK_SQUARE_OPEN) ); + for(const auto& frag : fragments) + { + push_path(toks, crate, {"fmt", "rt", "v1", "Argument"}); + toks.push_back( TokenTree(TOK_BRACE_OPEN) ); + + push_toks(toks, Token(TOK_IDENT, "position"), TOK_COLON ); + push_path(toks, crate, {"fmt", "rt", "v1", "Position", "Next"}); + push_toks(toks, TOK_COMMA); + + push_toks(toks, Token(TOK_IDENT, "format"), TOK_COLON ); + push_path(toks, crate, {"fmt", "rt", "v1", "FormatSpec"}); + toks.push_back( TokenTree(TOK_BRACE_OPEN) ); + { + push_toks(toks, Token(TOK_IDENT, "fill"), TOK_COLON, Token(uint64_t(frag.args.align_char), CORETYPE_CHAR), TOK_COMMA ); + + push_toks(toks, Token(TOK_IDENT, "align"), TOK_COLON); + const char* align_var_name = nullptr; + switch( frag.args.align ) + { + case FmtArgs::Align::Unspec: align_var_name = "Unknown"; break; + case FmtArgs::Align::Left: align_var_name = "Left"; break; + case FmtArgs::Align::Center: align_var_name = "Center"; break; + case FmtArgs::Align::Right: align_var_name = "Right"; break; + } + push_path(toks, crate, {"fmt", "rt", "v1", "Alignment", align_var_name}); + push_toks(toks, TOK_COMMA); + + push_toks(toks, Token(TOK_IDENT, "flags"), TOK_COLON); + uint64_t flags = 0; + if(frag.args.alternate) + flags |= 1 << 2; + push_toks(toks, Token(uint64_t(flags), CORETYPE_U32)); + push_toks(toks, TOK_COMMA); + + push_toks(toks, Token(TOK_IDENT, "precision"), TOK_COLON ); + if( frag.args.prec_is_arg || frag.args.prec != 0 ) { + push_path(toks, crate, {"fmt", "rt", "v1", "Count", "Is"}); + push_toks(toks, TOK_PAREN_OPEN); + if( frag.args.prec_is_arg ) { + push_toks(toks, TOK_STAR, Token(TOK_IDENT, FMT("a" << frag.args.prec)) ); + } + else { + push_toks(toks, Token(uint64_t(frag.args.prec), CORETYPE_UINT) ); + } + toks.push_back( TokenTree(TOK_PAREN_CLOSE) ); + } + else { + push_path(toks, crate, {"fmt", "rt", "v1", "Count", "Implied"}); + } + toks.push_back( TokenTree(TOK_COMMA) ); + + push_toks(toks, Token(TOK_IDENT, "width"), TOK_COLON ); + if( frag.args.width_is_arg || frag.args.width != 0 ) { + push_path(toks, crate, {"fmt", "rt", "v1", "Count", "Is"}); + push_toks(toks, TOK_PAREN_OPEN); + if( frag.args.width_is_arg ) { + push_toks(toks, TOK_STAR, Token(TOK_IDENT, FMT("a" << frag.args.width)) ); + } + else { + push_toks(toks, Token(uint64_t(frag.args.width), CORETYPE_UINT) ); + } + toks.push_back( TokenTree(TOK_PAREN_CLOSE) ); + } + else { + push_path(toks, crate, {"fmt", "rt", "v1", "Count", "Implied"}); + } + toks.push_back( TokenTree(TOK_COMMA) ); + } + toks.push_back( TokenTree(TOK_BRACE_CLOSE) ); + + toks.push_back( TokenTree(TOK_BRACE_CLOSE) ); + toks.push_back( TokenTree(TOK_COMMA) ); + } toks.push_back( TokenTree(TOK_SQUARE_CLOSE) ); } // ) |