Showing the changes to src/core/chuck_parse.cpp (1 of 181 files). Show all
Unified · Split
| @@ -0,0 +1,230 @@ | ||
| 1 | +/*---------------------------------------------------------------------------- | |
| 2 | + ChucK Concurrent, On-the-fly Audio Programming Language | |
| 3 | + Compiler and Virtual Machine | |
| 4 | + | |
| 5 | + Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved. | |
| 6 | + http://chuck.stanford.edu/ | |
| 7 | + http://chuck.cs.princeton.edu/ | |
| 8 | + | |
| 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. | |
| 13 | + | |
| 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. | |
| 18 | + | |
| 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 | +-----------------------------------------------------------------------------*/ | |
| 24 | + | |
| 25 | +//----------------------------------------------------------------------------- | |
| 26 | +// file: chuck_parse.cpp | |
| 27 | +// desc: chuck parser interface (using flex and bison) | |
| 28 | +// | |
| 29 | +// author: Ge Wang (ge@ccrma.stanford.edu | gewang@cs.princeton.edu) | |
| 30 | +// date: Autumn 2002 - original in chuck_main.cpp | |
| 31 | +// Autumn 2005 - this file | |
| 32 | +//----------------------------------------------------------------------------- | |
| 33 | +#include "chuck_parse.h" | |
| 34 | +#include "chuck_errmsg.h" | |
| 35 | +#include <string.h> | |
| 36 | +using namespace std; | |
| 37 | + | |
| 38 | + | |
| 39 | +// global | |
| 40 | +static char g_filename[1024] = ""; | |
| 41 | + | |
| 42 | +// external | |
| 43 | +extern "C" { | |
| 44 | + extern FILE *yyin; | |
| 45 | +} | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | +//----------------------------------------------------------------------------- | |
| 51 | +// name: open_cat_ck() | |
| 52 | +// desc: ... | |
| 53 | +//----------------------------------------------------------------------------- | |
| 54 | +FILE * open_cat_ck( c_str fname ) | |
| 55 | +{ | |
| 56 | + FILE * fd = NULL; | |
| 57 | + if( !(fd = fopen( fname, "rb" )) ) | |
| 58 | + if( !strstr( fname, ".ck" ) && !strstr( fname, ".CK" ) ) | |
| 59 | + { | |
| 60 | + strcat( fname, ".ck" ); | |
| 61 | + fd = fopen( fname, "rb" ); | |
| 62 | + } | |
| 63 | + return fd; | |
| 64 | +} | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | +//----------------------------------------------------------------------------- | |
| 69 | +// name: win32_tmpfile() | |
| 70 | +// desc: replacement for broken tmpfile() on Windows Vista + 7 | |
| 71 | +//----------------------------------------------------------------------------- | |
| 72 | +#ifdef __PLATFORM_WIN32__ | |
| 73 | + | |
| 74 | +#include <windows.h> | |
| 75 | + | |
| 76 | +FILE *win32_tmpfile() | |
| 77 | +{ | |
| 78 | + char tmp_path[MAX_PATH]; | |
| 79 | + char file_path[MAX_PATH]; | |
| 80 | + FILE * file = NULL; | |
| 81 | + | |
| 82 | + if(GetTempPath(256, tmp_path) == 0) | |
| 83 | + return NULL; | |
| 84 | + | |
| 85 | + if(GetTempFileName(tmp_path, "mA", 0, file_path) == 0) | |
| 86 | + return NULL; | |
| 87 | + | |
| 88 | + file = fopen(file_path, "wb+D"); | |
| 89 | + | |
| 90 | + return file; | |
| 91 | +} | |
| 92 | + | |
| 93 | +#endif | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | +//----------------------------------------------------------------------------- | |
| 98 | +// name: chuck_parse() | |
| 99 | +// desc: ... | |
| 100 | +//----------------------------------------------------------------------------- | |
| 101 | +t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code ) | |
| 102 | +{ | |
| 103 | + t_CKBOOL clo = FALSE; | |
| 104 | + t_CKBOOL ret = FALSE; | |
| 105 | + | |
| 106 | + // sanity check | |
| 107 | + if( fd && code ) | |
| 108 | + { | |
| 109 | + CK_FPRINTF_STDERR( "[chuck](via parser): (internal) both fd and code specified!\n" ); | |
| 110 | + return FALSE; | |
| 111 | + } | |
| 112 | + | |
| 113 | + // prepare code | |
| 114 | + if( code ) | |
| 115 | + { | |
| 116 | + // ! | |
| 117 | + assert( fd == NULL ); | |
| 118 | + // generate temp file | |
| 119 | +#ifdef __PLATFORM_WIN32__ | |
| 120 | + fd = win32_tmpfile(); | |
| 121 | +#else | |
| 122 | + fd = tmpfile(); | |
| 123 | +#endif | |
| 124 | + // flag it to close | |
| 125 | + clo = TRUE; | |
| 126 | + // write | |
| 127 | + fwrite( code, sizeof(char), strlen(code), fd ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + /* | |
| 131 | + // use code from memory buffer if its available | |
| 132 | + if( code ) | |
| 133 | + { | |
| 134 | + // copy name | |
| 135 | + strcpy( g_filename, fname ); | |
| 136 | + // reset | |
| 137 | + if( EM_reset( g_filename, NULL ) == FALSE ) goto cleanup; | |
| 138 | + | |
| 139 | + // TODO: clean g_program | |
| 140 | + g_program = NULL; | |
| 141 | + | |
| 142 | + // clean | |
| 143 | + yyrestart( NULL ); | |
| 144 | + | |
| 145 | + // load string (yy_scan_string will copy the C string) | |
| 146 | + YY_BUFFER_STATE ybs = yy_scan_string( code ); | |
| 147 | + if( !ybs ) goto cleanup; | |
| 148 | + | |
| 149 | + // parse | |
| 150 | + if( !( yyparse() == 0 ) ) goto cleanup; | |
| 151 | + | |
| 152 | + // delete the lexer buffer | |
| 153 | + yy_delete_buffer( ybs ); | |
| 154 | + | |
| 155 | + } | |
| 156 | + */ | |
| 157 | + | |
| 158 | + // remember filename | |
| 159 | + strcpy( g_filename, fname ); | |
| 160 | + | |
| 161 | + // test it | |
| 162 | + if( !fd ) { | |
| 163 | + fd = open_cat_ck( g_filename ); | |
| 164 | + if( !fd ) strcpy( g_filename, fname ); | |
| 165 | + else clo = TRUE; | |
| 166 | + } | |
| 167 | + | |
| 168 | + // reset | |
| 169 | + if( EM_reset( g_filename, fd ) == FALSE ) goto cleanup; | |
| 170 | + | |
| 171 | + // lexer/parser | |
| 172 | + // TODO: if( yyin ) { fclose( yyin ); yyin = NULL; } | |
| 173 | + | |
| 174 | + // if no fd, open | |
| 175 | + if( !fd ) { fd = fopen( g_filename, "r" ); if( fd ) clo = TRUE; } | |
| 176 | + // if still none | |
| 177 | + if( !fd ) { EM_error2( 0, "no such file or directory" ); goto cleanup; } | |
| 178 | + // set to beginning | |
| 179 | + else fseek( fd, 0, SEEK_SET ); | |
| 180 | + | |
| 181 | + // reset yyin to fd | |
| 182 | + yyrestart( fd ); | |
| 183 | + | |
| 184 | + // check | |
| 185 | + if( yyin == NULL ) goto cleanup; | |
| 186 | + | |
| 187 | + // TODO: clean g_program | |
| 188 | + g_program = NULL; | |
| 189 | + | |
| 190 | + // parse | |
| 191 | + if( !(yyparse( ) == 0) ) goto cleanup; | |
| 192 | + | |
| 193 | + // flag success | |
| 194 | + ret = TRUE; | |
| 195 | + | |
| 196 | +cleanup: | |
| 197 | + | |
| 198 | + // done | |
| 199 | + if( clo ) fclose( fd ); | |
| 200 | + | |
| 201 | + return ret; | |
| 202 | +} | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | +//------------------------------------------------------------------------------ | |
| 208 | +// name: reset_parse() | |
| 209 | +// desc: ... | |
| 210 | +//------------------------------------------------------------------------------ | |
| 211 | +void reset_parse( ) | |
| 212 | +{ | |
| 213 | + // empty file name | |
| 214 | + EM_change_file( NULL ); | |
| 215 | +} | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | +//----------------------------------------------------------------------------- | |
| 221 | +// name: parseLine() | |
| 222 | +// desc: ... | |
| 223 | +//----------------------------------------------------------------------------- | |
| 224 | +t_CKBOOL SyntaxQuery::parseLine( const std::string & line, SyntaxTokenList & tokens ) | |
| 225 | +{ | |
| 226 | + // clear the token list | |
| 227 | + tokens.howmany = 0; | |
| 228 | + | |
| 229 | + return TRUE; | |
| 230 | +} | |