2021-04-25 22:56:31 +01:00
|
|
|
/*
|
2021-10-12 20:51:03 +01:00
|
|
|
* Serene Programming Language
|
2021-04-25 22:56:31 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
2021-04-25 22:56:31 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 2.
|
2021-04-25 22:56:31 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2021-04-25 22:56:31 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-04-25 22:56:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serene/exprs/call.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-04-25 22:56:31 +01:00
|
|
|
#include "serene/errors/error.h"
|
2021-05-09 12:21:26 +01:00
|
|
|
#include "serene/exprs/def.h"
|
2021-04-25 22:56:31 +01:00
|
|
|
#include "serene/exprs/expression.h"
|
|
|
|
#include "serene/exprs/list.h"
|
|
|
|
#include "serene/exprs/symbol.h"
|
|
|
|
#include "serene/reader/semantics.h"
|
2021-09-27 13:05:15 +01:00
|
|
|
#include "serene/utils.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-09-27 13:05:15 +01:00
|
|
|
#include <llvm/Support/Casting.h>
|
|
|
|
#include <llvm/Support/ErrorHandling.h>
|
|
|
|
#include <llvm/Support/FormatVariadic.h>
|
2021-04-25 22:56:31 +01:00
|
|
|
|
|
|
|
namespace serene {
|
|
|
|
namespace exprs {
|
|
|
|
|
|
|
|
ExprType Call::getType() const { return ExprType::Call; };
|
|
|
|
|
|
|
|
std::string Call::toString() const {
|
|
|
|
return llvm::formatv("<Call {0} {1}>", this->target->toString(),
|
|
|
|
astToString(&this->params));
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:05:15 +01:00
|
|
|
MaybeNode Call::analyze(SereneContext &ctx) {
|
|
|
|
UNUSED(ctx);
|
|
|
|
return EmptyNode;
|
|
|
|
};
|
2021-04-25 22:56:31 +01:00
|
|
|
|
|
|
|
bool Call::classof(const Expression *e) {
|
|
|
|
return e->getType() == ExprType::Call;
|
|
|
|
};
|
|
|
|
|
2021-05-06 19:17:57 +01:00
|
|
|
MaybeNode Call::make(SereneContext &ctx, List *list) {
|
2021-08-07 17:41:19 +01:00
|
|
|
// TODO: replace this with a runtime check
|
2021-05-09 12:21:26 +01:00
|
|
|
assert((list->count() != 0) && "Empty call? Seriously ?");
|
2021-04-25 22:56:31 +01:00
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
// Let's find out what is the first element of the list
|
2021-04-25 22:56:31 +01:00
|
|
|
auto maybeFirst = list->elements[0]->analyze(ctx);
|
|
|
|
|
|
|
|
if (!maybeFirst) {
|
2021-05-08 19:00:24 +01:00
|
|
|
// There's something wrong with the first element. Return the error
|
|
|
|
return maybeFirst;
|
2021-04-25 22:56:31 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
Node first = maybeFirst.getValue();
|
2021-05-09 21:50:56 +01:00
|
|
|
|
|
|
|
// No rewrite is needed for the first element
|
|
|
|
if (!first) {
|
|
|
|
first = list->elements[0];
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
Node targetNode;
|
2021-05-09 21:50:56 +01:00
|
|
|
Ast rawParams;
|
|
|
|
|
|
|
|
if (list->count() > 1) {
|
|
|
|
rawParams = list->from(1);
|
|
|
|
}
|
2021-05-08 19:00:24 +01:00
|
|
|
|
|
|
|
// We need to create the Call node based on the type of the first
|
|
|
|
// element after it being analyzed.
|
2021-04-25 22:56:31 +01:00
|
|
|
switch (first->getType()) {
|
2021-05-08 19:00:24 +01:00
|
|
|
|
|
|
|
// In case of a Symbol, We should look it up in the current scope and
|
|
|
|
// if it resolves to a value. Then we have to make sure that the
|
|
|
|
// return value is callable.
|
2021-04-25 22:56:31 +01:00
|
|
|
case ExprType::Symbol: {
|
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
auto *sym = llvm::dyn_cast<Symbol>(first.get());
|
2021-05-09 12:21:26 +01:00
|
|
|
|
2021-10-17 14:33:16 +01:00
|
|
|
if (sym == nullptr) {
|
2021-05-09 12:21:26 +01:00
|
|
|
llvm_unreachable("Couldn't case to Symbol while the type is symbol!");
|
|
|
|
}
|
2021-08-07 21:09:18 +01:00
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
// TODO: Lookup the symbol in the namespace via a method that looks
|
|
|
|
// into the current environment.
|
2021-10-13 11:27:54 +01:00
|
|
|
auto maybeResult = ctx.getCurrentNS().semanticEnv.lookup(sym->name);
|
2021-05-08 19:00:24 +01:00
|
|
|
|
2021-05-24 21:17:33 +01:00
|
|
|
if (!maybeResult.hasValue()) {
|
2021-05-09 12:21:26 +01:00
|
|
|
std::string msg =
|
2021-05-24 21:17:33 +01:00
|
|
|
llvm::formatv("Can't resolve the symbol '{0}'", sym->name);
|
2021-10-24 12:34:20 +01:00
|
|
|
return makeErrorful<Node>(sym->location, errors::CantResolveSymbol, msg);
|
2021-05-08 19:00:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
targetNode = maybeResult.getValue();
|
2021-04-25 22:56:31 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-09 12:21:26 +01:00
|
|
|
case ExprType::Def:
|
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
// If the first element was a Call itself we need to just chain it
|
|
|
|
// with a new call. It would be something like `((blah 1) 4)`. `blah`
|
|
|
|
// should return a callable expression itself, which we need to let
|
|
|
|
// the typechecker to check
|
|
|
|
case ExprType::Call:
|
|
|
|
// If the first element was a function, then just use it as the target
|
|
|
|
// of the call. It would be like `((fn (x) x) 4)`
|
2021-04-25 22:56:31 +01:00
|
|
|
case ExprType::Fn: {
|
2021-05-08 19:00:24 +01:00
|
|
|
targetNode = first;
|
2021-04-25 22:56:31 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-05-08 19:00:24 +01:00
|
|
|
|
|
|
|
// Otherwise we don't know how to call the first element.
|
2021-04-25 22:56:31 +01:00
|
|
|
default: {
|
2021-05-09 12:21:26 +01:00
|
|
|
std::string msg = llvm::formatv("Don't know how to call a '{0}'",
|
|
|
|
stringifyExprType(first->getType()));
|
2021-10-24 12:34:20 +01:00
|
|
|
return makeErrorful<Node>(first->location, errors::DontKnowHowToCallNode,
|
2021-05-09 12:21:26 +01:00
|
|
|
msg);
|
2021-04-25 22:56:31 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
auto analyzedParams = reader::analyze(ctx, rawParams);
|
|
|
|
|
|
|
|
if (!analyzedParams) {
|
|
|
|
return MaybeNode::error(analyzedParams.getError());
|
|
|
|
}
|
|
|
|
|
|
|
|
return makeSuccessfulNode<Call>(list->location, targetNode,
|
|
|
|
analyzedParams.getValue());
|
2021-04-25 22:56:31 +01:00
|
|
|
};
|
|
|
|
} // namespace exprs
|
|
|
|
} // namespace serene
|