2021-04-17 19:36:25 +01:00
|
|
|
/*
|
2021-04-11 00:02:56 +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.
|
|
|
|
*/
|
|
|
|
|
2021-04-17 19:36:25 +01:00
|
|
|
#include "serene/exprs/def.h"
|
2021-04-22 10:20:15 +01:00
|
|
|
#include "serene/errors/error.h"
|
2021-04-11 00:02:56 +01:00
|
|
|
#include "serene/exprs/list.h"
|
2021-04-22 10:20:15 +01:00
|
|
|
#include "serene/exprs/symbol.h"
|
2021-04-17 19:36:25 +01:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2021-04-11 00:02:56 +01:00
|
|
|
|
|
|
|
namespace serene {
|
|
|
|
namespace exprs {
|
|
|
|
|
2021-04-17 19:36:25 +01:00
|
|
|
ExprType Def::getType() const { return ExprType::Def; };
|
2021-04-11 00:02:56 +01:00
|
|
|
|
2021-04-17 19:36:25 +01:00
|
|
|
std::string Def::toString() const {
|
2021-04-22 00:49:11 +01:00
|
|
|
return llvm::formatv("<Def {0} -> {1}>", this->binding,
|
|
|
|
this->value->toString());
|
2021-04-17 19:36:25 +01:00
|
|
|
}
|
2021-04-11 00:02:56 +01:00
|
|
|
|
2021-04-17 19:36:25 +01:00
|
|
|
maybe_node Def::analyze(reader::SemanticContext &ctx) {
|
2021-04-20 19:33:25 +01:00
|
|
|
return Result<node>::success(nullptr);
|
2021-04-17 19:36:25 +01:00
|
|
|
};
|
2021-04-11 00:02:56 +01:00
|
|
|
|
2021-04-17 19:36:25 +01:00
|
|
|
bool Def::classof(const Expression *e) {
|
|
|
|
return e->getType() == ExprType::Def;
|
2021-04-11 00:02:56 +01:00
|
|
|
};
|
|
|
|
|
2021-04-23 16:48:14 +01:00
|
|
|
maybe_node Def::make(List *list) {
|
2021-04-22 10:20:15 +01:00
|
|
|
// TODO: Add support for docstring as the 3rd argument (4th element)
|
|
|
|
if (list->count() != 3) {
|
2021-04-23 16:48:14 +01:00
|
|
|
std::string msg = llvm::formatv("Expected 3 got {0}", list->count());
|
|
|
|
return Result<node>::success(makeAndCast<errors::Error>(
|
|
|
|
&errors::DefWrongNumberOfArgs, list->elements[0], msg));
|
2021-04-22 10:20:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *binding = llvm::dyn_cast<Symbol>(list->elements[1].get());
|
|
|
|
|
|
|
|
if (!binding) {
|
2021-04-23 16:48:14 +01:00
|
|
|
return Result<node>::success(makeAndCast<errors::Error>(
|
|
|
|
&errors::DefExpectSymbol, list->elements[1], ""));
|
2021-04-22 10:20:15 +01:00
|
|
|
}
|
|
|
|
|
2021-04-23 16:48:14 +01:00
|
|
|
node def = exprs::make<Def>(list->location, binding->name, list->elements[2]);
|
|
|
|
return Result<node>::success(def);
|
2021-04-22 00:49:11 +01:00
|
|
|
};
|
2021-04-11 00:02:56 +01:00
|
|
|
} // namespace exprs
|
|
|
|
} // namespace serene
|