Move structures and free procedures back to dedicated files

This commit is contained in:
Camden Dixie O'Brien 2024-10-26 19:20:13 +01:00
parent 3a578e190f
commit b2a2cb9036
6 changed files with 105 additions and 87 deletions

View File

@ -6,7 +6,7 @@
#ifndef DESUGAR_H #ifndef DESUGAR_H
#define DESUGAR_H #define DESUGAR_H
#include "parse.h" #include "regex.h"
bool desugar_regex(parse_tree_t *regex); bool desugar_regex(parse_tree_t *regex);

View File

@ -5,7 +5,6 @@
#include "parse.h" #include "parse.h"
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#define CLASS_START_CAPACITY 4 #define CLASS_START_CAPACITY 4
@ -168,8 +167,8 @@ static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
return -1; return -1;
} }
result result = parse_term(
= parse_term(input + used, rem - used, &out->contents[out->count]); input + used, rem - used, &out->contents[out->count]);
if (result < 0) if (result < 0)
break; break;
++out->count; ++out->count;
@ -218,38 +217,3 @@ int parse_expr(const char *input, int rem, parse_tree_t *out)
return used; return used;
} }
void parse_free_tree_children(const parse_tree_t *t)
{
if (NULL != t->contents) {
for (int i = 0; i < t->count; ++i)
parse_free_sequence_children(&t->contents[i]);
free(t->contents);
}
}
void parse_free_sequence_children(const parse_sequence_t *s)
{
if (NULL != s->contents) {
for (int i = 0; i < s->count; ++i) {
switch (s->contents[i].type) {
case PARSE_TERM_CLASS:
parse_free_class_children(&s->contents[i].class);
break;
case PARSE_TERM_SUBEXPR:
parse_free_tree_children(&s->contents[i].subexpr);
break;
default:
break;
}
}
free(s->contents);
}
}
void parse_free_class_children(const parse_class_t *c)
{
if (NULL != c->contents)
free(c->contents);
}

View File

@ -6,54 +6,8 @@
#ifndef PARSE_H #ifndef PARSE_H
#define PARSE_H #define PARSE_H
#include <stdbool.h> #include "regex.h"
typedef struct {
bool negated;
int count, capacity;
char *contents;
} parse_class_t;
typedef enum {
PARSE_QUANTIFIER_NONE,
PARSE_QUANTIFIER_STAR,
PARSE_QUANTIFIER_PLUS,
PARSE_QUANTIFIER_QMARK,
} parse_quantifier_t;
typedef enum {
PARSE_TERM_WILDCARD,
PARSE_TERM_CLASS,
PARSE_TERM_LITERAL,
PARSE_TERM_SUBEXPR,
PARSE_TERM_EMPTY,
} parse_term_type_t;
struct _parse_term;
typedef struct {
int count, capacity;
struct _parse_term *contents;
} parse_sequence_t;
typedef struct {
int count, capacity;
parse_sequence_t *contents;
} parse_tree_t;
typedef struct _parse_term {
parse_quantifier_t quantifier;
parse_term_type_t type;
union {
parse_class_t class;
char literal;
parse_tree_t subexpr;
};
} parse_term_t;
int parse_expr(const char *input, int rem, parse_tree_t *out); int parse_expr(const char *input, int rem, parse_tree_t *out);
void parse_free_tree_children(const parse_tree_t *t);
void parse_free_sequence_children(const parse_sequence_t *s);
void parse_free_class_children(const parse_class_t *c);
#endif #endif

42
lib/regex.c Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "regex.h"
#include <stdlib.h>
void parse_free_tree_children(const parse_tree_t *t)
{
if (NULL != t->contents) {
for (int i = 0; i < t->count; ++i)
parse_free_sequence_children(&t->contents[i]);
free(t->contents);
}
}
void parse_free_sequence_children(const parse_sequence_t *s)
{
if (NULL != s->contents) {
for (int i = 0; i < s->count; ++i) {
switch (s->contents[i].type) {
case PARSE_TERM_CLASS:
parse_free_class_children(&s->contents[i].class);
break;
case PARSE_TERM_SUBEXPR:
parse_free_tree_children(&s->contents[i].subexpr);
break;
default:
break;
}
}
free(s->contents);
}
}
void parse_free_class_children(const parse_class_t *c)
{
if (NULL != c->contents)
free(c->contents);
}

57
lib/regex.h Normal file
View File

@ -0,0 +1,57 @@
/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#ifndef REGEX_H
#define REGEX_H
#include <stdbool.h>
typedef struct {
bool negated;
int count, capacity;
char *contents;
} parse_class_t;
typedef enum {
PARSE_QUANTIFIER_NONE,
PARSE_QUANTIFIER_STAR,
PARSE_QUANTIFIER_PLUS,
PARSE_QUANTIFIER_QMARK,
} parse_quantifier_t;
typedef enum {
PARSE_TERM_WILDCARD,
PARSE_TERM_CLASS,
PARSE_TERM_LITERAL,
PARSE_TERM_SUBEXPR,
PARSE_TERM_EMPTY,
} parse_term_type_t;
struct _parse_term;
typedef struct {
int count, capacity;
struct _parse_term *contents;
} parse_sequence_t;
typedef struct {
int count, capacity;
parse_sequence_t *contents;
} parse_tree_t;
typedef struct _parse_term {
parse_quantifier_t quantifier;
parse_term_type_t type;
union {
parse_class_t class;
char literal;
parse_tree_t subexpr;
};
} parse_term_t;
void parse_free_tree_children(const parse_tree_t *t);
void parse_free_sequence_children(const parse_sequence_t *s);
void parse_free_class_children(const parse_class_t *c);
#endif

View File

@ -9,7 +9,8 @@ if [ ! -e build ]; then mkdir build; else rm build/*; fi
# Build library # Build library
clang $CFLAGS -Ilib -c lib/parse.c -o build/parse.o clang $CFLAGS -Ilib -c lib/parse.c -o build/parse.o
clang $CFLAGS -Ilib -c lib/desugar.c -o build/desugar.o clang $CFLAGS -Ilib -c lib/desugar.c -o build/desugar.o
ar -crs build/lib.a build/parse.o build/desugar.o clang $CFLAGS -Ilib -c lib/regex.c -o build/regex.o
ar -crs build/lib.a build/parse.o build/desugar.o build/regex.o
# Build tests # Build tests
clang $CFLAGS -Itests -c tests/testing.c -o build/testing.o clang $CFLAGS -Itests -c tests/testing.c -o build/testing.o