diff --git a/src/parsing.cpp b/src/parsing.cpp index 0e6b0e6..5b52066 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -49,6 +49,25 @@ namespace parsing { } } + Result, std::string> parse_array_postfix(token::TokenStream& stream) { + token::TokenStream inner{ stream }; + try { + std::optional returned{}; + + inner.expect(token::Type::Symbol, "["); + if (inner.peek().type == token::Type::LiteralInt) { + returned = std::stoi(inner.next().content); + } + inner.expect(token::Type::Symbol, "]"); + + stream.m_position = inner.m_position; + return returned; + } + catch (std::runtime_error& error) { + return std::string{ error.what() }; + } + } + Result, std::string> parse_plain_expression(token::TokenStream& stream) { token::TokenStream inner{ stream }; try { @@ -244,6 +263,15 @@ namespace parsing { auto ty = parse_type(inner).unwrap(); auto name = inner.expect(token::Type::Ident); + auto array_postfix = parse_array_postfix(inner); + while (array_postfix.ok()) { + auto postfix = array_postfix.unwrap(); + ty = std::shared_ptr{ + new types::ArrayType(ty, postfix) + }; + array_postfix = parse_array_postfix(inner); + } + std::optional> expr = {}; if (inner.peek().type == token::Type::Symbol && inner.peek().content == "=") { inner.expect(token::Type::Symbol, "="); diff --git a/src/types.cpp b/src/types.cpp index 7235a8f..b358b02 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -153,6 +153,11 @@ namespace types { return out.str(); } + std::pair> ArrayType::load(codegen::Builder&, llvm::Value* ptr) { + auto self = std::make_shared(*this); + return std::pair(ptr, self); + } + uint32_t ArrayType::size() { if (this->m_size) { return (*this->m_size) * this->m_inner->size(); diff --git a/src/types.h b/src/types.h index 29994bf..beffead 100644 --- a/src/types.h +++ b/src/types.h @@ -93,6 +93,7 @@ namespace types { virtual ~ArrayType() override = default; virtual std::string formatted() override; virtual llvm::Type* codegen(codegen::Builder& builder) override; + virtual std::pair> load(codegen::Builder& builder, llvm::Value* ptr) override; virtual uint32_t size() override; };