Add literal strings
This commit is contained in:
parent
ac7731446e
commit
bd76e8676f
@ -9,6 +9,12 @@ namespace AST {
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string StringLiteralExpression::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << "\"" << this->m_value << "\"";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string ValueReferenceExpression::formatted() {
|
||||
return this->m_name;
|
||||
}
|
||||
|
||||
10
src/ast.h
10
src/ast.h
@ -41,6 +41,16 @@ namespace AST {
|
||||
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 {
|
||||
private:
|
||||
std::string m_name;
|
||||
|
||||
@ -22,7 +22,19 @@ namespace AST {
|
||||
|
||||
return codegen::StackValue{
|
||||
llvm::ConstantInt::get(ty, this->m_value),
|
||||
std::unique_ptr<types::Type>{stack_type}
|
||||
std::unique_ptr<types::Type>{stack_type}
|
||||
};
|
||||
}
|
||||
|
||||
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},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -59,6 +59,12 @@ namespace parsing {
|
||||
auto expr = new AST::IntLiteralExpression{ token.metadata, std::stoi(token.content) };
|
||||
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) {
|
||||
stream.m_position = inner.m_position;
|
||||
|
||||
|
||||
@ -23,6 +23,8 @@ namespace token {
|
||||
return "Symbol";
|
||||
case token::Type::LiteralInt:
|
||||
return "LiteralInt";
|
||||
case token::Type::LiteralStr:
|
||||
return "LiteralStr";
|
||||
|
||||
case token::Type::ReturnKeyword:
|
||||
return "Return";
|
||||
@ -140,6 +142,16 @@ namespace token {
|
||||
} while (std::isdigit(c));
|
||||
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)) {
|
||||
std::string content{};
|
||||
do {
|
||||
|
||||
@ -11,6 +11,7 @@ namespace token {
|
||||
Ident,
|
||||
Symbol,
|
||||
LiteralInt,
|
||||
LiteralStr,
|
||||
|
||||
ReturnKeyword,
|
||||
IfKeyword,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user