Skip to content

Commit

Permalink
Add the displaying of source code and point to the location of an err…
Browse files Browse the repository at this point in the history
…or in error.c

Change benchmarking to run each test multiple times (5), and reduce the number of loops in each test ( /100) so that benchmarking is quicker to run, and average the results for consistency.  The results no longer equate to the acutal time the test took in sec.
  • Loading branch information
James Parkinson committed Jun 30, 2021
1 parent 55c8914 commit ac4b2b4
Show file tree
Hide file tree
Showing 67 changed files with 488 additions and 409 deletions.
91 changes: 74 additions & 17 deletions awka/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ static int missing_rparen[] =
static int missing_rbrace[] =
{ EOF, a_BEGIN, a_END , 0 } ;

static void missing( c, n , ln)
static void
missing( c, n , ln)
int c ;
char *n ;
int ln ;
Expand All @@ -120,7 +121,7 @@ static void missing( c, n , ln)
{ s0 = pfile_name ; s1 = ": " ; }
else s0 = s1 = "" ;

errmsg(0, "%s%sline %u: missing %c near %s" ,s0, s1, ln, c, n) ;
errmsg(0, "%s%sline %u: missing %c near %s" ,s0, s1, ln, c, n) ;
}

void yyerror(s)
Expand Down Expand Up @@ -168,7 +169,7 @@ void yyerror(s)
switch ( current_token )
{
case UNEXPECTED :
unexpected_char() ;
unexpected_char() ;
goto done ;

case BAD_DECIMAL :
Expand Down Expand Up @@ -211,9 +212,38 @@ void errmsg VA_ALIST2(int , errnum, char *, format)
fprintf( stderr, "\n") ;
}

char *
char_repeat( int n, char c )
{
char * dest = malloc(n+1);
memset(dest, c, n);
dest[n] = '\0';
return dest;
}

char *
get_source_line() {
FILE *fp = NULL;
static int bufferLength = 254;
char *buffer = malloc( bufferLength + 1);
int linecount = token_lineno - 1;

buffer[0] = '\0';
if ( !pfile_name ) return (buffer);
if (!(fp = fopen(pfile_name, "r"))) return (buffer);

while(fgets(buffer, bufferLength, fp)) {
if ( !linecount-- )
break;
}
fclose(fp);
return (buffer) ;
}

void compile_error VA_ALIST(char *, format)
va_list args ;
char *s0, *s1 ;
char *lnstr = malloc( 20 );
char *s0, *s1, *sc, *fill ;

/* with multiple program files put program name in
error message */
Expand All @@ -222,11 +252,19 @@ void compile_error VA_ALIST(char *, format)
else
{ s0 = s1 = "" ; }

fprintf(stderr, "%s: %s%sline %u: " , progname, s0, s1,token_lineno) ;
sprintf(lnstr, "line %d: ",token_lineno);
sc = get_source_line();

fprintf(stderr, "%s: %s%s%s%s" , progname, s0, s1, lnstr, sc) ;

fill = char_repeat((int) line_pos-1,' ');
fprintf(stderr, "%s: %s%s%s%s^ " , progname, s0, s1, lnstr, fill) ;
VA_START(args, char *, format) ;
vfprintf(stderr, format, args) ;
va_end(args) ;
fprintf(stderr, "\n") ;
free(sc);
free(lnstr);
if ( ++compile_error_count == MAX_COMPILE_ERRORS ) exit(2) ;
}

Expand All @@ -245,16 +283,16 @@ void rt_error VA_ALIST( char *, format)

