| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 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 dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: chuck_parse.cpp |
| 28 | // desc: chuck parser interface (using flex and bison) |
| 29 | // |
| 30 | // author: Ge Wang ([email protected] | [email protected]) |
| 31 | // date: Autumn 2002 - original in chuck_main.cpp |
| 32 | // Autumn 2005 - this file |
| 33 | //----------------------------------------------------------------------------- |
| 34 | #include "chuck_parse.h" |
| 35 | #include "chuck_errmsg.h" |
| 36 | #include "chuck_compile.h" |
| 37 | #include "util_string.h" |
| 38 | #include "util_platforms.h" |
| 39 | |
| 40 | #include <string.h> |
| 41 | #include <sys/stat.h> |
| 42 | #if defined(__PLATFORM_WINDOWS__) |
| 43 | #include "dirent_win32.h" |
| 44 | #endif |
| 45 | |
| 46 | #include <string> |
| 47 | using namespace std; |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | //----------------------------------------------------------------------------- |
| 53 | // external from lexer |
| 54 | //----------------------------------------------------------------------------- |
| 55 | extern "C" { |
| 56 | extern FILE * yyin; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | //----------------------------------------------------------------------------- |
| 63 | // name: cleanup_AST() |
| 64 | // desc: clean up the abstract syntax tree |
| 65 | //----------------------------------------------------------------------------- |
| 66 | static void cleanup_AST() |
| 67 | { |
| 68 | // check |
| 69 | if( !g_program ) return; |
| 70 | |
| 71 | // log |
| 72 | EM_log( CK_LOG_FINE, "cleaning up abstract syntax tree..." ); |
| 73 | // do it |
| 74 | delete_program( g_program ); |
| 75 | // reset to NULL |
| 76 | g_program = NULL; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | //----------------------------------------------------------------------------- |
| 83 | // name: chuck_parse() |
| 84 | // desc: INPUT: chuck code (either from file or actual code) to be parsed |
| 85 | // OUTPUT: abstract syntax tree representation of the code |
| 86 | //----------------------------------------------------------------------------- |
| 87 | t_CKBOOL chuck_parse( Chuck_CompileTarget * target ) |
| 88 | { |
| 89 | // return value |
| 90 | t_CKBOOL ret = FALSE; |
| 91 | // file descriptor (should be open if compiling from file) |
| 92 | FILE * fd = target->fd2parse; |
| 93 | // our own lexer/parser buffer |
| 94 | YY_BUFFER_STATE yyCodeBuffer = NULL; |
| 95 | // where the code is coming from |
| 96 | CompileFileSource source; |
| 97 | |
| 98 | // check for conflict |
| 99 | if( fd && target->codeLiteral != "" ) |
| 100 | { |
| 101 | // no warning... quietly let the code literal take precedence | 1.5.4.0 (ge) |
| 102 | // CK_FPRINTF_STDERR( "[chuck](parser): (internal) code and FILE descriptor both present!\n" ); |
| 103 | // CK_FPRINTF_STDERR( "[chuck](parser): |- ignoring FILE descriptor...\n" ); |
| 104 | } |
| 105 | |
| 106 | // if actual code was passed in |
| 107 | if( target->codeLiteral != "" ) |
| 108 | { |
| 109 | // clean lexer |
| 110 | yyrestart( NULL ); |
| 111 | // set to initial condition | 1.5.2.4 (ge) added |
| 112 | yyinitial(); |
| 113 | // load string (yy_scan_string will copy the C string) |
| 114 | yyCodeBuffer = yy_scan_string( target->codeLiteral.c_str() ); |
| 115 | // if could not load |
| 116 | if( !yyCodeBuffer ) goto cleanup; |
| 117 | // set source |
| 118 | source.setCode( target->codeLiteral.c_str() ); |
| 119 | } |
| 120 | else if( fd != NULL ) |
| 121 | { |
| 122 | // set to beginning |
| 123 | fseek( fd, 0, SEEK_SET ); |
| 124 | // reset yyin to fd |
| 125 | yyrestart( fd ); |
| 126 | // set to initial condition | 1.5.0.5 (ge) added |
| 127 | yyinitial(); |
| 128 | |
| 129 | // check |
| 130 | if( yyin == NULL ) goto cleanup; |
| 131 | |
| 132 | // set source |
| 133 | source.setFile( fd ); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | CK_FPRINTF_STDERR( "[chuck](parser): nothing to parse (empty code / file)...\n" ); |
| 138 | CK_FPRINTF_STDERR( "[chuck](parser): |- no action taken\n" ); |
| 139 | return FALSE; |
| 140 | } |
| 141 | |
| 142 | // start error message for new filename |
| 143 | EM_start_filename( target->filename.c_str() ); |
| 144 | // set current input source |
| 145 | EM_setCurrentFileSource( source ); |
| 146 | |
| 147 | // ensure abstract syntax tree is clean | 1.5.0.5 (ge) added, finally |
| 148 | target->cleanupAST(); |
| 149 | |
| 150 | // parse |
| 151 | if( !(yyparse() == 0) ) goto cleanup; |
| 152 | |
| 153 | // flag success |
| 154 | ret = TRUE; |
| 155 | |
| 156 | cleanup: |
| 157 | |
| 158 | // flush |
| 159 | // yyflush(); |
| 160 | |
| 161 | // set to NULL | 1.5.0.5 (ge) added to reset yyin |
| 162 | yyin = NULL; |
| 163 | |
| 164 | // clean up lexer buffer, if we used one |
| 165 | if( yyCodeBuffer ) |
| 166 | { yy_delete_buffer( yyCodeBuffer ); yyCodeBuffer = NULL; } |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | //------------------------------------------------------------------------------ |
| 175 | // name: reset_parse() |
| 176 | // desc: reset parse after a parse |
| 177 | //------------------------------------------------------------------------------ |
| 178 | void reset_parse( ) |
| 179 | { |
| 180 | // empty file name |
| 181 | EM_reset_filename(); |
| 182 | // unset target |
| 183 | EM_setCurrentTarget( NULL ); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | //----------------------------------------------------------------------------- |
| 190 | // name: parseLine() |
| 191 | // desc: ... |
| 192 | //----------------------------------------------------------------------------- |
| 193 | t_CKBOOL SyntaxQuery::parseLine( const std::string & line, SyntaxTokenList & tokens ) |
| 194 | { |
| 195 | // clear the token list |
| 196 | tokens.howmany = 0; |
| 197 | |
| 198 | return TRUE; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | //----------------------------------------------------------------------------- |
| 206 | // absyn to string functions |
| 207 | //----------------------------------------------------------------------------- |
| 208 | #include "chuck_type.h" |
| 209 | |
| 210 | // expression to string conversion |
| 211 | string absyn_exp2str( a_Exp exp, t_CKBOOL iterate = true ); |
| 212 | string absyn_binary2str( a_Exp_Binary binary ); |
| 213 | string absyn_unary2str( a_Exp_Unary unary ); |
| 214 | string absyn_op2str( ae_Operator op ); |
| 215 | string absyn_cast2str( a_Exp_Cast cast ); |
| 216 | string absyn_postfix2str( a_Exp_Postfix postfix ); |
| 217 | string absyn_dur2str( a_Exp_Dur dur ); |
| 218 | string absyn_primary2str( a_Exp_Primary primary ); |
| 219 | string absyn_array2str( a_Exp_Array array ); |
| 220 | string absyn_func_call2str( a_Exp_Func_Call func_call ); |
| 221 | string absyn_dot_member2str( a_Exp_Dot_Member dot_member ); |
| 222 | string absyn_exp_if2str( a_Exp_If exp_if ); |
| 223 | string absyn_decl2str( a_Exp_Decl decl ); |
| 224 | |
| 225 | // statement to string conversion |
| 226 | string absyn_stmt2str( a_Stmt stmt ); |
| 227 | string absyn_if2str( a_Stmt_If stmt_if ); |
| 228 | string absyn_for2str( a_Stmt_For stmt_for ); |
| 229 | string absyn_foreach2str( a_Stmt_ForEach stmt_foreach ); |
| 230 | string absyn_while2str( a_Stmt_While stmt_while ); |
| 231 | string absyn_until2str( a_Stmt_Until stmt_until ); |
| 232 | string absyn_loop2str( a_Stmt_Loop stmt_loop ); |
| 233 | string absyn_break2str( a_Stmt_Break stmt_break ); |
| 234 | string absyn_continue2str( a_Stmt_Continue stmt_continue ); |
| 235 | string absyn_return2str( a_Stmt_Return stmt_return ); |
| 236 | string absyn_doc2str( a_Stmt_Doc stmt_doc ); |
| 237 | string absyn_example2str( a_Stmt_Example stmt_example ); |
| 238 | string absyn_code2str( a_Stmt_Code stmt_code ); |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | //----------------------------------------------------------------------------- |
| 244 | // name: absyn_stmt2str() |
| 245 | // desc: convert abstract syntax stmt to string |
| 246 | //----------------------------------------------------------------------------- |
| 247 | std::string absyn2str( a_Stmt stmt ) |
| 248 | { |
| 249 | return absyn_stmt2str( stmt ); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | //----------------------------------------------------------------------------- |
| 256 | // name: absyn_stmt2str() |
| 257 | // desc: convert abstract syntax stmt to string |
| 258 | //----------------------------------------------------------------------------- |
| 259 | string absyn_stmt2str( a_Stmt stmt ) |
| 260 | { |
| 261 | // check |
| 262 | if( !stmt ) return ""; |
| 263 | |
| 264 | // str to be constructed |
| 265 | string str = ""; |
| 266 | |
| 267 | // loop over statements, check by type |
| 268 | switch( stmt->s_type ) |
| 269 | { |
| 270 | case ae_stmt_exp: // expression statement |
| 271 | str = absyn_exp2str( stmt->stmt_exp ); |
| 272 | break; |
| 273 | |
| 274 | case ae_stmt_if: // if statement |
| 275 | str = absyn_if2str( &stmt->stmt_if ); |
| 276 | break; |
| 277 | |
| 278 | case ae_stmt_for: // for statement |
| 279 | str = absyn_for2str( &stmt->stmt_for ); |
| 280 | break; |
| 281 | |
| 282 | case ae_stmt_foreach: // for statement |
| 283 | str = absyn_foreach2str( &stmt->stmt_foreach ); |
| 284 | break; |
| 285 | |
| 286 | case ae_stmt_while: // while statement |
| 287 | str = absyn_while2str( &stmt->stmt_while ); |
| 288 | break; |
| 289 | |
| 290 | case ae_stmt_until: // until statement |
| 291 | str = absyn_until2str( &stmt->stmt_until ); |
| 292 | break; |
| 293 | |
| 294 | case ae_stmt_loop: // loop statement |
| 295 | str = absyn_loop2str( &stmt->stmt_loop ); |
| 296 | break; |
| 297 | |
| 298 | case ae_stmt_break: // break statement |
| 299 | str = absyn_break2str( &stmt->stmt_break ); |
| 300 | break; |
| 301 | |
| 302 | case ae_stmt_continue: // continue statement |
| 303 | str = absyn_continue2str( &stmt->stmt_continue ); |
| 304 | break; |
| 305 | |
| 306 | case ae_stmt_return: // return statement |
| 307 | str = absyn_return2str( &stmt->stmt_return ); |
| 308 | break; |
| 309 | |
| 310 | case ae_stmt_import: // import statement |
| 311 | // (compile-time evaluated) |
| 312 | break; |
| 313 | |
| 314 | case ae_stmt_doc: // doc statement | 1.5.4.4 (ge) added |
| 315 | str = absyn_doc2str( &stmt->stmt_doc ); |
| 316 | break; |
| 317 | |
| 318 | case ae_stmt_example: // doc statement | 1.5.5.8 (ge,nick,alex) added |
| 319 | str = absyn_example2str( &stmt->stmt_example ); |
| 320 | break; |
| 321 | |
| 322 | case ae_stmt_code: // code segment |
| 323 | str = absyn_code2str( &stmt->stmt_code ); |
| 324 | break; |
| 325 | |
| 326 | // not implemented |
| 327 | case ae_stmt_switch: // switch statement |
| 328 | case ae_stmt_case: // case statement |
| 329 | case ae_stmt_gotolabel: // goto label statement |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | // return the constructed str |
| 334 | return str; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | //----------------------------------------------------------------------------- |
| 341 | // name: absyn2str() |
| 342 | // desc: convert abstract syntax exp to string |
| 343 | //----------------------------------------------------------------------------- |
| 344 | string absyn2str( a_Exp exp, t_CKBOOL appendSemicolon ) |
| 345 | { |
| 346 | return absyn_exp2str( exp ) + (appendSemicolon ? ";" : "" ); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | //----------------------------------------------------------------------------- |
| 353 | // name: name_demangle() |
| 354 | // desc: demangle chuck function names |
| 355 | // e.g., input: "version@0@Machine" output: "version" |
| 356 | //----------------------------------------------------------------------------- |
| 357 | string name_demangle( const string & name ) |
| 358 | { |
| 359 | // account for @( for vec2/3/4 literals | 1.5.4.2 (ge) added |
| 360 | size_t where = 0; |
| 361 | while( where < name.length() ) |
| 362 | { |
| 363 | // find the first '@' that's not "@(" |
| 364 | size_t pos = name.find_first_of( '@', where ); |
| 365 | // if not found or if found in the last character, we are done |
| 366 | if( pos == string::npos ) return name; |
| 367 | // check for ( |
| 368 | if( pos == name.length()-1 || name[pos+1] != '(' ) return name.substr(where,pos); |
| 369 | // otherwise, found @(... keep searching |
| 370 | where += 2; |
| 371 | } |
| 372 | // if reached here, no need to modify |
| 373 | return name; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | //----------------------------------------------------------------------------- |
| 380 | // name: absyn_exp2str() |
| 381 | // desc: convert abstract syntax exp to string |
| 382 | //----------------------------------------------------------------------------- |
| 383 | string absyn_exp2str( a_Exp exp, t_CKBOOL iterate ) |
| 384 | { |
| 385 | // check |
| 386 | if( !exp ) return ""; |
| 387 | |
| 388 | // the string to be constructed |
| 389 | string str = ""; |
| 390 | |
| 391 | // loop over |
| 392 | while( exp ) |
| 393 | { |
| 394 | switch( exp->s_type ) |
| 395 | { |
| 396 | case ae_exp_binary: |
| 397 | str += absyn_binary2str( &exp->binary ); |
| 398 | break; |
| 399 | |
| 400 | case ae_exp_unary: |
| 401 | str += absyn_unary2str( &exp->unary ); |
| 402 | break; |
| 403 | |
| 404 | case ae_exp_cast: |
| 405 | str += absyn_cast2str( &exp->cast ); |
| 406 | break; |
| 407 | |
| 408 | case ae_exp_postfix: |
| 409 | str += absyn_postfix2str( &exp->postfix ); |
| 410 | break; |
| 411 | |
| 412 | case ae_exp_dur: |
| 413 | str += absyn_dur2str( &exp->dur ); |
| 414 | break; |
| 415 | |
| 416 | case ae_exp_primary: |
| 417 | str += absyn_primary2str( &exp->primary ); |
| 418 | break; |
| 419 | |
| 420 | case ae_exp_array: |
| 421 | str += absyn_array2str( &exp->array ); |
| 422 | break; |
| 423 | |
| 424 | case ae_exp_func_call: |
| 425 | str += absyn_func_call2str( &exp->func_call ); |
| 426 | break; |
| 427 | |
| 428 | case ae_exp_dot_member: |
| 429 | str += absyn_dot_member2str( &exp->dot_member ); |
| 430 | break; |
| 431 | |
| 432 | case ae_exp_if: |
| 433 | str += absyn_exp_if2str( &exp->exp_if ); |
| 434 | break; |
| 435 | |
| 436 | case ae_exp_decl: |
| 437 | str += absyn_decl2str( &exp->decl ); |
| 438 | break; |
| 439 | |
| 440 | default: |
| 441 | return ""; |
| 442 | } |
| 443 | |
| 444 | // implicit cast | 1.5.4.2 (ge) commented out |
| 445 | // ...hmm can't remember why this is here |
| 446 | // if( exp->cast_to != NULL ) |
| 447 | // break; |
| 448 | |
| 449 | // break |
| 450 | if( !iterate ) break; |
| 451 | |
| 452 | // next |
| 453 | exp = exp->next; |
| 454 | // if more |
| 455 | if( exp ) str += ","; |
| 456 | } |
| 457 | |
| 458 | return str; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | //----------------------------------------------------------------------------- |
| 465 | // name: absyn_op2str() |
| 466 | // desc: convert abstract syntax operator to string |
| 467 | //----------------------------------------------------------------------------- |
| 468 | string absyn_op2str( ae_Operator op ) |
| 469 | { |
| 470 | return op2str( op ); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | //----------------------------------------------------------------------------- |
| 477 | // name: absyn_binary2str() |
| 478 | // desc: convert abstract syntax binary to string |
| 479 | //----------------------------------------------------------------------------- |
| 480 | string absyn_binary2str( a_Exp_Binary binary ) |
| 481 | { |
| 482 | string lhs = absyn_exp2str(binary->lhs); |
| 483 | string op = absyn_op2str(binary->op); |
| 484 | string rhs = absyn_exp2str(binary->rhs); |
| 485 | |
| 486 | // use paren? |
| 487 | bool paren = false; |
| 488 | // spacing? |
| 489 | bool spacing = false; |
| 490 | |
| 491 | switch(binary->op) |
| 492 | { |
| 493 | case ae_op_plus_chuck: |
| 494 | case ae_op_minus_chuck: |
| 495 | case ae_op_times_chuck: |
| 496 | case ae_op_divide_chuck: |
| 497 | case ae_op_s_or_chuck: |
| 498 | case ae_op_s_and_chuck: |
| 499 | case ae_op_shift_left_chuck: |
| 500 | case ae_op_shift_right_chuck: |
| 501 | case ae_op_percent_chuck: |
| 502 | case ae_op_s_xor_chuck: |
| 503 | case ae_op_chuck: |
| 504 | case ae_op_unchuck: |
| 505 | case ae_op_upchuck: |
| 506 | case ae_op_downchuck: |
| 507 | case ae_op_at_chuck: |
| 508 | case ae_op_eq: |
| 509 | case ae_op_neq: |
| 510 | case ae_op_lt: |
| 511 | case ae_op_gt: |
| 512 | case ae_op_le: |
| 513 | case ae_op_ge: |
| 514 | case ae_op_and: |
| 515 | case ae_op_or: |
| 516 | paren = false; |
| 517 | spacing = true; |
| 518 | break; |
| 519 | default: |
| 520 | paren = true; |
| 521 | spacing = false; |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | // (lhs OP rhs) |
| 526 | string str = string(paren?"(":"") + lhs + (spacing?" ":"") + op + (spacing?" ":"") + rhs + (paren?")":""); |
| 527 | // return |
| 528 | return str; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | //----------------------------------------------------------------------------- |
| 535 | // name: absyn_unary2str() |
| 536 | // desc: convert abstract syntax unary to string |
| 537 | //----------------------------------------------------------------------------- |
| 538 | string absyn_unary2str( a_Exp_Unary unary ) |
| 539 | { |
| 540 | string s; |
| 541 | |
| 542 | // check |
| 543 | switch( unary->op ) |
| 544 | { |
| 545 | case ae_op_new: |
| 546 | s = absyn_op2str(unary->op) + " " + unary->self->type->name(); |
| 547 | if( unary->ctor.invoked ) s += "(" + absyn_exp2str(unary->ctor.args) + ")"; |
| 548 | break; |
| 549 | default: |
| 550 | s = absyn_op2str(unary->op) + " " + absyn_exp2str(unary->exp); |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | return s; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | //----------------------------------------------------------------------------- |
| 561 | // name: absyn_cast2str() |
| 562 | // desc: convert abstract syntax cast to string |
| 563 | //----------------------------------------------------------------------------- |
| 564 | string absyn_cast2str( a_Exp_Cast cast ) |
| 565 | { |
| 566 | Chuck_Type * to = cast->self->type; |
| 567 | return absyn_exp2str(cast->exp) + "$" + to->base_name; |
| 568 | } |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | //----------------------------------------------------------------------------- |
| 574 | // name: absyn_cast2str() |
| 575 | // desc: convert abstract syntax cast to string |
| 576 | //----------------------------------------------------------------------------- |
| 577 | string absyn_postfix2str( a_Exp_Postfix postfix ) |
| 578 | { |
| 579 | return absyn_exp2str( postfix->exp ) + absyn_op2str( postfix->op ); |
| 580 | } |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | //----------------------------------------------------------------------------- |
| 586 | // name: absyn_dur2str() |
| 587 | // desc: convert abstract syntax dur to string |
| 588 | //----------------------------------------------------------------------------- |
| 589 | string absyn_dur2str( a_Exp_Dur dur ) |
| 590 | { |
| 591 | return absyn_exp2str(dur->base) + "::" + absyn_exp2str(dur->unit); |
| 592 | } |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | //----------------------------------------------------------------------------- |
| 598 | // name: absyn_primary2str() |
| 599 | // desc: convert abstract syntax primary to string |
| 600 | //----------------------------------------------------------------------------- |
| 601 | string absyn_primary2str( a_Exp_Primary primary ) |
| 602 | { |
| 603 | string str; |
| 604 | |
| 605 | // find out exp |
| 606 | switch( primary->s_type ) |
| 607 | { |
| 608 | case ae_primary_var: str = S_name(primary->var); break; |
| 609 | case ae_primary_num: str = to_string(primary->num); break; |
| 610 | case ae_primary_float: str = to_string(primary->fnum); break; |
| 611 | case ae_primary_str: str = string("\"") + primary->str + "\""; break; |
| 612 | case ae_primary_char: str = string("'") + primary->chr + "'"; break; |
| 613 | case ae_primary_array: str = "["+absyn_exp2str(primary->array->exp_list)+"]"; break; |
| 614 | case ae_primary_complex: str = "#("+absyn_exp2str(primary->complex->re)+","+absyn_exp2str(primary->complex->im)+")"; break; |
| 615 | case ae_primary_polar: str = "%("+absyn_exp2str(primary->polar->mod)+","+absyn_exp2str(primary->polar->phase)+")"; break; |
| 616 | case ae_primary_vec: str = "@("+absyn_exp2str(primary->vec->args,TRUE)+")"; break; |
| 617 | case ae_primary_exp: str = absyn_exp2str(primary->exp); break; |
| 618 | case ae_primary_hack: str = "<<< " + absyn_exp2str(primary->exp) + " >>>"; break; |
| 619 | case ae_primary_nil: str = "(void)"; break; |
| 620 | default: str ="[PRIMARY]"; break; |
| 621 | } |
| 622 | |
| 623 | return str; |
| 624 | } |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | //----------------------------------------------------------------------------- |
| 630 | // name: absyn_array2str() |
| 631 | // desc: convert abstract syntax array to string |
| 632 | //----------------------------------------------------------------------------- |
| 633 | string absyn_array2str( a_Exp_Array array ) |
| 634 | { |
| 635 | string str; |
| 636 | a_Array_Sub sub = NULL; |
| 637 | |
| 638 | // get the sub |
| 639 | sub = array->indices; |
| 640 | // expressions |
| 641 | a_Exp e = sub->exp_list; |
| 642 | |
| 643 | // base of the array |
| 644 | str = absyn_exp2str(array->base); |
| 645 | |
| 646 | // loop over |
| 647 | while( e ) |
| 648 | { |
| 649 | // each index |
| 650 | str += "[" + absyn_exp2str(e,false) + "]"; |
| 651 | // advance |
| 652 | e = e->next; |
| 653 | } |
| 654 | |
| 655 | return str; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | //----------------------------------------------------------------------------- |
| 662 | // name: absyn_func_call2str() |
| 663 | // desc: convert abstract syntax func_call to string |
| 664 | //----------------------------------------------------------------------------- |
| 665 | string absyn_func_call2str( a_Exp_Func_Call func_call ) |
| 666 | { |
| 667 | string str; |
| 668 | // function name |
| 669 | str = name_demangle( absyn_exp2str( func_call->func ) ); |
| 670 | // args |
| 671 | str += "(" + absyn_exp2str( func_call->args ) + ")"; |
| 672 | // done |
| 673 | return str; |
| 674 | } |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | //----------------------------------------------------------------------------- |
| 680 | // name: absyn_dot_member2str() |
| 681 | // desc: convert abstract syntax dot_member to string |
| 682 | //----------------------------------------------------------------------------- |
| 683 | string absyn_dot_member2str( a_Exp_Dot_Member dot_member ) |
| 684 | { |
| 685 | // return |
| 686 | return absyn_exp2str(dot_member->base) + "." + name_demangle(S_name(dot_member->xid)); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | //----------------------------------------------------------------------------- |
| 693 | // name: absyn_exp_if2str() |
| 694 | // desc: convert abstract syntax exp_if to string |
| 695 | //----------------------------------------------------------------------------- |
| 696 | string absyn_exp_if2str( a_Exp_If exp_if ) |
| 697 | { |
| 698 | return "("+absyn_exp2str(exp_if->cond) + "?" + absyn_exp2str(exp_if->if_exp) |
| 699 | + ":" + absyn_exp2str(exp_if->else_exp)+")"; |
| 700 | } |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | //----------------------------------------------------------------------------- |
| 706 | // name: absyn_decl2str() |
| 707 | // desc: convert abstract syntax decl to string |
| 708 | //----------------------------------------------------------------------------- |
| 709 | string absyn_decl2str( a_Exp_Decl decl ) |
| 710 | { |
| 711 | a_Var_Decl_List list = decl->var_decl_list; |
| 712 | string str; |
| 713 | a_Exp e = NULL; |
| 714 | |
| 715 | if( decl->is_global ) str += "global "; |
| 716 | else if( decl->is_static ) str += "static "; |
| 717 | |
| 718 | while( list ) |
| 719 | { |
| 720 | // type |
| 721 | str += list->var_decl->value->type->base_name + " " + (decl->type->ref ? "@ " : "") + list->var_decl->value->name; |
| 722 | // constructor? |
| 723 | if( list->var_decl->ctor.invoked ) |
| 724 | { |
| 725 | str += "(" + absyn_exp2str(list->var_decl->ctor.args) + ")"; |
| 726 | } |
| 727 | // array? |
| 728 | if( list->var_decl->array ) |
| 729 | { |
| 730 | e = list->var_decl->array->exp_list; |
| 731 | if( e ) |
| 732 | { |
| 733 | // loop over |
| 734 | while( e ) |
| 735 | { |
| 736 | // each index |
| 737 | str += "[" + absyn_exp2str(e,false) + "]"; |
| 738 | // advance |
| 739 | e = e->next; |
| 740 | } |
| 741 | } |
| 742 | else |
| 743 | { |
| 744 | for( t_CKINT i = 0; i < list->var_decl->value->type->array_depth; i++ ) |
| 745 | { |
| 746 | str += "[]"; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | list = list->next; |
| 752 | if( list ) str += ","; |
| 753 | } |
| 754 | |
| 755 | return str; |
| 756 | } |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | //----------------------------------------------------------------------------- |
| 762 | // name: absyn_if2str() |
| 763 | // desc: convert abstract syntax if to string |
| 764 | //----------------------------------------------------------------------------- |
| 765 | string absyn_if2str( a_Stmt_If stmt_if ) |
| 766 | { |
| 767 | return "if( " + absyn_exp2str( stmt_if->cond ) + " )"; |
| 768 | } |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | //----------------------------------------------------------------------------- |
| 774 | // name: absyn_for2str() |
| 775 | // desc: convert abstract syntax for to string |
| 776 | //----------------------------------------------------------------------------- |
| 777 | string absyn_for2str( a_Stmt_For stmt_for ) |
| 778 | { |
| 779 | return "for( " + absyn_stmt2str( stmt_for->c1 ) + "; " |
| 780 | + absyn_stmt2str( stmt_for->c2 ) + "; " |
| 781 | + absyn_exp2str( stmt_for->c3 ) + " )"; |
| 782 | } |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | //----------------------------------------------------------------------------- |
| 788 | // name: absyn_foreach2str() |
| 789 | // desc: convert abstract syntax for-each to string |
| 790 | //----------------------------------------------------------------------------- |
| 791 | string absyn_foreach2str( a_Stmt_ForEach stmt_foreach ) |
| 792 | { |
| 793 | return "for( " + absyn_exp2str( stmt_foreach->theIter ) + " : " |
| 794 | + absyn_exp2str( stmt_foreach->theArray ) + " )"; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | //----------------------------------------------------------------------------- |
| 801 | // name: absyn_while2str() |
| 802 | // desc: convert abstract syntax while to string |
| 803 | //----------------------------------------------------------------------------- |
| 804 | string absyn_while2str( a_Stmt_While stmt_while ) |
| 805 | { |
| 806 | return "while( " + absyn_exp2str( stmt_while->cond ) + " )"; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | //----------------------------------------------------------------------------- |
| 813 | // name: absyn_until2str() |
| 814 | // desc: convert abstract syntax until to string |
| 815 | //----------------------------------------------------------------------------- |
| 816 | string absyn_until2str( a_Stmt_Until stmt_until ) |
| 817 | { |
| 818 | return "until( " + absyn_exp2str( stmt_until->cond ) + " )"; |
| 819 | } |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | //----------------------------------------------------------------------------- |
| 825 | // name: absyn_loop2str() |
| 826 | // desc: convert abstract syntax loop to string |
| 827 | //----------------------------------------------------------------------------- |
| 828 | string absyn_loop2str( a_Stmt_Loop stmt_loop ) |
| 829 | { |
| 830 | return "repeat( " + absyn_exp2str( stmt_loop->cond ) + " )"; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | //----------------------------------------------------------------------------- |
| 837 | // name: absyn_break2str() |
| 838 | // desc: convert abstract syntax break to string |
| 839 | //----------------------------------------------------------------------------- |
| 840 | string absyn_break2str( a_Stmt_Break stmt_break ) |
| 841 | { |
| 842 | return "break;"; |
| 843 | } |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | //----------------------------------------------------------------------------- |
| 849 | // name: absyn_continue2str() |
| 850 | // desc: convert abstract syntax continue to string |
| 851 | //----------------------------------------------------------------------------- |
| 852 | string absyn_continue2str( a_Stmt_Continue stmt_continue ) |
| 853 | { |
| 854 | return "continue;"; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | //----------------------------------------------------------------------------- |
| 861 | // name: absyn_return2str() |
| 862 | // desc: convert abstract syntax return to string |
| 863 | //----------------------------------------------------------------------------- |
| 864 | string absyn_return2str( a_Stmt_Return stmt_return ) |
| 865 | { |
| 866 | return "return " + absyn_exp2str( stmt_return->val ) + ";"; |
| 867 | } |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | //----------------------------------------------------------------------------- |
| 873 | // name: absyn_doc2str() | 1.5.4.4 (ge) added |
| 874 | // desc: convert abstract syntax doc stmt to string |
| 875 | //----------------------------------------------------------------------------- |
| 876 | string absyn_doc2str( a_Stmt_Doc stmt_doc ) |
| 877 | { |
| 878 | // check |
| 879 | if( !stmt_doc || !stmt_doc->list ) return "@doc [empty]"; |
| 880 | |
| 881 | // more than one target in list? |
| 882 | if( stmt_doc->list->next == NULL ) |
| 883 | { |
| 884 | // return one item format |
| 885 | return "@doc \"" + string(stmt_doc->list->desc) + "\";"; |
| 886 | } |
| 887 | |
| 888 | // the string to be constructed |
| 889 | string str = "@doc [ "; |
| 890 | // the current target |
| 891 | a_Doc i = stmt_doc->list; |
| 892 | // iterate |
| 893 | while( i ) |
| 894 | { |
| 895 | // append |
| 896 | str += "\"" + string( i->desc ) + "\""; |
| 897 | // next |
| 898 | i = i->next; |
| 899 | // check |
| 900 | str += ( i ? ", " : " "); |
| 901 | } |
| 902 | // end string |
| 903 | str += "];"; |
| 904 | // return it |
| 905 | return str; |
| 906 | } |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | //----------------------------------------------------------------------------- |
| 912 | // name: absyn_example2str() | 1.5.5.8 (ge,nick,alex) added |
| 913 | // desc: convert abstract syntax doc stmt to string |
| 914 | //----------------------------------------------------------------------------- |
| 915 | string absyn_example2str( a_Stmt_Example stmt_example ) |
| 916 | { |
| 917 | // check |
| 918 | if( !stmt_example || !stmt_example->list ) return "@example [empty]"; |
| 919 | |
| 920 | // more than one target in list? |
| 921 | if( stmt_example->list->next == NULL ) |
| 922 | { |
| 923 | // return one item format |
| 924 | return "@example \"" + string(stmt_example->list->desc) + "\";"; |
| 925 | } |
| 926 | |
| 927 | // the string to be constructed |
| 928 | string str = "@example [ "; |
| 929 | // the current target |
| 930 | a_Example i = stmt_example->list; |
| 931 | // iterate |
| 932 | while( i ) |
| 933 | { |
| 934 | // append |
| 935 | str += "\"" + string( i->desc ) + "\""; |
| 936 | // next |
| 937 | i = i->next; |
| 938 | // check |
| 939 | str += ( i ? ", " : " "); |
| 940 | } |
| 941 | // end string |
| 942 | str += "];"; |
| 943 | // return it |
| 944 | return str; |
| 945 | } |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | //----------------------------------------------------------------------------- |
| 951 | // name: absyn_stmt2str() |
| 952 | // desc: convert abstract syntax decl to string |
| 953 | //----------------------------------------------------------------------------- |
| 954 | string absyn_code2str( a_Stmt_Code stmt_code ) |
| 955 | { |
| 956 | return ""; |
| 957 | } |