2021-04-24 14:39:43 +01:00
|
|
|
/*
|
|
|
|
* Serene programming language.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serene/exprs/fn.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-04-24 14:39:43 +01:00
|
|
|
#include "serene/errors/error.h"
|
|
|
|
#include "serene/exprs/expression.h"
|
|
|
|
#include "serene/exprs/list.h"
|
|
|
|
#include "serene/exprs/symbol.h"
|
2021-04-24 20:18:33 +01:00
|
|
|
#include "serene/reader/semantics.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-05-09 12:21:26 +01:00
|
|
|
#include "llvm/Support/Casting.h"
|
2021-04-24 14:39:43 +01:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
|
|
|
|
namespace serene {
|
|
|
|
namespace exprs {
|
|
|
|
|
2021-06-01 20:47:46 +01:00
|
|
|
Fn::Fn(SereneContext &ctx, reader::LocationRange &loc, List &args, Ast body)
|
|
|
|
: Expression(loc), args(args), body(body) {
|
|
|
|
this->setName(
|
|
|
|
llvm::formatv("___fn___{0}", ctx.getCurrentNS()->nextFnCounter()));
|
|
|
|
};
|
|
|
|
|
2021-04-24 14:39:43 +01:00
|
|
|
ExprType Fn::getType() const { return ExprType::Fn; };
|
|
|
|
|
|
|
|
std::string Fn::toString() const {
|
|
|
|
return llvm::formatv("<Fn {0} {1} to {2}>",
|
|
|
|
this->name.empty() ? "Anonymous" : this->name,
|
|
|
|
this->args.toString(),
|
|
|
|
this->body.empty() ? "<>" : astToString(&this->body));
|
|
|
|
}
|
|
|
|
|
2021-05-09 12:21:26 +01:00
|
|
|
MaybeNode Fn::analyze(SereneContext &ctx) { return EmptyNode; };
|
2021-04-24 14:39:43 +01:00
|
|
|
|
|
|
|
bool Fn::classof(const Expression *e) { return e->getType() == ExprType::Fn; };
|
|
|
|
|
2021-06-01 20:47:46 +01:00
|
|
|
void Fn::setName(std::string n) { this->name = n; };
|
|
|
|
|
2021-04-25 23:05:21 +01:00
|
|
|
MaybeNode Fn::make(SereneContext &ctx, List *list) {
|
2021-04-24 14:39:43 +01:00
|
|
|
// TODO: Add support for docstring as the 3rd argument (4th element)
|
|
|
|
if (list->count() < 2) {
|
2021-05-06 19:17:57 +01:00
|
|
|
return makeErrorful<Node>(list->elements[0]->location,
|
|
|
|
&errors::FnNoArgsList,
|
|
|
|
"The argument list is mandatory.");
|
2021-04-24 14:39:43 +01:00
|
|
|
}
|
|
|
|
|
2021-04-24 18:04:04 +01:00
|
|
|
Symbol *fnSym = llvm::dyn_cast<Symbol>(list->elements[0].get());
|
|
|
|
assert((fnSym && fnSym->name == "fn") &&
|
|
|
|
"The first element of the list should be a 'fn'.");
|
|
|
|
|
2021-04-24 14:39:43 +01:00
|
|
|
List *args = llvm::dyn_cast<List>(list->elements[1].get());
|
|
|
|
|
|
|
|
if (!args) {
|
|
|
|
std::string msg =
|
|
|
|
llvm::formatv("Arguments of a function has to be a list, got '{0}'",
|
|
|
|
stringifyExprType(list->elements[1]->getType()));
|
2021-05-06 19:17:57 +01:00
|
|
|
return makeErrorful<Node>(list->elements[1]->location,
|
|
|
|
&errors::FnArgsMustBeList, msg);
|
2021-04-24 14:39:43 +01:00
|
|
|
}
|
|
|
|
|
2021-04-25 23:07:08 +01:00
|
|
|
Ast body;
|
2021-04-24 14:39:43 +01:00
|
|
|
|
|
|
|
if (list->count() > 2) {
|
2021-06-13 22:58:46 +01:00
|
|
|
body = std::vector<Node>(list->begin() + 2, list->end());
|
2021-04-24 20:18:33 +01:00
|
|
|
auto maybeAst = reader::analyze(ctx, body);
|
|
|
|
|
|
|
|
if (!maybeAst) {
|
2021-05-06 19:17:57 +01:00
|
|
|
return MaybeNode::error(std::move(maybeAst.getError()));
|
2021-04-24 20:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
body = maybeAst.getValue();
|
2021-04-24 14:39:43 +01:00
|
|
|
}
|
|
|
|
|
2021-06-01 20:47:46 +01:00
|
|
|
return makeSuccessfulNode<Fn>(ctx, list->location, *args, body);
|
2021-04-24 14:39:43 +01:00
|
|
|
};
|
|
|
|
} // namespace exprs
|
|
|
|
} // namespace serene
|