compiler: Updated toolchain for proper byte literal support
authorLucas Bajolet <r4pass@hotmail.com>
Wed, 27 May 2015 17:39:38 +0000 (13:39 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Fri, 29 May 2015 14:39:37 +0000 (10:39 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

src/astprinter.nit
src/compiler/abstract_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 1a4d804..61a1083 100644 (file)
@@ -97,6 +97,13 @@ redef class AIntExpr
        end
 end
 
+redef class AByteExpr
+       redef fun accept_printer(v)
+       do
+               v.write(value.to_s)
+       end
+end
+
 redef class ANewExpr
        redef fun accept_printer(v)
        do
index 259c597..853feb1 100644 (file)
@@ -2866,6 +2866,10 @@ 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))
+end
+
 redef class AFloatExpr
        redef fun expr(v) do return v.float_instance("{self.n_float.text}") # FIXME use value, not n_float
 end
index 1e187e2..b7426e1 100644 (file)
@@ -66,7 +66,7 @@ private class DivByZeroVisitor
                # 1. We need a `/` operation
                if not node isa ASlashExpr then return
 
-               # 2. The second operand must be a integer literal
+               # 2. The second operand must be an integer literal
                var op2 = node.n_expr2
                if not op2 isa AIntExpr then return
 
index 3626d33..16e0b48 100644 (file)
@@ -1638,6 +1638,13 @@ redef class AIntExpr
        end
 end
 
+redef class AByteExpr
+       redef fun expr(v)
+       do
+               return v.byte_instance(self.value.as(not null))
+       end
+end
+
 redef class AFloatExpr
        redef fun expr(v)
        do
index 1d64458..f9b8b5f 100644 (file)
@@ -146,6 +146,58 @@ redef class AOctIntExpr
        end
 end
 
+redef class AByteExpr
+       # The value of the literal int once computed.
+       var value: nullable Byte
+end
+
+redef class ADecByteExpr
+       redef fun accept_literal(v)
+       do
+               var t = self.n_bytenum.text
+               value = t.substring(0, t.length - 2).to_i.to_b
+       end
+end
+
+redef class AHexByteExpr
+       redef fun accept_literal(v)
+       do
+               var t = self.n_hex_bytenum.text
+               var s = t.substring(2, t.length - 4).remove_underscores
+               if s.is_empty then
+                       v.toolcontext.error(location, "Error: invalid hexadecimal literal")
+                       return
+               end
+               value = s.to_hex.to_b
+       end
+end
+
+redef class ABinByteExpr
+       redef fun accept_literal(v)
+       do
+               var t = self.n_bin_bytenum.text
+               var s = t.substring(2, t.length - 4).remove_underscores
+               if s.is_empty then
+                       v.toolcontext.error(location, "Error: invalid binary literal")
+                       return
+               end
+               value = s.to_bin.to_b
+       end
+end
+
+redef class AOctByteExpr
+       redef fun accept_literal(v)
+       do
+               var t = self.n_oct_bytenum.text
+               var s = t.substring(2, t.length - 4).remove_underscores
+               if s.is_empty then
+                       v.toolcontext.error(location, "Error: invalid octal literal")
+                       return
+               end
+               value = s.to_oct.to_b
+       end
+end
+
 redef class AFloatExpr
        # The value of the literal float once computed.
        var value: nullable Float
index f1cce80..0a948d2 100644 (file)
@@ -1317,6 +1317,9 @@ redef class AAttrPropdef
                                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")
+                                       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")
                                        if cla != null then mtype = cla.mclass_type
index bcd9f8e..e27a98e 100644 (file)
@@ -517,6 +517,13 @@ redef class AIntExpr
        end
 end
 
+redef class AByteExpr
+       redef fun accept_rapid_type_visitor(v)
+       do
+               v.add_type(self.mtype.as(MClassType))
+       end
+end
+
 redef class AFloatExpr
        redef fun accept_rapid_type_visitor(v)
        do
index c26ca0f..3c7c9b1 100644 (file)
@@ -1342,6 +1342,15 @@ redef class AIntExpr
        end
 end
 
+redef class AByteExpr
+       redef fun accept_typing(v)
+       do
+               var mclass = v.get_mclass(self, "Byte")
+               if mclass == null then return # Forward error
+               self.mtype = mclass.mclass_type
+       end
+end
+
 redef class AFloatExpr
        redef fun accept_typing(v)
        do