From 0e583044628a0c338eb3064b3c8c8c2cc380a87d Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sun, 25 Apr 2021 22:56:31 +0100 Subject: [PATCH] Create the boilerplate of the call expression --- include/serene/context.h | 16 +++++++- include/serene/exprs/call.h | 74 +++++++++++++++++++++++++++++++++ include/serene/namespace.h | 4 +- src/serene/exprs/call.cpp | 81 +++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 include/serene/exprs/call.h create mode 100644 src/serene/exprs/call.cpp diff --git a/include/serene/context.h b/include/serene/context.h index b8dbd31..1276e8f 100644 --- a/include/serene/context.h +++ b/include/serene/context.h @@ -25,10 +25,22 @@ #ifndef SERENE_CONTEXT_H #define SERENE_CONTEXT_H +#include "serene/environment.h" +#include "llvm/ADT/StringRef.h" + namespace serene { -class SereneContext { -public: +class Namespace; + +namespace exprs { +class Expression; +using node = std::shared_ptr; +} // namespace exprs + +struct SereneContext { + // llvm::DenseMap namespaces; + + Environment semanticEnv; SereneContext(){}; }; diff --git a/include/serene/exprs/call.h b/include/serene/exprs/call.h new file mode 100644 index 0000000..35088b0 --- /dev/null +++ b/include/serene/exprs/call.h @@ -0,0 +1,74 @@ +/* -*- 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_CALL_H +#define EXPRS_CALL_H + +#include "serene/context.h" +#include "serene/errors/error.h" +#include "serene/exprs/expression.h" +#include "serene/exprs/list.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Error.h" +#include +#include + +namespace serene { + +namespace exprs { +class List; + +/// This data structure represents a function. with a collection of +/// arguments and the ast of a body +class Call : public Expression { + +public: + node target; + ast params; + + Call(reader::LocationRange &loc, ast params) + : Expression(loc), params(params){}; + + ExprType getType() const; + std::string toString() const; + maybe_node analyze(SereneContext &); + + static bool classof(const Expression *e); + + /// Creates a call node out of a list. + /// For exmaple: `(somefn (param1 param2) param3)`. This function + /// is supposed to be used in the semantic analysis phase. + /// + /// \param ctx The semantic analysis context object. + /// \param list the list in question. + + static maybe_node make(SereneContext &ctx, List *list); + + ~Call() = default; +}; + +} // namespace exprs +} // namespace serene + +#endif diff --git a/include/serene/namespace.h b/include/serene/namespace.h index 3843b7a..d479fa1 100644 --- a/include/serene/namespace.h +++ b/include/serene/namespace.h @@ -1,7 +1,7 @@ -/** +/* -*- C++ -*- * Serene programming language. * - * Copyright (c) 2020 Sameer Rahmani + * 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 diff --git a/src/serene/exprs/call.cpp b/src/serene/exprs/call.cpp new file mode 100644 index 0000000..45b652a --- /dev/null +++ b/src/serene/exprs/call.cpp @@ -0,0 +1,81 @@ +/* + * 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. + */ + +#include "serene/exprs/call.h" +#include "serene/errors/error.h" +#include "serene/exprs/expression.h" +#include "serene/exprs/list.h" +#include "serene/exprs/symbol.h" +#include "serene/reader/semantics.h" +#include "llvm/Support/FormatVariadic.h" + +namespace serene { +namespace exprs { + +ExprType Call::getType() const { return ExprType::Call; }; + +std::string Call::toString() const { + return llvm::formatv("", this->target->toString(), + astToString(&this->params)); +} + +maybe_node Call::analyze(SereneContext &ctx) { + return Result::success(nullptr); +}; + +bool Call::classof(const Expression *e) { + return e->getType() == ExprType::Call; +}; + +maybe_node Call::make(SereneContext &ctx, List *list) { + assert((list->count() == 0) && "Empty call? Seriously ?"); + + auto maybeFirst = list->elements[0]->analyze(ctx); + node first; + + if (!maybeFirst) { + return Result::error(std::move(maybeFirst.getError())); + } + + switch (first->getType()) { + case ExprType::Symbol: { + + break; + } + + case ExprType::Fn: { + break; + } + case ExprType::List: { + break; + } + default: { + break; + } + }; + + return Result::success(nullptr); +}; +} // namespace exprs +} // namespace serene