*** empty log message ***
[chuck-blob.git] / exile / v1 / src / chuck_errmsg.cpp
blobc8d8c8c3aee2ab9e84703c29cec9b51ad32d8123
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
22 U.S.A.
23 -----------------------------------------------------------------------------*/
26 * errmsg.cpp - functions used in all phases of the compiler to give
27 * error messages about the Tiger program.
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <string.h>
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;
41 int EM_tokPos = 0;
42 int EM_lineNum = 1;
43 static char g_buffer[1024] = "";
44 static char g_lasterror[1024] = "[chuck]: (no error)";
46 extern "C" {
47 extern FILE *yyin;
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);
56 l->i=i; l->rest=rest;
57 return l;
61 void EM_newline(void)
63 lineNum++;
64 EM_lineNum++;
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, ... )
80 va_list ap;
81 IntList lines = linePos;
82 int num = lineNum;
84 anyErrors = TRUE;
85 while( lines && lines->i >= pos )
87 lines = lines->rest;
88 num--;
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 );
100 va_end(ap);
101 fprintf(stderr, "\n");
102 strcat( g_lasterror, g_buffer );
106 void EM_error2( int line, char * message, ... )
108 va_list ap;
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 );
120 va_end( ap );
122 strcat( g_lasterror, g_buffer );
123 fprintf( stderr, "\n" );
127 void EM_error3( char * message, ... )
129 va_list ap;
131 g_lasterror[0] = '\0';
132 g_buffer[0] = '\0';
134 va_start( ap, message );
135 vfprintf( stderr, message, ap );
136 vsprintf( g_buffer, message, ap );
137 va_end( 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 );
150 if( fd ) yyin = fd;
151 else yyin = fopen( fname, "r" );
152 if (!yyin)
153 EM_error2( 0, "no such file or directory" );
154 else
155 fseek( yyin, 0, SEEK_SET );
157 EM_lineNum = 0;
158 return (yyin != 0);
162 const char * EM_lasterror()
164 return g_lasterror;