]> ocean-lang.org Git - ocean/blob - csrc/indent_test.mdc
d8e945ec3dab627994bb91492c080aac1d56b1a9
[ocean] / csrc / indent_test.mdc
1 This parser is primarily for testing indent and linebreak handling.
2 It reads a very simple code fragment with if/else statements and
3 simple assignments with expressions, and then prints out the same
4 with complete bracketing and indenting.
5
6 # File: indent_test.mk
7         myCFLAGS := -Wall -g -fplan9-extensions
8         CFLAGS := $(filter-out $(myCFLAGS),$(CFLAGS)) $(myCFLAGS)
9         myLDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc
10         LDLIBS := $(filter-out $(myLDLIBS),$(LDLIBS)) $(myLDLIBS)
11
12         all :: itest
13         itest.c itest.h : indent_test.mdc parsergen libparser.o libscanner.o libmdcode.o
14                 ./parsergen -o itest --LALR --tag indent indent_test.mdc
15         indent_test.mk: indent_test.mdc md2c
16                 ./md2c indent_test.mdc
17         itest: itest.c
18
19         doitest: itest itest.code
20                 ./itest itest.code
21         checkitest: itest itest.code
22                 @grep -v '^#' itest.out > .tmp.out
23                 @./itest itest.code | diff -u - .tmp.out || echo itest FAILED
24         demos :: doitest
25         tests :: checkitest
26
27 # indent: header
28
29  ./parsergen -o itest --LALR indent_test.cgm
30  cc -o itest itest.c lib*.o -licuuc -lgmp
31  ./itest itest.code
32
33         struct expression {
34                 struct text op;
35                 struct expression *left, *right;
36         };
37         struct statement {
38                 struct statement *next;
39                 struct expression *expr;
40                 struct statement *thenpart;
41                 struct statement *elsepart;
42         };
43
44 # indent: code
45         #include <unistd.h>
46         #include <stdlib.h>
47         #include <fcntl.h>
48         #include <sys/mman.h>
49         #include <stdio.h>
50         #include "mdcode.h"
51         #include "scanner.h"
52         #include "parser.h"
53         #include "itest.h"
54
55         static void free_expression(struct expression *e)
56         {
57                 if (!e)
58                         return;
59                 free_expression(e->left);
60                 free_expression(e->right);
61                 free(e);
62         }
63
64         static void free_statement(struct statement *s)
65         {
66                 if (!s)
67                         return;
68                 free_statement(s->next);
69                 free_expression(s->expr);
70                 free_statement(s->thenpart);
71                 free_statement(s->elsepart);
72                 free(s);
73         }
74
75         static void print_expression(struct expression *e)
76         {
77                 if (e->left && e->right) printf("(");
78                 if (e->left)
79                         print_expression(e->left);
80                 if (e->op.len)
81                         printf("%.*s", e->op.len, e->op.txt);
82                 if (e->right)
83                         print_expression(e->right);
84                 if (e->left && e->right) printf(")");
85         }
86
87         static void print_statement(struct statement *s, int depth)
88         {
89                 if (!s)
90                         return;
91                 if (!s->thenpart) {
92                         printf("%*.s", depth, "");
93                         print_expression(s->expr);
94                         printf(";\n");
95                 } else {
96                         printf("%*.sif ", depth, "");
97                         print_expression(s->expr);
98                         printf(":\n");
99                         print_statement(s->thenpart, depth+4);
100                         if (s->elsepart) {
101                                 printf("%*.selse:\n", depth, "");
102                                 print_statement(s->elsepart, depth+4);
103                         }
104                 }
105                 print_statement(s->next, depth);
106         }
107
108         int main(int argc, char *argv[])
109         {
110                 int fd = open(argv[1], O_RDONLY);
111                 int len = lseek(fd, 0, 2);
112                 char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
113                 struct section *s = code_extract(file, file+len, NULL);
114                 struct token_config config = {
115                         .ignored = (1 << TK_line_comment)
116                                  | (1 << TK_block_comment),
117                         .number_chars = ".,_+-",
118                         .word_start = "",
119                         .word_cont = "",
120                 };
121                 parse_itest(s->code, &config, argc > 2 ? stderr : NULL);
122                 while (s) {
123                         struct section *t = s->next;
124                         code_free(s->code);
125                         free(s);
126                         s = t;
127                 }
128                 exit(0);
129         }
130
131 # indent: grammar
132
133 ~~~~~~
134
135 Program -> Statementlist ${ print_statement($1, 0); }$
136
137 OptNL -> NEWLINE
138         |
139
140 $*statement
141         Statementlist ->  Statements ${ $0 = $<1; }$
142
143         Statements -> Statements Statement ${
144                                 {
145                                         struct statement **s;
146                                         $0 = $<1;
147                                         s = &$0;
148                                         while (*s)
149                                                 s = &(*s)->next;
150                                         *s = $<2;
151                                 }
152                                 }$
153                         | Statement ${ $0 = $<1; }$
154                         | ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$
155
156         Open -> {
157                 | NEWLINE {
158         Close -> }
159                 | NEWLINE }
160         Block -> Open Statementlist Close ${ $0 = $<2; }$
161                 | Open SimpleStatements } ${ $0 = $<2; }$
162                 | : Statementlist ${ $0 = $<2; }$
163
164         SimpleStatements -> SimpleStatements ; SimpleStatement ${
165                         {
166                                 struct statement **s;
167                                 $0 = $<1;
168                                 s = &$0;
169                                 while (*s)
170                                         s = &(*s)->next;
171                                 *s = $<3;
172                         }
173                         }$
174                 | SimpleStatement ${ $0 = $<1; }$
175                 | SimpleStatements ; ${ $0 = $<1; }$
176
177         SimpleStatement -> Factor = Expression ${
178                         $0 = calloc(1, sizeof(struct statement));
179                         $0->expr = calloc(1, sizeof(struct expression));
180                         $0->expr->left = $<1;
181                         $0->expr->op = $2.txt;
182                         $0->expr->right = $<3;
183                         }$
184         Statement -> SimpleStatements NEWLINE ${
185                         $0 = $<1;
186                         }$
187                 | IfStatement ${ $0 = $<1; }$
188                 | Statement NEWLINE ${ $0 = $<1; }$
189
190         IfStatement -> if Expression Block OptNL ${
191                                 $0 = calloc(1, sizeof(struct statement));
192                                 $0->expr = $<2;
193                                 $0->thenpart = $<3;
194                                 }$
195                 | if Expression : SimpleStatements ${
196                                 $0 = calloc(1, sizeof(struct statement));
197                                 $0->expr = $<2;
198                                 $0->thenpart = $<4;
199                                 }$
200                 | if Expression Block OptNL else Block ${
201                                 $0 = calloc(1, sizeof(struct statement));
202                                 $0->expr = $<2;
203                                 $0->thenpart = $<3;
204                                 $0->elsepart = $<6;
205                                 }$
206                 | if Expression Block OptNL else : SimpleStatements ${
207                                 $0 = calloc(1, sizeof(struct statement));
208                                 $0->expr = $<2;
209                                 $0->thenpart = $<3;
210                                 $0->elsepart = $<7;
211                                 }$
212                 | if Expression Block OptNL else IfStatement ${
213                                 $0 = calloc(1, sizeof(struct statement));
214                                 $0->expr = $<2;
215                                 $0->thenpart = $<3;
216                                 $0->elsepart = $<6;
217                                 }$
218
219 $*expression
220         Expression -> Expression + Term ${
221                                 $0 = calloc(1, sizeof(struct expression));
222                                 $0->op = $2.txt;
223                                 $0->left = $<1;
224                                 $0->right = $<3;
225                         }$
226                     | Expression - Term ${
227                                 $0 = calloc(1, sizeof(struct expression));
228                                 $0->op = $2.txt;
229                                 $0->left = $<1;
230                                 $0->right = $<3;
231                         }$
232                     | Term ${ $0 = $<1; }$
233         Term -> Term * Factor ${
234                                 $0 = calloc(1, sizeof(struct expression));
235                                 $0->op = $2.txt;
236                                 $0->left = $<1;
237                                 $0->right = $<3;
238                         }$
239               | Term / Factor ${
240                                 $0 = calloc(1, sizeof(struct expression));
241                                 $0->op = $2.txt;
242                                 $0->left = $<1;
243                                 $0->right = $<3;
244                         }$
245               | Factor ${ $0 = $<1; }$
246         Factor -> IDENTIFIER ${
247                                 $0 = calloc(1, sizeof(struct expression));
248                                 $0->op = $1.txt;
249                         }$
250 ~~~~~~
251
252 # File: itest.code
253
254         # test code
255         ~~~~~~
256         hello = yes; mister = no;
257         there = x;
258         all = y;
259         if cond + cond2 :
260                 hello = x;
261                 hello2 = x;
262
263                 sum = val +
264                  val;
265
266                 if condX:
267                  foo = x *
268                     x +
269                     y
270                     / two;
271          else if cond2:
272                 there1 =x
273                 there1a=x
274         there2=x
275         there3=x;
276         all = y;
277         if true {yes=x;} else : no=x
278         if true: yes = no; no = yes;
279
280         if false {
281                 print = OK
282         } else {
283                 print = not_OK
284         }
285
286         if a:
287                 if b:
288                         c= d
289         x = y
290
291 # File: itest.out
292         (hello=yes);
293         (mister=no);
294         (there=x);
295         (all=y);
296         if (cond+cond2):
297             (hello=x);
298             (hello2=x);
299             (sum=(val+val));
300             if condX:
301                 (foo=((x*x)+(y/two)));
302         else:
303             if cond2:
304                 (there1=x);
305                 (there1a=x);
306         (there2=x);
307         (there3=x);
308         (all=y);
309         if true:
310             (yes=x);
311         else:
312             (no=x);
313         if true:
314             (yes=no);
315             (no=yes);
316         if false:
317             (print=OK);
318         else:
319             (print=not_OK);
320         if a:
321             if b:
322                 (c=d);
323         (x=y);