2021-04-05 19:02:30 +01:00
|
|
|
/* -*- C++ -*-
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EXPRS_EXPRESSION_H
|
|
|
|
#define EXPRS_EXPRESSION_H
|
|
|
|
|
2021-04-08 19:59:47 +01:00
|
|
|
#include "serene/reader/location.h"
|
2021-04-12 23:18:32 +01:00
|
|
|
#include "serene/utils.h"
|
2021-04-05 19:02:30 +01:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace serene {
|
|
|
|
|
2021-04-12 23:18:32 +01:00
|
|
|
namespace reader {
|
|
|
|
class SemanticContext;
|
|
|
|
}
|
|
|
|
|
2021-04-11 00:02:56 +01:00
|
|
|
/// Contains all the builtin AST expressions including those which do not appear
|
|
|
|
/// in the syntax directly. Like function definitions.
|
2021-04-05 19:02:30 +01:00
|
|
|
namespace exprs {
|
|
|
|
|
2021-04-06 00:21:43 +01:00
|
|
|
/// This enum represent the expression type and **not** the value type.
|
|
|
|
enum class ExprType {
|
|
|
|
Symbol,
|
|
|
|
List,
|
2021-04-11 00:02:56 +01:00
|
|
|
Number,
|
2021-04-06 00:21:43 +01:00
|
|
|
};
|
2021-04-14 21:59:22 +01:00
|
|
|
class Expression;
|
|
|
|
|
|
|
|
using node = std::shared_ptr<Expression>;
|
|
|
|
using maybe_node = Result<node>;
|
|
|
|
using ast = llvm::SmallVector<node, 0>;
|
2021-04-06 00:21:43 +01:00
|
|
|
|
2021-04-11 00:02:56 +01:00
|
|
|
/// The base class of the expressions which provides the common interface for
|
|
|
|
/// the expressions to implement.
|
2021-04-05 19:02:30 +01:00
|
|
|
class Expression {
|
|
|
|
public:
|
2021-04-11 00:02:56 +01:00
|
|
|
/// The location range provide information regarding to where in the input
|
|
|
|
/// string the current expression is used.
|
2021-04-10 15:36:16 +01:00
|
|
|
reader::LocationRange location;
|
|
|
|
|
|
|
|
Expression(const reader::LocationRange &loc) : location(loc){};
|
|
|
|
virtual ~Expression() = default;
|
|
|
|
|
2021-04-11 00:02:56 +01:00
|
|
|
/// Returns the type of the expression. We need this funciton to perform
|
|
|
|
/// dynamic casting of expression object to implementations such as lisp or
|
|
|
|
/// symbol.
|
2021-04-10 15:36:16 +01:00
|
|
|
virtual ExprType getType() const = 0;
|
2021-04-11 00:02:56 +01:00
|
|
|
|
|
|
|
/// The AST representation of an expression
|
2021-04-10 15:36:16 +01:00
|
|
|
virtual std::string toString() const = 0;
|
2021-04-12 23:18:32 +01:00
|
|
|
|
2021-04-14 21:59:22 +01:00
|
|
|
virtual maybe_node analyze(reader::SemanticContext &) = 0;
|
2021-04-05 19:02:30 +01:00
|
|
|
};
|
2021-04-12 23:18:32 +01:00
|
|
|
|
2021-04-11 00:02:56 +01:00
|
|
|
/// Create a new `node` of type `T` and forwards any given parameter
|
2021-04-10 15:36:16 +01:00
|
|
|
/// to the constructor of type `T`. This is the **official way** to create
|
|
|
|
/// a new `Expression`. Here is an example:
|
|
|
|
/// \code
|
2021-04-11 00:02:56 +01:00
|
|
|
/// auto list = make<List>();
|
2021-04-10 15:36:16 +01:00
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// \param[args] Any argument with any type passed to this function will be
|
|
|
|
/// passed to the constructor of type T.
|
2021-04-11 00:02:56 +01:00
|
|
|
/// \return A shared pointer to an Expression
|
|
|
|
template <typename T, typename... Args> node make(Args &&...args) {
|
2021-04-10 15:36:16 +01:00
|
|
|
return std::shared_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
|
};
|
|
|
|
|
2021-04-11 00:02:56 +01:00
|
|
|
/// Create a new `node` of type `T` and forwards any given parameter
|
|
|
|
/// to the constructor of type `T`. This is the **official way** to create
|
|
|
|
/// a new `Expression`. Here is an example:
|
|
|
|
/// \code
|
|
|
|
/// auto list = make<List>();
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// \param[args] Any argument with any type passed to this function will be
|
|
|
|
/// passed to the constructor of type T.
|
|
|
|
/// \return A shared pointer to a value of type T.
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
std::shared_ptr<T> makeAndCast(Args &&...args) {
|
|
|
|
return std::shared_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
|
};
|
2021-04-10 15:36:16 +01:00
|
|
|
|
2021-04-05 19:02:30 +01:00
|
|
|
} // namespace exprs
|
|
|
|
} // namespace serene
|
|
|
|
|
|
|
|
#endif
|