big update to readme
This commit is contained in:
23
tests/core.c
23
tests/core.c
@@ -13,7 +13,7 @@ void test_ident(void) {
|
||||
|
||||
/* ^[a-zA-Z_][a-zA-Z0-9_]*$ */
|
||||
|
||||
mpc_parser_t* Ident = mpc_ends(
|
||||
mpc_parser_t* Ident = mpc_enclose(
|
||||
mpc_also(
|
||||
mpc_else(mpc_alpha(), mpc_underscore()),
|
||||
mpc_many1(mpc_or(3, mpc_alpha(), mpc_underscore(), mpc_digit()), mpcf_strfold),
|
||||
@@ -35,10 +35,10 @@ void test_ident(void) {
|
||||
|
||||
void test_maths(void) {
|
||||
|
||||
mpc_parser_t* Expr = mpc_new();
|
||||
mpc_parser_t* Factor = mpc_new();
|
||||
mpc_parser_t* Term = mpc_new();
|
||||
mpc_parser_t* Maths = mpc_new();
|
||||
mpc_parser_t* Expr = mpc_new("expr");
|
||||
mpc_parser_t* Factor = mpc_new("factor");
|
||||
mpc_parser_t* Term = mpc_new("term");
|
||||
mpc_parser_t* Maths = mpc_new("maths");
|
||||
|
||||
mpc_define(Expr, mpc_else(
|
||||
mpc_and(3, mpcf_maths, Factor, mpc_oneof("*/"), Factor, free, free),
|
||||
@@ -55,7 +55,7 @@ void test_maths(void) {
|
||||
mpc_parens(Expr, free)
|
||||
));
|
||||
|
||||
mpc_define(Maths, mpc_ends(Expr, free));
|
||||
mpc_define(Maths, mpc_enclose(Expr, free));
|
||||
|
||||
PT_ASSERT(mpc_match(Maths, "1", (int[]){ 1 }, int_eq, free, int_print));
|
||||
PT_ASSERT(mpc_match(Maths, "(5)", (int[]){ 5 }, int_eq, free, int_print));
|
||||
@@ -63,16 +63,7 @@ void test_maths(void) {
|
||||
PT_ASSERT(mpc_unmatch(Maths, "a", (int[]){ 0 }, int_eq, free, int_print));
|
||||
PT_ASSERT(mpc_unmatch(Maths, "2b+4", (int[]){ 2 }, int_eq, free, int_print));
|
||||
|
||||
mpc_undefine(Expr);
|
||||
mpc_undefine(Factor);
|
||||
mpc_undefine(Term);
|
||||
mpc_undefine(Maths);
|
||||
|
||||
mpc_delete(Expr);
|
||||
mpc_delete(Factor);
|
||||
mpc_delete(Term);
|
||||
mpc_delete(Maths);
|
||||
|
||||
mpc_cleanup(4, Expr, Factor, Term, Maths);
|
||||
}
|
||||
|
||||
void suite_core(void) {
|
||||
|
@@ -1,54 +1,56 @@
|
||||
#include "ptest.h"
|
||||
#include "../mpc.h"
|
||||
|
||||
bool ast_eq(void* x, void* y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void test_grammar(void) {
|
||||
|
||||
mpc_parser_t* Test = mpc_new();
|
||||
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(Test, mpca_grammar("'c'*"));
|
||||
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_print(Test);
|
||||
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_undefine(Test);
|
||||
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"));
|
||||
|
||||
mpc_delete(Test);
|
||||
|
||||
mpc_parser_t* Expression = mpc_new();
|
||||
mpc_parser_t* Product = mpc_new();
|
||||
mpc_parser_t* Value = mpc_new();
|
||||
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));
|
||||
|
||||
mpc_define(Expression, mpca_grammar("<0> (('+' | '-') <0>)*", Product));
|
||||
mpc_define(Product, mpca_grammar("<0> (('*' | '/') <0>)*", Value));
|
||||
mpc_define(Value, mpca_grammar("/[0-9]/ | '(' <0> ')'", Expression));
|
||||
|
||||
mpc_print(Expression);
|
||||
mpc_print(Product);
|
||||
mpc_print(Value);
|
||||
mpc_ast_delete(t0);
|
||||
mpc_ast_delete(t1);
|
||||
mpc_ast_delete(t2);
|
||||
|
||||
mpc_ast_t* empty = mpc_ast_empty();
|
||||
|
||||
/*
|
||||
PT_ASSERT(mpc_match(Expression, "1", empty, ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
||||
PT_ASSERT(mpc_match(Expression, "(5)", empty, ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
||||
PT_ASSERT(mpc_match(Expression, "(4*2)+5", empty, ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
||||
PT_ASSERT(mpc_match(Expression, "a", empty, ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
||||
PT_ASSERT(mpc_match(Expression, "2b+4", empty, ast_eq, (mpc_dtor_t)mpc_ast_delete, (void(*)(void*))mpc_ast_print));
|
||||
*/
|
||||
|
||||
mpc_ast_delete(empty);
|
||||
|
||||
mpc_undefine(Expression);
|
||||
mpc_undefine(Product);
|
||||
mpc_undefine(Value);
|
||||
|
||||
mpc_delete(Expression);
|
||||
mpc_delete(Product);
|
||||
mpc_delete(Value);
|
||||
|
||||
mpc_cleanup(4, Expr, Prod, Value, Maths);
|
||||
}
|
||||
|
||||
void suite_grammar(void) {
|
||||
|
@@ -39,12 +39,9 @@ void test_regex_range(void) {
|
||||
mpc_parser_t* re0 = mpc_re("abg[abcdef]");
|
||||
mpc_parser_t* re1 = mpc_re("y*[a-z]");
|
||||
mpc_parser_t* re2 = mpc_re("zz(p+)?[A-Z_0\\]123]*");
|
||||
mpc_parser_t* re3 = mpc_re("[^56hy].*$");
|
||||
|
||||
mpc_print(re0);
|
||||
mpc_print(re1);
|
||||
mpc_print(re2);
|
||||
mpc_print(re3);
|
||||
mpc_parser_t* re3 = mpc_re("^[^56hy].*$");
|
||||
|
||||
/* TODO: Testing */
|
||||
|
||||
mpc_delete(re0);
|
||||
mpc_delete(re1);
|
||||
|
Reference in New Issue
Block a user