diff --git a/include/serene/errors/traits.h b/include/serene/errors/traits.h new file mode 100644 index 0000000..e3f4e07 --- /dev/null +++ b/include/serene/errors/traits.h @@ -0,0 +1,44 @@ +/* -*- C++ -*- + * Serene programming language. + * + * Copyright (c) 2019-2021 Sameer Rahmani + * + * 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 +class IError : public TraitBase { +public: + IError(){}; + IError(const IError &) = delete; + + ErrorVariant *getVariant(); + std::string getMessage(); +}; + +} // namespace serene::errors +#endif diff --git a/include/serene/exprs/expression.h b/include/serene/exprs/expression.h index 855d898..2e84bec 100644 --- a/include/serene/exprs/expression.h +++ b/include/serene/exprs/expression.h @@ -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 @@ -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; diff --git a/include/serene/exprs/traits.h b/include/serene/exprs/traits.h new file mode 100644 index 0000000..944c7c7 --- /dev/null +++ b/include/serene/exprs/traits.h @@ -0,0 +1,81 @@ +/* -*- C++ -*- + * Serene programming language. + * + * Copyright (c) 2019-2021 Sameer Rahmani + * + * 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 +class ITypeable : public TraitBase { +public: + ITypeable(){}; + ITypeable(const ITypeable &) = delete; + ExprType getType() const { return this->Object().getType(); } +}; + +template +class IAnalyzable : public TraitBase { +public: + IAnalyzable(){}; + IAnalyzable(const IAnalyzable &) = delete; + auto analyze(SereneContext &ctx); +}; + +template +class SExp : public WithTrait { + +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 diff --git a/include/serene/reader/traits.h b/include/serene/reader/traits.h new file mode 100644 index 0000000..070a2f4 --- /dev/null +++ b/include/serene/reader/traits.h @@ -0,0 +1,45 @@ +/* -*- C++ -*- + * Serene programming language. + * + * Copyright (c) 2019-2021 Sameer Rahmani + * + * 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 +class ILocatable : public TraitBase { +public: + ILocatable(){}; + ILocatable(const ILocatable &) = delete; + serene::reader::LocationRange &where() const { + return this->Object().where(); + } +}; + +template serene::reader::LocationRange &where(ILocatable &); +} // namespace serene::reader +#endif diff --git a/include/serene/traits.h b/include/serene/traits.h index a0a61d3..012f8c7 100644 --- a/include/serene/traits.h +++ b/include/serene/traits.h @@ -109,6 +109,17 @@ protected: }; }; +template +class IDebuggable : public TraitBase { +public: + IDebuggable(){}; + IDebuggable(const IDebuggable &) = delete; + std::string toString() const { return this->Object().toString(); } +}; + +template std::string toString(IDebuggable &t) { + return t.toString(); +} }; // namespace serene #endif diff --git a/src/serene/CMakeLists.txt b/src/serene/CMakeLists.txt index 0f18d0a..4db7981 100644 --- a/src/serene/CMakeLists.txt +++ b/src/serene/CMakeLists.txt @@ -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"