From 5917f2f50e13e450e979e6338737318eb33cf76e Mon Sep 17 00:00:00 2001 From: Mattias Ajander <mattias@ajander.se> Date: Mon, 31 Mar 2025 23:18:33 +0200 Subject: [PATCH] Changed include path, moved ProgramNode to BlockNode to be used later. --- include/ast/{ProgramNode.h => BlockNode.h} | 10 +++++++--- include/parser/Parser.h | 8 ++++---- source/ast/{ProgramNode.cc => BlockNode.cc} | 17 +++++++++++------ source/parser/Parser.cc | 6 +++--- 4 files changed, 25 insertions(+), 16 deletions(-) rename include/ast/{ProgramNode.h => BlockNode.h} (63%) rename source/ast/{ProgramNode.cc => BlockNode.cc} (60%) 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 9bc7507..dbb6944 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 008617e..add438b 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 5389f94..8893c99 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 949eaf2..2d3c518 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) -- GitLab