From 90eb787162e9d29d30f23497309086e162b5864a Mon Sep 17 00:00:00 2001 From: fogus Date: Thu, 9 Sep 2010 11:36:14 -0400 Subject: [PATCH] Added over and rot --- src/rforth.rb | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/rforth.rb b/src/rforth.rb index 83d2e25..6957eda 100644 --- a/src/rforth.rb +++ b/src/rforth.rb @@ -29,18 +29,34 @@ class RForth def initial_dictionary 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 a = @stack.pop b = @stack.pop @stack << a << b end - d.word(':') { define_word } - d.word('.') { @s_out.print( "#{@stack.last}\n" ) } - d.word('cr') { @s_out.puts } - d.word('+') { @stack << (@stack.pop + @stack.pop) } - d.word('*') { @stack << (@stack.pop * @stack.pop) } + d.word('over') do + a = @stack.pop + b = @stack.pop + @stack << b << a << b + 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 a = @stack.pop b = @stack.pop @@ -52,6 +68,13 @@ class RForth b = @stack.pop @stack << b / a 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 end