aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Davies <sdavies@sdc.com.au>2014-09-03 12:23:22 +0930
committerStephen Davies <sdavies@sdc.com.au>2014-09-03 12:23:22 +0930
commitae131ae925aa5ba54fb7f8ae105e5cbbf3655f06 (patch)
tree59f9636292a874af6c4d5c5018810e933992cd64
parent8006ef4ca16fa8264dcc1e849783e1f4ce4978a1 (diff)
downloadegawk-ae131ae925aa5ba54fb7f8ae105e5cbbf3655f06.tar.gz
egawk-ae131ae925aa5ba54fb7f8ae105e5cbbf3655f06.tar.bz2
egawk-ae131ae925aa5ba54fb7f8ae105e5cbbf3655f06.zip
ll except functions looking OK
-rw-r--r--awkgram.y50
-rw-r--r--profile.c72
2 files changed, 78 insertions, 44 deletions
diff --git a/awkgram.y b/awkgram.y
index 664b0c84..1cf22987 100644
--- a/awkgram.y
+++ b/awkgram.y
@@ -209,7 +209,6 @@ program
| program nls
| program LEX_EOF
{
-/* add any outstanding comments to end. But how? */
next_sourcefile();
if (sourcefile == srcfiles)
process_deferred();
@@ -291,9 +290,22 @@ library
pattern
: /* empty */
- { $$ = NULL; rule = Rule; }
+ { rule = Rule;
+ if (comment != NULL){
+ $$ = list_create(comment);
+ comment = NULL;
+ } else
+ $$ = NULL;
+ }
| exp
- { $$ = $1; rule = Rule; }
+ { rule = Rule;
+ if (comment != NULL){
+ $$ = list_prepend($1, comment);
+ comment = NULL;
+ } else
+ $$ = $1;
+ }
+
| exp ',' opt_nls exp
{
INSTRUCTION *tp;
@@ -323,6 +335,7 @@ pattern
| LEX_BEGIN
{
static int begin_seen = 0;
+ INSTRUCTION *ip;
if (do_lint_old && ++begin_seen == 2)
warning_ln($1->source_line,
_("old awk does not support multiple `BEGIN' or `END' rules"));
@@ -359,10 +372,12 @@ pattern
action
: l_brace statements r_brace opt_semi opt_nls
{
+ INSTRUCTION *ip;
if ($2 == NULL)
- $$ = list_create(instruction(Op_no_op));
+ ip = list_create(instruction(Op_no_op));
else
- $$ = $2;
+ ip = $2;
+ $$ = ip;
}
;
@@ -467,7 +482,7 @@ statements
}
} else {
if (comment != NULL){
- list_append($2, comment);
+ list_prepend($2, comment);
comment = NULL;
}
$$ = list_merge($1, $2);
@@ -488,7 +503,7 @@ statement
: semi opt_nls
{ $$ = NULL; }
| l_brace statements r_brace
- { $$ = $2; }
+ { $$ = $2; }
| if_statement
{
if (do_pretty_print)
@@ -2240,6 +2255,8 @@ mk_program()
cp = end_block;
else
cp = list_merge(begin_block, end_block);
+ if (comment != NULL)
+ (void) list_append(cp, comment);
(void) list_append(cp, ip_atexit);
(void) list_append(cp, instruction(Op_stop));
@@ -2272,6 +2289,8 @@ mk_program()
if (begin_block != NULL)
cp = list_merge(begin_block, cp);
+ if (comment != NULL)
+ (void) list_append(cp, comment);
(void) list_append(cp, ip_atexit);
(void) list_append(cp, instruction(Op_stop));
@@ -2969,7 +2988,7 @@ int get_comment(void)
} while (isspace(c) && c != END_FILE) ;
if ( c == END_FILE)
break;
- else if (c != '\#'){
+ else if (c != '#'){
pushback();
break;
} else
@@ -2977,17 +2996,9 @@ int get_comment(void)
} else
break;
}
- if (comment != NULL) {
- size_t new = comment->memory->stlen + (tok - tokstart) + 2;
- erealloc(comment->memory->stptr, char *, new, "yylex");
- memcpy(comment->memory->stptr + comment->memory->stlen, tokstart, (tok - tokstart));
- comment->memory->stlen += (tok - tokstart);
- comment->memory->stptr[comment->memory->stlen] = '\0';
- } else {
- comment = bcalloc(Op_comment, 1, sourceline);
- comment->source_file = source;
- comment->memory = make_str_node(tokstart, tok - tokstart, 0);
- }
+ comment = bcalloc(Op_comment, 1, sourceline);
+ comment->source_file = source;
+ comment->memory = make_str_node(tokstart, tok - tokstart, 0);
return c;
}
@@ -3228,7 +3239,6 @@ retry:
return lasttok = NEWLINE_EOF;
}
}
-// sourceline++;
return lasttok = NEWLINE;
case '@':
diff --git a/profile.c b/profile.c
index 51ef9715..4db29c9a 100644
--- a/profile.c
+++ b/profile.c
@@ -36,7 +36,8 @@ static bool is_scalar(int type);
static int prec_level(int type);
static void pp_push(int type, char *s, int flag);
static NODE *pp_pop(void);
-static void pp_free(NODE *n);
+static void pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header);
+static void print_comment(INSTRUCTION *pc, long in);
const char *redir2str(int redirtype);
#define pp_str vname
@@ -174,9 +175,12 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header)
char *str;
NODE *t2;
INSTRUCTION *ip;
+ INSTRUCTION *ic;
+ INSTRUCTION *i2;
NODE *m;
char *tmp;
int rule;
+ long lind;
static int rule_count[MAXRULE];
for (pc = startp; pc != endp; pc = pc->nexti) {
@@ -189,16 +193,30 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header)
rule = pc->in_rule;
if (rule != Rule) {
+ ip = (pc + 1)->firsti;
+ if (ip->opcode == Op_comment){
+ /* print pre-begin/end comments */
+ print_comment(ip, 0);
+ ip = pc->nexti->nexti;
+ }
if (! rule_count[rule]++)
fprintf(prof_fp, _("\t# %s block(s)\n\n"), ruletab[rule]);
fprintf(prof_fp, "\t%s {\n", ruletab[rule]);
- ip = (pc + 1)->firsti;
} else {
if (! rule_count[rule]++)
fprintf(prof_fp, _("\t# Rule(s)\n\n"));
- ip = pc->nexti;
- indent(ip->exec_count);
+ ic = ip = pc->nexti;
+ i2 = (pc + 1)->firsti;
+ lind = ip->exec_count;
+ /*print pre-block comments */
+ if(ip->opcode == Op_exec_count && ip->nexti->opcode == Op_comment)ip = ip->nexti;
+ if(ip->opcode == Op_comment){
+ print_comment(ip, lind);
+ if (ip->nexti->nexti == (pc + 1)->firsti)
+ ip = ip->nexti->nexti;
+ }
if (ip != (pc + 1)->firsti) { /* non-empty pattern */
+ indent(lind);
pprint(ip->nexti, (pc + 1)->firsti, false);
t1 = pp_pop();
fprintf(prof_fp, "%s {", t1->pp_str);
@@ -210,7 +228,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header)
fprintf(prof_fp, "\n");
} else {
- fprintf(prof_fp, "{\n");
+ fprintf(prof_fp, "\t{\n");
ip = (pc + 1)->firsti;
}
ip = ip->nexti;
@@ -874,27 +892,11 @@ cleanup:
break;
case Op_comment:
- {
- char *text;
- size_t count;
- bool after_newline = false;
-
- count = pc->memory->stlen;
- text = pc->memory->stptr;
-
- indent(SPACEOVER); /* is this correct? Where should comments go? */
- for (; count > 0; count--, text++) {
- if (after_newline) {
- indent(SPACEOVER);
- after_newline = false;
- }
- putc(*text, prof_fp);
- if (*text == '\n')
- after_newline = true;
- }
- }
+ print_comment(pc, 0);
break;
+ case Op_list:
+ break;
default:
cant_happen();
}
@@ -977,6 +979,28 @@ print_lib_list(FILE *prof_fp)
fprintf(prof_fp, "\n");
}
+/* print comment text with proper indentation */
+static void
+print_comment(INSTRUCTION* pc, long in){
+ char *text;
+ size_t count;
+ bool after_newline = false;
+
+ count = pc->memory->stlen;
+ text = pc->memory->stptr;
+
+ indent(in); /* is this correct? Where should comments go? */
+ for (; count > 0; count--, text++) {
+ if (after_newline) {
+ indent(in);
+ after_newline = false;
+ }
+ putc(*text, prof_fp);
+ if (*text == '\n')
+ after_newline = true;
+ }
+}
+
/* dump_prog --- dump the program */
/*