Permalink
Browse files

Factor apart Integer and Float into separate classes

  • Loading branch information...
1 parent 9e7b74b commit de889e34bb0d461b79ab94282574c05eb62dbd91 Tony Arcieri committed Dec 18, 2010
Showing with 51 additions and 37 deletions.
  1. +18 −0 src/builtins/float.re
  2. +23 −0 src/builtins/integer.re
  3. +6 −35 src/builtins/numeric.re
  4. +4 −2 src/core/reia_dispatch.erl
View
18 src/builtins/float.re
@@ -0,0 +1,18 @@
+#
+# float.re: Methods of the Float builtin
+# Copyright (C)2010 Tony Arcieri
+#
+# Redistribution is permitted under the MIT license. See LICENSE for details.
+#
+
+class Float < Numeric
+ def class; Float; end
+
+ def to_s
+ erl.io_lib.format("~f".to_list(), [self]).to_string()
+ end
+
+ def to_integer; erl.round(self); end
+
+ def to_float; self; end
+end
View
23 src/builtins/integer.re
@@ -0,0 +1,23 @@
+#
+# integer.re: Methods of the Integer builtin
+# Copyright (C)2010 Tony Arcieri
+#
+# Redistribution is permitted under the MIT license. See LICENSE for details.
+#
+
+class Integer < Numeric
+ def class; Integer; end
+
+ def to_s
+ erl.integer_to_list(self).to_string()
+ end
+
+ def times(&block)
+ erl.lists.foreach(block, erl.lists.seq(0, self - 1))
+ self
+ end
+
+ def to_integer; self; end
+
+ def to_float; self * 1.0; end
+end
View
41 src/builtins/numeric.re
@@ -1,48 +1,19 @@
#
-# numeric.re: Methods of the Numeric builtin
+# numeric.re: Methods common to Integers and Floats
# Copyright (C)2010 Tony Arcieri
#
# Redistribution is permitted under the MIT license. See LICENSE for details.
#
class Numeric
- def class; Numeric; end
-
- def to_s
- list = if erl.is_integer(self)
- erl.integer_to_list(self)
- elseif erl.is_float(self)
- erl.io_lib.format("~f".to_list(), [self])
- end
-
- list.to_string()
- end
-
+ # alias_method :inspect, :to_s
def inspect
to_s()
end
- def times(&block)
- erl.lists.foreach(block, erl.lists.seq(0, self - 1))
- self
- end
-
- def to_integer
- if erl.is_float(self)
- erl.round(self)
- else
- self
- end
- end
-
- def to_float
- if erl.is_integer(self)
- self * 1.0
- else
- self
- end
- end
-
+ # alias_method :to_i, :to_integer
def to_i; to_integer(); end
+
+ # alias_method :to_f, :to_float
def to_f; to_float(); end
-end
+end
View
6 src/core/reia_dispatch.erl
@@ -10,8 +10,10 @@
-include("reia_types.hrl").
% Dispatch incoming calls
-call(Receiver, Method, Arguments, Block) when is_integer(Receiver) or is_float(Receiver) ->
- 'Numeric':call({Receiver, Method, Arguments}, Block);
+call(Receiver, Method, Arguments, Block) when is_integer(Receiver) ->
+ 'Integer':call({Receiver, Method, Arguments}, Block);
+call(Receiver, Method, Arguments, Block) when is_float(Receiver) ->
+ 'Float':call({Receiver, Method, Arguments}, Block);
call(Receiver, Method, Arguments, Block) when is_list(Receiver) ->
'List':call({Receiver, Method, Arguments}, Block);
call(#reia_object{class = Class} = Receiver, Method, Arguments, Block) ->

0 comments on commit de889e3

Please sign in to comment.