X-Git-Url: https://ocean-lang.org/code/?a=blobdiff_plain;f=csrc%2Findent_test.mdc;h=5fbd8eb414c18a2375ce184af3263d09c95838a0;hb=36e687aa5a84b52dc4add6ae2dc5f7abe71e3e2d;hp=792e152523055781b282ac2d261fcae324f0fc79;hpb=34189a207bc903e9ff96549ad222fa20f2e41508;p=ocean diff --git a/csrc/indent_test.mdc b/csrc/indent_test.mdc index 792e152..5fbd8eb 100644 --- a/csrc/indent_test.mdc +++ b/csrc/indent_test.mdc @@ -4,26 +4,30 @@ simple assignments with expressions, and then prints out the same with complete bracketing and indenting. # File: indent_test.mk - myCFLAGS := -Wall -g -fplan9-extensions - CFLAGS := $(filter-out $(myCFLAGS),$(CFLAGS)) $(myCFLAGS) - LDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc + itestCFLAGS := -Wall -g -fplan9-extensions + itestLDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc all :: itest - itest.c itest.h : indent_test.mdc parsergen libparser.o libscanner.o libmdcode.o + itest.c itest.h : indent_test.mdc parsergen ./parsergen -o itest --LALR --tag indent indent_test.mdc - indent_test.mk: indent_test.mdc md2c + indent_test.mk itest.code: indent_test.mdc md2c ./md2c indent_test.mdc - itest: itest.c + itest: itest.c | $(filter %.o,$(itestLDLIBS)) + $(CC) $(itestCFLAGS) $^ $(itestLDLIBS) -o $@ - tests:: itest itest.code + doitest: itest itest.code ./itest itest.code + checkitest: itest itest.code + @grep -v '^#' itest.out > .tmp.out + @./itest itest.code | diff -u .tmp.out - || echo itest FAILED + @sed -e 'i\ + ' itest.code > itest2.code + @./itest itest2.code| diff -u .tmp.out - || echo itest2 FAILED + demos :: doitest + tests :: checkitest # indent: header - ./parsergen -o itest --LALR indent_test.cgm - cc -o itest itest.c lib*.o -licuuc -lgmp - ./itest itest.code - struct expression { struct text op; struct expression *left, *right; @@ -106,8 +110,6 @@ with complete bracketing and indenting. char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); struct section *s = code_extract(file, file+len, NULL); struct token_config config = { - .ignored = (1 << TK_line_comment) - | (1 << TK_block_comment), .number_chars = ".,_+-", .word_start = "", .word_cont = "", @@ -122,52 +124,51 @@ with complete bracketing and indenting. exit(0); } - # indent: grammar ~~~~~~ -Program -> Statementlist ${ print_statement($1, 0); }$ +$TERM if { } : * + - / ; = + +Program -> OptNL Statementlist ${ print_statement($S, 0); }$ -OptNL -> NEWLINE - | +OptNL -> + | OptNL NEWLINE +Newlines -> NEWLINE + | Newlines NEWLINE $*statement - Statementlist -> Statements ${ $0 = $<1; }$ - - Statements -> Statements Statement ${ - { - struct statement **s; - $0 = $<1; - s = &$0; - while (*s) - s = &(*s)->next; - *s = $<2; - } - }$ - | Statement ${ $0 = $<1; }$ - | ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$ - Open -> { - | NEWLINE { - Close -> } - | NEWLINE } - Block -> Open Statementlist Close ${ $0 = $<2; }$ - | Open SimpleStatements } ${ $0 = $<2; }$ - | : Statementlist ${ $0 = $2; $2 = NULL; }$ + Statementlist -> Statementlist Statement ${ + { + struct statement **s; + $0 = $<1; + s = &$0; + while (*s) + s = &(*s)->next; + *s = $<2; + } + }$ + | Statement ${ $0 = $<1; }$ + + Block -> { IN OptNL Statementlist OUT OptNL } ${ $0 = $ SimpleStatements ; SimpleStatement ${ { struct statement **s; - $0 = $<1; + $0 = $next; - *s = $<3; + *s = $ Factor = Expression ${ $0 = calloc(1, sizeof(struct statement)); @@ -176,41 +177,46 @@ $*statement $0->expr->op = $2.txt; $0->expr->right = $<3; }$ - Statement -> SimpleStatements NEWLINE ${ - $0 = $<1; - }$ - | IfStatement ${ $0 = $<1; }$ - | Statement NEWLINE ${ $0 = $<1; }$ + Statement -> SimpleStatements Newlines ${ $0 = $next = $IS->next; + $IS->next = NULL; + $0->elsepart = $next = $IS->next; + $IS->next = NULL; + $0->elsepart = $ if Expression Block OptNL ${ - $0 = calloc(1, sizeof(struct statement)); - $0->expr = $<2; - $0->thenpart = $<3; - }$ - | if Expression : SimpleStatements ${ - $0 = calloc(1, sizeof(struct statement)); - $0->expr = $<2; - $0->thenpart = $<4; - }$ - | if Expression Block OptNL else Block ${ - $0 = calloc(1, sizeof(struct statement)); - $0->expr = $<2; - $0->thenpart = $<3; - $0->elsepart = $<6; - }$ - | if Expression Block OptNL else : SimpleStatements ${ - $0 = calloc(1, sizeof(struct statement)); - $0->expr = $<2; - $0->thenpart = $<3; - $0->elsepart = $<7; - }$ - | if Expression Block OptNL else IfStatement ${ + $RIGHT else + + IfHead -> if Expression Block ${ $0 = calloc(1, sizeof(struct statement)); - $0->expr = $<2; - $0->thenpart = $<3; - $0->elsepart = $<6; + $0->expr = $thenpart = $ else Block Newlines ${ $0 = $elsepart = $IS; + $0->next = $IS->next; + $IS->next = NULL; + }$ + | else IfHead Newlines IfSuffix ${ + $0 = $elsepart = $IS; + $0->next = $IS->next; + $IS->next = NULL; + }$ + $*expression Expression -> Expression + Term ${ $0 = calloc(1, sizeof(struct expression)); @@ -224,7 +230,7 @@ $*expression $0->left = $<1; $0->right = $<3; }$ - | Term ${ $0 = $1; $1 = NULL; }$ + | Term ${ $0 = $<1; }$ Term -> Term * Factor ${ $0 = calloc(1, sizeof(struct expression)); $0->op = $2.txt; @@ -237,7 +243,7 @@ $*expression $0->left = $<1; $0->right = $<3; }$ - | Factor ${ $0 = $1; $1 = NULL; }$ + | Factor ${ $0 = $<1; }$ Factor -> IDENTIFIER ${ $0 = calloc(1, sizeof(struct expression)); $0->op = $1.txt; @@ -266,11 +272,27 @@ $*expression else if cond2: there1 =x there1a=x + if cond + cond2 : + hello = x; + hello2 = x; + + sum = val + + val; + + if condX: + foo = x * + x + + y + / two; + else if cond2: + there1 =x + there1a=x there2=x there3=x; all = y; if true {yes=x;} else : no=x if true: yes = no; no = yes; + if false: yes=ok; else: no=ok if false { print = OK @@ -281,5 +303,56 @@ $*expression if a: if b: c= d + else: + f=g x = y +# File: itest.out + (hello=yes); + (mister=no); + (there=x); + (all=y); + if (cond+cond2): + (hello=x); + (hello2=x); + (sum=(val+val)); + if condX: + (foo=((x*x)+(y/two))); + else: + if cond2: + (there1=x); + (there1a=x); + if (cond+cond2): + (hello=x); + (hello2=x); + (sum=(val+val)); + if condX: + (foo=((x*x)+(y/two))); + else: + if cond2: + (there1=x); + (there1a=x); + (there2=x); + (there3=x); + (all=y); + if true: + (yes=x); + else: + (no=x); + if true: + (yes=no); + (no=yes); + if false: + (yes=ok); + else: + (no=ok); + if false: + (print=OK); + else: + (print=not_OK); + if a: + if b: + (c=d); + else: + (f=g); + (x=y);