1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
26 * errmsg.cpp - functions used in all phases of the compiler to give
27 * error messages about the Tiger program.
34 #include "chuck_utils.h"
35 #include "chuck_errmsg.h"
38 t_CKBOOL anyErrors
= FALSE
;
39 static c_str fileName
= "";
40 static int lineNum
= 1;
43 static char g_buffer
[1024] = "";
44 static char g_lasterror
[1024] = "[chuck]: (no error)";
49 typedef struct intList
{int i
; struct intList
*rest
;} *IntList
;
50 static IntList linePos
=NULL
;
53 static IntList
intList( int i
, IntList rest
)
55 IntList l
= (IntList
)checked_malloc(sizeof *l
);
65 linePos
= intList(EM_tokPos
, linePos
);
69 const char * mini( const char * str
)
71 int len
= strlen( str
);
72 const char * p
= str
+ len
;
73 while( p
!= str
&& *p
!= '/' && *p
!= '\\' ) p
--;
74 return ( p
== str
|| strlen(p
+1) == 0 ? p
: p
+1 );
78 void EM_error( int pos
, char *message
, ... )
81 IntList lines
= linePos
;
85 while( lines
&& lines
->i
>= pos
)
91 fprintf( stderr
, "('%s'):", *fileName
? mini(fileName
) : "chuck" );
92 sprintf( g_lasterror
, "('%s'):", *fileName
? mini(fileName
) : "chuck" );
93 if(lines
) fprintf(stderr
, "line(%d).char(%d):", num
, pos
-lines
->i
);
94 if(lines
) { sprintf( g_buffer
, "line(%d).char(%d):", num
, pos
-lines
->i
); strcat( g_lasterror
, g_buffer
); }
95 fprintf(stderr
, " " );
96 strcat( g_lasterror
, " " );
97 va_start(ap
, message
);
98 vfprintf(stderr
, message
, ap
);
99 vsprintf( g_buffer
, message
, ap
);
101 fprintf(stderr
, "\n");
102 strcat( g_lasterror
, g_buffer
);
106 void EM_error2( int line
, char * message
, ... )
110 fprintf( stderr
, "[%s]:", *fileName
? mini(fileName
) : "chuck" );
111 sprintf( g_lasterror
, "[%s]:", *fileName
? mini(fileName
) : "chuck" );
112 if(line
) fprintf( stderr
, "line(%d):", line
);
113 if(line
) { sprintf( g_buffer
, "line(%d):", line
); strcat( g_lasterror
, g_buffer
); }
114 fprintf( stderr
, " " );
115 strcat( g_lasterror
, " " );
117 va_start( ap
, message
);
118 vfprintf( stderr
, message
, ap
);
119 vsprintf( g_buffer
, message
, ap
);
122 strcat( g_lasterror
, g_buffer
);
123 fprintf( stderr
, "\n" );
127 void EM_error3( char * message
, ... )
131 g_lasterror
[0] = '\0';
134 va_start( ap
, message
);
135 vfprintf( stderr
, message
, ap
);
136 vsprintf( g_buffer
, message
, ap
);
139 strcat( g_lasterror
, g_buffer
);
140 fprintf( stderr
, "\n" );
144 t_CKBOOL
EM_reset( c_str fname
, FILE * fd
)
146 anyErrors
= FALSE
; fileName
= fname
? fname
: (c_str
)""; lineNum
= 1; EM_lineNum
= 1;
147 linePos
= intList( 0, NULL
);
149 // if( yyin ) fclose( yyin );
151 else yyin
= fopen( fname
, "r" );
153 EM_error2( 0, "no such file or directory" );
155 fseek( yyin
, 0, SEEK_SET
);
162 const char * EM_lasterror()