! sqrsh (sqr shell) ! Copyright (C) 1997 Ray Ontko & Company ! ! This program is free software; you can redistribute it and/or modify ! it under the terms of the GNU General Public License as published by ! the Free Software Foundation; either version 2 of the License, or ! (at your option) any later version. ! ! This program is distributed in the hope that it will be useful, ! but WITHOUT ANY WARRANTY; without even the implied warranty of ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! GNU General Public License for more details. ! ! You should have received a copy of the GNU General Public License ! along with this program; if not, write to the Free Software ! Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ! !++ ! ! optimize ! ! optimize iname oname ! ! change: let #firstime = 1 ! into: move 1 to #firstime ! change: let #first = &second ! into: move &second to #first ! ! NOTE: this is an experimental module. Carefully compare the ! output file to your original code before use. ! !-- !but ! 1 ignore: let #first = #second + #third ! 2 ignore: let #first = #second+#third ! 3 ignore: let #first = #second + ! #third ! 4 ignore: let #first = #second ! + #third ! 5 ignore: let #first = #second + 37 ! 6 ignore: let #first = #second+37 ! 7 ignore: let #first = #second + ! 37 ! 8 ignore: let #first = #second ! + 37 ! the + operator in 1-8 can be *, /, %, ^, -, || ! 9 ignore: let #first = function(#second, #third) !10 ignore: let $first = function($second, '11') !11 ignore: ! this comment for let #first = 15 !12 however, do process lines correctly that have a comment at the tail ! like: let $invest=&invest ! this is one comment ! and: let $invest = &invest! this is another comment ! ! History: ! Name Date Comment ! ---------------- ---------- ---------------------------------------- ! Ray Ontko 1997/06/01 original idea ! Peter Reeve-Newson May 1999 first attempt at 'let' to 'move' ! Ray Ontko 1999/08/07 added fixup_files ! removed keep_old logic ! allowed for !! (sort of) !------------------------------------------------------------------ optimize begin-procedure optimize local move 'optimize.tmp' to $tname move 'optimize.bak' to $bname if isnull( $_p1 ) input $iname 'Input file' else move $_p1 to $iname end-if if isnull( $_p2 ) input $oname 'Output file' else move $_p2 to $oname end-if do optimize_file( $iname , $tname ) do fixup_files( $tname , $oname , $bname ) end-procedure ! optimize !------------------------------------------------------------- optimize_file begin-procedure optimize_file( $infile , $outfile ) open $infile as 1 for-reading record={SQRSH_LINE_MAX}:vary status=#status if #status != 0 show 'error opening "' $infile '" for input' else open $outfile as 2 for-writing record={SQRSH_LINE_MAX}:vary status=#status if #status != 0 show 'error opening "' $outfile '" for output' else #ifdef OPTIMIZE_KEEPOLD input $keepold maxlen=1 'D = drop the old "let" line? or else keep as comment?' uppercase $keepold if $keepold <> 'D' let $keepold = 'K' end-if display $keepold #end-if read 1 into $line:{SQRSH_LINE_MAX} while not #_end-file move 0 to #already_read ! if this is a comment line, then skip it do find_comment($line,#_comment) if #_comment goto write_and_read end-if ! if "let" is NOT found, then skip it (exception "delete" not programmed) let #let_begin = instr($line, 'let', 0) if #let_begin = 0 goto write_and_read end-if #ifdef OPTIMIZE_KEEPOLD let $oldline = '!!' || $line #end-if let $line_start = substr($line, 0, #let_begin - 1) let #destin_begin = #let_begin + 3 let #destin_end = instr($line, '=', #destin_begin) let #destin_len = #destin_end - #destin_begin let $destin = substr($line, #destin_begin, #destin_len) let $destin = rtrim($destin, ' ') let $destin = ltrim($destin, ' ') let #line_len = length($line) let #source_begin = #destin_end + 1 let #source_len = #line_len - #source_begin + 1 let $source = substr($line, #source_begin, #source_len) let $source = rtrim($source, ' ') let $source = ltrim($source, ' ') ! If the source starts with a math symbol or is a function (begins ! with an alpha), then we have a "let" statement which cannot be ! changed to a move, BUT a number is okay. ! The source should start with #(35), $(36), &(38), or 0(48) to 9(57) ! to have the let changed to a move. do find_math($source,#_math) if #_math goto write_and_read end-if let $function = substr($source, 1, 1) let #function = ascii($function) if #function > 57 goto write_and_read end-if ! If there's some code following the source, then we have to look at it; ! go thru the "source" looking for a "source-two". ! If the source-two is a comment, okay then change let to move and attach the comment; ! otherwise we have a compound "let" statement which cannot be changed to a move; move '' to $source_one move '' to $source_two move 0 to #_blank move 0 to #_two_sources move 2 to #string_loc let #string_len = length ($source) while #string_loc <= #string_len let $char = substr($source, #string_loc, 1) let #char = ascii($char) if #char = 32 ! - it's a space move 1 to #_blank else if #char = 9 ! - it's a tab move 1 to #_blank else if #char = 33 and substr($source, #string_loc + 1, 1) != '!!' ! --- it's a comment so put it all in source-two move 1 to #_two_sources let $source_one = substr($source,1, #string_loc - 1) let $source_two = substr($source, #string_loc, #string_len + 1 - #string_loc) let $source_one = rtrim($source_one, ' ') break else if #char = 43 ! math fun of + or #char = 47 ! math fun of / or #char = 42 ! math fun of * or #char = 37 ! math fun % or #char = 94 ! math fun ^ or #char = 124 ! math fun | goto write_and_read else if #char = 45 ! a dash or a minus? if #_blank = 1 ! minus after a blank goto write_and_read end-if ! minus or dash, must look at next character let $charnext = substr($source, (#string_loc + 1), 1) let #charnext = ascii($charnext) if #charnext < 48 ! is not a number or alpha goto write_and_read ! is a variable end-if else end-if end-if end-if end-if end-if if #char = 33 and substr($source, #string_loc + 1, 1) = '!!' add 1 to #string_loc end-if add 1 to #string_loc end-while ! Before you put out the changed line you've got to look on the next line ! at its first character. If it's a math symbol then the present line was a ! compound let which can't change to a move read 1 into $nextline:{SQRSH_LINE_MAX} move 1 to #already_read do find_math($nextline,#_math) if #_math goto write_and_read else if #_two_sources let $s = $line_start || 'move ' || $source_one || ' to ' || $destin || ' ' || $source_two else let $s = $line_start || 'move ' || $source || ' to ' || $destin end-if end-if #ifdef OPTIMIZE_KEEPOLD if $keepold = 'K' write 2 from $oldline end-if #end-if write 2 from $s goto read_again write_and_read: write 2 from $line read_again: if #already_read = 1 move $nextline to $line else read 1 into $line:{SQRSH_LINE_MAX} end-if end-while close 2 end-if close 1 end-if end-procedure ! optimize_file !---------------------------------------------------------------- find_comment begin-procedure find_comment($instring,#_comment) move 0 to #_comment move 1 to #string_loc let #string_len = length ($instring) while #string_loc <= #string_len let $char = substr($instring, #string_loc, 1) let #char = ascii($char) if #char = 32 or #char = 9 ! - its blank else if #char = 33 ! --- comment move 1 to #_comment end-if ! else -- other stuff break end-if add 1 to #string_loc end-while end-procedure ! find_comment !----------------------------------------------------------------- find_math begin-procedure find_math($instring,#_math) move 0 to #_math move 1 to #string_loc let #string_len = length ($instring) while #string_loc <= #string_len let $char = substr($instring, #string_loc, 1) let #char = ascii($char) if #char = 32 or #char = 9 ! - its blank else if #char = 43 ! math fun of + or #char = 45 ! math fun of - or #char = 47 ! math fun of / or #char = 42 ! math fun of * or #char = 37 ! math fun % or #char = 94 ! math fun ^ or #char = 124 ! math fun | move 1 to #_math end-if ! else -- other stuff break end-if add 1 to #string_loc end-while end-procedure ! find_math