added parsing for function literals
This commit is contained in:
35
src/ast/expressions/function.cpp
Normal file
35
src/ast/expressions/function.cpp
Normal 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
|
21
src/ast/expressions/function.hpp
Normal file
21
src/ast/expressions/function.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "ast/expressions/identifier.hpp"
|
||||
#include "ast/statements/block.hpp"
|
||||
#include "token/token.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace ast {
|
||||
struct function_literal : expression {
|
||||
function_literal(token::token);
|
||||
token::token token;
|
||||
std::vector<identifier*> parameters;
|
||||
ast::block_stmt* block;
|
||||
|
||||
std::string token_literal() const override;
|
||||
std::string str() const override;
|
||||
~function_literal();
|
||||
};
|
||||
} // namespace ast
|
@@ -59,6 +59,11 @@ namespace parser {
|
||||
std::bind(&parser::parse_if_then_else, this)
|
||||
);
|
||||
|
||||
register_prefix(
|
||||
token::type::FUNCTION,
|
||||
std::bind(&parser::parse_function, this)
|
||||
);
|
||||
|
||||
using namespace std::placeholders;
|
||||
register_infix(
|
||||
{token::type::PLUS,
|
||||
@@ -243,6 +248,39 @@ namespace parser {
|
||||
);
|
||||
};
|
||||
|
||||
ast::function_literal* parser::parse_function() {
|
||||
TRACE_FUNCTION;
|
||||
ast::function_literal* ret = new ast::function_literal(current);
|
||||
if (!expect_next(token::type::LPAREN)) {
|
||||
delete ret;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
while (next.type != token::type::RPAREN
|
||||
&& expect_next(token::type::IDENTIFIER)) {
|
||||
ret->parameters.push_back(parse_identifier());
|
||||
if (next.type == token::type::RPAREN)
|
||||
break;
|
||||
if (!expect_next(token::type::COMMA)) {
|
||||
// delete ret;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (!expect_next(token::type::RPAREN)) {
|
||||
// delete ret;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!expect_next(token::type::LBRACE)) {
|
||||
// delete ret;
|
||||
return nullptr;
|
||||
}
|
||||
ret->block = parse_block();
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
ast::prefix_expr* parser::parse_prefix_expr() {
|
||||
TRACE_FUNCTION;
|
||||
ast::prefix_expr* ret = new ast::prefix_expr(current, current.literal);
|
||||
|
Reference in New Issue
Block a user