nightshade

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

types.ads (5142B)



with Ada.Strings.Hash;
with Ada.Containers.Vectors;
with Ada.Containers.Indefinite_Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Containers.Indefinite_Hashed_Maps;

package Types is

   type Token_Kind is
     (Ident,
      Float_Literal,
      Int_Literal,
      Str_Literal,
      Bool_Literal,
      L_Paren,
      R_Paren,
      L_Bracket,
      R_Bracket,
      L_Brace,
      R_Brace,
      ColonColon,
      Colon,
      Comma,
      Separator,
      Keyword,
      Unknown,
      EOF);

   type Source_Ref is not null access constant String;
   type File_Name_Ref is not null access constant String;

   type Token is record
      Kind      : Token_Kind;
      Lexeme    : Unbounded_String;
      Line, Col : Positive;
   end record;

   package Token_Vector is new Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Token);
   type Expr;
   type Expr_Ref is access Expr;

   --------------------------
   -- Parser related types --
   --------------------------

   type AST_Scope;

   type AST_Scope_Ref is access AST_Scope;

   type AST_Type_Info;
   type AST_Function_Info;

   type AST_Type_Info_Ref is access all AST_Type_Info;
   type AST_Function_Info_Ref is access all AST_Function_Info;

   type Param is record
      Name : Unbounded_String;
      Kind : Unbounded_String;
   end record;

   package Param_Vectors is new Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Param);

   package Expr_Vectors is new
     Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Expr_Ref);

   type Stmt_Kind is (Stmt_Call_Kind);

   type Call_Expr is record
      Name        : Unbounded_String;
      Params      : Expr_Vectors.Vector;
      Entity_Type : AST_Type_Info_Ref;
   end record;

   type Stmt (Kind : Stmt_Kind) is record
      Scope : AST_Scope_Ref;
      case Kind is
         when Stmt_Call_Kind =>
            Call : Call_Expr;

            --  when Stmt_If =>
            --     Iff : If_Stmt;

            --  when Stmt_While =>
            --     Whilee : While_Stmt;

            --  when Stmt_Expr =>
            --     Expr : Expr_Stmt;
      end case;
   end record;

   package Stmt_Vectors is new
     Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Stmt);

   type Expr_Kind is (Expr_Call, String_Literal);
   type Expr (Kind : Expr_Kind := Expr_Call) is record
      Entity_Type : AST_Type_Info_Ref;
      case Kind is
         when Expr_Call =>
            Call : Call_Expr;

         when String_Literal =>
            Lit_Str   : Unbounded_String;
            File_Name : File_Name_Ref;
            Line      : Positive;
            Col       : Positive;
      end case;
   end record;

   type Func_Decl is record
      Name        : Unbounded_String;
      Params      : Param_Vectors.Vector;
      Return_Kind : Unbounded_String; -- TODO convert return type to a type we can type check and spit IR for
      Stmts       : Stmt_Vectors.Vector;
      Public      : Boolean;
      File_Name   : File_Name_Ref;
      Line        : Positive;
      Col         : Positive;
   end record;

   type Decl_Kind is (Function_Kind);

   type Decl (Kind : Decl_Kind) is record
      Scope : AST_Scope_Ref;
      case Kind is
         when Function_Kind =>
            Func : Func_Decl;

            --  when Struct_Kind =>
            --     Struct : Struct_Decl;
      end case;
   end record;

   package Declaration_Vectors is new
     Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Decl);

   ---------------------------
   -- Binding related types --
   ---------------------------

   type AST_Symbol_Kind_Enum is (SYM_FUNC, SYM_VAR, SYM_LET);
   type Type_Kind_Enum is (TY_STRING, TY_INT, TY_FLOAT, TY_BOOL, TY_VOID, TY_FUNC, TY_ERROR);

   type AST_Type_List is array (Positive range <>) of AST_Type_Info_Ref;
   type AST_Type_List_Ref is access all AST_Type_List;

   type AST_Function_Info is record
      Params      : AST_Type_List_Ref;
      Return_Type : AST_Type_Info_Ref;
   end record;

   type AST_Type_Info (Kind : Type_Kind_Enum := TY_ERROR) is record
      case Kind is
         when TY_FUNC =>
            Func : AST_Function_Info;

         when others =>
            null;
      end case;
   end record;

   --------------------------------
   -- symbol info -----------------
   -- made of kind and type info --
   --------------------------------

   type AST_Symbol_Info is record
      Name        : Unbounded_String;
      Kind        : AST_Symbol_Kind_Enum;
      Entity_Type : AST_Type_Info_Ref;
   end record;

   -------------------------------------
   -- hashmap of String : Symbol_Info --
   -------------------------------------

   package Symbols_Map is new
     Ada.Containers.Indefinite_Hashed_Maps
       (Key_Type        => String,
        Element_Type    => AST_Symbol_Info, -- symbol's info
        Hash            => Ada.Strings.Hash,
        Equivalent_Keys => "=");

   -------------
   --- scope ---
   -------------

   type AST_Scope is record
      Symbols : Symbols_Map.Map;
      Parent  : AST_Scope_Ref;
   end record;

end Types;