Add pointer types

This commit is contained in:
Sofia 2026-04-11 21:45:54 +03:00
parent 6855360a97
commit ac7731446e
3 changed files with 18 additions and 7 deletions

View File

@ -12,26 +12,37 @@ namespace parsing {
try { try {
auto token = inner.expect(token::Type::Ident); auto token = inner.expect(token::Type::Ident);
stream.m_position = inner.m_position;
// TODO eventually make this be potentially more than one word // TODO eventually make this be potentially more than one word
std::string type_name = token.content; std::string type_name = token.content;
std::unique_ptr<types::Type> returned{};
if (type_name == "int") { if (type_name == "int") {
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Int }; auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Int };
return std::unique_ptr<types::Type>{ ty }; returned = std::unique_ptr<types::Type>{ ty };
} }
else if (type_name == "char") { else if (type_name == "char") {
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Char }; auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Char };
return std::unique_ptr<types::Type>{ ty }; returned = std::unique_ptr<types::Type>{ ty };
} }
else if (type_name == "void") { else if (type_name == "void") {
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Void }; auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Void };
return std::unique_ptr<types::Type>{ ty }; returned = std::unique_ptr<types::Type>{ ty };
} }
else { else {
throw std::runtime_error("Expected type name, got " + type_name); throw std::runtime_error("Expected type name, got " + type_name);
} }
while (inner.peek().type == token::Type::Symbol && inner.peek().content == "*") {
inner.next();
auto ty = new types::PointerType{ std::move(returned) };
returned = std::unique_ptr<types::Type>{ ty };
}
stream.m_position = inner.m_position;
return std::move(returned);
} }
catch (std::runtime_error& error) { catch (std::runtime_error& error) {
return std::string{ error.what() }; return std::string{ error.what() };

View File

@ -140,7 +140,7 @@ namespace types {
std::string PointerType::formatted() { std::string PointerType::formatted() {
std::stringstream out{ "" }; std::stringstream out{ "" };
out << this->m_inner << "*"; out << this->m_inner->formatted() << "*";
return out.str(); return out.str();
} }

2
test.c
View File

@ -1,4 +1,4 @@
void printf(char b); void printf(char* b);
int fibonacci(int n) { int fibonacci(int n) {
if (n < 2) if (n < 2)