Add Fn expression and List rewrite to Fn

This commit is contained in:
Sameer Rahmani 2021-04-24 14:39:43 +01:00
parent 16d02a0fb1
commit 573550ca07
9 changed files with 222 additions and 48 deletions

View File

@ -35,6 +35,8 @@ enum ErrID {
E0000,
E0001,
E0002,
E0003,
E0004,
};
struct ErrorVariant {
@ -55,6 +57,12 @@ static ErrorVariant
static ErrorVariant DefWrongNumberOfArgs(
E0002, "Wrong number of arguments is passed to the 'def' form.", "");
static ErrorVariant FnNoArgsList(E0003, "'fn' form requires an argument list.",
"");
static ErrorVariant FnArgsMustBeList(E0004, "'fn' arguments should be a list.",
"");
static std::map<ErrID, ErrorVariant *> ErrDesc = {
{E0000, &UnknownError},
{E0001, &DefExpectSymbol},

View File

@ -53,7 +53,6 @@ public:
maybe_node analyze(reader::SemanticContext &);
static bool classof(const Expression *e);
// static std::shared_ptr<errors::Error> isValid(List *);
static maybe_node make(List *);
~Def() = default;
};

View File

@ -46,6 +46,12 @@ enum class ExprType {
Number,
Def,
Error,
Fn,
};
/// The string represantion of built in expr types (NOT DATATYPES).
static const char *exprTypes[] = {
"Symbol", "List", "Number", "Def", "Error", "Fn",
};
class Expression;
@ -115,7 +121,9 @@ std::shared_ptr<T> makeAndCast(Args &&...args) {
/// Convert the given AST to string by calling the `toString` method
/// of each node.
std::string toString(ast &);
std::string astToString(const ast *);
/// Converts the given ExprType to string.
std::string stringifyExprType(ExprType);
/// Converts the given AST to string and prints it out
void dump(ast &);

67
include/serene/exprs/fn.h Normal file
View File

@ -0,0 +1,67 @@
/* -*- 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_FN_H
#define EXPRS_FN_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 <memory>
#include <string>
namespace serene {
namespace exprs {
class List;
/// This data structure represents a function. with a collection of
/// arguments and the ast of a body
class Fn : public Expression {
public:
std::string name;
// TODO: Use a coll type instead of a list here
List args;
ast body;
Fn(reader::LocationRange &loc, List &args, ast body)
: Expression(loc), args(args), body(body){};
ExprType getType() const;
std::string toString() const;
maybe_node analyze(reader::SemanticContext &);
static bool classof(const Expression *e);
static maybe_node make(List *);
~Fn() = default;
};
} // namespace exprs
} // namespace serene
#endif

View File

@ -7,6 +7,7 @@ set(HEADER_LIST
"${INCLUDE_DIR}/serene/exprs/list.h"
"${INCLUDE_DIR}/serene/exprs/number.h"
"${INCLUDE_DIR}/serene/exprs/def.h"
"${INCLUDE_DIR}/serene/exprs/fn.h"
# Reader
"${INCLUDE_DIR}/serene/reader/reader.h"
@ -32,6 +33,7 @@ add_library(serene
exprs/number.cpp
exprs/expression.cpp
exprs/def.cpp
exprs/fn.cpp
serene.cpp

View File

@ -28,22 +28,24 @@
namespace serene {
namespace exprs {
std::string toString(ast &tree) {
if (tree.size() == 0) {
std::string astToString(const ast *tree) {
if (tree->size() == 0) {
return "";
}
std::string result = tree.at(0)->toString();
std::string result = tree->at(0)->toString();
for (unsigned int i = 1; i < tree.size(); i++) {
result = llvm::formatv("{0} {1}", result, tree.at(i)->toString());
for (unsigned int i = 1; i < tree->size(); i++) {
result = llvm::formatv("{0} {1}", result, tree->at(i)->toString());
}
return result;
}
std::string stringifyExprType(ExprType t) { return exprTypes[(int)t]; };
/// Dump the given AST tree to the standard out
void dump(ast &tree) { llvm::outs() << toString(tree) << "\n"; };
void dump(ast &tree) { llvm::outs() << astToString(&tree) << "\n"; };
} // namespace exprs
} // namespace serene

79
src/serene/exprs/fn.cpp Normal file
View File

@ -0,0 +1,79 @@
/*
* 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.
*/
#include "serene/exprs/fn.h"
#include "serene/errors/error.h"
#include "serene/exprs/expression.h"
#include "serene/exprs/list.h"
#include "serene/exprs/symbol.h"
#include "llvm/Support/FormatVariadic.h"
namespace serene {
namespace exprs {
ExprType Fn::getType() const { return ExprType::Fn; };
std::string Fn::toString() const {
return llvm::formatv("<Fn {0} {1} to {2}>",
this->name.empty() ? "Anonymous" : this->name,
this->args.toString(),
this->body.empty() ? "<>" : astToString(&this->body));
}
maybe_node Fn::analyze(reader::SemanticContext &ctx) {
return Result<node>::success(nullptr);
};
bool Fn::classof(const Expression *e) { return e->getType() == ExprType::Fn; };
maybe_node Fn::make(List *list) {
// TODO: Add support for docstring as the 3rd argument (4th element)
if (list->count() < 2) {
std::string msg =
llvm::formatv("The argument list is mandatory.", list->count());
return Result<node>::success(makeAndCast<errors::Error>(
&errors::FnNoArgsList, list->elements[0], msg));
}
List *args = llvm::dyn_cast<List>(list->elements[1].get());
if (!args) {
std::string msg =
llvm::formatv("Arguments of a function has to be a list, got '{0}'",
stringifyExprType(list->elements[1]->getType()));
return Result<node>::success(makeAndCast<errors::Error>(
&errors::FnArgsMustBeList, list->elements[1], msg));
}
ast body;
if (list->count() > 2) {
body = std::vector<node>(list->begin() + 2, list->end());
}
node fn = exprs::make<Fn>(list->location, *args, body);
return Result<node>::success(fn);
};
} // namespace exprs
} // namespace serene

View File

@ -25,6 +25,7 @@
#include "serene/exprs/list.h"
#include "serene/errors/error.h"
#include "serene/exprs/def.h"
#include "serene/exprs/fn.h"
#include "serene/exprs/symbol.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
@ -43,6 +44,7 @@ List::List(const reader::LocationRange &loc, ast elems)
: Expression(loc), elements(elems){};
ExprType List::getType() const { return ExprType::List; };
std::string List::toString() const {
std::string s{this->elements.empty() ? "-" : ""};
@ -65,24 +67,9 @@ maybe_node List::analyze(reader::SemanticContext &ctx) {
return Def::make(this);
}
/* if (sym->name == "fn") { */
/* auto maybeErr = Fn::isValid(this); */
/* if (maybeErr) { */
/* // Not a valid `def` form */
/* return Result<node>::success(maybeErr); */
/* } */
/* Symbol *binding = llvm::dyn_cast<Symbol>(elements[1].get()); */
/* if (!binding) { */
/* llvm_unreachable("Def::isValid should of catch this."); */
/* } */
/* node def = make<Def>(location, binding->name, elements[2]); */
/* return Result<node>::success(def); */
/* } */
if (sym->name == "fn") {
return Fn::make(this);
}
}
}

View File

@ -25,9 +25,9 @@
#include "../test_helpers.cpp.inc"
#include "serene/exprs/expression.h"
#include "serene/exprs/list.h"
#include "serene/exprs/symbol.h"
#include "serene/reader/reader.h"
#include "serene/reader/semantics.h"
#include "serene/exprs/symbol.h"
#include <catch2/catch.hpp>
namespace serene {
@ -54,7 +54,8 @@ TEST_CASE("List Expression", "[expression]") {
auto list3 = make<List>(*range.get(), elements);
CHECK(list3->toString() == "<List <List -> <List <List ->> <Symbol example>>");
CHECK(list3->toString() ==
"<List <List -> <List <List ->> <Symbol example>>");
auto l = llvm::dyn_cast<List>(list.get());
@ -73,34 +74,55 @@ TEST_CASE("List Expression", "[expression]") {
expr = l->at(2);
REQUIRE_FALSE(expr.hasValue());
for(auto x : *l) {
for (auto x : *l) {
CHECK(x->getType() == ExprType::Symbol);
}
};
TEST_CASE("List semantic analysis of 'def'", "[semantic]") {
reader::Semantics analyzer;
auto ast = reader::read("(def (a) b)");
auto afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(astToString(&afterAst.getValue()) == "<Error E1: >");
TEST_CASE("List semantic analysis", "[semantic]") {
reader::Semantics analyzer;
auto ast = reader::read("(def (a) b)");
auto afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(toString(afterAst.getValue()) == "<Error E1: >");
ast = reader::read("(def a)");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(astToString(&afterAst.getValue()) == "<Error E2: Expected 3 got 2>");
ast = reader::read("(def a)");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(toString(afterAst.getValue()) == "<Error E2: Expected 3 got 2>");
ast = reader::read("(def a b c)");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(astToString(&afterAst.getValue()) == "<Error E2: Expected 3 got 4>");
ast = reader::read("(def a b c)");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(toString(afterAst.getValue()) == "<Error E2: Expected 3 got 4>");
ast = reader::read("(def a b)");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(astToString(&afterAst.getValue()) == "<Def a -> <Symbol b>>");
}
ast = reader::read("(def a b)");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(toString(afterAst.getValue()) == "<Def a -> <Symbol b>>");
TEST_CASE("List semantic analysis for 'fn'", "[semantic]") {
reader::Semantics analyzer;
auto ast = reader::read("(fn)");
auto afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(astToString(&afterAst.getValue()) == "<Error E3: The argument list is mandatory.>");
}
ast = reader::read("(fn ())");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(astToString(&afterAst.getValue()) == "<Fn Anonymous <List -> to <>>");
ast = reader::read("(fn (a b c) a a a)");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(astToString(&afterAst.getValue()) == "<Fn Anonymous <List -> to <Symbol a> <Symbol a> <Symbol a>>");
ast = reader::read("(fn () a b)");
afterAst = analyzer.analyze(ast.getValue());
REQUIRE(afterAst);
CHECK(astToString(&afterAst.getValue()) == "<Fn Anonymous <List -> to <Symbol a> <Symbol b>>");
}
} // namespace exprs
} // namespace serene