diff --git a/src/ast.cpp b/src/ast.cpp index a97a72a..76a8045 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -11,6 +11,16 @@ namespace AST { return out.str(); } + std::string InitializationStatement::formatted() { + std::stringstream out{ "" }; + out << this->m_type->formatted() << " " << this->m_name; + if (this->m_expr) { + out << " = " << this->m_expr->get()->formatted(); + } + out << ";"; + return out.str(); + } + std::string IntLiteralExpression::formatted() { std::stringstream out{ "" }; out << this->m_value; diff --git a/src/ast.h b/src/ast.h index ba0d174..8bd0a1f 100644 --- a/src/ast.h +++ b/src/ast.h @@ -52,6 +52,20 @@ namespace AST { virtual void codegen(codegen::Builder& builder) override; }; + class InitializationStatement : public Statement { + private: + std::unique_ptr m_type; + std::string m_name; + std::optional> m_expr; + public: + InitializationStatement(std::unique_ptr ty, std::string name, std::optional> expr) + : m_type{ std::move(ty) }, m_name{ name }, m_expr{ std::move(expr) } { + } + virtual ~InitializationStatement() override = default; + virtual std::string formatted() override; + virtual void codegen(codegen::Builder& builder) override; + }; + class TopLevelStatement : public Node { public: virtual void codegen(codegen::Builder& builder) = 0; diff --git a/src/codegen.cpp b/src/codegen.cpp index f6a9bd9..783c075 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -22,6 +22,10 @@ namespace AST { builder.builder->CreateRet(value); } + void InitializationStatement::codegen(codegen::Builder& builder) { + return; + } + void Function::codegen(codegen::Builder& builder) { auto ret_ty = this->m_return_ty->codegen(builder); std::vector params{};