From 57c69adf13069b9daf3ed59f4401f16de3312975 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Mon, 29 Nov 2021 19:39:06 +1100 Subject: [PATCH] oceani: indent grammar more consistently. - all products are not indented, whether they start with a non-term or a '|'. - Code is indented once - plus internal indents. Signed-off-by: NeilBrown --- csrc/oceani.mdc | 842 ++++++++++++++++++++++++------------------------ 1 file changed, 420 insertions(+), 422 deletions(-) diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index 5a728b6..7371535 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -1185,44 +1185,38 @@ executable. $*val Value -> True ${ - $0 = new_val(Tbool, $1); - $0->val.bool = 1; - }$ - | False ${ - $0 = new_val(Tbool, $1); - $0->val.bool = 0; - }$ - | NUMBER ${ - $0 = new_val(Tnum, $1); - { - char tail[3]; - if (number_parse($0->val.num, tail, $1.txt) == 0) - mpq_init($0->val.num); // UNTESTED - if (tail[0]) - tok_err(c, "error: unsupported number suffix", - &$1); - } - }$ - | STRING ${ - $0 = new_val(Tstr, $1); - { - char tail[3]; - string_parse(&$1, '\\', &$0->val.str, tail); - if (tail[0]) - tok_err(c, "error: unsupported string suffix", - &$1); - } - }$ - | MULTI_STRING ${ - $0 = new_val(Tstr, $1); - { - char tail[3]; - string_parse(&$1, '\\', &$0->val.str, tail); + $0 = new_val(Tbool, $1); + $0->val.bool = 1; + }$ + | False ${ + $0 = new_val(Tbool, $1); + $0->val.bool = 0; + }$ + | NUMBER ${ { + char tail[3]; + $0 = new_val(Tnum, $1); + if (number_parse($0->val.num, tail, $1.txt) == 0) + mpq_init($0->val.num); // UNTESTED if (tail[0]) - tok_err(c, "error: unsupported string suffix", + tok_err(c, "error: unsupported number suffix", &$1); - } - }$ + } }$ + | STRING ${ { + char tail[3]; + $0 = new_val(Tstr, $1); + string_parse(&$1, '\\', &$0->val.str, tail); + if (tail[0]) + tok_err(c, "error: unsupported string suffix", + &$1); + } }$ + | MULTI_STRING ${ { + char tail[3]; + $0 = new_val(Tstr, $1); + string_parse(&$1, '\\', &$0->val.str, tail); + if (tail[0]) + tok_err(c, "error: unsupported string suffix", + &$1); + } }$ ###### print exec cases case Xval: @@ -1999,7 +1993,7 @@ correctly. v->where_decl, NULL, 0, NULL); } } }$ - | IDENTIFIER :: ${ { + | IDENTIFIER :: ${ { struct variable *v = var_decl(c, $1.txt); $0 = new_pos(var, $1); $0->var = v; @@ -2015,7 +2009,7 @@ correctly. v->where_decl, NULL, 0, NULL); } } }$ - | IDENTIFIER : Type ${ { + | IDENTIFIER : Type ${ { struct variable *v = var_decl(c, $1.txt); $0 = new_pos(var, $1); $0->var = v; @@ -2033,7 +2027,7 @@ correctly. v->where_decl, NULL, 0, NULL); } } }$ - | IDENTIFIER :: Type ${ { + | IDENTIFIER :: Type ${ { struct variable *v = var_decl(c, $1.txt); $0 = new_pos(var, $1); $0->var = v; @@ -2067,7 +2061,7 @@ correctly. } } cast(var, $0)->var = v; - } }$ + } }$ ###### print exec cases case Xvar: @@ -2177,8 +2171,8 @@ simple "Value" (to be explained later). ###### Grammar $*exec Term -> Value ${ $0 = $<1; }$ - | Variable ${ $0 = $<1; }$ - ## term grammar + | Variable ${ $0 = $<1; }$ + ## term grammar Thus far the complex types we have are arrays and structs. @@ -2682,79 +2676,79 @@ function will be needed. ###### top level grammar DeclareStruct -> struct IDENTIFIER FieldBlock Newlines ${ { - struct type *t = - add_type(c, $2.txt, &structure_prototype); - int cnt = 0; - struct fieldlist *f; - - for (f = $3; f; f=f->prev) - cnt += 1; - - t->structure.nfields = cnt; - t->structure.fields = calloc(cnt, sizeof(struct field)); - f = $3; - while (cnt > 0) { - int a = f->f.type->align; - cnt -= 1; - t->structure.fields[cnt] = f->f; - if (t->size & (a-1)) - t->size = (t->size | (a-1)) + 1; - t->structure.fields[cnt].offset = t->size; - t->size += ((f->f.type->size - 1) | (a-1)) + 1; - if (a > t->align) - t->align = a; - f->f.init = NULL; - f = f->prev; - } - } }$ + struct type *t = + add_type(c, $2.txt, &structure_prototype); + int cnt = 0; + struct fieldlist *f; + + for (f = $3; f; f=f->prev) + cnt += 1; + + t->structure.nfields = cnt; + t->structure.fields = calloc(cnt, sizeof(struct field)); + f = $3; + while (cnt > 0) { + int a = f->f.type->align; + cnt -= 1; + t->structure.fields[cnt] = f->f; + if (t->size & (a-1)) + t->size = (t->size | (a-1)) + 1; + t->structure.fields[cnt].offset = t->size; + t->size += ((f->f.type->size - 1) | (a-1)) + 1; + if (a > t->align) + t->align = a; + f->f.init = NULL; + f = f->prev; + } + } }$ $*fieldlist FieldBlock -> { IN OptNL FieldLines OUT OptNL } ${ $0 = $ SimpleFieldList Newlines ${ $0 = $prev = $prev = $ Field ${ $0 = $prev = $prev = $ IDENTIFIER : Type = Expression ${ { - int ok; + int ok; - $0 = calloc(1, sizeof(struct fieldlist)); - $0->f.name = $1.txt; - $0->f.type = $<3; - $0->f.init = NULL; - do { - ok = 1; - propagate_types($<5, c, &ok, $3, 0); - } while (ok == 2); - if (!ok) - c->parse_error = 1; // UNTESTED - else { - struct value vl = interp_exec(c, $5, NULL); - $0->f.init = global_alloc(c, $0->f.type, NULL, &vl); - } - } }$ - | IDENTIFIER : Type ${ - $0 = calloc(1, sizeof(struct fieldlist)); - $0->f.name = $1.txt; - $0->f.type = $<3; - if ($0->f.type->prepare_type) - $0->f.type->prepare_type(c, $0->f.type, 1); - }$ + $0 = calloc(1, sizeof(struct fieldlist)); + $0->f.name = $1.txt; + $0->f.type = $<3; + $0->f.init = NULL; + do { + ok = 1; + propagate_types($<5, c, &ok, $3, 0); + } while (ok == 2); + if (!ok) + c->parse_error = 1; // UNTESTED + else { + struct value vl = interp_exec(c, $5, NULL); + $0->f.init = global_alloc(c, $0->f.type, NULL, &vl); + } + } }$ + | IDENTIFIER : Type ${ + $0 = calloc(1, sizeof(struct fieldlist)); + $0->f.name = $1.txt; + $0->f.type = $<3; + if ($0->f.type->prepare_type) + $0->f.type->prepare_type(c, $0->f.type, 1); + }$ ###### forward decls static void structure_print_type(struct type *t, FILE *f); @@ -2993,50 +2987,50 @@ further detailed when Expression Lists are introduced. $*variable FuncName -> IDENTIFIER ${ { - struct variable *v = var_decl(c, $1.txt); - struct var *e = new_pos(var, $1); + struct variable *v = var_decl(c, $1.txt); + struct var *e = new_pos(var, $1); + e->var = v; + if (v) { + v->where_decl = e; + $0 = v; + } else { + v = var_ref(c, $1.txt); e->var = v; - if (v) { - v->where_decl = e; - $0 = v; - } else { - v = var_ref(c, $1.txt); - e->var = v; - type_err(c, "error: function '%v' redeclared", - e, NULL, 0, NULL); - type_err(c, "info: this is where '%v' was first declared", - v->where_decl, NULL, 0, NULL); - free_exec(e); - } - } }$ + type_err(c, "error: function '%v' redeclared", + e, NULL, 0, NULL); + type_err(c, "info: this is where '%v' was first declared", + v->where_decl, NULL, 0, NULL); + free_exec(e); + } + } }$ $*binode Args -> ArgsLine NEWLINE ${ $0 = $left; - *bp = $left; + *bp = $ ${ $0 = NULL; }$ - | Varlist ${ $0 = $<1; }$ - | Varlist ; ${ $0 = $<1; }$ + | Varlist ${ $0 = $<1; }$ + | Varlist ; ${ $0 = $<1; }$ Varlist -> Varlist ; ArgDecl ${ - $0 = new(binode); - $0->op = List; - $0->left = $right = $op = List; - $0->left = NULL; - $0->right = $op = List; + $0->left = $right = $op = List; + $0->left = NULL; + $0->right = $ IDENTIFIER : FormalType ${ { @@ -3173,7 +3167,7 @@ Term - others will follow. $*exec Expression -> Term ${ $0 = $ ExpressionList , Expression ${ - $0 = new(binode); - $0->op = List; - $0->left = $<1; - $0->right = $<3; - }$ - | Expression ${ - $0 = new(binode); - $0->op = List; - $0->left = NULL; - $0->right = $<1; - }$ + $0 = new(binode); + $0->op = List; + $0->left = $<1; + $0->right = $<3; + }$ + | Expression ${ + $0 = new(binode); + $0->op = List; + $0->left = NULL; + $0->right = $<1; + }$ ### Expressions: Boolean @@ -3320,42 +3314,42 @@ evaluate the second expression if not necessary. $LEFT not ###### expression grammar - | Expression or Expression ${ { - struct binode *b = new(binode); - b->op = Or; - b->left = $<1; - b->right = $<3; - $0 = b; - } }$ - | Expression or else Expression ${ { - struct binode *b = new(binode); - b->op = OrElse; - b->left = $<1; - b->right = $<4; - $0 = b; - } }$ + | Expression or Expression ${ { + struct binode *b = new(binode); + b->op = Or; + b->left = $<1; + b->right = $<3; + $0 = b; + } }$ + | Expression or else Expression ${ { + struct binode *b = new(binode); + b->op = OrElse; + b->left = $<1; + b->right = $<4; + $0 = b; + } }$ - | Expression and Expression ${ { - struct binode *b = new(binode); - b->op = And; - b->left = $<1; - b->right = $<3; - $0 = b; - } }$ - | Expression and then Expression ${ { - struct binode *b = new(binode); - b->op = AndThen; - b->left = $<1; - b->right = $<4; - $0 = b; - } }$ + | Expression and Expression ${ { + struct binode *b = new(binode); + b->op = And; + b->left = $<1; + b->right = $<3; + $0 = b; + } }$ + | Expression and then Expression ${ { + struct binode *b = new(binode); + b->op = AndThen; + b->left = $<1; + b->right = $<4; + $0 = b; + } }$ - | not Expression ${ { - struct binode *b = new(binode); - b->op = Not; - b->right = $<2; - $0 = b; - } }$ + | not Expression ${ { + struct binode *b = new(binode); + b->op = Not; + b->right = $<2; + $0 = b; + } }$ ###### print binode cases case And: @@ -3477,12 +3471,12 @@ expression operator, and the `CMPop` non-terminal will match one of them. ###### Grammar $eop - CMPop -> < ${ $0.op = Less; }$ - | > ${ $0.op = Gtr; }$ - | <= ${ $0.op = LessEq; }$ - | >= ${ $0.op = GtrEq; }$ - | == ${ $0.op = Eql; }$ - | != ${ $0.op = NEql; }$ + CMPop -> < ${ $0.op = Less; }$ + | > ${ $0.op = Gtr; }$ + | <= ${ $0.op = LessEq; }$ + | >= ${ $0.op = GtrEq; }$ + | == ${ $0.op = Eql; }$ + | != ${ $0.op = NEql; }$ ###### print binode cases @@ -3622,17 +3616,17 @@ parentheses around an expression converts it into a Term, ###### Grammar $eop - Eop -> + ${ $0.op = Plus; }$ - | - ${ $0.op = Minus; }$ + Eop -> + ${ $0.op = Plus; }$ + | - ${ $0.op = Minus; }$ - Uop -> + ${ $0.op = Absolute; }$ - | - ${ $0.op = Negate; }$ - | $ ${ $0.op = StringConv; }$ + Uop -> + ${ $0.op = Absolute; }$ + | - ${ $0.op = Negate; }$ + | $ ${ $0.op = StringConv; }$ - Top -> * ${ $0.op = Times; }$ - | / ${ $0.op = Divide; }$ - | % ${ $0.op = Rem; }$ - | ++ ${ $0.op = Concat; }$ + Top -> * ${ $0.op = Times; }$ + | / ${ $0.op = Divide; }$ + | % ${ $0.op = Rem; }$ + | ++ ${ $0.op = Concat; }$ ###### print binode cases case Plus: @@ -3869,78 +3863,78 @@ the common header for all reductions to use. $*binode Block -> { IN OptNL Statementlist OUT OptNL } ${ $0 = $ OpenScope { IN OptNL Statementlist OUT OptNL } ${ $0 = $ { OpenScope IN OptNL Statementlist OUT OptNL } ${ $0 = $ { IN OptNL Statementlist OUT OptNL } ${ $0 = $ ComplexStatements ${ $0 = reorder_bilist($ ComplexStatements ComplexStatement ${ - if ($2 == NULL) { - $0 = $<1; - } else { - $0 = new(binode); - $0->op = Block; - $0->left = $<1; - $0->right = $<2; - } - }$ - | ComplexStatement ${ - if ($1 == NULL) { - $0 = NULL; - } else { - $0 = new(binode); - $0->op = Block; - $0->left = NULL; - $0->right = $<1; - } - }$ - - $*exec - ComplexStatement -> SimpleStatements Newlines ${ - $0 = reorder_bilist($ SimpleStatements ; SimpleStatement ${ + if ($2 == NULL) { + $0 = $<1; + } else { $0 = new(binode); $0->op = Block; $0->left = $<1; - $0->right = $<3; - }$ - | SimpleStatement ${ + $0->right = $<2; + } + }$ + | ComplexStatement ${ + if ($1 == NULL) { + $0 = NULL; + } else { $0 = new(binode); $0->op = Block; $0->left = NULL; $0->right = $<1; - }$ + } + }$ + + $*exec + ComplexStatement -> SimpleStatements Newlines ${ + $0 = reorder_bilist($ SimpleStatements ; SimpleStatement ${ + $0 = new(binode); + $0->op = Block; + $0->left = $<1; + $0->right = $<3; + }$ + | SimpleStatement ${ + $0 = new(binode); + $0->op = Block; + $0->left = NULL; + $0->right = $<1; + }$ $TERM pass $*exec SimpleStatement -> pass ${ $0 = NULL; }$ - | ERROR ${ tok_err(c, "Syntax error in statement", &$1); }$ - ## SimpleStatement Grammar + | ERROR ${ tok_err(c, "Syntax error in statement", &$1); }$ + ## SimpleStatement Grammar ###### print binode cases case Block: @@ -4111,31 +4105,31 @@ it is declared, and error will be raised as the name is created as ###### SimpleStatement Grammar | Term = Expression ${ - $0 = b= new(binode); - b->op = Assign; - b->left = $<1; - b->right = $<3; - }$ + $0 = b= new(binode); + b->op = Assign; + b->left = $<1; + b->right = $<3; + }$ | VariableDecl = Expression ${ - $0 = b= new(binode); - b->op = Declare; - b->left = $<1; - b->right =$<3; - }$ + $0 = b= new(binode); + b->op = Declare; + b->left = $<1; + b->right =$<3; + }$ | VariableDecl ${ - if ($1->var->where_set == NULL) { - type_err(c, - "Variable declared with no type or value: %v", - $1, NULL, 0, NULL); - free_var($1); - } else { - $0 = b = new(binode); - b->op = Declare; - b->left = $<1; - b->right = NULL; - } - }$ + if ($1->var->where_set == NULL) { + type_err(c, + "Variable declared with no type or value: %v", + $1, NULL, 0, NULL); + free_var($1); + } else { + $0 = b = new(binode); + b->op = Declare; + b->left = $<1; + b->right = NULL; + } + }$ ###### print binode cases @@ -4420,136 +4414,136 @@ casepart` to track a list of case parts. // ForPart, SwitchPart, and IfPart open scopes, o we have to close // them. WhilePart opens and closes its own scope. CondStatement -> ForPart OptNL ThenPart OptNL WhilePart CondSuffix ${ - $0 = $forpart = $thenpart = $looppart = $forpart = $looppart = $looppart = $condpart = $next = $0->casepart; - $0->casepart = $condpart = $next = $0->casepart; - $0->casepart = $condpart = $IP.condpart; $IP.condpart = NULL; - $0->thenpart = $IP.thenpart; $IP.thenpart = NULL; - // This is where we close an "if" statement - var_block_close(c, CloseSequential, $0); - }$ + $0 = $forpart = $thenpart = $looppart = $forpart = $looppart = $looppart = $condpart = $next = $0->casepart; + $0->casepart = $condpart = $next = $0->casepart; + $0->casepart = $condpart = $IP.condpart; $IP.condpart = NULL; + $0->thenpart = $IP.thenpart; $IP.thenpart = NULL; + // This is where we close an "if" statement + var_block_close(c, CloseSequential, $0); + }$ CondSuffix -> IfSuffix ${ - $0 = $<1; - }$ - | Newlines CasePart CondSuffix ${ - $0 = $next = $0->casepart; - $0->casepart = $next = $0->casepart; - $0->casepart = $next = $0->casepart; + $0->casepart = $next = $0->casepart; + $0->casepart = $ Newlines ${ $0 = new(cond_statement); }$ - | Newlines ElsePart ${ $0 = $ else OpenBlock Newlines ${ - $0 = new(cond_statement); - $0->elsepart = $elsepart); - }$ - | else OpenScope CondStatement ${ - $0 = new(cond_statement); - $0->elsepart = $elsepart); - }$ + $0 = new(cond_statement); + $0->elsepart = $elsepart); + }$ + | else OpenScope CondStatement ${ + $0 = new(cond_statement); + $0->elsepart = $elsepart); + }$ $*casepart CasePart -> case Expression OpenScope ColonBlock ${ - $0 = calloc(1,sizeof(struct casepart)); - $0->value = $action = $action); - }$ + $0 = calloc(1,sizeof(struct casepart)); + $0->value = $action = $action); + }$ $*exec // These scopes are closed in CondStatement ForPart -> for OpenBlock ${ - $0 = $ then OpenBlock ${ - $0 = $ while UseBlock OptNL do OpenBlock ${ - $0 = new(binode); - $0->op = Loop; - $0->left = $right = $right); - var_block_close(c, CloseSequential, $0); - }$ - | while OpenScope Expression OpenScope ColonBlock ${ - $0 = new(binode); - $0->op = Loop; - $0->left = $right = $right); - var_block_close(c, CloseSequential, $0); - }$ + $0 = new(binode); + $0->op = Loop; + $0->left = $right = $right); + var_block_close(c, CloseSequential, $0); + }$ + | while OpenScope Expression OpenScope ColonBlock ${ + $0 = new(binode); + $0->op = Loop; + $0->left = $right = $right); + var_block_close(c, CloseSequential, $0); + }$ $cond_statement IfPart -> if UseBlock OptNL then OpenBlock ${ - $0.condpart = $ switch OpenScope Expression ${ - $0 = $ - | OptNL NEWLINE + | OptNL NEWLINE + Newlines -> NEWLINE - | Newlines NEWLINE + | Newlines NEWLINE DeclarationList -> Declaration - | DeclarationList Declaration + | DeclarationList Declaration Declaration -> ERROR Newlines ${ - tok_err(c, // UNTESTED - "error: unhandled parse error", &$1); - }$ - | DeclareConstant - | DeclareFunction - | DeclareStruct + tok_err(c, // UNTESTED + "error: unhandled parse error", &$1); + }$ + | DeclareConstant + | DeclareFunction + | DeclareStruct ## top level grammar @@ -4856,21 +4851,24 @@ searching through for the Nth constant for decreasing N. $TERM const DeclareConstant -> const { IN OptNL ConstList OUT OptNL } Newlines - | const { SimpleConstList } Newlines - | const IN OptNL ConstList OUT Newlines - | const SimpleConstList Newlines + | const { SimpleConstList } Newlines + | const IN OptNL ConstList OUT Newlines + | const SimpleConstList Newlines ConstList -> ConstList SimpleConstLine - | SimpleConstLine + | SimpleConstLine + SimpleConstList -> SimpleConstList ; Const - | Const - | SimpleConstList ; + | Const + | SimpleConstList ; + SimpleConstLine -> SimpleConstList Newlines - | ERROR Newlines ${ tok_err(c, "Syntax error in constant", &$1); }$ + | ERROR Newlines ${ tok_err(c, "Syntax error in constant", &$1); }$ $*type CType -> Type ${ $0 = $<1; }$ - | ${ $0 = NULL; }$ + | ${ $0 = NULL; }$ + $void Const -> IDENTIFIER :: CType = Expression ${ { int ok; @@ -5024,32 +5022,32 @@ is a bit more interesting at this level. $*variable DeclareFunction -> func FuncName ( OpenScope ArgsLine ) Block Newlines ${ - $0 = declare_function(c, $