Add tests for symbol and numbers

This commit is contained in:
Sameer Rahmani 2022-03-08 15:58:02 +00:00
parent df2300498b
commit 1e8d23b8ef
11 changed files with 42 additions and 38 deletions

View File

@ -35,11 +35,10 @@
#define SERENE_DIAGNOSTICS_H
#include "serene/errors.h"
#include "serene/export.h"
#include "serene/reader/location.h"
#include "serene/source_mgr.h"
#include <serene/export.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/raw_ostream.h>
#include <mlir/IR/Diagnostics.h>

View File

@ -21,8 +21,7 @@
#include "serene/errors/base.h"
#include "serene/errors/errc.h"
#include <serene/export.h>
#include "serene/export.h"
#define GET_CLASS_DEFS
#include "serene/errors/errs.h.inc"

View File

@ -20,6 +20,7 @@
#define SERENE_EXPRS_LIST_H
#include "serene/context.h"
#include "serene/export.h"
#include "serene/exprs/expression.h"
#include <llvm/ADT/Optional.h>
@ -32,7 +33,7 @@ namespace exprs {
/// This class represents a List in the AST level and not the List as the data
/// type.
class List : public Expression {
class SERENE_EXPORT List : public Expression {
public:
// Internal elements of the lest (small vector of shared pointers to
// expressions)

View File

@ -20,6 +20,7 @@
#define SERENE_EXPRS_NUMBER_H
#include "serene/context.h"
#include "serene/export.h"
#include "serene/exprs/expression.h"
#include "serene/namespace.h"
@ -32,7 +33,7 @@ namespace exprs {
/// positive and negative numbers. This is not a data type representative.
/// So it won't cast to actual numeric types and it has a string container
/// to hold the parsed value.
struct Number : public Expression {
struct SERENE_EXPORT Number : public Expression {
// TODO: Use a variant here instead to store different number types
std::string value;

View File

@ -20,11 +20,10 @@
#define SERENE_EXPRS_SYMBOL_H
#include "serene/context.h"
#include "serene/export.h"
#include "serene/exprs/expression.h"
#include "serene/namespace.h"
#include <serene/export.h>
#include <llvm/ADT/StringRef.h>
#include <string>

View File

@ -18,13 +18,12 @@
#include "serene/namespace.h"
#include "serene/context.h"
#include "serene/export.h"
#include "serene/exprs/expression.h"
#include "serene/llvm/IR/Value.h"
#include "serene/semantics.h"
#include "serene/slir/slir.h"
#include <serene/export.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/FormatVariadic.h>
#include <llvm/Support/raw_ostream.h>

View File

@ -29,14 +29,14 @@ namespace exprs {
TEST_CASE("Public Expression API", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
auto sym = make<Symbol>(*range.get(), "example");
auto sym = make<Symbol>(*range, "example", "user");
REQUIRE(sym->getType() == ExprType::Symbol);
CHECK(sym->toString() == "<Symbol example>");
CHECK(sym->toString() == "<Symbol user/example>");
auto list = makeAndCast<List>(*range.get(), sym);
auto list = makeAndCast<List>(*range, sym);
CHECK(list->toString() == "<List <Symbol example>>");
CHECK(list->toString() == "<List <Symbol user/example>>");
};
} // namespace exprs

View File

@ -21,10 +21,11 @@
#include "serene/exprs/symbol.h"
#include "serene/namespace.h"
#include "serene/reader/reader.h"
#include "serene/reader/semantics.h"
#include "serene/semantics.h"
#include "../test_helpers.cpp.inc"
#include <catch2/catch_all.hpp>
#include <llvm/Support/Error.h>
namespace serene {
namespace exprs {
@ -32,15 +33,15 @@ namespace exprs {
TEST_CASE("List Expression", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
Node sym = make<Symbol>(*range.get(), llvm::StringRef("example"));
Node sym1 = make<Symbol>(*range.get(), llvm::StringRef("example1"));
Node sym = make<Symbol>(*range, "example");
Node sym1 = make<Symbol>(*range, "example1");
Node list = make<List>(*range.get());
Node list = make<List>(*range);
REQUIRE(list->getType() == ExprType::List);
CHECK(list->toString() == "<List ->");
Node list2 = make<List>(*range.get(), list);
Node list2 = make<List>(*range, list);
CHECK(list2->toString() == "<List <List ->>");
Ast elements;
@ -48,7 +49,7 @@ TEST_CASE("List Expression", "[expression]") {
elements.push_back(list2);
elements.push_back(sym);
auto list3 = make<List>(*range.get(), elements);
auto list3 = make<List>(*range, elements);
CHECK(list3->toString() ==
"<List <List -> <List <List ->> <Symbol example>>");
@ -75,14 +76,20 @@ TEST_CASE("List Expression", "[expression]") {
}
};
TEST_CASE("List semantic analysis of 'def'", "[semantic]") {
auto ctx = makeSereneContext();
auto ns = makeNamespace(*ctx, "user", llvm::None);
auto ast = reader::read("(def (a) b)");
auto afterAst = reader::analyze(*ctx, ast.getValue());
TEST_CASE("List semantic analysis of 'def'", "[semantic,expression,list]") {
auto ctx = makeSereneContext();
auto ns = ctx->makeNamespace("user", llvm::None);
auto ast =
llvm::cantFail(reader::read(*ctx, "(def (a) b)", "user", llvm::None));
SemanticEnv env;
semantics::AnalysisState state(ns, env);
auto afterAst = semantics::analyze(state, ast);
REQUIRE_FALSE(afterAst);
// Fetch the first error
CHECK(afterAst.getError()[0]->toString() == "<Error E1: >");
CHECK(afterAst.takeError() == "<Error E1: >");
ast = reader::read("(def a)");
afterAst = reader::analyze(*ctx, ast.getValue());

View File

@ -19,19 +19,18 @@
#include "serene/exprs/number.h"
#include "../test_helpers.cpp.inc"
#include <catch2/catch_all.hpp>
namespace serene {
namespace exprs {
TEST_CASE("Number Expression", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
auto num1 = makeAndCast<Number>(*range.get(), "3", false, false);
auto num2 = makeAndCast<Number>(*range.get(), "3.4", false, true);
auto num1 = makeAndCast<Number>(*range, "3", false, false);
auto num2 = makeAndCast<Number>(*range, "3.4", false, true);
// Hence the `isNeg` being true. We need to provide the sign as the input
// anyway
auto num3 = makeAndCast<Number>(*range.get(), "3", true, false);
auto num4 = makeAndCast<Number>(*range.get(), "-3", true, false);
auto num3 = makeAndCast<Number>(*range, "3", true, false);
auto num4 = makeAndCast<Number>(*range, "-3", true, false);
CHECK(num1->toString() == "<Number 3>");
CHECK(num2->toString() == "<Number 3.4>");

View File

@ -26,10 +26,10 @@ namespace exprs {
TEST_CASE("Public Symbol API", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
auto sym = make<Symbol>(*range.get(), "example");
auto sym = make<Symbol>(*range, "example", "user");
REQUIRE(sym->getType() == ExprType::Symbol);
CHECK(sym->toString() == "<Symbol example>");
CHECK(sym->toString() == "<Symbol user/example>");
};
} // namespace exprs
} // namespace serene

View File

@ -20,11 +20,11 @@
#include "./context_tests.cpp.inc"
#include "./environment_tests.cpp.inc"
#include "./errors/error_tests.cpp.inc"
#include "./exprs/expression_tests.cpp.inc"
//#include "./exprs/list_tests.cpp.inc"
#include "./exprs/number_tests.cpp.inc"
#include "./exprs/symbol_tests.cpp.inc"
#include "./setup.cpp.inc"
// #include "./exprs/expression_tests.cpp.inc"
// #include "./exprs/list_tests.cpp.inc"
// #include "./exprs/number_tests.cpp.inc"
// #include "./exprs/symbol_tests.cpp.inc"
// #include "./namespace_tests.cpp.inc"
#include "./reader/reader_tests.cpp.inc"
// #include "./traits_tests.cpp.inc"