Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

modify src/parser/actions.pm to return CardinalFloat instead Float:

  to keep ruby class inheritances
add new classes:
  CardinalNumeric
  CardinalFloat
add new methods:
  Integer#chr
  Integer#even
  Integer#odd
  Float#to_f
  Float#zero
fixme:
  Cardinal#round
    round does not return C<self>
    see ruby-1.9.2-p180/numeric.c:3188
    also src/classes/Integer.pir:326
  • Loading branch information...
commit c4f6029eda3defc678533766fdc484b222390a17 1 parent a6751a2
@lateau lateau authored
View
21 Rakefile
@@ -362,7 +362,7 @@ file "src/gen_actions.pir" => [:config, "src/parser/actions.pm"] do
parrot("src/parser/actions.pm","src/gen_actions.pir",$config[:nqp],'pir')
end
-builtins = FileList.new("src/builtins/guts.pir", "src/builtins/control.pir", "src/builtins/say.pir", "src/builtins/cmp.pir", "src/builtins/op.pir", "src/classes/Object.pir", "src/classes/Exception.pir", "src/classes/NilClass.pir", "src/classes/String.pir", "src/classes/Integer.pir", "src/classes/Array.pir", "src/classes/Hash.pir", "src/classes/Range.pir", "src/classes/TrueClass.pir", "src/classes/FalseClass.pir", "src/classes/Kernel.pir", "src/classes/Time.pir", "src/classes/Math.pir", "src/classes/GC.pir", "src/classes/IO.pir", "src/classes/Proc.pir", "src/classes/File.pir", "src/classes/FileStat.pir", "src/classes/Dir.pir", "src/builtins/globals.pir", "src/builtins/eval.pir", "src/classes/Continuation.pir")
+builtins = FileList.new("src/builtins/guts.pir", "src/builtins/control.pir", "src/builtins/say.pir", "src/builtins/cmp.pir", "src/builtins/op.pir", "src/classes/Object.pir", "src/classes/Exception.pir", "src/classes/NilClass.pir", "src/classes/String.pir", "src/classes/Numeric.pir", "src/classes/Integer.pir", "src/classes/Float.pir", "src/classes/Array.pir", "src/classes/Hash.pir", "src/classes/Range.pir", "src/classes/TrueClass.pir", "src/classes/FalseClass.pir", "src/classes/Kernel.pir", "src/classes/Time.pir", "src/classes/Math.pir", "src/classes/GC.pir", "src/classes/IO.pir", "src/classes/Proc.pir", "src/classes/File.pir", "src/classes/FileStat.pir", "src/classes/Dir.pir", "src/builtins/globals.pir", "src/builtins/eval.pir", "src/classes/Continuation.pir")
file "src/gen_builtins.pir" => builtins do
puts "Generating src/gen_builtins.pir"
@@ -529,10 +529,25 @@ namespace :test do |ns|
test "integer/cmp.t"
test "integer/pred.t"
test "integer/chr.t"
- test "integer/ord.t"
+ test "integer/odd.t"
+ test "integer/even.t"
+ test "integer/round.t"
desc "Run tests on Integer."
- task :all => [:integer, :times, :cmp, :pred, :ord]
+ task :all => [:integer, :times, :cmp, :pred, :chr, :odd, :even, :round]
+ end
+
+ namespace :numeric do
+ desc "Run tests on Numeric."
+ task :all
+ end
+
+ namespace :float do
+ test "float/to_f.t"
+ test "float/zero.t"
+
+ desc "Run tests on Float"
+ task :all => [:to_f, :zero]
end
namespace :kernel do
View
56 src/classes/Float.pir
@@ -0,0 +1,56 @@
+# $Id$
+
+=head1 NAME
+
+src/classes/Float.pir - Cardinal CardinalFloat class and related functions
+
+=head1 Methods
+
+=over
+
+=item onload()
+
+initialize CardinalFloat and it's inheritance
+
+=cut
+
+.namespace ['CardinalFloat']
+
+.sub 'onload' :anon :load :init
+ .local pmc cardinalmeta, floatproto
+ cardinalmeta = get_hll_global ['CardinalObject'], '!CARDINALMETA'
+ floatproto = cardinalmeta.'new_class'('CardinalFloat', 'parent'=>'parrot;Float CardinalNumeric')
+ cardinalmeta.'register'('Float', 'parent'=>'CardinalObject', 'protoobject'=>floatproto)
+.end
+
+=item to_f()
+
+return C<self>
+
+=cut
+
+.sub 'to_f' :method
+ .return (self)
+.end
+
+=item zero?()
+
+return true when if C<self> is 0.0
+
+=cut
+
+.sub 'zero?' :method
+ eq self, 0.0, is_zero
+
+ $P0 = get_hll_global 'false'
+ goto done
+
+ is_zero:
+ $P0 = get_hll_global 'true'
+ done:
+ .return ($P0)
+.end
+
+=back
+
+=cut
View
63 src/classes/Integer.pir
@@ -20,7 +20,7 @@ CardinalInteger - Cardinal integers
.sub 'onload' :anon :init :load
.local pmc cardinalmeta, intproto
cardinalmeta = get_hll_global ['CardinalObject'], '!CARDINALMETA'
- intproto = cardinalmeta.'new_class'('CardinalInteger', 'parent'=>'parrot;Integer CardinalObject')
+ intproto = cardinalmeta.'new_class'('CardinalInteger', 'parent'=>'parrot;Integer CardinalNumeric')
cardinalmeta.'register'('Float', 'parent'=>'CardinalObject', 'protoobject'=>intproto)
.end
@@ -49,6 +49,44 @@ Returns a Perl representation of the CardinalInteger.
.return($P0)
.end
+=item odd?()
+
+Return true if C<self> is an odd number
+
+=cut
+
+.sub 'odd?' :method
+ $I0 = self
+ $P0 = get_hll_global 'true'
+
+ band $I0, 1
+ eq $I0, 1, done
+
+ even_num:
+ $P0 = get_hll_global 'false'
+ done:
+ .return ($P0)
+.end
+
+=item 'even?'
+
+Return true if C<self> is an even number
+
+=cut
+
+.sub 'even?' :method
+ $I0 = self
+ $P0 = get_hll_global 'true'
+
+ band $I0, 1
+ eq $I0, 0, done
+
+ odd_num:
+ $P0 = get_hll_global 'false'
+ done:
+ .return ($P0)
+.end
+
=item _get_bool()
Return true when integers are queried about the their truth value
@@ -75,12 +113,10 @@ Returns a CardinalString representation of the CardinalInteger.
.end
=item
-to_i()
to_int()
floor()
ceil()
truncate()
-ord()
All return C<self>
@@ -110,10 +146,6 @@ All return C<self>
.return(self)
.end
-.sub 'ord' :method
- .return (self)
-.end
-
=item
Returns 1
@@ -291,14 +323,25 @@ Round at given precision
=cut
+# Fix me: Integer#round does not return C<self>
.sub 'round' :method
.param pmc ndigits :optional
.param int has_ndigits :opt_flag
+ .local pmc rst
- # TODO
- # not yet impletmented
+ rst = self
+ unless has_ndigits goto done
- .return(self)
+ positive:
+ # TODO: return C<self> as float
+ negative:
+ # TODO: raise C<self> to 10 ^ abs(ndigits)
+ range_error:
+ # TODO: capture overflow then throw RangeError
+ argv_error:
+ # TODO: capture overflow then throw ArgumentError
+ done:
+ .return (rst)
.end
=back
View
15 src/classes/Numeric.pir
@@ -0,0 +1,15 @@
+# $Id$
+
+=head1 NAME
+
+src/classes/Numeric.pir - Cardinal CardinalNumeric class and related functions
+
+=cut
+
+.namespace ['CardinalNumeric']
+
+.sub 'onload' :anon :load :init
+ .local pmc cardinalmeta, numericproto
+ cardinalmeta = get_hll_global ['CardinalObject'], '!CARDINALMETA'
+ numericproto = cardinalmeta.'new_class'('CardinalNumeric', 'parent'=>'CardinalObject')
+.end
View
2  src/parser/actions.pm
@@ -779,7 +779,7 @@ method assoc($/) {
}
method float($/) {
- make PAST::Val.new( :value( ~$/ ), :returns('Float'), :node($/) );
+ make PAST::Val.new( :value( ~$/ ), :returns('CardinalFloat'), :node($/) );
}
method integer($/) {
View
7 t/float/to_f.t
@@ -0,0 +1,7 @@
+require 'Test'
+include Test
+
+plan 1
+
+a = 1.0
+is a, 1.0
View
10 t/float/zero.t
@@ -0,0 +1,10 @@
+require 'Test'
+include Test
+
+plan 2
+
+a = 0.0
+is a.zero?, true
+
+b = 1.0
+is b.zero?, false
View
9 t/integer/chr.t
@@ -1,7 +1,7 @@
require 'Test'
include Test
-plan 4
+plan 5
exclamation = 33.chr
is exclamation, '!'
@@ -12,8 +12,5 @@ is ua, 'A'
tilde = 126.chr
is tilde, '~'
-# TODO:
-#
-# * call chr with num over 128
-# * call chr with CardinalEncoding
-is 1, 0
+skip 'chr with internal encoding', 'pir'
+skip 'chr with Encoding opt', 'pir'
View
10 t/integer/even.t
@@ -0,0 +1,10 @@
+require 'Test'
+include Test
+
+plan 2
+
+a = 0.even?
+is a, true
+
+b = 1.even?
+is b, false
View
10 t/integer/odd.t
@@ -0,0 +1,10 @@
+require 'Test'
+include Test
+
+plan 2
+
+a = 1.odd?
+is a, true
+
+b = 2.odd?
+is b, false
View
10 t/integer/round.t
@@ -0,0 +1,10 @@
+require 'Test'
+include Test
+
+plan 3
+
+a = 1.round
+is a, 1
+
+skip 'round with positive ndigit', 'pir'
+skip 'round with negative ndigit', 'pir'
Please sign in to comment.
Something went wrong with that request. Please try again.