Fixed a bug in the way that function existing words were being looked up during a function definition.

This commit is contained in:
fogus 2010-09-09 12:31:38 -04:00
parent 90eb787162
commit 1eb65258c5
2 changed files with 11 additions and 3 deletions

View file

@ -11,3 +11,8 @@ A simple Forth interpreter in Ruby (1.9).
. .
9 9
: sq dup * ;
2 sq
.
4

View file

@ -51,7 +51,7 @@ class RForth
@stack << b << a << c @stack << b << a << c
end end
# quotations # functions
d.word(':') { define_word } d.word(':') { define_word }
# math # math
@ -72,6 +72,7 @@ class RForth
# aux words # aux words
d.word('.') { @s_out.print( "#{@stack.pop}\n" ) } d.word('.') { @s_out.print( "#{@stack.pop}\n" ) }
d.word('.S') { @s_out.print( "#{@stack}\n" ) } d.word('.S') { @s_out.print( "#{@stack}\n" ) }
d.word('.D') { pp @dictionary }
d.word('cr') { @s_out.puts } d.word('cr') { @s_out.puts }
d.word('bye') { exit } d.word('bye') { exit }
@ -83,7 +84,7 @@ class RForth
blocks = [] blocks = []
while (word = read_word) while (word = read_word)
break if word == ';' break if word == ';'
entry = @dictionary.word(word) entry = @dictionary[word]
raise "no such word: #{word}" unless entry raise "no such word: #{word}" unless entry
if entry[:immediate] if entry[:immediate]
entry[:block].call entry[:block].call
@ -93,7 +94,9 @@ class RForth
end end
@dictionary.word(name) do @dictionary.word(name) do
blocks.each {|b| b.call} blocks.each do |b|
b.call
end
end end
end end