Linux Merge
This commit is contained in:
15
mpc.c
15
mpc.c
@@ -1691,7 +1691,7 @@ static void mpc_print_unretained(mpc_parser_t* p, bool force) {
|
||||
if (p->type == MPC_TYPE_FAIL) { printf("<fail>"); }
|
||||
if (p->type == MPC_TYPE_LIFT) { printf("<lift>"); }
|
||||
if (p->type == MPC_TYPE_EXPECT) {
|
||||
printf(p->data.expect.m);
|
||||
printf("%s", p->data.expect.m);
|
||||
/*mpc_print_unretained(p->data.expect.x, false);*/
|
||||
}
|
||||
|
||||
@@ -2039,9 +2039,6 @@ static mpc_val_t* mpca_grammar_lift(void) {
|
||||
}
|
||||
|
||||
static mpc_val_t* mpca_grammar_fold_repeat(mpc_val_t* x, mpc_val_t* y) {
|
||||
|
||||
printf("Got Repeat '%s'\n", (char*)y);
|
||||
|
||||
if (strcmp(y, "*") == 0) { free(y); return mpca_many(x); }
|
||||
if (strcmp(y, "+") == 0) { free(y); return mpca_many1(x); }
|
||||
if (strcmp(y, "?") == 0) { free(y); return mpca_maybe(x); }
|
||||
@@ -2051,15 +2048,11 @@ static mpc_val_t* mpca_grammar_fold_repeat(mpc_val_t* x, mpc_val_t* y) {
|
||||
}
|
||||
|
||||
static mpc_val_t* mpc_grammar_apply_string(mpc_val_t* x) {
|
||||
mpc_parser_t* p = mpc_ast(mpc_string(mpcf_unescape(x)));
|
||||
free(x);
|
||||
return p;
|
||||
return mpc_ast(mpc_string(mpcf_unescape(x)));
|
||||
}
|
||||
|
||||
static mpc_val_t* mpc_grammar_apply_char(mpc_val_t* x) {
|
||||
mpc_parser_t* p = mpc_ast(mpc_char(*(char*)mpcf_unescape(x)));
|
||||
free(x);
|
||||
return p;
|
||||
static mpc_val_t* mpc_grammar_apply_char(mpc_val_t* x) {;
|
||||
return mpc_ast(mpc_char(*(char*)mpcf_unescape(x)));
|
||||
}
|
||||
|
||||
static mpc_val_t* mpc_grammar_apply_regex(mpc_val_t* x) {
|
||||
|
3
mpc.h
3
mpc.h
@@ -10,6 +10,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
/*
|
||||
** Error Type
|
||||
@@ -266,4 +267,4 @@ mpc_parser_t* mpca_and(int n, ...);
|
||||
mpc_parser_t* mpca_ends(mpc_parser_t* a);
|
||||
mpc_parser_t* mpca_grammar(const char* grammar, ...);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@@ -1,38 +0,0 @@
|
||||
#include "../mpc.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool string_eq(void* x, void* y) { return (strcmp(x, y) == 0); }
|
||||
static void string_print(void* x) { printf("'%s'", (char*)x); }
|
||||
|
||||
bool suite_ident(void) {
|
||||
|
||||
/* ^[a-zA-Z_][a-zA-Z0-9_]*$ */
|
||||
|
||||
mpc_parser_t* Ident = mpc_new();
|
||||
|
||||
mpc_define(Ident, mpc_ends(
|
||||
mpc_also(
|
||||
mpc_either(mpc_alpha(), mpc_underscore()),
|
||||
mpc_many1(mpc_or(3, mpc_alpha(), mpc_underscore(), mpc_digit()), mpcf_strfold),
|
||||
free, mpcf_strfold
|
||||
),
|
||||
free)
|
||||
);
|
||||
|
||||
mpc_print(Ident);
|
||||
|
||||
mpc_test(Ident, "test", "test", string_eq, free, string_print);
|
||||
mpc_test(Ident, " blah", "", string_eq, free, string_print);
|
||||
mpc_test(Ident, "anoth21er", "anoth21er", string_eq, free, string_print);
|
||||
mpc_test(Ident, "du__de", "du__de", string_eq, free, string_print);
|
||||
mpc_test(Ident, "some spaces", "", string_eq, free, string_print);
|
||||
mpc_test(Ident, "", "", string_eq, free, string_print);
|
||||
mpc_test(Ident, "18nums", "", string_eq, free, string_print);
|
||||
|
||||
mpc_undefine(Ident);
|
||||
mpc_delete(Ident);
|
||||
|
||||
return true;
|
||||
}
|
61
tests/math.c
61
tests/math.c
@@ -1,61 +0,0 @@
|
||||
#include "../mpc.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool int_eq(void* x, void* y) {
|
||||
return (*(int*)x == *(int*)y);
|
||||
}
|
||||
|
||||
static void int_print(void* x) {
|
||||
printf("'%i'", *((int*)x));
|
||||
}
|
||||
|
||||
bool suite_math(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_define(Expr, mpc_either(
|
||||
mpc_and(3, mpcf_maths, Factor, mpc_oneof("*/"), Factor, free, free),
|
||||
Factor
|
||||
));
|
||||
|
||||
mpc_define(Factor, mpc_either(
|
||||
mpc_and(3, mpcf_maths, Term, mpc_oneof("+-"), Term, free, free),
|
||||
Term
|
||||
));
|
||||
|
||||
mpc_define(Term, mpc_either(
|
||||
mpc_int(),
|
||||
mpc_parens(Expr, free)
|
||||
));
|
||||
|
||||
mpc_define(Maths, mpc_ends(Expr, free));
|
||||
|
||||
mpc_print(Expr);
|
||||
mpc_print(Factor);
|
||||
mpc_print(Term);
|
||||
mpc_print(Maths);
|
||||
|
||||
mpc_test(Maths, "1", (int[]){ 1 }, int_eq, free, int_print);
|
||||
mpc_test(Maths, "(5)", (int[]){ 5 }, int_eq, free, int_print);
|
||||
mpc_test(Maths, "(4*2)+5", (int[]){ 13 }, int_eq, free, int_print);
|
||||
mpc_test(Maths, "a", (int[]){ 0 }, int_eq, free, int_print);
|
||||
mpc_test(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);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
Reference in New Issue
Block a user