2021-04-05 19:02:30 +01:00
|
|
|
/*
|
2020-07-16 15:48:06 +01:00
|
|
|
* Serene programming language.
|
|
|
|
*
|
2021-04-05 19:02:30 +01:00
|
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
2020-08-02 21:08:35 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
|
|
|
* 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-05 19:02:30 +01:00
|
|
|
#include "serene/exprs/list.h"
|
2021-04-22 00:49:11 +01:00
|
|
|
#include "serene/errors/error.h"
|
2021-05-09 12:21:26 +01:00
|
|
|
#include "serene/exprs/call.h"
|
2021-04-17 19:36:25 +01:00
|
|
|
#include "serene/exprs/def.h"
|
2021-05-09 12:21:26 +01:00
|
|
|
#include "serene/exprs/expression.h"
|
2021-04-24 14:39:43 +01:00
|
|
|
#include "serene/exprs/fn.h"
|
2021-04-17 19:36:25 +01:00
|
|
|
#include "serene/exprs/symbol.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2021-04-18 18:39:48 +01:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2021-04-08 19:59:47 +01:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2021-04-17 00:18:13 +01:00
|
|
|
#include <iterator>
|
2021-04-08 19:59:47 +01:00
|
|
|
|
|
|
|
namespace serene {
|
|
|
|
namespace exprs {
|
2021-04-10 15:36:16 +01:00
|
|
|
|
2021-05-09 12:21:26 +01:00
|
|
|
List::List(const List &l) : Expression(l.location) {
|
|
|
|
this->elements = l.elements;
|
|
|
|
};
|
|
|
|
|
2021-04-25 23:02:52 +01:00
|
|
|
List::List(const reader::LocationRange &loc, Node &e) : Expression(loc) {
|
2021-04-18 17:32:33 +01:00
|
|
|
elements.push_back(e);
|
2021-04-10 15:36:16 +01:00
|
|
|
};
|
|
|
|
|
2021-04-25 23:07:08 +01:00
|
|
|
List::List(const reader::LocationRange &loc, Ast elems)
|
2021-04-18 17:32:33 +01:00
|
|
|
: Expression(loc), elements(elems){};
|
2021-04-10 15:36:16 +01:00
|
|
|
|
|
|
|
ExprType List::getType() const { return ExprType::List; };
|
2021-04-24 14:39:43 +01:00
|
|
|
|
2021-04-10 15:36:16 +01:00
|
|
|
std::string List::toString() const {
|
2021-04-08 19:59:47 +01:00
|
|
|
std::string s{this->elements.empty() ? "-" : ""};
|
|
|
|
|
|
|
|
for (auto &n : this->elements) {
|
2021-04-10 15:36:16 +01:00
|
|
|
s = llvm::formatv("{0} {1}", s, n->toString());
|
2021-04-08 19:59:47 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 00:49:11 +01:00
|
|
|
return llvm::formatv("<List {0}>", s);
|
2021-04-10 15:36:16 +01:00
|
|
|
};
|
|
|
|
|
2021-04-25 23:05:21 +01:00
|
|
|
MaybeNode List::analyze(SereneContext &ctx) {
|
2021-04-18 18:39:48 +01:00
|
|
|
if (!elements.empty()) {
|
|
|
|
auto *first = elements[0].get();
|
|
|
|
|
|
|
|
if (first->getType() == ExprType::Symbol) {
|
|
|
|
auto *sym = llvm::dyn_cast<Symbol>(first);
|
|
|
|
|
|
|
|
if (sym) {
|
|
|
|
if (sym->name == "def") {
|
2021-04-24 19:10:18 +01:00
|
|
|
return Def::make(ctx, this);
|
2021-04-23 16:48:14 +01:00
|
|
|
}
|
2021-04-22 00:49:11 +01:00
|
|
|
|
2021-04-24 14:39:43 +01:00
|
|
|
if (sym->name == "fn") {
|
2021-04-24 19:10:18 +01:00
|
|
|
return Fn::make(ctx, this);
|
2021-04-24 14:39:43 +01:00
|
|
|
}
|
2021-04-18 18:39:48 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 12:21:26 +01:00
|
|
|
|
|
|
|
return Call::make(ctx, this);
|
2021-04-18 18:39:48 +01:00
|
|
|
}
|
2021-04-17 19:36:25 +01:00
|
|
|
|
2021-05-09 12:21:26 +01:00
|
|
|
return EmptyNode;
|
2021-04-13 00:45:08 +01:00
|
|
|
};
|
|
|
|
|
2021-04-10 15:36:16 +01:00
|
|
|
bool List::classof(const Expression *e) {
|
|
|
|
return e->getType() == ExprType::List;
|
|
|
|
};
|
|
|
|
|
2021-04-17 19:36:25 +01:00
|
|
|
/// Return an iterator to be used with the `for` loop. It's implicitly called by
|
|
|
|
/// the for loop.
|
2021-04-25 23:02:52 +01:00
|
|
|
std::vector<Node>::const_iterator List::cbegin() { return elements.begin(); }
|
2021-04-17 19:36:25 +01:00
|
|
|
|
|
|
|
/// Return an iterator to be used with the `for` loop. It's implicitly called by
|
|
|
|
/// the for loop.
|
2021-04-25 23:02:52 +01:00
|
|
|
std::vector<Node>::const_iterator List::cend() { return elements.end(); }
|
2021-04-17 19:36:25 +01:00
|
|
|
|
|
|
|
/// Return an iterator to be used with the `for` loop. It's implicitly called by
|
|
|
|
/// the for loop.
|
2021-04-25 23:02:52 +01:00
|
|
|
std::vector<Node>::iterator List::begin() { return elements.begin(); }
|
2021-04-17 19:36:25 +01:00
|
|
|
|
|
|
|
/// Return an iterator to be used with the `for` loop. It's implicitly called by
|
|
|
|
/// the for loop.
|
2021-04-25 23:02:52 +01:00
|
|
|
std::vector<Node>::iterator List::end() { return elements.end(); }
|
2021-04-17 19:36:25 +01:00
|
|
|
|
|
|
|
size_t List::count() const { return elements.size(); }
|
|
|
|
|
|
|
|
llvm::Optional<Expression *> List::at(uint index) {
|
|
|
|
if (index >= elements.size()) {
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::Optional<Expression *>(this->elements[index].get());
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
Ast List::from(uint index) {
|
2021-05-09 21:50:56 +01:00
|
|
|
|
2021-05-08 19:00:24 +01:00
|
|
|
if (index < elements.size()) {
|
|
|
|
return Ast(elements.begin() + index, elements.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ast();
|
|
|
|
}
|
|
|
|
|
2021-04-25 23:02:52 +01:00
|
|
|
void List::append(Node n) { elements.push_back(std::move(n)); }
|
2021-04-08 19:59:47 +01:00
|
|
|
} // namespace exprs
|
|
|
|
} // namespace serene
|