diff options
Diffstat (limited to 'src/expand/mod.cpp')
-rw-r--r-- | src/expand/mod.cpp | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 5d6d3234..66a7308a 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -41,6 +41,12 @@ void Register_Synext_Macro_Static(MacroDef* def) { g_macros_list = def; } +void Expand_Init() +{ + // TODO: Initialise all macros here. + void Expand_init_assert(); Expand_init_assert(); +} + void ExpandDecorator::unexpected(const Span& sp, const AST::Attribute& mi, const char* loc_str) const { @@ -86,7 +92,7 @@ void Expand_Attrs(::AST::AttributeList& attrs, AttrStage stage, ::AST::Crate& c Expand_Attrs(attrs, stage, [&](const auto& sp, const auto& d, const auto& a){ d.handle(sp, a, crate, mod, impl); }); } -::std::unique_ptr<TokenStream> Expand_Macro( +::std::unique_ptr<TokenStream> Expand_Macro_Inner( const ::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::Module& mod, Span mi_span, const ::std::string& name, const ::std::string& input_ident, TokenTree& input_tt ) @@ -144,6 +150,17 @@ void Expand_Attrs(::AST::AttributeList& attrs, AttrStage stage, ::AST::Crate& c // Error - Unknown macro name ERROR(mi_span, E0000, "Unknown macro '" << name << "'"); } +::std::unique_ptr<TokenStream> Expand_Macro( + const ::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::Module& mod, + Span mi_span, const ::std::string& name, const ::std::string& input_ident, TokenTree& input_tt + ) +{ + auto rv = Expand_Macro_Inner(crate, modstack, mod, mi_span, name, input_ident, input_tt); + assert(rv); + rv->parse_state().module = &mod; + rv->parse_state().crate = &crate; + return rv; +} ::std::unique_ptr<TokenStream> Expand_Macro(const ::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::Module& mod, ::AST::MacroInvocation& mi) { return Expand_Macro(crate, modstack, mod, mi.span(), mi.name(), mi.input_ident(), mi.input_tt()); @@ -755,6 +772,7 @@ struct CExpandExpr: case ::AST::ExprNode_BinOp::RANGE_INC: { // NOTE: Not language items auto core_crate = (crate.m_load_std == ::AST::Crate::LOAD_NONE ? "" : "core"); + auto path_None = ::AST::Path(core_crate, { ::AST::PathNode("option"), ::AST::PathNode("Option"), ::AST::PathNode("None") }); auto path_RangeInclusive_NonEmpty = ::AST::Path(core_crate, { ::AST::PathNode("ops"), ::AST::PathNode("RangeInclusive") }); auto path_RangeToInclusive = ::AST::Path(core_crate, { ::AST::PathNode("ops"), ::AST::PathNode("RangeToInclusive") }); @@ -763,6 +781,7 @@ struct CExpandExpr: ::AST::ExprNode_StructLiteral::t_values values; values.push_back({ {}, "start", mv$(node.m_left) }); values.push_back({ {}, "end" , mv$(node.m_right) }); + values.push_back({ {}, "is_empty", ::AST::ExprNodeP(new ::AST::ExprNode_NamedValue(mv$(path_None))) }); replacement.reset( new ::AST::ExprNode_StructLiteral(mv$(path_RangeInclusive_NonEmpty), nullptr, mv$(values)) ); } else @@ -786,12 +805,19 @@ struct CExpandExpr: auto path_Err = ::AST::Path(core_crate, {::AST::PathNode("result"), ::AST::PathNode("Result"), ::AST::PathNode("Err")}); auto path_From = ::AST::Path(core_crate, {::AST::PathNode("convert"), ::AST::PathNode("From")}); path_From.nodes().back().args().m_types.push_back( ::TypeRef(node.span()) ); + // TODO: Lang item (needs lang items enumerated earlier) + //auto it = crate.m_lang_items.find("try"); + //ASSERT_BUG(node.span(), it != crate.m_lang_items.end(), "Can't find the `try` lang item"); + //auto path_Try = it->second; + auto path_Try = ::AST::Path(core_crate, {::AST::PathNode("ops"), ::AST::PathNode("Try")}); + auto path_Try_into_result = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(node.span()), path_Try, { ::AST::PathNode("into_result") }); + auto path_Try_from_error = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(node.span()), path_Try, { ::AST::PathNode("from_error") }); // Desugars into // ``` - // match `m_value` { + // match `Try::into_result(m_value)` { // Ok(v) => v, - // Err(e) => return Err(From::from(e)), + // Err(e) => return Try::from_error(From::from(e)), // } // ``` @@ -802,7 +828,7 @@ struct CExpandExpr: nullptr, ::AST::ExprNodeP( new ::AST::ExprNode_NamedValue( ::AST::Path(::AST::Path::TagLocal(), "v") ) ) )); - // `Err(e) => return Err(From::from(e)),` + // `Err(e) => return Try::from_error(From::from(e)),` arms.push_back(::AST::ExprNode_Match_Arm( ::make_vec1( ::AST::Pattern(::AST::Pattern::TagNamedTuple(), node.span(), path_Err, ::make_vec1( ::AST::Pattern(::AST::Pattern::TagBind(), node.span(), "e") )) ), nullptr, @@ -810,7 +836,7 @@ struct CExpandExpr: ::AST::ExprNode_Flow::RETURN, "", ::AST::ExprNodeP(new ::AST::ExprNode_CallPath( - ::AST::Path(path_Err), + ::AST::Path(path_Try_from_error), ::make_vec1( ::AST::ExprNodeP(new ::AST::ExprNode_CallPath( ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(node.span()), mv$(path_From), { ::AST::PathNode("from") }), @@ -821,7 +847,13 @@ struct CExpandExpr: )) )); - replacement.reset(new ::AST::ExprNode_Match( mv$(node.m_value), mv$(arms) )); + replacement.reset(new ::AST::ExprNode_Match( + ::AST::ExprNodeP(new AST::ExprNode_CallPath( + mv$(path_Try_into_result), + ::make_vec1( mv$(node.m_value) ) + )), + mv$(arms) + )); } } }; |