Annotation of gforth/stuff.fs, revision 1.73

1.1       anton       1: \ miscelleneous words
                      2: 
1.73    ! anton       3: \ Copyright (C) 1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012 Free Software Foundation, Inc.
1.1       anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
1.57      anton       9: \ as published by the Free Software Foundation, either version 3
1.1       anton      10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
1.57      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       anton      19: 
1.71      anton      20: : save-mem-dict ( addr1 u -- addr2 u )
                     21:     here swap dup allot ( addr1 addr2 u )
                     22:     2dup 2>r move 2r> ;
                     23: 
                     24: ' usable-dictionary-end @ dodefer: = [if]
                     25:     require glocals.fs
                     26: [else]
                     27:     require glocals-1.60.fs
                     28: [then]
                     29: 
1.8       anton      30: 
1.6       anton      31: ' require alias needs ( ... "name" -- ... ) \ gforth
1.11      crook      32: \G An alias for @code{require}; exists on other systems (e.g., Win32Forth).
1.1       anton      33: \ needs is an F-PC name. we will probably switch to 'needs' in the future
                     34: 
                     35: \ a little more compiler security
                     36: 
                     37: \ currently not used by Gforth, but maybe by add-ons e.g., the 486asm
                     38: AUser CSP
                     39: 
                     40: : !CSP ( -- )
                     41:     sp@ csp ! ;
                     42: 
                     43: : ?CSP ( -- )
                     44:     sp@ csp @ <> -22 and throw ;
1.2       anton      45: 
1.4       anton      46: \ DMIN and DMAX
                     47: 
1.13      crook      48: : dmin ( d1 d2 -- d ) \ double d-min
1.5       pazsan     49:     2over 2over d> IF  2swap  THEN 2drop ;
1.2       anton      50: 
1.13      crook      51: 
                     52: : dmax ( d1 d2 -- d ) \ double d-max
1.5       pazsan     53:     2over 2over d< IF  2swap  THEN 2drop ;
1.4       anton      54: 
                     55: \ shell commands
                     56: 
1.10      crook      57: 0 Value $? ( -- n ) \ gforth dollar-question
1.12      crook      58: \G @code{Value} -- the exit status returned by the most recently executed
1.9       crook      59: \G @code{system} command.
1.4       anton      60: 
1.14      anton      61: : system ( c-addr u -- ) \ gforth
1.36      anton      62: \G Pass the string specified by @var{c-addr u} to the host operating
                     63: \G system for execution in a sub-shell.  The value of the environment
                     64: \G variable @code{GFORTHSYSTEMPREFIX} (or its default value) is
                     65: \G prepended to the string (mainly to support using @code{command.com}
                     66: \G as shell in Windows instead of whatever shell Cygwin uses by
                     67: \G default; @pxref{Environment variables}).
1.4       anton      68:     (system) throw TO $? ;
1.9       crook      69: 
1.4       anton      70: : sh ( "..." -- ) \ gforth
1.9       crook      71: \G Parse a string and use @code{system} to pass it to the host
                     72: \G operating system for execution in a sub-shell.
1.4       anton      73:     '# parse cr system ;
                     74: 
1.8       anton      75: \ stuff
                     76: 
1.10      crook      77: : ]L ( compilation: n -- ; run-time: -- n ) \ gforth
                     78:     \G equivalent to @code{] literal}
1.8       anton      79:     ] postpone literal ;
                     80: 
1.23      anton      81: [ifundef] in-dictionary?
1.8       anton      82: : in-dictionary? ( x -- f )
                     83:     forthstart dictionary-end within ;
1.23      anton      84: [endif]
1.8       anton      85: 
                     86: : in-return-stack? ( addr -- f )
                     87:     rp0 @ swap - [ forthstart 6 cells + ]L @ u< ;
1.17      anton      88: 
                     89: \ const-does>
                     90: 
                     91: : compile-literals ( w*u u -- ; run-time: -- w*u ) recursive
1.18      anton      92:     \ compile u literals, starting with the bottommost one
1.17      anton      93:     ?dup-if
                     94:        swap >r 1- compile-literals
                     95:        r> POSTPONE literal
                     96:     endif ;
                     97: 
                     98: : compile-fliterals ( r*u u -- ; run-time: -- w*u ) recursive
