From 8f7e550c2ee0b2520e6a0233e2b2f3d8715f1b98 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Wed, 27 May 2015 13:39:38 -0400 Subject: [PATCH] compiler: Updated toolchain for proper byte literal support Signed-off-by: Lucas Bajolet --- src/astprinter.nit | 7 +++++ src/compiler/abstract_compiler.nit | 4 +++ src/frontend/div_by_zero.nit | 2 +- src/interpreter/naive_interpreter.nit | 7 +++++ src/literal.nit | 52 +++++++++++++++++++++++++++++++++ src/modelize/modelize_property.nit | 3 ++ src/rapid_type_analysis.nit | 7 +++++ src/semantize/typing.nit | 9 ++++++ 8 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/astprinter.nit b/src/astprinter.nit index 1a4d804..61a1083 100644 --- a/src/astprinter.nit +++ b/src/astprinter.nit @@ -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 diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 259c597..853feb1 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -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 diff --git a/src/frontend/div_by_zero.nit b/src/frontend/div_by_zero.nit index 1e187e2..b7426e1 100644 --- a/src/frontend/div_by_zero.nit +++ b/src/frontend/div_by_zero.nit @@ -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 diff --git a/src/interpreter/naive_interpreter.nit b/src/interpreter/naive_interpreter.nit index 3626d33..16e0b48 100644 --- a/src/interpreter/naive_interpreter.nit +++ b/src/interpreter/naive_interpreter.nit @@ -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 diff --git a/src/literal.nit b/src/literal.nit index 1d64458..f9b8b5f 100644 --- a/src/literal.nit +++ b/src/literal.nit @@ -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 diff --git a/src/modelize/modelize_property.nit b/src/modelize/modelize_property.nit index f1cce80..0a948d2 100644 --- a/src/modelize/modelize_property.nit +++ b/src/modelize/modelize_property.nit @@ -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 diff --git a/src/rapid_type_analysis.nit b/src/rapid_type_analysis.nit index bcd9f8e..e27a98e 100644 --- a/src/rapid_type_analysis.nit +++ b/src/rapid_type_analysis.nit @@ -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 diff --git a/src/semantize/typing.nit b/src/semantize/typing.nit index c26ca0f..3c7c9b1 100644 --- a/src/semantize/typing.nit +++ b/src/semantize/typing.nit @@ -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 -- 1.7.9.5