diff --git a/include/ast/ProgramNode.h b/include/ast/BlockNode.h similarity index 63% rename from include/ast/ProgramNode.h rename to include/ast/BlockNode.h index 9bc7507908a67a5ebf3ec99508155fb146eb21ee..dbb6944bf78fcfea42ba840b18220c4ca3e0a60d 100644 --- a/include/ast/ProgramNode.h +++ b/include/ast/BlockNode.h @@ -1,15 +1,18 @@ #pragma once + #include "ast/Node.h" namespace funk { -class ProgramNode : public Node + +class BlockNode : public Node { public: - ProgramNode(const SourceLocation& loc); - ~ProgramNode(); + BlockNode(const SourceLocation& loc); + ~BlockNode(); void add(Node* statement); + Vector<Node*> get_statements() const; Node* evaluate() const override; String to_s() const override; @@ -17,4 +20,5 @@ public: private: Vector<Node*> statements; }; + } // namespace funk diff --git a/include/parser/Parser.h b/include/parser/Parser.h index 008617eba55ebc92012bc349cfa9776497c4d279..add438b6e88bbb8564f2a737a37d4f672ab084b7 100644 --- a/include/parser/Parser.h +++ b/include/parser/Parser.h @@ -10,11 +10,11 @@ #include "token/Token.h" #include "utils/Common.h" -#include "ast/BinaryOpNode.h" -#include "ast/LiteralNode.h" +#include "ast/BlockNode.h" #include "ast/Node.h" -#include "ast/ProgramNode.h" -#include "ast/UnaryOpNode.h" +#include "ast/expression/BinaryOpNode.h" +#include "ast/expression/LiteralNode.h" +#include "ast/expression/UnaryOpNode.h" namespace funk { diff --git a/source/ast/ProgramNode.cc b/source/ast/BlockNode.cc similarity index 60% rename from source/ast/ProgramNode.cc rename to source/ast/BlockNode.cc index 5389f9483123eb35812bde2b88cd89721f998996..8893c999e0fdfa49bcf76090875e9eba1d5bc689 100644 --- a/source/ast/ProgramNode.cc +++ b/source/ast/BlockNode.cc @@ -1,20 +1,25 @@ -#include "ast/ProgramNode.h" +#include "ast/BlockNode.h" namespace funk { -ProgramNode::ProgramNode(const SourceLocation& loc) : Node(loc), statements{} {} +BlockNode::BlockNode(const SourceLocation& loc) : Node(loc), statements{} {} -ProgramNode::~ProgramNode() +BlockNode::~BlockNode() { for (Node* statement : statements) { delete statement; } } -void ProgramNode::add(Node* statement) +void BlockNode::add(Node* statement) { statements.push_back(statement); } -Node* ProgramNode::evaluate() const +Vector<Node*> BlockNode::get_statements() const +{ + return statements; +} + +Node* BlockNode::evaluate() const { if (cached_eval) { return cached_eval; } @@ -23,7 +28,7 @@ Node* ProgramNode::evaluate() const return cached_eval = result; } -String ProgramNode::to_s() const +String BlockNode::to_s() const { String repr{}; diff --git a/source/parser/Parser.cc b/source/parser/Parser.cc index 949eaf2e90ef4ca1a04375c05700ebed4955a81b..2d3c518b50405f44a60b36f684ea0dad6b4e58ad 100644 --- a/source/parser/Parser.cc +++ b/source/parser/Parser.cc @@ -11,9 +11,9 @@ Node* Parser::parse() { LOG_DEBUG("Parse program"); - ProgramNode* program = new ProgramNode(SourceLocation(filename, 0, 0)); - while (!done()) { program->add(parse_statement()); } - return program; + BlockNode* block = new BlockNode(SourceLocation(filename, 0, 0)); + while (!done()) { block->add(parse_statement()); } + return block; } Parser Parser::load(String filename)