1.18      anton      99:     \ compile u fliterals, starting with the bottommost one
1.17      anton     100:     ?dup-if
                    101:        { F: r } 1- compile-fliterals
                    102:        r POSTPONE fliteral
                    103:     endif ;
                    104: 
                    105: : (const-does>) ( w*uw r*ur uw ur target "name" -- )
1.18      anton     106:     \ define a colon definition "name" containing w*uw r*ur as
                    107:     \ literals and a call to target.
1.17      anton     108:     { uw ur target }
                    109:     header docol: cfa, \ start colon def without stack junk
                    110:     ur compile-fliterals uw compile-literals
                    111:     target compile, POSTPONE exit reveal ;
                    112: 
1.35      anton     113: : const-does> ( run-time: w*uw r*ur uw ur "name" -- ) \ gforth
                    114:     \G Defines @var{name} and returns.
                    115:     \G  
1.18      anton     116:     \G @var{name} execution: pushes @var{w*uw r*ur}, then performs the
                    117:     \G code following the @code{const-does>}.
1.17      anton     118:     here >r 0 POSTPONE literal
                    119:     POSTPONE (const-does>)
                    120:     POSTPONE ;
                    121:     noname : POSTPONE rdrop
1.32      anton     122:     latestxt r> cell+ ! \ patch the literal
1.17      anton     123: ; immediate
1.19      anton     124: 
1.20      anton     125: \ !! rewrite slurp-file using slurp-fid
1.34      anton     126: : slurp-file ( c-addr1 u1 -- c-addr2 u2 ) \ gforth
1.20      anton     127:     \G @var{c-addr1 u1} is the filename, @var{c-addr2 u2} is the file's contents
1.19      anton     128:     r/o bin open-file throw >r
                    129:     r@ file-size throw abort" file too large"
                    130:     dup allocate throw swap
                    131:     2dup r@ read-file throw over <> abort" could not read whole file"
                    132:     r> close-file throw ;
                    133: 
1.45      anton     134: : slurp-fid ( fid -- addr u ) \ gforth
                    135: \G @var{addr u} is the content of the file @var{fid}
                    136:     { fid }
1.20      anton     137:     0 0 begin ( awhole uwhole )
                    138:        dup 1024 + dup >r extend-mem ( anew awhole uwhole R: unew )
                    139:        rot r@ fid read-file throw ( awhole uwhole uread R: unew )
                    140:        r> 2dup =
                    141:     while ( awhole uwhole uread unew )
                    142:        2drop
                    143:     repeat
                    144:     - + dup >r resize throw r> ;
