2013-09-23 22:41:58 +01:00
|
|
|
#include "ptest.h"
|
|
|
|
#include "../mpc.h"
|
|
|
|
|
|
|
|
void test_grammar(void) {
|
|
|
|
|
2013-09-26 13:15:00 +01:00
|
|
|
mpc_parser_t* Expr = mpc_new("expression");
|
|
|
|
mpc_parser_t* Prod = mpc_new("product");
|
|
|
|
mpc_parser_t* Value = mpc_new("value");
|
|
|
|
mpc_parser_t* Maths = mpc_new("maths");
|
|
|
|
|
|
|
|
mpc_define(Expr, mpca_grammar(" <product> (('+' | '-') <product>)* ", Prod));
|
|
|
|
mpc_define(Prod, mpca_grammar(" <value> (('*' | '/') <value>)* ", Value));
|
|
|
|
mpc_define(Value, mpca_grammar(" /[0-9]+/ | '(' <expression> ')' ", Expr));
|
|
|
|
mpc_define(Maths, mpca_total(Expr));
|
|
|
|
|
|
|
|
mpc_ast_t* t0 = mpc_ast_build(1, "root", mpc_ast_new("value", "24"));
|
|
|
|
mpc_ast_t* t1 = mpc_ast_build(1, "root",
|
|
|
|
mpc_ast_build(3, "value",
|
|
|
|
mpc_ast_new("char", "("),
|
|
|
|
mpc_ast_new("value", "5"),
|
|
|
|
mpc_ast_new("char", ")")));
|
|
|
|
|
|
|
|
mpc_ast_t* t2 = mpc_ast_build(3, "root",
|
|
|
|
|
|
|
|
mpc_ast_build(3, "value",
|
|
|
|
mpc_ast_new("char", "("),
|
|
|
|
mpc_ast_build(3, "expression",
|
|
|
|
|
|
|
|
mpc_ast_build(5, "product",
|
|
|
|
mpc_ast_new("value", "4"),
|
|
|
|
mpc_ast_new("char", "*"),
|
|
|
|
mpc_ast_new("value", "2"),
|
|
|
|
mpc_ast_new("char", "*"),
|
|
|
|
mpc_ast_new("value", "11")),
|
|
|
|
|
|
|
|
mpc_ast_new("char", "+"),
|
|
|
|
mpc_ast_new("value", "2")),
|
|
|
|
mpc_ast_new("char", ")")),
|
|
|
|
|
|
|
|
mpc_ast_new("char", "+"),
|
|
|
|
mpc_ast_new("value", "5"));
|
2013-09-27 00:35:34 +01:00
|
|
|
|
2013-09-26 13:15:00 +01:00
|
|
|
PT_ASSERT(mpc_match(Maths, " 24 ", t0, (bool(*)(void*,void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
|
|
|
PT_ASSERT(mpc_match(Maths, "(5)", t1, (bool(*)(void*,void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
|
|
|
PT_ASSERT(mpc_match(Maths, "(4 * 2 * 11 + 2) + 5", t2, (bool(*)(void*,void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
|
|
|
PT_ASSERT(mpc_unmatch(Maths, "a", t0, (bool(*)(void*,void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
|
|
|
PT_ASSERT(mpc_unmatch(Maths, "2b+4", t0, (bool(*)(void*,void*))mpc_ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
2013-09-27 00:35:34 +01:00
|
|
|
|
2013-09-26 13:15:00 +01:00
|
|
|
mpc_ast_delete(t0);
|
|
|
|
mpc_ast_delete(t1);
|
|
|
|
mpc_ast_delete(t2);
|
2013-09-27 00:35:34 +01:00
|
|
|
|
2013-09-26 13:15:00 +01:00
|
|
|
mpc_cleanup(4, Expr, Prod, Value, Maths);
|
2013-09-27 00:35:34 +01:00
|
|
|
|
2013-09-23 22:41:58 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 17:58:14 +01:00
|
|
|
void test_language(void) {
|
|
|
|
|
|
|
|
mpc_parser_t* Expr = mpc_new("expression");
|
|
|
|
mpc_parser_t* Prod = mpc_new("product");
|
|
|
|
mpc_parser_t* Value = mpc_new("value");
|
|
|
|
mpc_parser_t* Maths = mpc_new("maths");
|
|
|
|
|
|
|
|
mpca_lang(
|
|
|
|
" \
|
|
|
|
expression : <product> (('+' | '-') <product>)*; \
|
|
|
|
product : <value> (('*' | '/') <value>)*; \
|
|
|
|
value : /[0-9]+/ | '(' <expression> ')'; \
|
2013-09-27 10:41:42 +01:00
|
|
|
maths : /^/ <expression> /$/; \
|
2013-09-26 17:58:14 +01:00
|
|
|
",
|
|
|
|
Expr, Prod, Value, Maths);
|
|
|
|
|
|
|
|
mpc_cleanup(4, Expr, Prod, Value, Maths);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_language_file(void) {
|
|
|
|
|
|
|
|
mpc_parser_t* Expr = mpc_new("expression");
|
|
|
|
mpc_parser_t* Prod = mpc_new("product");
|
|
|
|
mpc_parser_t* Value = mpc_new("value");
|
|
|
|
mpc_parser_t* Maths = mpc_new("maths");
|
|
|
|
|
2013-09-27 00:35:34 +01:00
|
|
|
mpca_lang_file("./tests/maths.grammar", Expr, Prod, Value, Maths);
|
2013-09-26 17:58:14 +01:00
|
|
|
|
|
|
|
mpc_cleanup(4, Expr, Prod, Value, Maths);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-23 22:41:58 +01:00
|
|
|
void suite_grammar(void) {
|
|
|
|
pt_add_test(test_grammar, "Test Grammar", "Suite Grammar");
|
2013-09-26 17:58:14 +01:00
|
|
|
pt_add_test(test_language, "Test Language", "Suite Grammar");
|
|
|
|
pt_add_test(test_language_file, "Test Language File", "Suite Grammar");
|
2013-09-23 22:41:58 +01:00
|
|
|
}
|