Format const in type

This commit is contained in:
Sofia 2026-04-16 19:46:47 +03:00
parent 8e6980715e
commit efb4ce85ac
3 changed files with 28 additions and 22 deletions

View File

@ -15,6 +15,7 @@ namespace parsing {
bool is_const = false;
if (inner.peek().type == token::Type::Ident && inner.peek().content == "const") {
is_const = true;
inner.next();
}
auto token = inner.expect(token::Type::Ident);

View File

@ -42,18 +42,27 @@ namespace types {
}
std::string FundamentalType::formatted() {
std::stringstream out{ "" };
if (this->m_const)
out << "const ";
switch (this->m_ty) {
case FundamentalTypeKind::Int:
return "Int";
out << "Int";
break;
case FundamentalTypeKind::Bool:
return "Bool";
out << "Bool";
break;
case FundamentalTypeKind::Char:
return "Char";
out << "Char";
break;
case FundamentalTypeKind::Void:
return "Void";
out << "Void";
break;
default:
return "Unknown";
out << "Unknown";
break;
}
return out.str();
}
std::optional<std::shared_ptr<Type>> Type::return_type() {
@ -96,6 +105,9 @@ namespace types {
std::string FunctionType::formatted() {
std::stringstream out{ "" };
if (this->m_const)
out << "const ";
out << "(";
int counter = 0;
@ -131,6 +143,9 @@ namespace types {
std::string PointerType::formatted() {
std::stringstream out{ "" };
out << this->m_inner->formatted() << "*";
if (this->m_const)
out << " const";
return out.str();
}
@ -147,6 +162,9 @@ namespace types {
std::string ArrayType::formatted() {
std::stringstream out{ "" };
if (this->m_const)
out << "const[] ";
out << this->m_inner->formatted();
out << "[" << this->m_size << "]";
return out.str();
@ -163,6 +181,9 @@ namespace types {
std::string StructType::formatted() {
std::stringstream out{ "" };
if (this->m_const)
out << "const ";
out << "struct(" << this->m_id << ")";
if (this->m_is_ref)
out << "(ref)";
@ -202,22 +223,6 @@ namespace types {
return size;
}
// std::string StructRef::formatted() {
// std::stringstream out{ "" };
// out << "struct(ref) " << this->m_name;
// return out.str();
// }
// std::pair<llvm::Value*, std::shared_ptr<Type>> StructRef::load(codegen::Builder&, llvm::Value* ptr, codegen::TypeMap&) {
// auto self = std::make_shared<StructRef>(*this);
// return std::pair(ptr, self);
// }
// uint32_t StructRef::size() {
// return this->m_referred->size();
// }
bool types_equal(std::shared_ptr<types::Type> type1, std::shared_ptr<types::Type> type2) {
if (type1->m_kind != type2->m_kind)
return false;

2
test.c
View File

@ -6,7 +6,7 @@ int fibonacci(int n) {
return fibonacci(n - 1) + fibonacci(n - 2);
}
void change_first(char* otus) {
void change_first(const char* otus) {
otus[0] = 115;
}