Skip to content

Commit

Permalink
modify src/parser/actions.pm to return CardinalFloat instead Float:
Browse files Browse the repository at this point in the history
  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
Daehyub Kim committed May 15, 2011
1 parent a6751a2 commit c4f6029
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 20 deletions.
21 changes: 18 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions src/classes/Float.pir
Original file line number Diff line number Diff line change
@@ -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
63 changes: 53 additions & 10 deletions src/classes/Integer.pir
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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>

Expand Down Expand Up @@ -110,10 +146,6 @@ All return C<self>
.return(self)
.end

.sub 'ord' :method
.return (self)
.end

=item

Returns 1
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/classes/Numeric.pir
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/parser/actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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($/) {
Expand Down
7 changes: 7 additions & 0 deletions t/float/to_f.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'Test'
include Test

plan 1

a = 1.0
is a, 1.0
10 changes: 10 additions & 0 deletions t/float/zero.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'Test'
include Test

plan 2

a = 0.0
is a.zero?, true

b = 1.0
is b.zero?, false
9 changes: 3 additions & 6 deletions t/integer/chr.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'Test'
include Test

plan 4
plan 5

exclamation = 33.chr
is exclamation, '!'
Expand All @@ -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'
10 changes: 10 additions & 0 deletions t/integer/even.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'Test'
include Test

plan 2

a = 0.even?
is a, true

b = 1.even?
is b, false
10 changes: 10 additions & 0 deletions t/integer/odd.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'Test'
include Test

plan 2

a = 1.odd?
is a, true

b = 2.odd?
is b, false
10 changes: 10 additions & 0 deletions t/integer/round.t
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit c4f6029

Please sign in to comment.