--  Verada predefined library: what Vibe's Python forms need. Python
--  prints a value without Ada's leading space, and a Boolean as `True` or `False`.
with Ada.Exceptions;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Text_IO;
with Verada.Strings.Texts;

package Verada.Vibe is
   --  Python's built-in exceptions that a Vibe program raises and handles by name (Python's
   --  hierarchy is the front end's: `except LookupError` handles IndexError and KeyError).
   --  AssertionError is Ada.Assertions.Assertion_Error and MemoryError Storage_Error, which
   --  `assert` and memory raise.
   ArithmeticError, ZeroDivisionError, OverflowError, FloatingPointError : exception;
   LookupError, IndexError, KeyError : exception;
   ValueError, TypeError, AttributeError, NameError, EOFError : exception;
   RuntimeError, NotImplementedError, RecursionError, StopIteration : exception;
   UnicodeError, PermissionError, FileNotFoundError, OSError, TimeoutError : exception;

   --  Python's print of a piece, a text: its code points as UTF-8 (D171)
   procedure Write (Item : Verada.Strings.Texts.Text)
     with Global => (In_Out => Ada.Text_IO.File_System);

   --  a text as an exception's or an assertion's message, which is a String: its code points
   --  as UTF-8, whole
   function Message (Item : Verada.Strings.Texts.Text) return String is
     (Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode (Verada.Strings.Texts.To_Wide_Wide_String (Item)))
     with Global => null;

   --  Python's str of an exception, its message: a Vibe message is UTF-8 (`Message`), and
   --  a byte sequence that is not UTF-8 reads as U+FFFD where it breaks
   function Message_Text (X : Ada.Exceptions.Exception_Occurrence) return Verada.Strings.Texts.Text is
     (Verada.Strings.Texts.To_Text_Of (Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Decode (Ada.Exceptions.Exception_Message (X))))
     with Global => null;

   --  Python's image of a value, from its Ada image (`X'Image`)
   --  (from where the Ada image starts, or the next index, and no longer than it or `False`)
   function Image (Ada_Image : String) return String
     with Global => null,
          Post => Image'Result'Length <= Integer'Max (Ada_Image'Length, 5)
                  and then (if Image'Result'Length > 0 then Image'Result'First >= 1 and then Image'Result'First - 1 <= Integer'Max (Ada_Image'First, 1));

   --  Python's image of a float: the fewest digits that read back as it (Python's repr)
   function Float_Image (X : Long_Float) return String
     with Global => null,
          Post => Float_Image'Result'First = 1 and then Float_Image'Result'Length <= 32;

   --  Python's repr of an int, a float, a bool and a str (the str quoted, with Python's
   --  escapes, as Verada.Vibe.Formatting's Repr writes it): how a list, a dict, a set or
   --  a tuple shows its elements
   function Repr (Value : Integer) return Verada.Strings.Texts.Text
     with Global => null;
   function Repr (Value : Long_Float) return Verada.Strings.Texts.Text
     with Global => null;
   function Repr (Value : Boolean) return Verada.Strings.Texts.Text
     with Global => null;
   function Repr (Value : Verada.Strings.Texts.Text) return Verada.Strings.Texts.Text
     with Global => null;

   --  the elements' reprs so far and the next, separated by ", " (a collection's elements,
   --  `[for X of C => Repr (X)]'Reduce (Joined, "")`: no repr is empty, so the first is
   --  the one with nothing before it)
   function Joined (So_Far, Next : Verada.Strings.Texts.Text) return Verada.Strings.Texts.Text
     with Global => null;

   --  a dict's key and value, by their reprs: `k: v`
   function Paired (Key, Value : Verada.Strings.Texts.Text) return Verada.Strings.Texts.Text
     with Global => null;

   --  a collection's repr from its elements' joined: between its brackets, or the empty
   --  collection's repr when there are none (`[]`, `{}`, `set()`)
   function Enclosed (Elements : Verada.Strings.Texts.Text; Open, Close, Empty : String) return Verada.Strings.Texts.Text
     with Global => null;

   --  Python's slice bound (`xs[a:b]`'s a or b) for a sequence of this length: a position from 0,
   --  counted from the end when negative, then kept within 0 .. Length
   function Slice_Bound (Bound : Integer; Length : Natural) return Natural is
     (if Bound < 0 then (if Bound + Length < 0 then 0 else Bound + Length)
      elsif Bound > Length then Length
      else Bound)
     with Global => null;

   --  Python's slice bound for a negative step (`s[a:b:-1]`'s a or b): a position from 0,
   --  counted from the end when negative, then kept within -1 .. Length - 1 (-1 is before the
   --  first element, where a backward slice may stop)
   function Slice_Bound_Down (Bound : Integer; Length : Natural) return Integer is
     (if Bound < 0 then (if Bound + Length < 0 then -1 else Bound + Length)
      elsif Bound >= Length then Length - 1
      else Bound)
     with Global => null,
          Post => Slice_Bound_Down'Result in -1 .. Length - 1;
private
   procedure Float_Digits (X : Long_Float; Item : out String; Count : out Natural)
     with Import, Convention => Ada, External_Name => "verada_rt_python_float",
          Global => null,
          Post => Count <= Item'Length;
end Verada.Vibe;
