Add literal strings

This commit is contained in:
Sofia 2026-04-11 22:03:38 +03:00
parent ac7731446e
commit bd76e8676f
7 changed files with 49 additions and 1 deletions

View File

@ -9,6 +9,12 @@ namespace AST {
return out.str(); return out.str();
} }
std::string StringLiteralExpression::formatted() {
std::stringstream out{ "" };
out << "\"" << this->m_value << "\"";
return out.str();
}
std::string ValueReferenceExpression::formatted() { std::string ValueReferenceExpression::formatted() {
return this->m_name; return this->m_name;
} }

View File

@ -41,6 +41,16 @@ namespace AST {
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override; virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
}; };
class StringLiteralExpression : public Expression {
private:
std::string m_value;
public:
StringLiteralExpression(token::Metadata meta, std::string value) : Expression{ meta }, m_value{ value } {}
virtual ~StringLiteralExpression() override = default;
virtual std::string formatted() override;
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
};
class ValueReferenceExpression : public Expression { class ValueReferenceExpression : public Expression {
private: private:
std::string m_name; std::string m_name;

View File

@ -26,6 +26,18 @@ namespace AST {
}; };
} }
codegen::StackValue StringLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope&) {
auto stack_type = new types::PointerType{ std::make_unique<types::FundamentalType>(types::FundamentalTypeKind::Char) };
auto str = llvm::StringRef{ this->m_value.c_str() };
return codegen::StackValue{
builder.builder->CreateGlobalString(str),
std::unique_ptr<types::Type>{stack_type},
};
}
codegen::StackValue ValueReferenceExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) { codegen::StackValue ValueReferenceExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
auto value = scope.values.find(this->m_name); auto value = scope.values.find(this->m_name);
if (value != scope.values.end()) { if (value != scope.values.end()) {

View File

@ -59,6 +59,12 @@ namespace parsing {
auto expr = new AST::IntLiteralExpression{ token.metadata, std::stoi(token.content) }; auto expr = new AST::IntLiteralExpression{ token.metadata, std::stoi(token.content) };
return std::unique_ptr<AST::Expression>{ expr }; return std::unique_ptr<AST::Expression>{ expr };
} }
else if (token.type == token::Type::LiteralStr) {
stream.m_position = inner.m_position;
auto expr = new AST::StringLiteralExpression{ token.metadata, token.content };
return std::unique_ptr<AST::Expression>{ expr };
}
else if (token.type == token::Type::Ident) { else if (token.type == token::Type::Ident) {
stream.m_position = inner.m_position; stream.m_position = inner.m_position;

View File

@ -23,6 +23,8 @@ namespace token {
return "Symbol"; return "Symbol";
case token::Type::LiteralInt: case token::Type::LiteralInt:
return "LiteralInt"; return "LiteralInt";
case token::Type::LiteralStr:
return "LiteralStr";
case token::Type::ReturnKeyword: case token::Type::ReturnKeyword:
return "Return"; return "Return";
@ -140,6 +142,16 @@ namespace token {
} while (std::isdigit(c)); } while (std::isdigit(c));
tokens.push_back(token::Token{ token::Type::LiteralInt, content, meta + content.size() }); tokens.push_back(token::Token{ token::Type::LiteralInt, content, meta + content.size() });
} }
else if (c == '\"') {
std::string content{};
c = text[++i]; // Skip initial "
do {
content += c;
c = text[++i];
} while (c != '\"');
i++; // Skip second "
tokens.push_back(token::Token{ token::Type::LiteralStr, content, meta + (content.size() + 2) });
}
else if (std::isalpha(c)) { else if (std::isalpha(c)) {
std::string content{}; std::string content{};
do { do {

View File

@ -11,6 +11,7 @@ namespace token {
Ident, Ident,
Symbol, Symbol,
LiteralInt, LiteralInt,
LiteralStr,
ReturnKeyword, ReturnKeyword,
IfKeyword, IfKeyword,

1
test.c
View File

@ -7,5 +7,6 @@ int fibonacci(int n) {
} }
int main() { int main() {
printf("hello world!");
return fibonacci(10); return fibonacci(10);
} }