1.24      anton     145: 
1.25      anton     146: \ ]] ... [[
                    147: 
1.69      pazsan    148: : [[ ( -- ) \ gforth left-bracket-bracket
                    149: \G switch from postpone state to compile state
                    150:     \ this is only a marker; it is never really interpreted
                    151:     compile-only-error ; immediate
                    152: 
                    153: [ifdef] compiler1
1.25      anton     154: : compile-literal ( n -- )
                    155:     postpone literal ;
                    156: 
1.42      anton     157: : compile-compile-literal ( n -- )
                    158:     compile-literal postpone compile-literal ;
                    159: 
                    160: : compile-2literal ( n1 n2 -- )
                    161:     postpone 2literal ;
                    162: 
                    163: : compile-compile-2literal ( n1 n2 -- )
                    164:     compile-2literal postpone compile-2literal ;
                    165: 
                    166: : postponer1 ( c-addr u -- ... xt )
1.61      anton     167:     2dup find-name
                    168:     [ifdef] run-prelude run-prelude [then]
1.60      anton     169:     dup if ( c-addr u nt )
1.25      anton     170:        nip nip name>comp
                    171:        2dup [comp'] [[ d= if
1.46      pazsan    172:            2drop ['] compiler1 is parser1 ['] noop
1.25      anton     173:        else
1.42      anton     174:            ['] postpone,
1.25      anton     175:        endif
                    176:     else
                    177:        drop
1.42      anton     178:        2dup 2>r snumber? dup if
1.25      anton     179:            0> IF
1.42      anton     180:                ['] compile-compile-2literal
                    181:             ELSE
                    182:                 ['] compile-compile-literal
1.25      anton     183:            THEN
1.42      anton     184:            2rdrop
1.25      anton     185:        ELSE
1.42      anton     186:            drop 2r> no.extensions
1.25      anton     187:        THEN
                    188:     then ;
                    189: 
1.63      anton     190: : ]] ( -- ) \ gforth right-bracket-bracket
                    191:     \G switch into postpone state
1.42      anton     192:     ['] postponer1 is parser1 state on ; immediate restrict
1.68      pazsan    193: [then]
                    194: 
                    195: [ifdef] compiler-r
                    196: : postponer-r ( addr u -- ... xt )
                    197:     forth-recognizer do-recognizer
                    198:     over [ s" [[" find-name ] Literal =
                    199:     IF  2drop [comp'] ] drop ELSE  ['] >postpone  THEN ;
                    200: 
                    201: : ]] ( -- ) \ gforth right-bracket-bracket
                    202:     \G switch into postpone state
                    203:     ['] postponer-r is parser1 state on ; immediate restrict
                    204: [then]
1.43      anton     205: 
1.64      anton     206: comp'  literal drop alias postpone-literal
                    207: comp' 2literal drop alias postpone-2literal
                    208: comp' fliteral drop alias postpone-fliteral
                    209: comp' sliteral drop alias postpone-sliteral
1.63      anton     210: 
                    211: : ]]L ( postponing: x -- ; compiling: -- x ) \ gforth right-bracket-bracket-l
                    212: \G Shortcut for @code{]] literal}.
                    213:     ]] postpone-literal ]] [[ ; immediate
                    214: 
                    215: : ]]2L ( postponing: x1 x2 -- ; compiling: -- x1 x2 ) \ gforth right-bracket-bracket-two-l
                    216: \G Shortcut for @code{]] 2literal}.
                    217:     ]] postpone-2literal ]] [[ ; immediate
                    218: 
                    219: : ]]FL ( postponing: r -- ; compiling: -- r ) \ gforth right-bracket-bracket-f-l
                    220: \G Shortcut for @code{]] fliteral}.
                    221:     ]] postpone-fliteral ]] [[ ; immediate
                    222: 
                    223: : ]]SL ( postponing: addr1 u -- ; compiling: -- addr2 u ) \ gforth right-bracket-bracket-s-l
                    224: \G Shortcut for @code{]] sliteral}; if the string already has been
                    225: \G allocated permanently, you can use @code{]]2L} instead.
                    226:     ]] postpone-sliteral ]] [[ ; immediate
                    227: 
1.26      anton     228: \ f.rdp
                    229: 
1.27      anton     230: : push-right ( c-addr u1 u2 cfill -- )
1.26      anton     231:     \ move string at c-addr u1 right by u2 chars (without exceeding
                    232:     \ the original bound); fill the gap with cfill
1.27      anton     233:     >r over min dup >r rot dup >r ( u1 u2 c-addr R: cfill u2 c-addr )
1.26      anton     234:     dup 2swap /string cmove>
                    235:     r> r> r> fill ;
                    236: 
1.27      anton     237: : f>buf-rdp-try { f: rf c-addr ur nd up um1 -- um2 }
                    238:     \ um1 is the mantissa length to try, um2 is the actual mantissa length
                    239:     c-addr ur um1 /string '0 fill
                    240:     rf c-addr um1 represent if { nexp fsign }
1.26      anton     241:        nd nexp + up >=
                    242:        ur nd - 1- dup { beforep } fsign + nexp 0 max >= and if
                    243:            \ fixed-point notation
1.27      anton     244:            c-addr ur beforep nexp - dup { befored } '0 push-right
1.44      anton     245:             befored 1+ ur >= if \ <=1 digit left, will be pushed out by '.'
                    246:                 rf fabs f2* 0.1e nd s>d d>f f** f> if \ round last digit
                    247:                     '1 c-addr befored + 1- c!
                    248:                 endif
                    249:             endif
1.26      anton     250:            c-addr beforep 1- befored min dup { beforez } 0 max bl fill
                    251:            fsign if
                    252:                '- c-addr beforez 1- 0 max + c!
                    253:            endif
1.27      anton     254:            c-addr ur beforep /string 1 '. push-right
                    255:            nexp nd +
1.26      anton     256:        else \ exponential notation
1.27      anton     257:            c-addr ur 1 /string 1 '. push-right
1.26      anton     258:            fsign if
1.27      anton     259:                c-addr ur 1 '- push-right
1.26      anton     260:            endif
                    261:            nexp 1- s>d tuck dabs <<# #s rot sign 'E hold #> { explen }
1.27      anton     262:            ur explen - 1- fsign + { mantlen }
                    263:            mantlen 0< if \ exponent too large
1.26      anton     264:                drop c-addr ur '* fill
                    265:            else
                    266:                c-addr ur + 0 explen negate /string move
                    267:            endif
1.27      anton     268:            #>> mantlen
1.26      anton     269:        endif
                    270:     else \ inf or nan
                    271:        if \ negative
1.27      anton     272:            c-addr ur 1 '- push-right
1.26      anton     273:        endif
1.27      anton     274:        drop ur
1.26      anton     275:        \ !! align in some way?
1.27      anton     276:     endif
                    277:     1 max ur min ;
                    278: 
1.29      anton     279: : f>buf-rdp ( rf c-addr +nr +nd +np -- ) \ gforth
1.28      anton     280: \G Convert @i{rf} into a string at @i{c-addr nr}.  The conversion
                    281: \G rules and the meanings of @i{nr nd np} are the same as for
1.27      anton     282: \G @code{f.rdp}.
                    283:     \ first, get the mantissa length, then convert for real.  The
                    284:     \ mantissa length is wrong in a few cases because of different
                    285:     \ rounding; In most cases this does not matter, because the
                    286:     \ mantissa is shorter than expected and the final digits are 0;
                    287:     \ but in a few cases the mantissa gets longer.  Then it is
                    288:     \ conceivable that you will see a result that is rounded too much.
                    289:     \ However, I have not been able to construct an example where this
                    290:     \ leads to an unexpected result.
                    291:     swap 0 max swap 0 max
                    292:     fdup 2over 2over 2 pick f>buf-rdp-try f>buf-rdp-try drop ;
1.26      anton     293: 
1.28      anton     294: : f>str-rdp ( rf +nr +nd +np -- c-addr nr ) \ gforth
                    295: \G Convert @i{rf} into a string at @i{c-addr nr}.  The conversion
                    296: \G rules and the meanings of @i{nr +nd np} are the same as for
1.26      anton     297: \G @code{f.rdp}.  The result in in the pictured numeric output buffer
                    298: \G and will be destroyed by anything destroying that buffer.
1.28      anton     299:     rot holdptr @ 1- 0 rot negate /string ( rf +nd np c-addr nr )
1.26      anton     300:     over holdbuf u< -&17 and throw
                    301:     2tuck 2>r f>buf-rdp 2r> ;
                    302: 
1.28      anton     303: : f.rdp ( rf +nr +nd +np -- ) \ gforth
1.26      anton     304: \G Print float @i{rf} formatted.  The total width of the output is
1.30      anton     305: \G @i{nr}.  For fixed-point notation, the number of digits after the
                    306: \G decimal point is @i{+nd} and the minimum number of significant
                    307: \G digits is @i{np}.  @code{Set-precision} has no effect on
                    308: \G @code{f.rdp}.  Fixed-point notation is used if the number of
                    309: \G siginicant digits would be at least @i{np} and if the number of
                    310: \G digits before the decimal point would fit.  If fixed-point notation
                    311: \G is not used, exponential notation is used, and if that does not
                    312: \G fit, asterisks are printed.  We recommend using @i{nr}>=7 to avoid
                    313: \G the risk of numbers not fitting at all.  We recommend
                    314: \G @i{nr}>=@i{np}+5 to avoid cases where @code{f.rdp} switches to
                    315: \G exponential notation because fixed-point notation would have too
                    316: \G few significant digits, yet exponential notation offers fewer
                    317: \G significant digits.  We recommend @i{nr}>=@i{nd}+2, if you want to
                    318: \G have fixed-point notation for some numbers.  We recommend
                    319: \G @i{np}>@i{nr}, if you want to have exponential notation for all
                    320: \G numbers.
1.26      anton     321:     f>str-rdp type ;
                    322: 
                    323: 0 [if]
                    324: : testx ( rf ur nd up -- )
                    325:     '| emit f.rdp ;
                    326: 
                    327: : test ( -- )
                    328:     -0.123456789123456789e-20
                    329:     40 0 ?do
                    330:        cr
                    331:        fdup 7 3 1 testx
                    332:        fdup 7 3 4 testx
                    333:        fdup 7 3 0 testx
                    334:        fdup 7 7 1 testx
                    335:        fdup 7 5 1 testx
                    336:        fdup 7 0 2 testx
                    337:        fdup 5 2 1 testx
                    338:        fdup 4 2 1 testx
                    339:        fdup 18 8 5 testx
                    340:        '| emit
                    341:        10e f*
                    342:     loop ;
                    343: [then]
1.33      anton     344: 
                    345: : f.s ( -- ) \ gforth f-dot-s
1.51      anton     346: \G Display the number of items on the floating-point stack, followed
                    347: \G by a list of the items (but not more than specified by
                    348: \G @code{maxdepth-.s}; TOS is the right-most item.
1.33      anton     349:     ." <" fdepth 0 .r ." > " fdepth 0 max maxdepth-.s @ min dup 0 
                    350:     ?DO  dup i - 1- floats fp@ + f@ 16 5 11 f.rdp space LOOP  drop ; 
1.37      anton     351: 
                    352: \ defer stuff
                    353: 
1.39      pazsan    354: [ifundef] defer@ : defer@ >body @ ; [then]
                    355: 
1.37      anton     356: :noname    ' defer@ ;
                    357: :noname    postpone ['] postpone defer@ ;
                    358: interpret/compile: action-of ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth
                    359: \G @i{Xt} is the XT that is currently assigned to @i{name}.
                    360: 
                    361: ' action-of
                    362: comp' action-of drop
                    363: interpret/compile: what's ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth-obsolete
1.38      anton     364: \G Old name of @code{action-of}
1.40      anton     365: 
                    366: 
                    367: : typewhite ( addr n -- ) \ gforth
                    368: \G Like type, but white space is printed instead of the characters.
                    369:     \ bounds u+do
                    370:     0 max bounds ?do
                    371:        i c@ #tab = if \ check for tab
                    372:            #tab
                    373:        else
                    374:            bl
                    375:        then
                    376:        emit
                    377:     loop ;
                    378: 
1.49      anton     379: \ w and l stuff
                    380: 
                    381: environment-wordlist >order
                    382: 
1.50      anton     383: 16 address-unit-bits / 1 max constant /w ( -- u ) \ gforth slash-w
1.49      anton     384: \G address units for a 16-bit value
                    385:     
1.50      anton     386: 32 address-unit-bits / 1 max constant /l ( -- u ) \ gforth slash-l
1.49      anton     387: \G address units for a 32-bit value
                    388: 
                    389: previous
1.48      anton     390: 
                    391: [ifdef] uw@
1.49      anton     392: \ Open firmware names
1.48      anton     393: ' uw@ alias w@ ( addr -- u )
                    394: ' ul@ alias l@ ( addr -- u )
1.49      anton     395: \ ' sw@ alias <w@ ( addr -- n )
1.52      anton     396: [then]
                    397: 
                    398: \ safe output redirection
                    399: 
1.55      anton     400: : outfile-execute ( ... xt file-id -- ... ) \ gforth
                    401:     \G execute @i{xt} with the output of @code{type} etc. redirected to
                    402:     \G @i{file-id}.
                    403:     outfile-id { oldfid } try
                    404:        to outfile-id execute 0
                    405:     restore
                    406:        oldfid to outfile-id
                    407:     endtry
                    408:     throw ;
1.52      anton     409: 
1.55      anton     410: : infile-execute ( ... xt file-id -- ... ) \ gforth
                    411:     \G execute @i{xt} with the input of @code{key} etc. redirected to
                    412:     \G @i{file-id}.
                    413:     infile-id { oldfid } try
                    414:        to infile-id execute 0
                    415:     restore
                    416:        oldfid to infile-id
                    417:     endtry
                    418:     throw ;
1.52      anton     419: 
1.55      anton     420: \ safe BASE wrapper
1.52      anton     421: 
1.55      anton     422: : base-execute ( i*x xt u -- j*x ) \ gforth
                    423:     \G execute @i{xt} with the content of @code{BASE} being @i{u}, and
                    424:     \G restoring the original @code{BASE} afterwards.
                    425:     base @ { oldbase } \ use local, because TRY blocks the return stack
                    426:     try
                    427:        base ! execute 0
                    428:     restore
                    429:        oldbase base !
                    430:     endtry
                    431:     throw ;
1.52      anton     432: 
1.58      anton     433: \ th
                    434: 
                    435: : th ( addr1 u -- addr2 )
                    436:     cells + ;
1.62      pazsan    437: 
                    438: \ \\\ - skip to end of file
                    439: 
                    440: : \\\ ( -- ) \ gforth
                    441:     \G skip remaining source file
                    442:     source-id dup 0> IF
                    443:        >r r@ file-size throw r> reposition-file throw
                    444:        BEGIN  refill 0= UNTIL  postpone \  THEN ; immediate
1.66      anton     445: 
                    446: \ WORD SWORD
                    447: 
                    448: : sword  ( char -- addr len ) \ gforth-obsolete s-word
                    449: \G Parses like @code{word}, but the output is like @code{parse} output.
                    450: \G @xref{core-idef}.
                    451:     \ this word was called PARSE-WORD until 0.3.0, but Open Firmware and
                    452:     \ dpANS6 A.6.2.2008 have a word with that name that behaves
                    453:     \ differently (like NAME).
                    454:     source 2dup >r >r >in @ over min /string
                    455:     rot dup bl = IF
                    456:         drop (parse-white)
                    457:     ELSE
                    458:         (word)
                    459:     THEN
                    460: [ has? new-input [IF] ]
                    461:     2dup input-lexeme!
                    462: [ [THEN] ]
                    463:     2dup + r> - 1+ r> min >in ! ;
                    464: 
                    465: : word   ( char "<chars>ccc<char>-- c-addr ) \ core
                    466:     \G Skip leading delimiters. Parse @i{ccc}, delimited by
                    467:     \G @i{char}, in the parse area. @i{c-addr} is the address of a
                    468:     \G transient region containing the parsed string in
                    469:     \G counted-string format. If the parse area was empty or
                    470:     \G contained no characters other than delimiters, the resulting
                    471:     \G string has zero length. A program may replace characters within
                    472:     \G the counted string. OBSOLESCENT: the counted string has a
                    473:     \G trailing space that is not included in its length.
                    474:     sword here place  bl here count + c!  here ;
1.72      anton     475: 
                    476: \ quotations
                    477: 
                    478: :noname  false :noname ;
                    479: :noname  locals-wordlist last @ lastcfa @
                    480:     postpone AHEAD
                    481:     locals-list @ locals-list off
                    482:     postpone SCOPE
                    483:     true  :noname  ;
                    484: interpret/compile: [: ( compile-time: -- quotation-sys ) \ gforth bracket-colon
                    485: \G Starts a quotation
                    486: 
                    487: : ;] ( compile-time: quotation-sys -- ; run-time: -- xt ) \ gforth semi-bracket
                    488:     \g ends a quotation
                    489:     POSTPONE ; >r IF
                    490:        ]  postpone ENDSCOPE
                    491:        locals-list !
                    492:        postpone THEN
                    493:        lastcfa ! last ! to locals-wordlist
                    494:        r> postpone ALiteral
                    495:     ELSE  r>  THEN ( xt ) ; immediate

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>