void bozo(s)
char *s ;
{
errmsg(0, "bozo: %s" , s) ;
{
errmsg(0, "bozo: %s" , s) ;
exit(3) ;
}

void overflow(s, size)
char *s ; unsigned size ;
{
{
errmsg(0 , "program limit exceeded: %s size=%u", s, size) ;
exit(2) ;
exit(2) ;
}


Expand All @@ -271,17 +309,36 @@ static void rt_where()
/* run time */
void rt_overflow(s, size)
char *s ; unsigned size ;
{
{
errmsg(0 , "program limit exceeded: %s size=%u", s, size) ;
rt_where() ;
exit(2) ;
}

/* compile time */
void
unexpected_char()
{ int c = yylval.ival ;
unexpected_char( void )
{
int c = yylval.ival ;
char *lnstr = malloc( 20 );
char *s0, *s1, *sc, *fill;

if ( pfile_name )
{ s0 = pfile_name ; s1 = ": " ; }
else s0 = s1 = "" ;

sprintf(lnstr, "line %d: ",token_lineno);

sc = get_source_line();
fprintf(stderr, "%s: %s%s%s%s", progname, s0, s1, lnstr, sc) ;
free(sc);

/* point to error location then show the error message */
fill = char_repeat((int) line_pos-1,' ');
fprintf(stderr, "%s: %s%s%s%s^ ", progname, s0, s1, lnstr, fill) ;
free(fill);
free(lnstr);

fprintf(stderr, "%s: %u: ", progname, token_lineno) ;
if ( c > ' ' && c < 127 )
fprintf(stderr, "unexpected character '%c'\n" , c) ;
else
Expand Down Expand Up @@ -320,9 +377,9 @@ void type_error(p)
/* a minimal vfprintf */
int simple_vfprintf( fp, format, argp)
FILE *fp ;
char *format ;
char *format ;
va_list argp ;
{
{
char *q , *p, *t ;
int l_flag ;
char xbuff[64] ;
Expand All @@ -331,7 +388,7 @@ int simple_vfprintf( fp, format, argp)
xbuff[0] = '%' ;

while ( *q != 0 )
{
{
if ( *q != '%' )
{
putc(*q, fp) ; q++ ; continue ;
Expand Down Expand Up @@ -374,7 +431,7 @@ int simple_vfprintf( fp, format, argp)
break ;

default:
putc('%', fp) ;
putc('%', fp) ;
q = p ;
break ;
}
Expand Down
69 changes: 60 additions & 9 deletions awka/scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@
#include "files.h"

/* static functions */
static void PROTO(scan_fillbuff, (void)) ;
static void PROTO(scan_open, (void)) ;
static int PROTO(slow_next, (void)) ;
static void PROTO(eat_comment, (void)) ;
static void PROTO(eat_semi_colon, (void)) ;
static void PROTO(scan_fillbuff, (void)) ;
static void PROTO(scan_open, (void)) ;
static int PROTO(slow_next, (void)) ;
static unsigned char PROTO(next, (void)) ;
static void PROTO(un_next, (void)) ;
static void PROTO(eat_comment, (void)) ;
static void PROTO(eat_semi_colon, (void)) ;
static double PROTO(collect_decimal, (int, int *)) ;
static int PROTO(collect_string, (void)) ;
static int PROTO(collect_RE, (void)) ;
static int PROTO(collect_string, (void)) ;
static int PROTO(collect_RE, (void)) ;


/*-----------------------------
Expand All @@ -74,6 +76,7 @@ extern char **vardeclare;
extern int vdec_no, vdec_allc;

int _true_re = 0;
int line_pos = 0 ;

void PROTO(init_extbi, (void));

Expand Down Expand Up @@ -144,6 +147,16 @@ scan_cleanup()
scan_code['\r'] = SC_UNEXPECTED ;
}

static unsigned char
next()
{
line_pos++;
return (*buffp ? *buffp++ : slow_next());
}

static void
un_next() { buffp--; line_pos--; }

/*--------------------------------
global variables shared by yyparse() and yylex()
and used for error messages too
Expand All @@ -157,6 +170,7 @@ int paren_cnt ;
int brace_cnt ;
int print_flag ; /* changes meaning of '>' */
int getline_flag ; /* changes meaning of '<' */
//int line_pos = 0 ;


/*----------------------------------------
Expand Down Expand Up @@ -207,10 +221,12 @@ slow_next()
ZFREE(q) ;
scan_open() ;
token_lineno = lineno = 1 ;
line_pos = 0 ;
}
else break /* real eof */ ;
}

line_pos++;
return *buffp++ ; /* note can un_next() , eof which is zero */
}

Expand Down Expand Up @@ -250,7 +266,7 @@ eat_comment()
{
/* navigate to the next word */
while (c == ' ' || c == '\t') c = next() ;
if (c == '\n' || c == '\0') return ;
if (c == '\n' || c == '\0') { line_pos = 0 ; return ; }

i = 0 ;
while (c != ' ' && c != '\t' && c != '\n' && c != '\0')
Expand Down Expand Up @@ -335,7 +351,9 @@ eat_nl() /* eat all space including newlines */

while (scan_code[c = next()] == SC_SPACE) ;
if (c == '\n')
{
token_lineno = ++lineno ;
}
else if (c == 0)
{
un_next() ;
Expand All @@ -356,8 +374,10 @@ eat_nl() /* eat all space including newlines */

default:
un_next() ;
line_pos = 0;
return ;
}
line_pos = 0;
}

int
Expand Down Expand Up @@ -392,6 +412,7 @@ yylex()
if (c == '\n')
{
token_lineno = ++lineno ;
line_pos = 0;
goto reswitch ;
}

Expand Down Expand Up @@ -451,7 +472,36 @@ yylex()
ct_ret(COMMA) ;

case SC_MUL:
test1_ret('=', MUL_ASG, MUL) ;
// * or *= or **=
//test1_ret('=', MUL_ASG, MUL) ;
switch (next())
{
case '*': // **
yylval.ival = '*' ;
string_buff[0] =
string_buff[1] = '*' ;
string_buff[2] = 0 ;
if (c = next() == '=')
{
// **=
yylval.ival = '=' ;
string_buff[2] = '=';
string_buff[3] = 0;
ct_ret(POW_ASG);
}
yylval.ival = c ;
string_buff[2] = c;
string_buff[3] = 0;
ct_ret(UNEXPECTED);

case '=': // *=
ct_ret(MUL_ASG) ;

default: // *
un_next() ;
ct_ret(MUL) ;
}


case SC_DIV:
{
Expand Down Expand Up @@ -1109,6 +1159,7 @@ collect_string()
{
p-- ;
lineno++ ;
line_pos = 0;
}
else if (c == 0) un_next() ;
else
Expand Down
4 changes: 1 addition & 3 deletions awka/scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ the GNU General Public License, version 2, 1991.
#include "parse.h"
#endif

int line_pos;

extern char scan_code[256] ;

Expand Down Expand Up @@ -88,9 +89,6 @@ void PROTO( unexpected_char, (void) ) ;

#define ct_ret(x) return current_token = (x)

#define next() (*buffp ? *buffp++ : slow_next())
#define un_next() buffp--

#define test1_ret(c,x,d) if ( next() == (c) ) ct_ret(x) ;\
else { un_next() ; ct_ret(d) ; }

Expand Down
Loading

0 comments on commit ac4b2b4

Please sign in to comment.