stylistic changes mostly

This commit is contained in:
russ 2010-09-10 06:46:54 -04:00
parent 38214e1544
commit f11b1c5438

View file

@ -1,8 +1,9 @@
require 'pp' require 'pp'
class Dictionary class Dictionary
def initialize def initialize( &block )
@entries = {} @entries = {}
block.call( self ) if block
end end
def word( name, &block ) def word( name, &block )
@ -30,40 +31,41 @@ class RForth
end end
def initial_dictionary def initial_dictionary
Dictionary.new Dictionary.new do |d|
.word('dup') { @stack << @stack.last } d.word('dup') { @stack << @stack.last }
.word('?dup') { @stack << @stack.last unless @stack.last == 0 } d.word('?dup') { @stack << @stack.last unless @stack.last == 0 }
.word('drop') { @stack.pop } d.word('drop') { @stack.pop }
.word('swap') { @stack += [@stack.pop, @stack.pop] } d.word('swap') { @stack += [@stack.pop, @stack.pop] }
.word('over') do d.word('over') do
a = @stack.pop a = @stack.pop
b = @stack.pop b = @stack.pop
@stack << b << a << b @stack << b << a << b
end end
.word('rot') do d.word('rot') do
a = @stack.pop a = @stack.pop
b = @stack.pop b = @stack.pop
c = @stack.pop c = @stack.pop
@stack << b << a << c @stack << b << a << c
end end
.word(':') { define_word } d.word(':') { define_word }
.word('+') { @stack << (@stack.pop + @stack.pop) } d.word('+') { @stack << (@stack.pop + @stack.pop) }
.word('*') { @stack << (@stack.pop * @stack.pop) } d.word('*') { @stack << (@stack.pop * @stack.pop) }
.word('-') do d.word('-') do
a = @stack.pop a = @stack.pop
b = @stack.pop b = @stack.pop
@stack << b - a @stack << b - a
end end
.word('/') do d.word('/') do
a = @stack.pop a = @stack.pop
b = @stack.pop b = @stack.pop
@stack << b / a @stack << b / a
end end
.word('.') { @s_out.print( "#{@stack.pop}\n" ) } d.word('.') { @s_out.print( "#{@stack.pop}\n" ) }
.word('.S') { @s_out.print( "#{@stack}\n" ) } d.word('.S') { @s_out.print( "#{@stack}\n" ) }
.word('.D') { pp @dictionary } d.word('.D') { pp @dictionary }
.word('cr') { @s_out.puts } d.word('cr') { @s_out.puts }
.word('bye') { exit } d.word('bye') { exit }
end
end end
def define_word def define_word