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