parent
83c7ea1fb9
commit
8f56eddcbd
|
@ -0,0 +1,57 @@
|
|||
\ compute the nth fibonacci number, starting at 0
|
||||
: fib ( n -- n2 )
|
||||
dup ( n n )
|
||||
2 < if
|
||||
drop 1 ( 1 )
|
||||
else
|
||||
dup 2 - recurse ( n fib[n-2] )
|
||||
swap ( fib[n-2] n )
|
||||
1 - recurse ( fib[n-2] fib[n-1] )
|
||||
+ ( n2 )
|
||||
then ;
|
||||
|
||||
create memo 100 cells allot
|
||||
|
||||
\ initialize it
|
||||
: init ( n -- )
|
||||
dup ( n n )
|
||||
if ( n )
|
||||
dup 0 swap ( n 0 n )
|
||||
1 - ( n 0 n-1 )
|
||||
cells memo + ( n n-1+memo )
|
||||
1 - ( n 0 n-1+memo)
|
||||
! ( n )
|
||||
1 - ( n-1 )
|
||||
recurse
|
||||
else
|
||||
drop
|
||||
then ;
|
||||
|
||||
: fib2x ( n -- n2 )
|
||||
dup ( n n )
|
||||
2 < if ( n )
|
||||
drop 1 ( 1 )
|
||||
else
|
||||
dup cells memo + ( n n+memo )
|
||||
@ ( n ${n+memo} )
|
||||
dup 0 > if ( n ${n+memo} )
|
||||
swap drop ( ${n+memo} )
|
||||
else
|
||||
drop ( n )
|
||||
dup dup ( n n n )
|
||||
2 - recurse ( n n fib2[n-2] )
|
||||
swap ( n fib2[n-2] n )
|
||||
1 - recurse ( n fib2[n-2] fib2[n-1] )
|
||||
+ ( n fib2[n] )
|
||||
dup rot ( fib2[n] fib2[n] n )
|
||||
cells memo + ! ( fib2[n] )
|
||||
then
|
||||
then ;
|
||||
|
||||
: fib2
|
||||
\ allocate a lookup table and run fib2x
|
||||
100 init fib2x ;
|
||||
|
||||
45 fib2
|
||||
.s cr
|
||||
bye
|
|
@ -44,8 +44,8 @@ class GforthLoadFile(Interact):
|
|||
Interact._execute(self, w, bname='*GForth*', cmd='gforth')
|
||||
b = w.application.get_buffer_by_name('*GForth*')
|
||||
time.sleep(0.1)
|
||||
for line in w.buffer.lines:
|
||||
b.pipe_write(line + '\n')
|
||||
path = w.buffer.path
|
||||
b.pipe_write('s" ' + path + '" included\n')
|
||||
|
||||
class Forth(Fundamental):
|
||||
modename = 'FORTH'
|
||||
|
|
Loading…
Reference in New Issue