From c40a9f39ce5a7cf9eca38a5d92382e209105c2f8 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 3 Aug 2015 18:21:35 -0400 Subject: [PATCH] src: Modified compilers for the support of the new Integers Signed-off-by: Lucas Bajolet --- src/astbuilder.nit | 8 ++-- src/astprinter.nit | 9 +---- src/compiler/abstract_compiler.nit | 13 ++++--- src/compiler/java_compiler.nit | 17 +++++--- src/frontend/div_by_zero.nit | 2 +- src/interpreter/naive_interpreter.nit | 13 ++----- src/literal.nit | 69 ++++----------------------------- src/modelize/modelize_property.nit | 15 ++++--- src/rapid_type_analysis.nit | 9 +---- src/semantize/typing.nit | 19 ++++----- 10 files changed, 55 insertions(+), 119 deletions(-) diff --git a/src/astbuilder.nit b/src/astbuilder.nit index 7dafd2f..ef969d7 100644 --- a/src/astbuilder.nit +++ b/src/astbuilder.nit @@ -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 diff --git a/src/astprinter.nit b/src/astprinter.nit index 61a1083..91aee2f 100644 --- a/src/astprinter.nit +++ b/src/astprinter.nit @@ -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) diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 21ad2a6..f0a66ac 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -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 diff --git a/src/compiler/java_compiler.nit b/src/compiler/java_compiler.nit index c229f88..f1e3437 100644 --- a/src/compiler/java_compiler.nit +++ b/src/compiler/java_compiler.nit @@ -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 diff --git a/src/frontend/div_by_zero.nit b/src/frontend/div_by_zero.nit index b7426e1..16802c0 100644 --- a/src/frontend/div_by_zero.nit +++ b/src/frontend/div_by_zero.nit @@ -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 diff --git a/src/interpreter/naive_interpreter.nit b/src/interpreter/naive_interpreter.nit index 61889c7..a484659 100644 --- a/src/interpreter/naive_interpreter.nit +++ b/src/interpreter/naive_interpreter.nit @@ -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 diff --git a/src/literal.nit b/src/literal.nit index 541f752..b183301 100644 --- a/src/literal.nit +++ b/src/literal.nit @@ -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 diff --git a/src/modelize/modelize_property.nit b/src/modelize/modelize_property.nit index 467fccd..824ac8c 100644 --- a/src/modelize/modelize_property.nit +++ b/src/modelize/modelize_property.nit @@ -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") diff --git a/src/rapid_type_analysis.nit b/src/rapid_type_analysis.nit index 05e2332..87e808c 100644 --- a/src/rapid_type_analysis.nit +++ b/src/rapid_type_analysis.nit @@ -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) diff --git a/src/semantize/typing.nit b/src/semantize/typing.nit index bd2fd0c..1b53f65 100644 --- a/src/semantize/typing.nit +++ b/src/semantize/typing.nit @@ -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 -- 1.7.9.5