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