[m-rev.] for post-commit review: handle all builtin types in library/term_to_xml.m

Julien Fischer jfischer at opturion.com
Wed Oct 16 17:33:41 AEDT 2019


For post-commit review by anyone.

----------------------------------------------------

Handle all builtin types in library/term_to_xml.m.

library/term_to_xml.m:
     As above, this module had not been updated to cover the new builtin
     integer types.

tests/hard_coded/write_xml.{m,exp,exp2}:
     Extend this test case to cover the new integer types.

     Document what the expected outputs correspond to.

Julien.

diff --git a/library/term_to_xml.m b/library/term_to_xml.m
index 1e3bcb4..027e7f1 100644
--- a/library/term_to_xml.m
+++ b/library/term_to_xml.m
@@ -213,15 +213,16 @@
      %   1. simple: The functors `[]', `[|]' and `{}' are mapped to the elements
      %   `List', `Nil' and `Tuple' respectively. Arrays are assigned the
      %   `Array' element. The builtin types are assigned the elements `Int',
-    %   `String', `Float' and `Char'. All other functors are assigned elements
-    %   with the same name as the functor provided the functor name is
-    %   well formed and does not start with a capital letter. Otherwise,
-    %   a mangled version of the functor name is used.
+    %   `Int8', `Int16', `Int32' `Int64', `UInt', `UInt8', `UInt16, `UInt32',
+    %   `UInt64', `String', `Float' and `Char'. All other functors are assigned
+    %   elements with the same name as the functor provided the functor name is
+    %   well formed and does not start with a capital letter. Otherwise, a
+    %   mangled version of the functor name is used.
      %
-    %   All elements except `Int', `String', `Float' and `Char' will have
+    %   All elements except those corresponding to builtin types will have
      %   their `functor', `arity', `type' and `field' (if there is a field name)
-    %   attributes set. `Int', `String', `Float' and `Char' elements will
-    %   just have their `type' and possibly their `field' attributes set.
+    %   attributes set. Elements corresponding to builtin types will just have
+    %   their `type' and possibly their `field' attributes set.
      %
      %   The `simple' mapping is designed to be easy to read and use, but
      %   may result in the same element being assigned to different functors.
@@ -746,14 +747,32 @@ array_element = "Array".
  :- pred is_primitive_type(type_desc::in, string::out) is semidet.

  is_primitive_type(TypeDesc, Element) :-
-    ( if TypeDesc = type_of("") then
+    ( if TypeDesc = type_of(_ : string) then
          Element = "String"
-    else if TypeDesc = type_of('c') then
+    else if TypeDesc = type_of(_ : character) then
          Element = "Char"
-    else if TypeDesc = type_of(1) then
+    else if TypeDesc = type_of(_ : int) then
          Element = "Int"
-    else if TypeDesc = type_of(1.0) then
+    else if TypeDesc = type_of(_ : float) then
          Element = "Float"
+    else if TypeDesc = type_of(_ : uint) then
+        Element = "UInt"
+    else if TypeDesc = type_of(_ : int8) then
+        Element = "Int8"
+    else if TypeDesc = type_of(_ : int16) then
+        Element = "Int16"
+    else if TypeDesc = type_of(_ : int32) then
+        Element = "Int32"
+    else if TypeDesc = type_of(_ : int64) then
+        Element = "Int64"
+    else if TypeDesc = type_of(_ : uint8) then
+        Element = "UInt8"
+    else if TypeDesc = type_of(_ : uint16) then
+        Element = "UInt16"
+    else if TypeDesc = type_of(_ : uint32) then
+        Element = "UInt32"
+    else if TypeDesc = type_of(_ : uint64) then
+        Element = "UInt64"
      else
          fail
      ).
@@ -857,14 +876,32 @@ make_du_functor(Functor, Arity) = du_functor(Functor, Arity).

  primitive_value(Univ, PrimValue) :-
      ( if univ_to_type(Univ, String) then
-        PrimValue = String`with_type`string
+        PrimValue = String : string
      else if univ_to_type(Univ, Char) then
          PrimValue = char_to_string(Char)
      else if univ_to_type(Univ, Int) then
          PrimValue = int_to_string(Int)
-    else
-        univ_to_type(Univ, Float),
+    else if univ_to_type(Univ, Float) then
          PrimValue = float_to_string(Float)
+    else if univ_to_type(Univ, UInt) then
+        PrimValue = uint_to_string(UInt)
+    else if univ_to_type(Univ, Int8) then
+        PrimValue = int8_to_string(Int8)
+    else if univ_to_type(Univ, Int16) then
+        PrimValue = int16_to_string(Int16)
+    else if univ_to_type(Univ, Int32) then
+        PrimValue = int32_to_string(Int32)
+    else if univ_to_type(Univ, Int64) then
+        PrimValue = int64_to_string(Int64)
+    else if univ_to_type(Univ, UInt8) then
+        PrimValue = uint8_to_string(UInt8)
+    else if univ_to_type(Univ, UInt16) then
+        PrimValue = uint16_to_string(UInt16)
+    else if univ_to_type(Univ, UInt32) then
+        PrimValue = uint32_to_string(UInt32)
+    else
+        univ_to_type(Univ, UInt64),
+        PrimValue = uint64_to_string(UInt64)
      ).

  %---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/write_xml.exp b/tests/hard_coded/write_xml.exp
index 204bb36..2c2f118 100644
--- a/tests/hard_coded/write_xml.exp
+++ b/tests/hard_coded/write_xml.exp
@@ -3,7 +3,7 @@ unsupported_dtd_type(pred(int))

  <?xml version="1.0"?>
  <?xml-stylesheet type="text/css" href="http://www.cs.mu.oz.au/a_css.css"?>
-<Array--array-46array-40write_xml-46mytype-41 functor="<<array>>" type="array.array(write_xml.mytype)" arity="12">
+<Array--array-46array-40write_xml-46mytype-41 functor="<<array>>" type="array.array(write_xml.mytype)" arity="13">
  	<Tag_Tag-45--1--write_xml-46mytype functor="Tag-" type="write_xml.mytype" arity="1">
  		<Int type="int">44</Int>
  	</Tag_Tag-45--1--write_xml-46mytype>
@@ -139,6 +139,18 @@ is a <string>&</String>
  	<pointer--1--write_xml-46mytype functor="pointer" type="write_xml.mytype" arity="1">
  		<c_pointer functor="c_pointer(0x0)" type="c_pointer" arity="0" />
  	</pointer--1--write_xml-46mytype>
+	<primitives--10--write_xml-46mytype functor="primitives" type="write_xml.mytype" arity="10">
+		<Int type="int">1</Int>
+		<UInt type="uint">2</UInt>
+		<Int8 type="int8">3</Int8>
+		<Int16 type="int16">4</Int16>
+		<Int32 type="int32">5</Int32>
+		<Int64 type="int64">6</Int64>
+		<UInt8 type="uint8">7</UInt8>
+		<UInt16 type="uint16">8</UInt16>
+		<UInt32 type="uint32">9</UInt32>
+		<UInt64 type="uint64">10</UInt64>
+	</primitives--10--write_xml-46mytype>
  </Array--array-46array-40write_xml-46mytype-41>
  Result 2:
  ok
@@ -526,7 +538,21 @@ is a <string>&</String>
  												<pointer functor="pointer" type="write_xml.mytype" arity="1">
  													<Unknown functor="c_pointer(0x0)" type="c_pointer" arity="0" />
  												</pointer>
-												<Nil functor="[]" type="list.list(write_xml.mytype)" arity="0" />
+												<List functor="[|]" type="list.list(write_xml.mytype)" arity="2">
+													<primitives functor="primitives" type="write_xml.mytype" arity="10">
+														<Int type="int">1</Int>
+														<UInt type="uint">2</UInt>
+														<Int8 type="int8">3</Int8>
+														<Int16 type="int16">4</Int16>
+														<Int32 type="int32">5</Int32>
+														<Int64 type="int64">6</Int64>
+														<UInt8 type="uint8">7</UInt8>
+														<UInt16 type="uint16">8</UInt16>
+														<UInt32 type="uint32">9</UInt32>
+														<UInt64 type="uint64">10</UInt64>
+													</primitives>
+													<Nil functor="[]" type="list.list(write_xml.mytype)" arity="0" />
+												</List>
  											</List>
  										</List>
  									</List>
diff --git a/tests/hard_coded/write_xml.exp2 b/tests/hard_coded/write_xml.exp2
index e749fb0..b041646 100644
--- a/tests/hard_coded/write_xml.exp2
+++ b/tests/hard_coded/write_xml.exp2
@@ -3,7 +3,7 @@ unsupported_dtd_type(pred(int))

  <?xml version="1.0"?>
  <?xml-stylesheet type="text/css" href="http://www.cs.mu.oz.au/a_css.css"?>
-<Array--array-46array-40write_xml-46mytype-41 functor="<<array>>" type="array.array(write_xml.mytype)" arity="12">
+<Array--array-46array-40write_xml-46mytype-41 functor="<<array>>" type="array.array(write_xml.mytype)" arity="13">
  	<Tag_Tag-45--1--write_xml-46mytype functor="Tag-" type="write_xml.mytype" arity="1">
  		<Int type="int">44</Int>
  	</Tag_Tag-45--1--write_xml-46mytype>
@@ -129,6 +129,18 @@ is a <string>&</String>
  	<pointer--1--write_xml-46mytype functor="pointer" type="write_xml.mytype" arity="1">
  		<c_pointer functor="c_pointer(0x0)" type="c_pointer" arity="0" />
  	</pointer--1--write_xml-46mytype>
+	<primitives--10--write_xml-46mytype functor="primitives" type="write_xml.mytype" arity="10">
+		<Int type="int">1</Int>
+		<UInt type="uint">2</UInt>
+		<Int8 type="int8">3</Int8>
+		<Int16 type="int16">4</Int16>
+		<Int32 type="int32">5</Int32>
+		<Int64 type="int64">6</Int64>
+		<UInt8 type="uint8">7</UInt8>
+		<UInt16 type="uint16">8</UInt16>
+		<UInt32 type="uint32">9</UInt32>
+		<UInt64 type="uint64">10</UInt64>
+	</primitives--10--write_xml-46mytype>
  </Array--array-46array-40write_xml-46mytype-41>
  Result 2:
  ok
@@ -516,7 +528,21 @@ is a <string>&</String>
  												<pointer functor="pointer" type="write_xml.mytype" arity="1">
  													<Unknown functor="c_pointer(0x0)" type="c_pointer" arity="0" />
  												</pointer>
-												<Nil functor="[]" type="list.list(write_xml.mytype)" arity="0" />
+												<List functor="[|]" type="list.list(write_xml.mytype)" arity="2">
+													<primitives functor="primitives" type="write_xml.mytype" arity="10">
+														<Int type="int">1</Int>
+														<UInt type="uint">2</UInt>
+														<Int8 type="int8">3</Int8>
+														<Int16 type="int16">4</Int16>
+														<Int32 type="int32">5</Int32>
+														<Int64 type="int64">6</Int64>
+														<UInt8 type="uint8">7</UInt8>
+														<UInt16 type="uint16">8</UInt16>
+														<UInt32 type="uint32">9</UInt32>
+														<UInt64 type="uint64">10</UInt64>
+													</primitives>
+													<Nil functor="[]" type="list.list(write_xml.mytype)" arity="0" />
+												</List>
  											</List>
  										</List>
  									</List>
diff --git a/tests/hard_coded/write_xml.m b/tests/hard_coded/write_xml.m
index d635c94..2dc8da4 100644
--- a/tests/hard_coded/write_xml.m
+++ b/tests/hard_coded/write_xml.m
@@ -1,6 +1,8 @@
  %---------------------------------------------------------------------------%
  % vim: ts=4 sw=4 et ft=mercury
  %---------------------------------------------------------------------------%
+% The .exp file is for the low-level C grades.
+% The .exp2 file is for the high-level C grades.

  :- module write_xml.

@@ -42,7 +44,10 @@
      ;       t(type_desc)
      ;       ctor(type_ctor_desc)
      ;       pointer(c_pointer)
-    ;       foreign(ftype).
+    ;       foreign(ftype)
+    ;       primitives(
+                int, uint, int8, int16, int32, int64,
+                uint8, uint16, uint32, uint64).

  :- type listPart
      --->    listPart(int)
@@ -166,7 +171,8 @@ main(!IO) :-
              t(type_of(!.M)),
              ctor(type_ctor(type_of(!.M))),
              foreign(F),
-            pointer(P)
+            pointer(P),
+            primitives(1, 2u, 3i8, 4i16, 5i32, 6i64, 7u8, 8u16, 9u32, 10u64)
          ],
          Map = !.M
      ),


More information about the reviews mailing list