src: Modified compilers for the support of the new Integers
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 3 Aug 2015 22:21:35 +0000 (18:21 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 4 Aug 2015 17:58:35 +0000 (13:58 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

src/astbuilder.nit
src/astprinter.nit
src/compiler/abstract_compiler.nit
src/compiler/java_compiler.nit
src/frontend/div_by_zero.nit
src/interpreter/naive_interpreter.nit
src/literal.nit
src/modelize/modelize_property.nit
src/rapid_type_analysis.nit
src/semantize/typing.nit

index 7dafd2f..ef969d7 100644 (file)
@@ -30,9 +30,9 @@ class ASTBuilder
        var anchor: nullable MClassType
 
        # Make a new Int literal
-       fun make_int(value: Int): AIntExpr
+       fun make_int(value: Int): AIntegerExpr
        do
-               return new ADecIntExpr.make(value, mmodule.int_type)
+               return new AIntegerExpr.make(value, mmodule.int_type)
        end
 
        # Make a new instatiation
@@ -260,11 +260,11 @@ redef class AType
        end
 end
 
-redef class ADecIntExpr
+redef class AIntegerExpr
        private init make(value: Int, t: MType)
        do
                self.value = value
-               self._n_number = new TNumber # dummy
+               self._n_integer = new TInteger # dummy
                self.mtype = t
        end
 end
index 61a1083..91aee2f 100644 (file)
@@ -90,14 +90,7 @@ redef class ABlockExpr
        end
 end
 
-redef class AIntExpr
-       redef fun accept_printer(v)
-       do
-               v.write(value.to_s)
-       end
-end
-
-redef class AByteExpr
+redef class AIntegerExpr
        redef fun accept_printer(v)
        do
                v.write(value.to_s)
index 21ad2a6..f0a66ac 100644 (file)
@@ -2911,12 +2911,13 @@ redef class AOrElseExpr
        end
 end
 
-redef class AIntExpr
-       redef fun expr(v) do return v.int_instance(self.value.as(not null))
-end
-
-redef class AByteExpr
-       redef fun expr(v) do return v.byte_instance(self.value.as(not null))
+redef class AIntegerExpr
+       redef fun expr(v) do
+               if value isa Int then return v.int_instance(value.as(Int))
+               if value isa Byte then return v.byte_instance(value.as(Byte))
+               # Should never happen
+               abort
+       end
 end
 
 redef class AFloatExpr
index c229f88..f1e3437 100644 (file)
@@ -2230,12 +2230,17 @@ redef class ANotExpr
        end
 end
 
-redef class AIntExpr
-       redef fun expr(v) do return v.int_instance(self.value.as(not null))
-end
-
-redef class AByteExpr
-       redef fun expr(v) do return v.byte_instance(self.value.as(not null))
+redef class AIntegerExpr
+       redef fun expr(v) do
+               if value isa Int then
+                       return v.int_instance(self.value.as(Int))
+               else if value isa Byte then
+                       return v.byte_instance(self.value.as(Byte))
+               else
+                       # Should not happen
+                       abort
+               end
+       end
 end
 
 redef class AFloatExpr
index b7426e1..16802c0 100644 (file)
@@ -68,7 +68,7 @@ private class DivByZeroVisitor
 
                # 2. The second operand must be an integer literal
                var op2 = node.n_expr2
-               if not op2 isa AIntExpr then return
+               if not op2 isa AIntegerExpr then return
 
                # 3. Its value must be 0
                # Note: because of `literal_phase` the `value` method exists
index 61889c7..a484659 100644 (file)
@@ -1515,17 +1515,12 @@ redef class AOrElseExpr
        end
 end
 
-redef class AIntExpr
+redef class AIntegerExpr
        redef fun expr(v)
        do
-               return v.int_instance(self.value.as(not null))
-       end
-end
-
-redef class AByteExpr
-       redef fun expr(v)
-       do
-               return v.byte_instance(self.value.as(not null))
+               if value isa Int then return v.int_instance(value.as(Int))
+               if value isa Byte then return v.byte_instance(value.as(Byte))
+               return null
        end
 end
 
index 541f752..b183301 100644 (file)
@@ -69,74 +69,21 @@ redef class AExpr
        # Return null if not an integer.
        fun as_int: nullable Int
        do
-               if not self isa AIntExpr then return null
-               return self.value.as(not null)
+               if not self isa AIntegerExpr then return null
+               return self.value.as(not null).to_i
        end
 end
 
-redef class AIntExpr
+redef class AIntegerExpr
        # The value of the literal int once computed.
-       var value: nullable Int
+       var value: nullable Numeric
 
-       redef fun accept_literal(v)
-       do
-               if not text.is_int then
-                       v.toolcontext.error(hot_location, "Error: invalid literal `{text}`")
-                       return
+       redef fun accept_literal(v) do
+               value = n_integer.text.to_num
+               if value == null then
+                       v.toolcontext.error(hot_location, "Error: invalid literal `{n_integer.text}`")
                end
-               value = text.to_i
        end
-
-       private fun text: String is abstract
-end
-
-redef class ADecIntExpr
-       redef fun text do return self.n_number.text
-end
-
-redef class AHexIntExpr
-       redef fun text do return self.n_hex_number.text
-end
-
-redef class ABinIntExpr
-       redef fun text do return self.n_bin_number.text
-end
-
-redef class AOctIntExpr
-       redef fun text do return self.n_oct_number.text
-end
-
-redef class AByteExpr
-       # The value of the literal int once computed.
-       var value: nullable Byte
-
-       redef fun accept_literal(v)
-       do
-               var s = text.substring(0, text.length - 2)
-               if not s.is_int then
-                       v.toolcontext.error(hot_location, "Error: invalid byte literalĀ `{text}`")
-                       return
-               end
-               value = s.to_i.to_b
-       end
-
-       private fun text: String is abstract
-end
-
-redef class ADecByteExpr
-       redef fun text do return self.n_bytenum.text
-end
-
-redef class AHexByteExpr
-       redef fun text do return self.n_hex_bytenum.text
-end
-
-redef class ABinByteExpr
-       redef fun text do return self.n_bin_bytenum.text
-end
-
-redef class AOctByteExpr
-       redef fun text do return self.n_oct_bytenum.text
 end
 
 redef class AFloatExpr
index 467fccd..824ac8c 100644 (file)
@@ -1347,11 +1347,16 @@ redef class AAttrPropdef
                        if nexpr != null then
                                if nexpr isa ANewExpr then
                                        mtype = modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, nexpr.n_type, true)
-                               else if nexpr isa AIntExpr then
-                                       var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Int")
-                                       if cla != null then mtype = cla.mclass_type
-                               else if nexpr isa AByteExpr then
-                                       var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Byte")
+                               else if nexpr isa AIntegerExpr then
+                                       var cla: nullable MClass = null
+                                       if nexpr.value isa Int then
+                                               cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Int")
+                                       else if nexpr.value isa Byte then
+                                               cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Byte")
+                                       else
+                                               # Should not happen, and should be updated as new types are added
+                                               abort
+                                       end
                                        if cla != null then mtype = cla.mclass_type
                                else if nexpr isa AFloatExpr then
                                        var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Float")
index 05e2332..87e808c 100644 (file)
@@ -535,14 +535,7 @@ redef class AExpr
        end
 end
 
-redef class AIntExpr
-       redef fun accept_rapid_type_visitor(v)
-       do
-               allocate_mtype(v)
-       end
-end
-
-redef class AByteExpr
+redef class AIntegerExpr
        redef fun accept_rapid_type_visitor(v)
        do
                allocate_mtype(v)
index bd2fd0c..1b53f65 100644 (file)
@@ -20,6 +20,7 @@ module typing
 
 import modelize
 import local_var_init
+import literal
 
 redef class ToolContext
        var typing_phase: Phase = new TypingPhase(self, [flow_phase, modelize_property_phase, local_var_init_phase])
@@ -1368,19 +1369,15 @@ redef class AFalseExpr
        end
 end
 
-redef class AIntExpr
+redef class AIntegerExpr
        redef fun accept_typing(v)
        do
-               var mclass = v.get_mclass(self, "Int")
-               if mclass == null then return # Forward error
-               self.mtype = mclass.mclass_type
-       end
-end
-
-redef class AByteExpr
-       redef fun accept_typing(v)
-       do
-               var mclass = v.get_mclass(self, "Byte")
+               var mclass: nullable MClass = null
+               if value isa Byte then
+                       mclass = v.get_mclass(self, "Byte")
+               else if value isa Int then
+                       mclass = v.get_mclass(self, "Int")
+               end
                if mclass == null then return # Forward error
                self.mtype = mclass.mclass_type
        end