2023-09-09 13:09:43 +00:00
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
#include "../lib/Parser.hpp"
|
|
|
|
#include "../lib/Factory.hpp"
|
|
|
|
|
|
|
|
class ParserTest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ParserTest() {}
|
|
|
|
virtual ~ParserTest() {}
|
|
|
|
|
2023-09-09 22:03:28 +00:00
|
|
|
void test_parser(std::string const& oracle, std::string const& source)
|
|
|
|
{
|
|
|
|
auto parser = jk::Factory(m_logger, "tests/parser").make_parser();
|
|
|
|
auto root = parser->parse(source);
|
|
|
|
|
|
|
|
REQUIRE(oracle == root->string());
|
|
|
|
}
|
|
|
|
|
2023-09-09 13:09:43 +00:00
|
|
|
protected:
|
|
|
|
jk::Logger m_logger;
|
|
|
|
};
|
|
|
|
|
2023-09-09 22:03:28 +00:00
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_funcall")
|
2023-09-09 13:09:43 +00:00
|
|
|
{
|
2023-09-09 22:03:28 +00:00
|
|
|
test_parser("PROG",
|
|
|
|
" ");
|
|
|
|
|
|
|
|
test_parser("PROG(FUNCALL(IDENT[hello]))",
|
|
|
|
" (hello) ");
|
|
|
|
|
|
|
|
test_parser("PROG(FUNCALL(IDENT[hello],INT[1],INT[2]))",
|
|
|
|
" (hello 1 2) ");
|
|
|
|
|
|
|
|
test_parser("PROG(FUNCALL(IDENT[hello],INT[1],INT[2],IDENT[bim!]))",
|
|
|
|
" (hello 1 2 bim!) ");
|
2023-09-09 13:09:43 +00:00
|
|
|
}
|
2023-09-10 06:06:34 +00:00
|
|
|
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_vardecl")
|
|
|
|
{
|
|
|
|
test_parser("PROG(VARDECL(IDENT[hello],INT[4]))",
|
|
|
|
" ($ hello 4) ");
|
|
|
|
|
|
|
|
test_parser("PROG(VARDECL(IDENT[world],"
|
|
|
|
"FUNCALL(IDENT[f],INT[3],INT[2])))",
|
|
|
|
" ($ world (f 3 2)) ");
|
|
|
|
}
|
2023-09-10 07:59:51 +00:00
|
|
|
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_lambda")
|
|
|
|
{
|
|
|
|
test_parser("PROG(LAMBDA(PARAMS("
|
|
|
|
"IDENT[x],IDENT[y]"
|
|
|
|
"),BODY("
|
|
|
|
"FUNCALL(IDENT[add],IDENT[y],IDENT[x])"
|
|
|
|
")))",
|
|
|
|
" (-> (x y) (add y x)) ");
|
|
|
|
|
|
|
|
test_parser("PROG(LAMBDA(PARAMS("
|
|
|
|
"IDENT[x]"
|
|
|
|
"),BODY("
|
|
|
|
"IDENT[x]"
|
|
|
|
")))",
|
|
|
|
" (-> (x) x)");
|
|
|
|
}
|
2023-09-10 09:10:30 +00:00
|
|
|
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_fundecl")
|
|
|
|
{
|
|
|
|
test_parser("PROG(VARDECL(IDENT[f],LAMBDA("
|
|
|
|
"PARAMS(IDENT[x],IDENT[y]),BODY("
|
|
|
|
"FUNCALL(IDENT[add],IDENT[y],IDENT[x])"
|
|
|
|
")"
|
|
|
|
")))",
|
|
|
|
" ($ (f x y) (add y x)) ");
|
|
|
|
}
|