added parsing for function literals

This commit is contained in:
Karma Riuk
2025-07-15 00:56:12 +02:00
parent 85f530f5f9
commit 0b2a12cef7
4 changed files with 228 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
#include "function.hpp"
#include <sstream>
namespace ast {
function_literal::function_literal(token::token token)
: token(std::move(token)),
block(nullptr) {};
std::string function_literal::token_literal() const {
return token.literal;
};
std::string function_literal::str() const {
std::stringstream ss;
ss << "fn (";
bool first = true;
for (auto& param : parameters) {
if (!first)
ss << ", ";
ss << param->str();
first = false;
}
ss << ")";
ss << block->str();
return ss.str();
};
function_literal::~function_literal() {
for (auto& param : parameters)
delete param;
if (block != nullptr)
delete block;
}
} // namespace ast