serene/src/serene/exprs/call.cpp

121 lines
4.1 KiB
C++
Raw Normal View History

/*
* 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/call.h"
#include "serene/errors/error.h"
#include "serene/exprs/expression.h"
#include "serene/exprs/list.h"
#include "serene/exprs/symbol.h"
#include "serene/reader/semantics.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/FormatVariadic.h"
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-05-06 21:00:58 +01:00
MaybeNode Call::analyze(SereneContext &ctx) { return EmptyNode; };
bool Call::classof(const Expression *e) {
return e->getType() == ExprType::Call;
};
MaybeNode Call::make(SereneContext &ctx, List *list) {
assert((list->count() == 0) && "Empty call? Seriously ?");
// Let's find out what is the first element of the list
auto maybeFirst = list->elements[0]->analyze(ctx);
if (!maybeFirst) {
// There's something wrong with the first element. Return the error
return maybeFirst;
}
Node first = maybeFirst.getValue();
Node targetNode;
Ast rawParams = list->from(1);
// We need to create the Call node based on the type of the first
// element after it being analyzed.
switch (first->getType()) {
// 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.
case ExprType::Symbol: {
auto *sym = llvm::dyn_cast<Symbol>(first.get());
// TODO: Lookup the symbol in the namespace via a method that looks
// into the current environment.
auto maybeResult = ctx.getCurrentNS()->semanticEnv.lookup(sym->name);
if (!maybeResult) {
return makeErrorful<Node>(
sym->location, &errors::CantResolveSymbol,
llvm::formatv("Can't resolve the symbol '{0}'!", sym->name));
}
targetNode = maybeResult.getValue();
break;
}
// 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)`
case ExprType::Fn: {
targetNode = first;
break;
}
// Otherwise we don't know how to call the first element.
default: {
return makeErrorful<Node>(
first, &errors::DontKnowHowToCallNode,
llvm::formatv("Don't know how to call a '{0}'",
stringifyExprType(first->getType())));
}
};
auto analyzedParams = reader::analyze(ctx, rawParams);
if (!analyzedParams) {
return MaybeNode::error(analyzedParams.getError());
}
return makeSuccessfulNode<Call>(list->location, targetNode,
analyzedParams.getValue());
};
} // namespace exprs
} // namespace serene