From f28314909065893a62fe6bcaea5642e717da9d9c Mon Sep 17 00:00:00 2001 From: Sofia Date: Wed, 15 Apr 2026 18:45:29 +0300 Subject: [PATCH] Parse tl-typedef --- src/parsing.cpp | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) 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