This repository has been archived on 2024-03-07. You can view files and clone it, but cannot push or open issues/pull-requests.
wongola/tests/Parser.cpp

93 lines
2.3 KiB
C++
Raw Normal View History

#include <catch2/catch.hpp>
#include "../lib/Lexer.hpp"
#include "../lib/Parser.hpp"
class ParserTest
{
public:
explicit ParserTest() {}
virtual ~ParserTest() {}
void test_parse(std::string const& oracle,
std::string const& source)
{
wg::Lexer lex;
lex.scan(source);
auto tokens = lex.all();
wg::Parser parser;
auto node = parser.parse(tokens);
REQUIRE(oracle == node->string());
}
protected:
};
2023-09-27 21:05:04 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_dir")
{
test_parse("PROG(DIR(IDENT[hello],IDENT[world]))",
"#hello world");
}
2023-09-27 21:05:04 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_int")
{
test_parse("PROG(INT[45])",
" 45; ");
test_parse("PROG(ADD(INT[1],MUL(INT[2],INT[3])))",
" 1 + 2 * 3; ");
test_parse("PROG(MUL(ADD(INT[1],INT[2]),INT[3]))",
" (1 + 2) * 3; ");
test_parse("PROG(SUB(INT[1],DIV(INT[2],INT[3])))",
" 1 - 2 / 3; ");
test_parse("PROG(DIV(SUB(INT[1],INT[2]),INT[3]))",
" (1 - 2) / 3; ");
test_parse("PROG(ADD(INT[1],MOD(INT[2],INT[3])))",
" 1 + 2 % 3; ");
test_parse("PROG(MOD(ADD(INT[1],INT[2]),INT[3]))",
" (1 + 2) % 3; ");
}
TEST_CASE_METHOD(ParserTest, "Parser_call")
{
test_parse("PROG(CALL(IDENT[hello],ARGS))",
" hello(); ");
test_parse("PROG(CALL(IDENT[hello_world],ARGS(IDENT[x])))",
" hello_world(x); ");
test_parse("PROG(CALL(IDENT[hello_world],ARGS(IDENT[x],INT[78])))",
" hello_world(x, 78); ");
}
TEST_CASE_METHOD(ParserTest, "Parser_fundecl")
{
test_parse("PROG(EXTERN(IDENT[hello],"
"PARAMS(IDENT[x],IDENT[y],TYPE[int]),RET(TYPE[int])))",
" extern fun hello(x, y int) int; ");
test_parse("PROG(FUNDECL(IDENT[couc],PARAMS,RET,BLOCK))",
" fun couc() {} ");
test_parse("PROG(FUNDECL(IDENT[couc],PARAMS("
"IDENT[x],IDENT[y],TYPE[int]"
"),RET,BLOCK(RETURN(INT[4]))))",
" fun couc(x, y int) { return 4; } ");
test_parse("PROG(FUNDECL(IDENT[couc],PARAMS("
"IDENT[x],IDENT[y],TYPE[int]"
"),RET(TYPE[int]),BLOCK(RETURN(INT[4]))))",
" fun couc(x, y int) int { return 4; } ");
test_parse("PROG(RETURN(ADD(CALL(IDENT[a],ARGS),INT[1])))",
" return a() + 1; ");
}