-
Notifications
You must be signed in to change notification settings - Fork 4
/
combinator.h
112 lines (85 loc) · 2.83 KB
/
combinator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright 2012 William Hart. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY William Hart ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL William Hart OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
typedef struct
{
char * input;
int alloc;
int length;
int start;
} input_t;
typedef enum
{
C_COMB, C_MATCH, C_EXPECT, C_LIT, C_FORWARD
} ctag_t;
typedef enum
{
T_NULL, T_INT, T_MUL, T_ADD, T_SUB, T_DIV, T_FORWARD, T_COMB
} tag_t;
typedef struct ast_t
{
tag_t typ;
struct ast_t * child;
struct ast_t * next;
char * str;
long l;
} ast_t;
struct comb_t;
typedef struct
{
ast_t * (*comb)(input_t *, struct comb_t *, struct comb_t *);
struct comb_t * cl1;
struct comb_t * cl2;
} closure_t;
typedef struct
{
char * str;
char * err;
} str2_t;
typedef struct comb_t
{
ctag_t type;
tag_t tag;
union
{
closure_t * clos;
char * str;
str2_t s2;
ast_t * (*lit)(input_t *);
struct comb_t ** forward;
};
} comb_t;
comb_t * forward(comb_t ** comb);
comb_t * Integer();
ast_t * match(input_t * in, tag_t tag, char * str);
ast_t * expect(input_t * in, tag_t tag, char * str, char * err);
ast_t * comb_or(input_t * in, comb_t * c1, comb_t * c2);
ast_t * comb_and(input_t * in, comb_t * c1, comb_t * c2);
ast_t * comb_laloop(input_t * in, comb_t * c1, comb_t * c2);
comb_t * Match(tag_t tag, char * str);
comb_t * Expect(tag_t tag, char * str, char * err);
comb_t * Comb(ast_t * (*comb)(input_t *, struct comb_t *, struct comb_t *),
comb_t * cl1, comb_t * cl2);
ast_t * parse(input_t * in, comb_t * c);
#define And(x, y) Comb(comb_and, x, y)
#define Or(x, y) Comb(comb_or, x, y)
#define Lloop(x, y) Comb(comb_laloop, x, y)
#define Combinator(x) \
comb_t * x; \
x = forward(&x);