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