From 191a03b6409811c49d8656e16ba2d3a58040b69c Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 10 Mar 2016 14:39:19 +0800 Subject: Expand - Stub format_args! impl --- src/expand/format_args.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/expand/format_args.cpp (limited to 'src/expand') diff --git a/src/expand/format_args.cpp b/src/expand/format_args.cpp new file mode 100644 index 00000000..0c809601 --- /dev/null +++ b/src/expand/format_args.cpp @@ -0,0 +1,53 @@ +/* + */ +#include +#include "../parse/common.hpp" +#include "../parse/parseerror.hpp" +#include "../parse/tokentree.hpp" +#include "../parse/lex.hpp" + +class CFormatArgsExpander: + public ExpandProcMacro +{ + bool expand_early() const override { return true; } + + ::std::unique_ptr expand(Span sp, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override + { + Token tok; + + auto lex = TTStream(tt); + if( ident != "" ) + ERROR(sp, E0000, "format_args! doesn't take an ident"); + + GET_CHECK_TOK(tok, lex, TOK_STRING); + auto format_string = mv$(tok.str()); + + // TODO: Interpolated expression "tokens" + ::std::map< ::std::string, TokenTree> named_args; + ::std::vector free_args; + + while( GET_TOK(tok, lex) == TOK_COMMA ) + { + if( lex.lookahead(0) == TOK_IDENT && lex.lookahead(1) == TOK_EQUAL ) + { + GET_CHECK_TOK(tok, lex, TOK_IDENT); + auto name = mv$(tok.str()); + GET_CHECK_TOK(tok, lex, TOK_EQUAL); + auto expr_tt = Parse_TT_Expr(lex); + + named_args.insert( ::std::make_pair(mv$(name), mv$(expr_tt)) ); + } + else + { + auto expr_tt = Parse_TT_Expr(lex); + free_args.push_back( mv$(expr_tt) ); + } + } + + // TODO: Expand format_args! + return box$( TTStreamO(TokenTree(::std::vector{TokenTree(TOK_PAREN_OPEN), TokenTree(TOK_PAREN_CLOSE)})) ); + } +}; + +STATIC_MACRO("format_args", CFormatArgsExpander); + -- cgit v1.2.3