From e9ff1c6fbbbac9ece2604876ab589ac282360446 Mon Sep 17 00:00:00 2001 From: Nicholas Date: Mon, 15 Nov 2021 15:08:03 -0800 Subject: Feat: added if/else branching and switch statement Unsure about my modification to the language. I found the parsing of the case body within switches to be odd - specifically that it parses liberally and then checks that it has case -> cmd structuring while it walks the code. This means the language is more permissive than the semantics. I modified it to be more explicit, but at the cost of having to end each case statement with a semicolon. I wanted a colon, but this is a valid word character and thus will be lexed as part of the word. --- src/cmd/rc/syntax.y | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'src/cmd/rc/syntax.y') diff --git a/src/cmd/rc/syntax.y b/src/cmd/rc/syntax.y index 0bdc776..9750002 100644 --- a/src/cmd/rc/syntax.y +++ b/src/cmd/rc/syntax.y @@ -65,13 +65,11 @@ ifbody: | block Telse nl cmd { $$ = maketree3(Tif, nil, $1, $2); } case: - Tcase words ';' { $$ = hangchild1($1, $2, 0); } -| Tcase words '\n' { $$ = hangchild1($1, $2, 0); } + Tcase words ';' nl cmdsln { $$ = hangchild2($1, $2, 0, $5, 1);} casebody: - cmd { $$ = maketree2(Tcasebody, $1, nil); } -| case casebody { $$ = maketree2(Tcasebody, $1, $2); } -| cmdsln casebody { $$ = maketree2(Tcasebody, $1, $2); } + /* empty */ { $$ = nil; } +| case casebody { $$ = (!$2) ? $1 : maketree2(';', $1, $2); } assign: executable '=' word { $$ = maketree2('=', $1, $3); } @@ -85,21 +83,21 @@ epilog: | redir epilog { $$ = hangchild1($1, $2, 1); } cmd: -/* empty */ %prec Twhile { $$ = nil; } -| basic { $$ = maketree1(Tbasic, $1); } -| block epilog { $$ = hangepilog($1, $2); } -| cmd Tpipe nl cmd { $$ = hangchild2($2, $1, 0, $4, 1); } -| cmd Tandand nl cmd { $$ = maketree2(Tandand, $1, $4); } -| cmd Toror nl cmd { $$ = maketree2(Toror, $1, $4); } -| redir cmd %prec Tbang { $$ = hangchild1($1, $2, 1); } -| assign cmd %prec Tbang { $$ = hangchild1($1, $2, 2); } -| Tbang cmd { $$ = maketree1(Tbang, $2); } -| Tsubshell cmd { $$ = maketree1(Tsubshell, $2); } -| Tfor '(' word ')' nl cmd { $$ = hangchild3($1, $3, nil, $6); } -| Tfor '(' word Tin words ')' nl cmd { $$ = hangchild3($1, $3, $5, $8); } -| Twhile paren nl cmd { $$ = hangchild2($1, $2, 0, $4, 1); } -| Tif paren nl ifbody { $$ = hangchild1($2, $1, 0); } -| Tswitch '(' word ')' nl '{' casebody '}' { $$ = hangchild2($1, $3, 0, $7, 1); } +/* empty */ %prec Twhile { $$ = nil; } +| basic { $$ = maketree1(Tbasic, $1); } +| block epilog { $$ = hangepilog($1, $2); } +| cmd Tpipe nl cmd { $$ = hangchild2($2, $1, 0, $4, 1); } +| cmd Tandand nl cmd { $$ = maketree2(Tandand, $1, $4); } +| cmd Toror nl cmd { $$ = maketree2(Toror, $1, $4); } +| redir cmd %prec Tbang { $$ = hangchild1($1, $2, 1); } +| assign cmd %prec Tbang { $$ = hangchild1($1, $2, 2); } +| Tbang cmd { $$ = maketree1(Tbang, $2); } +| Tsubshell cmd { $$ = maketree1(Tsubshell, $2); } +| Tfor '(' word ')' nl cmd { $$ = hangchild3($1, $3, nil, $6); } +| Tfor '(' word Tin words ')' nl cmd { $$ = hangchild3($1, $3, $5, $8); } +| Twhile paren nl cmd { $$ = hangchild2($1, $2, 0, $4, 1); } +| Tif paren nl ifbody { $$ = hangchild1($2, $2, 0); } +| Tswitch '(' word ')' nl '{' nl casebody '}' { $$ = hangchild2($1, $3, 0, $8, 1); } basic: executable -- cgit v1.2.1