Integer, Object, and String fixes.

This commit is contained in:
Peter Camilleri 2015-08-11 11:22:50 -04:00
parent a5b095009d
commit 4b99b8f03c
4 changed files with 76 additions and 17 deletions

View file

@ -130,6 +130,19 @@ class StandardLibraryTester < Minitest::Test
foorth_equal('try 10 .to_duration false / catch end', [])
foorth_equal('try 10 .to_duration f"%Z" catch end', [])
foorth_equal('try 11 false and catch end', [])
foorth_equal('try 11 false or catch end', [])
foorth_equal('try 11 false xor catch end', [])
foorth_equal('try 11 false << catch end', [])
foorth_equal('try 11 false >> catch end', [])
foorth_equal('try 11 false max catch end', [])
foorth_equal('try 11 false min catch end', [])
foorth_equal('try 11 false format catch end', [])
foorth_equal('try "a" false * catch end', [])
end
def test_some_logical_and

View file

@ -47,28 +47,58 @@ module XfOOrth
# Some bitwise operation words.
# [b,a] and [b&a]
Integer.create_shared_method('and', NosSpec, [],
&lambda {|vm| vm.poke(self & Integer.foorth_coerce(vm.peek)); })
Integer.create_shared_method('and', NosSpec, [], &lambda {|vm|
begin
vm.poke(self & Integer.foorth_coerce(vm.peek))
rescue
vm.data_stack.pop
raise
end
})
# [b,a] or [b|a]
Integer.create_shared_method('or', NosSpec, [],
&lambda {|vm| vm.poke(self | Integer.foorth_coerce(vm.peek)); })
Integer.create_shared_method('or', NosSpec, [], &lambda {|vm|
begin
vm.poke(self | Integer.foorth_coerce(vm.peek))
rescue
vm.data_stack.pop
raise
end
})
# [b,a] xor [b^a]
Integer.create_shared_method('xor', NosSpec, [],
&lambda {|vm| vm.poke(self ^ Integer.foorth_coerce(vm.peek)); })
Integer.create_shared_method('xor', NosSpec, [], &lambda {|vm|
begin
vm.poke(self ^ Integer.foorth_coerce(vm.peek))
rescue
vm.data_stack.pop
raise
end
})
# [a] com [~a]
Integer.create_shared_method('com', TosSpec, [],
&lambda {|vm| vm.push(~(self)); })
# [b,a] << [b<<a]
Integer.create_shared_method('<<', NosSpec, [],
&lambda {|vm| vm.poke(self << Integer.foorth_coerce(vm.peek)); })
Integer.create_shared_method('<<', NosSpec, [], &lambda {|vm|
begin
vm.poke(self << Integer.foorth_coerce(vm.peek))
rescue
vm.data_stack.pop
raise
end
})
# [b,a] >> [b>>a]
Integer.create_shared_method('>>', NosSpec, [],
&lambda {|vm| vm.poke(self >> Integer.foorth_coerce(vm.peek)); })
Integer.create_shared_method('>>', NosSpec, [], &lambda {|vm|
begin
vm.poke(self >> Integer.foorth_coerce(vm.peek))
rescue
vm.data_stack.pop
raise
end
})
end

View file

@ -57,15 +57,24 @@ module XfOOrth
# [b,a] max [max(b,a)]
Object.create_shared_method('max', NosSpec, [], &lambda {|vm|
other = vm.peek
vm.poke((self > self.foorth_coerce(other)) ? self : other)
begin
other = vm.peek
vm.poke((self > self.foorth_coerce(other)) ? self : other)
rescue
vm.data_stack.pop
raise
end
})
# [b,a] min [min(b,a)]
Object.create_shared_method('min', NosSpec, [], &lambda {|vm|
other = vm.peek
vm.poke((self < self.foorth_coerce(other)) ? self : other)
begin
other = vm.peek
vm.poke((self < self.foorth_coerce(other)) ? self : other)
rescue
vm.data_stack.pop
raise
end
})
end

View file

@ -66,6 +66,7 @@ module XfOOrth
begin
vm.poke(vm.peek % self.in_array)
rescue => err
vm.data_stack.pop
error "F40: Formating error: #{err.message}."
end
end
@ -198,8 +199,14 @@ module XfOOrth
&lambda {|vm| vm.poke(self << vm.peek.to_s); })
# ["b", n] * ["bbb..."]
String.create_shared_method('*', NosSpec, [],
&lambda {|vm| vm.poke(self * Integer.foorth_coerce(vm.peek)); })
String.create_shared_method('*', NosSpec, [], &lambda {|vm|
begin
vm.poke(self * Integer.foorth_coerce(vm.peek))
rescue
vm.data_stack.pop
raise
end
})
# ["abCD"] .to_upper ["ABCD"]
String.create_shared_method('.to_upper', TosSpec, [],