*** empty log message ***
[chuck-blob.git] / v2 / chuck_parse.cpp
blob358fdaa7dc342f1aab47a646396d165494bcc4fd
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 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_parse.cpp
27 // desc: chuck parser interface
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // date: Summer 2005
32 //-----------------------------------------------------------------------------
33 #include "chuck_parse.h"
34 #include "chuck_errmsg.h"
35 #include <string.h>
36 using namespace std;
39 // global
40 static char g_filename[1024] = "";
42 // external
43 extern "C" {
44 extern FILE *yyin;
48 //-----------------------------------------------------------------------------
49 // name: open_cat_ck()
50 // desc: ...
51 //-----------------------------------------------------------------------------
52 FILE * open_cat_ck( c_str fname )
54 FILE * fd = NULL;
55 if( !(fd = fopen( fname, "rb" )) )
56 if( !strstr( fname, ".ck" ) && !strstr( fname, ".CK" ) )
58 strcat( fname, ".ck" );
59 fd = fopen( fname, "rb" );
61 return fd;
67 //-----------------------------------------------------------------------------
68 // name: chuck_parse()
69 // desc: ...
70 //-----------------------------------------------------------------------------
71 t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code )
73 t_CKBOOL clo = FALSE;
74 t_CKBOOL ret = FALSE;
76 // sanity check
77 if( fd && code )
79 fprintf( stderr, "[chuck](via parser): (internal) both fd and code specified!\n" );
80 return FALSE;
83 // prepare code
84 if( code )
86 // !
87 assert( fd == NULL );
88 // generate temp file
89 fd = tmpfile();
90 // flag it to close
91 clo = TRUE;
92 // write
93 fwrite( code, sizeof(char), strlen(code), fd );
97 // use code from memory buffer if its available
98 if( code )
100 // copy name
101 strcpy( g_filename, fname );
102 // reset
103 if( EM_reset( g_filename, NULL ) == FALSE ) goto cleanup;
105 // TODO: clean g_program
106 g_program = NULL;
108 // clean
109 yyrestart( NULL );
111 // load string (yy_scan_string will copy the C string)
112 YY_BUFFER_STATE ybs = yy_scan_string( code );
113 if( !ybs ) goto cleanup;
115 // parse
116 if( !( yyparse() == 0 ) ) goto cleanup;
118 // delete the lexer buffer
119 yy_delete_buffer( ybs );
124 // remember filename
125 strcpy( g_filename, fname );
127 // test it
128 if( !fd ) {
129 fd = open_cat_ck( g_filename );
130 if( !fd ) strcpy( g_filename, fname );
131 else clo = TRUE;
134 // reset
135 if( EM_reset( g_filename, fd ) == FALSE ) goto cleanup;
137 // lexer/parser
138 // TODO: if( yyin ) { fclose( yyin ); yyin = NULL; }
140 // if no fd, open
141 if( !fd ) { fd = fopen( g_filename, "r" ); if( fd ) clo = TRUE; }
142 // if still none
143 if( !fd ) { EM_error2( 0, "no such file or directory" ); goto cleanup; }
144 // set to beginning
145 else fseek( fd, 0, SEEK_SET );
147 // reset yyin to fd
148 yyrestart( fd );
150 // check
151 if( yyin == NULL ) goto cleanup;
153 // TODO: clean g_program
154 g_program = NULL;
156 // parse
157 if( !(yyparse( ) == 0) ) goto cleanup;
159 // flag success
160 ret = TRUE;
162 cleanup:
164 // done
165 if( clo ) fclose( fd );
167 return ret;
173 //------------------------------------------------------------------------------
174 // name: reset_parse()
175 // desc: ...
176 //------------------------------------------------------------------------------
177 void reset_parse( )
179 // empty file name
180 EM_change_file( NULL );
186 //-----------------------------------------------------------------------------
187 // name: parseLine()
188 // desc: ...
189 //-----------------------------------------------------------------------------
190 t_CKBOOL SyntaxQuery::parseLine( const std::string & line, SyntaxTokenList & tokens )
192 // clear the token list
193 tokens.howmany = 0;
195 return TRUE;