Permalink
Browse files

Merge pull request #7 from lateau/lib191

Lib191
  • Loading branch information...
2 parents 3fa71e7 + 5ae2128 commit 6dc91ecc571079d9963deebd0ff05fcd1123ef14 @tadzik tadzik committed May 15, 2011
Showing with 281 additions and 7 deletions.
  1. +19 −2 Rakefile
  2. +1 −0 setup.pir
  3. +56 −0 src/classes/Float.pir
  4. +119 −4 src/classes/Integer.pir
  5. +15 −0 src/classes/Numeric.pir
  6. +1 −1 src/parser/actions.pm
  7. +7 −0 t/float/to_f.t
  8. +10 −0 t/float/zero.t
  9. +16 −0 t/integer/chr.t
  10. +10 −0 t/integer/even.t
  11. +10 −0 t/integer/odd.t
  12. +7 −0 t/integer/ord.t
  13. +10 −0 t/integer/round.t
View
@@ -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"
@@ -528,9 +528,26 @@ namespace :test do |ns|
test "integer/times.t"
test "integer/cmp.t"
test "integer/pred.t"
+ test "integer/chr.t"
+ test "integer/odd.t"
+ test "integer/even.t"
+ test "integer/round.t"
desc "Run tests on Integer."
- task :all => [:integer, :times, :cmp, :pred]
+ 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
@@ -71,6 +71,7 @@ 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/Array.pir
src/classes/Hash.pir
View
@@ -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
@@ -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
@@ -79,8 +117,8 @@ to_i()
to_int()
floor()
ceil()
-round()
truncate()
+ord()
All return C<self>
@@ -102,7 +140,7 @@ All return C<self>
.return(self)
.end
-.sub 'round' :method
+.sub 'ord' :method
.return(self)
.end
@@ -224,7 +262,7 @@ Return C<self> plus 1
=item pred()
-Return C<self> mius 1
+Return C<self> minus 1
=cut
@@ -235,6 +273,83 @@ Return C<self> mius 1
.return ($P1)
.end
+=item chr()
+
+Return a string represented by C<self>
+
+=cut
+
+.sub 'chr' :method
+ .param pmc enc :optional
+ .param int has_enc :opt_flag
+ .local int val
+ .local string tmp
+ .local pmc rst
+
+ val = self
+
+ if has_enc goto decode
+ if val > 0xff goto decode
+ if val >= 0x80 goto str_conv
+ if val < 0x00 goto range_error
+
+ ascii:
+ tmp = ''
+ chr tmp, val
+ rst = new 'CardinalString'
+ rst = tmp
+ goto done
+
+ str_conv:
+ rst = new 'CardinalString'
+ rst = rst.'new'(val)
+ goto done
+
+ decode:
+ # TODO:
+ #
+ # if has_enc : get encoding with enc. goto ascii when if enc is not valid
+ # if internal encoding is not null: decode with internal encoding
+ # if internal encoding is null: goto RangeError
+ print "Not Yet Implemented\n"
+
+ range_error:
+ # TODO:
+ # payload in Exception
+ $P0 = new 'RangeError'
+ throw $P0
+
+ done:
+ .return (rst)
+.end
+
+=item round()
+
+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
+
+ rst = self
+ unless has_ndigits goto done
+
+ 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
=cut
View
@@ -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
@@ -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
@@ -0,0 +1,7 @@
+require 'Test'
+include Test
+
+plan 1
+
+a = 1.0
+is a, 1.0
View
@@ -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
@@ -0,0 +1,16 @@
+require 'Test'
+include Test
+
+plan 5
+
+exclamation = 33.chr
+is exclamation, '!'
+
+ua = 65.chr
+is ua, 'A'
+
+tilde = 126.chr
+is tilde, '~'
+
+skip 'chr with internal encoding', 'pir'
+skip 'chr with Encoding opt', 'pir'
View
@@ -0,0 +1,10 @@
+require 'Test'
+include Test
+
+plan 2
+
+a = 0.even?
+is a, true
+
+b = 1.even?
+is b, false
View
@@ -0,0 +1,10 @@
+require 'Test'
+include Test
+
+plan 2
+
+a = 1.odd?
+is a, true
+
+b = 2.odd?
+is b, false
View
@@ -0,0 +1,7 @@
+require 'Test'
+include Test
+
+plan 1
+
+a = 1.ord
+is a, 1
View
@@ -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 6dc91ec

Please sign in to comment.