Add expression statements
This commit is contained in:
parent
48feebc714
commit
4d307d0eb9
@ -3,6 +3,13 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
namespace AST {
|
namespace AST {
|
||||||
|
std::string ExpressionStatement::formatted() {
|
||||||
|
std::stringstream out{ "" };
|
||||||
|
out << this->m_expr->formatted();
|
||||||
|
out << ";";
|
||||||
|
return out.str();
|
||||||
|
}
|
||||||
|
|
||||||
std::string ReturnStatement::formatted() {
|
std::string ReturnStatement::formatted() {
|
||||||
std::stringstream out{ "" };
|
std::stringstream out{ "" };
|
||||||
out << "return ";
|
out << "return ";
|
||||||
|
|||||||
12
src/ast.h
12
src/ast.h
@ -76,6 +76,18 @@ namespace AST {
|
|||||||
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ExpressionStatement : public Statement {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Expression> m_expr;
|
||||||
|
public:
|
||||||
|
ExpressionStatement(std::unique_ptr<Expression> expr)
|
||||||
|
: m_expr{ std::move(expr) } {
|
||||||
|
}
|
||||||
|
virtual ~ExpressionStatement() override = default;
|
||||||
|
virtual std::string formatted() override;
|
||||||
|
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
||||||
|
};
|
||||||
|
|
||||||
class TopLevelStatement : public Node {
|
class TopLevelStatement : public Node {
|
||||||
public:
|
public:
|
||||||
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) = 0;
|
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) = 0;
|
||||||
|
|||||||
@ -32,6 +32,14 @@ namespace AST {
|
|||||||
builder.builder->CreateRet(value);
|
builder.builder->CreateRet(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ExpressionStatement::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||||
|
if (!builder.block)
|
||||||
|
return;
|
||||||
|
|
||||||
|
builder.builder->SetInsertPoint(builder.block);
|
||||||
|
this->m_expr->codegen(builder, scope);
|
||||||
|
}
|
||||||
|
|
||||||
void InitializationStatement::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
void InitializationStatement::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||||
if (!builder.block)
|
if (!builder.block)
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -58,7 +58,8 @@ namespace parsing {
|
|||||||
inner.expect(token::Type::Symbol, ";");
|
inner.expect(token::Type::Symbol, ";");
|
||||||
|
|
||||||
stream.m_position = inner.m_position;
|
stream.m_position = inner.m_position;
|
||||||
return &std::make_unique<AST::InitializationStatement>(std::move(ty), name.content, std::move(expr));
|
auto init = new AST::InitializationStatement{ std::move(ty), name.content, std::move(expr) };
|
||||||
|
return new std::unique_ptr<AST::InitializationStatement>{ init };
|
||||||
}
|
}
|
||||||
catch (std::runtime_error error) {
|
catch (std::runtime_error error) {
|
||||||
return new std::string{ error.what() };
|
return new std::string{ error.what() };
|
||||||
@ -79,10 +80,16 @@ namespace parsing {
|
|||||||
return new std::unique_ptr<AST::Statement>{ ret };
|
return new std::unique_ptr<AST::Statement>{ ret };
|
||||||
}
|
}
|
||||||
else if (inner.peek().type == token::Type::Ident) {
|
else if (inner.peek().type == token::Type::Ident) {
|
||||||
auto init = parse_init_statement(inner);
|
if (auto init = parse_init_statement(inner); init.ok()) {
|
||||||
if (init.ok()) {
|
stream.m_position = inner.m_position;
|
||||||
return new std::unique_ptr<AST::Statement>{ init.unwrap() };
|
return new std::unique_ptr<AST::Statement>{ init.unwrap() };
|
||||||
}
|
}
|
||||||
|
else if (auto expr = parse_expression(inner); expr.ok()) {
|
||||||
|
stream.m_position = inner.m_position;
|
||||||
|
stream.expect(token::Type::Symbol, ";");
|
||||||
|
auto expr_statement = new AST::ExpressionStatement{ expr.unwrap() };
|
||||||
|
return new std::unique_ptr<AST::Statement>{ expr_statement };
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
throw std::runtime_error("Expected initialization statement");
|
throw std::runtime_error("Expected initialization statement");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user