Fix the line number info for else.

It has bugged me for a while that the goto for else would display the if's
source line.
This commit is contained in:
Bill Currie 2012-12-01 16:37:38 +09:00
parent 7e4ec4cee4
commit 54289da416
4 changed files with 30 additions and 10 deletions

View file

@ -591,7 +591,7 @@ expr_t *incop_expr (int op, expr_t *e, int postop);
expr_t *array_expr (expr_t *array, expr_t *index); expr_t *array_expr (expr_t *array, expr_t *index);
expr_t *pointer_expr (expr_t *pointer); expr_t *pointer_expr (expr_t *pointer);
expr_t *address_expr (expr_t *e1, expr_t *e2, struct type_s *t); expr_t *address_expr (expr_t *e1, expr_t *e2, struct type_s *t);
expr_t *build_if_statement (expr_t *test, expr_t *s1, expr_t *s2); expr_t *build_if_statement (expr_t *test, expr_t *s1, expr_t *els, expr_t *s2);
expr_t *build_while_statement (expr_t *test, expr_t *statement, expr_t *build_while_statement (expr_t *test, expr_t *statement,
expr_t *break_label, expr_t *continue_label); expr_t *break_label, expr_t *continue_label);
expr_t *build_do_while_statement (expr_t *statement, expr_t *test, expr_t *build_do_while_statement (expr_t *statement, expr_t *test,

View file

@ -2250,7 +2250,7 @@ address_expr (expr_t *e1, expr_t *e2, type_t *t)
} }
expr_t * expr_t *
build_if_statement (expr_t *test, expr_t *s1, expr_t *s2) build_if_statement (expr_t *test, expr_t *s1, expr_t *els, expr_t *s2)
{ {
int line = pr.source_line; int line = pr.source_line;
string_t file = pr.source_file; string_t file = pr.source_file;
@ -2272,6 +2272,11 @@ build_if_statement (expr_t *test, expr_t *s1, expr_t *s2)
} }
append_expr (if_expr, s1); append_expr (if_expr, s1);
if (els) {
pr.source_line = els->line;
pr.source_file = els->file;
}
if (s2) { if (s2) {
expr_t *nl = new_label_expr (); expr_t *nl = new_label_expr ();
append_expr (if_expr, goto_expr (nl)); append_expr (if_expr, goto_expr (nl));

View file

@ -183,7 +183,7 @@ int yylex (void);
%type <expr> opt_expr fexpr expr element_list element %type <expr> opt_expr fexpr expr element_list element
%type <expr> optional_state_expr think opt_step texpr %type <expr> optional_state_expr think opt_step texpr
%type <expr> statement statements compound_statement %type <expr> statement statements compound_statement
%type <expr> label break_label continue_label %type <expr> else label break_label continue_label
%type <expr> unary_expr cast_expr opt_arg_list arg_list %type <expr> unary_expr cast_expr opt_arg_list arg_list
%type <switch_block> switch_block %type <switch_block> switch_block
%type <symbol> identifier %type <symbol> identifier
@ -1087,11 +1087,11 @@ statement
} }
| IF '(' texpr ')' statement %prec IFX | IF '(' texpr ')' statement %prec IFX
{ {
$$ = build_if_statement ($3, $5, 0); $$ = build_if_statement ($3, $5, 0, 0);
} }
| IF '(' texpr ')' statement ELSE statement | IF '(' texpr ')' statement else statement
{ {
$$ = build_if_statement ($3, $5, $7); $$ = build_if_statement ($3, $5, $6, $7);
} }
| FOR break_label continue_label | FOR break_label continue_label
'(' opt_expr ';' opt_expr ';' opt_expr ')' statement '(' opt_expr ';' opt_expr ';' opt_expr ')' statement
@ -1120,6 +1120,14 @@ statement
} }
; ;
else
: ELSE
{
// this is only to get the the file and line number info
$$ = new_nil_expr ();
}
;
label label
: /* empty */ : /* empty */
{ {

View file

@ -133,7 +133,7 @@ int yylex (void);
%type <symbol> program_head identifier_list subprogram_head %type <symbol> program_head identifier_list subprogram_head
%type <symtab> param_scope %type <symtab> param_scope
%type <param> arguments parameter_list %type <param> arguments parameter_list
%type <expr> compound_statement optional_statements statement_list %type <expr> compound_statement else optional_statements statement_list
%type <expr> statement procedure_statement %type <expr> statement procedure_statement
%type <expr> expression_list expression unary_expr primary variable name %type <expr> expression_list expression unary_expr primary variable name
%type <op> sign %type <op> sign
@ -352,13 +352,13 @@ statement
} }
| procedure_statement | procedure_statement
| compound_statement | compound_statement
| IF expression THEN statement ELSE statement | IF expression THEN statement else statement
{ {
$$ = build_if_statement ($2, $4, $6); $$ = build_if_statement ($2, $4, $5, $6);
} }
| IF expression THEN statement %prec IFX | IF expression THEN statement %prec IFX
{ {
$$ = build_if_statement ($2, $4, 0); $$ = build_if_statement ($2, $4, 0, 0);
} }
| WHILE expression DO statement | WHILE expression DO statement
{ {
@ -372,6 +372,13 @@ statement
} }
; ;
else
: ELSE
{
$$ = new_nil_expr ();
}
;
variable variable
: name : name
| name '[' expression ']' { $$ = array_expr ($1, $3); } | name '[' expression ']' { $$ = array_expr ($1, $3); }