diff --git a/code_examples/fib.fs b/code_examples/fib.fs new file mode 100644 index 0000000..7724210 --- /dev/null +++ b/code_examples/fib.fs @@ -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 diff --git a/mode/forth.py b/mode/forth.py index 671cd8e..9ba3725 100644 --- a/mode/forth.py +++ b/mode/forth.py @@ -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'