nightshade

The nightshade programming language, compiler and tools (WIP)
Log | Files | Refs | README

utils.adb (2331B)



with Ada.Text_IO;                   use Ada.Text_IO;
with Ada.Strings.Unbounded;         use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;

package body Utils is
   function Read_File (File_Name : String) return String is
      F       : File_Type;
      Content : Unbounded_String := Null_Unbounded_String;
   begin
      Open (F, In_File, File_Name);

      while not End_Of_File (F) loop
         declare
            Line : Unbounded_String;
         begin
            Get_Line (F, Line);
            Append (Content, Line);
         end;
         if not End_Of_file (F) then
            Append (Content, ASCII.LF);
         end if;
      end loop;
      close (F);

      return To_String (Content);

   end Read_File;

   -----------
   -- print --
   -----------

   procedure Print_Statement (S : in Stmt; Indent : Natural) is
   begin
      Put (Indent * "  ");
      if S.Kind = Stmt_Call_Kind then
         Put ("Call: " & To_String (S.Call.Name) & "() with Params: ");
         for P of S.Call.Params loop
            if P.Kind = String_Literal then
               Put (To_String (P.Lit_Str) & " ");
            end if;
         end loop;
         Put_Line ("Scope: " & AST_Scope_Ref'Image (S.Scope));
      end if;
   end;

   procedure Print_Func_decl (F : in Func_Decl; N : Natural) is
   begin
      Put (N * "  ");
      Put_Line ("Name: " & To_String (F.Name));
      for P of F.Params loop
         Put (N * "  ");
         Put_Line (To_String (P.Name) & " : " & To_String (P.Kind));
      end loop;
      Put ((N * "  ") & "Returns: " & To_String (F.Return_Kind));
      Put_Line ("");
      Put (N * "  ");
      Put_Line ("Stmts:");
      for S of F.Stmts loop
         Print_Statement (S, N + 1);
      end loop;
   end;

   procedure Print_AST (Decls : Declaration_Vectors.Vector) is
   begin
      Put_Line ("------------------------------ Print AST: ------------------------------------");
      for Decl of Decls loop
         if Decl.Kind = Function_Kind then
            Put_Line ("Function: (at scope " & AST_Scope_Ref'Image (Decl.Scope) & " )");
            Print_Func_decl (Decl.Func, 1);
         end if;
      end loop;
      Put_Line ("-------------------------------------------------------------------------------");
   end Print_AST;

end Utils;