diff --git a/src/parsing.cpp b/src/parsing.cpp index 8c78300..255860f 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -404,7 +404,7 @@ namespace parsing { } } - Result, std::string> parse_top_level_statement(token::TokenStream& stream, Scope& scope) { + Result, std::string> parse_function(token::TokenStream& stream, Scope& scope) { token::TokenStream inner{ stream }; auto before_meta = inner.metadata(); try { @@ -492,4 +492,42 @@ namespace parsing { return std::string(error.what()); } } + + Result, std::string> parse_tl_typedef(token::TokenStream& stream, Scope& scope) { + token::TokenStream inner{ stream }; + auto before_meta = inner.metadata(); + try { + auto ty = parse_type(inner, scope).unwrap(); + + stream.m_position = inner.m_position; + auto tl_typedef = new AST::TopLevelTypedef{ + before_meta + stream.metadata(), ty + }; + return std::unique_ptr{tl_typedef}; + } + catch (std::runtime_error& error) { + return std::string(error.what()); + } + } + + Result, std::string> parse_top_level_statement(token::TokenStream& stream, Scope& scope) { + token::TokenStream inner{ stream }; + auto before_meta = inner.metadata(); + try { + if (auto func = parse_function(inner, scope); func.ok()) { + stream.m_position = inner.m_position; + return func.unwrap(); + } + else if (auto tl_typedef = parse_tl_typedef(inner, scope); tl_typedef.ok()) { + stream.m_position = inner.m_position; + return tl_typedef.unwrap(); + } + else { + throw std::runtime_error("Expected top-level statement, got " + inner.peek().formatted()); + } + } + catch (std::runtime_error& error) { + return std::string(error.what()); + } + } } \ No newline at end of file