Add necessary Traits to construct an Expression alternative

This commit is contained in:
Sameer Rahmani 2021-05-04 21:10:52 +01:00
parent d0defdbf54
commit add3765982
6 changed files with 186 additions and 15 deletions

View File

@ -0,0 +1,44 @@
/* -*- 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 ERRORS_TRAITS_H
#define ERRORS_TRAITS_H
#include "serene/context.h"
#include "serene/errors/constants.h"
#include "serene/traits.h"
namespace serene::errors {
template <typename ConcreteType>
class IError : public TraitBase<ConcreteType, IError> {
public:
IError(){};
IError(const IError &) = delete;
ErrorVariant *getVariant();
std::string getMessage();
};
} // namespace serene::errors
#endif

View File

@ -26,6 +26,7 @@
#define EXPRS_EXPRESSION_H
#include "serene/context.h"
#include "serene/exprs/traits.h"
#include "serene/reader/location.h"
#include "serene/utils.h"
#include <memory>
@ -40,21 +41,6 @@ class SereneContext;
/// in the syntax directly. Like function definitions.
namespace exprs {
/// This enum represent the expression type and **not** the value type.
enum class ExprType {
Symbol,
List,
Number,
Def,
Error,
Fn,
Call,
};
/// The string represantion of built in expr types (NOT DATATYPES).
static const char *exprTypes[] = {
"Symbol", "List", "Number", "Def", "Error", "Fn", "Call",
};
class Expression;

View File

@ -0,0 +1,81 @@
/* -*- 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_TRAITS_H
#define EXPRS_TRAITS_H
#include "serene/context.h"
#include "serene/reader/location.h"
#include "serene/reader/traits.h"
#include "serene/traits.h"
#include "serene/utils.h"
namespace serene::exprs {
/// This enum represent the expression type and **not** the value type.
enum class ExprType {
Symbol,
List,
Number,
Def,
Error,
Fn,
Call,
};
/// The string represantion of built in expr types (NOT DATATYPES).
static const char *exprTypes[] = {
"Symbol", "List", "Number", "Def", "Error", "Fn", "Call",
};
template <typename ConcreteType>
class ITypeable : public TraitBase<ConcreteType, ITypeable> {
public:
ITypeable(){};
ITypeable(const ITypeable &) = delete;
ExprType getType() const { return this->Object().getType(); }
};
template <typename ConcreteType>
class IAnalyzable : public TraitBase<ConcreteType, IAnalyzable> {
public:
IAnalyzable(){};
IAnalyzable(const IAnalyzable &) = delete;
auto analyze(SereneContext &ctx);
};
template <typename ConcreteType>
class SExp : public WithTrait<ConcreteType, ITypeable,
serene::reader::ILocatable, serene::IDebuggable> {
protected:
serene::reader::LocationRange location;
SExp(serene::reader::LocationRange &loc) : location(loc){};
public:
serene::reader::LocationRange where() const { return this->location; }
};
}; // namespace serene::exprs
#endif

View File

@ -0,0 +1,45 @@
/* -*- 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 READER_TRAITS_H
#define READER_TRAITS_H
#include "serene/reader/location.h"
#include "serene/traits.h"
namespace serene::reader {
template <typename ConcreteType>
class ILocatable : public TraitBase<ConcreteType, ILocatable> {
public:
ILocatable(){};
ILocatable(const ILocatable &) = delete;
serene::reader::LocationRange &where() const {
return this->Object().where();
}
};
template <typename T> serene::reader::LocationRange &where(ILocatable<T> &);
} // namespace serene::reader
#endif

View File

@ -109,6 +109,17 @@ protected:
};
};
template <typename ConcreteType>
class IDebuggable : public TraitBase<ConcreteType, IDebuggable> {
public:
IDebuggable(){};
IDebuggable(const IDebuggable &) = delete;
std::string toString() const { return this->Object().toString(); }
};
template <typename T> std::string toString(IDebuggable<T> &t) {
return t.toString();
}
}; // namespace serene
#endif

View File

@ -3,6 +3,7 @@ set(HEADER_LIST
"${INCLUDE_DIR}/serene/utils.h"
"${INCLUDE_DIR}/serene/context.h"
"${INCLUDE_DIR}/serene/environment.h"
"${INCLUDE_DIR}/serene/traits.h"
"${INCLUDE_DIR}/serene/exprs/expression.h"
"${INCLUDE_DIR}/serene/exprs/symbol.h"
@ -10,17 +11,20 @@ set(HEADER_LIST
"${INCLUDE_DIR}/serene/exprs/number.h"
"${INCLUDE_DIR}/serene/exprs/def.h"
"${INCLUDE_DIR}/serene/exprs/fn.h"
"${INCLUDE_DIR}/serene/exprs/traits.h"
# Reader
"${INCLUDE_DIR}/serene/reader/reader.h"
"${INCLUDE_DIR}/serene/reader/location.h"
"${INCLUDE_DIR}/serene/reader/errors.h"
"${INCLUDE_DIR}/serene/reader/semantics.h"
"${INCLUDE_DIR}/serene/reader/traits.h"
"${INCLUDE_DIR}/serene/errors.h"
"${INCLUDE_DIR}/serene/errors/error.h"
"${INCLUDE_DIR}/serene/errors/errc.h"
"${INCLUDE_DIR}/serene/errors/constants.h"
"${INCLUDE_DIR}/serene/errors/traits.h"
# "${INCLUDE_DIR}/serene/sir/sir.hpp"