Added over and rot

This commit is contained in:
fogus 2010-09-09 11:36:14 -04:00
parent b583b7f05b
commit 90eb787162

View file

@ -29,18 +29,34 @@ class RForth
def initial_dictionary def initial_dictionary
d = Dictionary.new d = Dictionary.new
d.word('dup') { @stack << @stack.last }
d.word('drop') { @stack.pop } # stack management
d.word('dup') { @stack << @stack.last }
d.word('?dup') { @stack << @stack.last unless @stack.last == 0 }
d.word('drop') { @stack.pop }
d.word('swap') do d.word('swap') do
a = @stack.pop a = @stack.pop
b = @stack.pop b = @stack.pop
@stack << a << b @stack << a << b
end end
d.word(':') { define_word } d.word('over') do
d.word('.') { @s_out.print( "#{@stack.last}\n" ) } a = @stack.pop
d.word('cr') { @s_out.puts } b = @stack.pop
d.word('+') { @stack << (@stack.pop + @stack.pop) } @stack << b << a << b
d.word('*') { @stack << (@stack.pop * @stack.pop) } end
d.word('rot') do
a = @stack.pop
b = @stack.pop
c = @stack.pop
@stack << b << a << c
end
# quotations
d.word(':') { define_word }
# math
d.word('+') { @stack << (@stack.pop + @stack.pop) }
d.word('*') { @stack << (@stack.pop * @stack.pop) }
d.word('-') do d.word('-') do
a = @stack.pop a = @stack.pop
b = @stack.pop b = @stack.pop
@ -52,6 +68,13 @@ class RForth
b = @stack.pop b = @stack.pop
@stack << b / a @stack << b / a
end end
# aux words
d.word('.') { @s_out.print( "#{@stack.pop}\n" ) }
d.word('.S') { @s_out.print( "#{@stack}\n" ) }
d.word('cr') { @s_out.puts }
d.word('bye') { exit }
d d
end end