types.anubis 5.11 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 21/01/2017
 * Time: 01:33
 * © Calexium 
 */

read generation/types_header.anubis
read generation/types_messages.anubis
read generation/graphviz/graphviz_main.anubis

read system/files.anubis
read system/string.anubis
read system/lists.anubis
read tools/streams.anubis
read tools/basis.anubis
read densaku_lib/types/ds_types.anubis
read generation/intermediate_type.anubis

type Type_File_Parts:
  type_file_part(
    String  transmit,
    List(String)  intermediate,
    String  declaration,
    String  message_str
  )
.

define String
  concat_transmit
  (
    List(Type_File_Parts) _parts
  )=
  join("",
      map((Type_File_Parts part)
        |->
        since part is type_file_part(transmit_str, _, _, _),
        transmit_str
      ,
      _parts
      )
  )
.

define List(String)
  unique_intermediate
  (
    List(String)  intermediate_types,
    List(String)  so_far
  )=
  if intermediate_types is
  {
    []        then so_far,
    [h . t ]  then unique_intermediate(t, append_once(so_far, h))
  }
.

define List(String)
  unique_intermediate
  (
    List(Type_File_Parts) _parts,
    List(String)          so_far
  )=
  if _parts is
  {
    []      then so_far,
    [h . t] then 
      since h is type_file_part(_, intermediate, _, _),
      unique_intermediate(intermediate, so_far)
  }
.



define String
  concat_intermediate
  (
    List(Type_File_Parts) _parts
  )=
  with final_list = unique_intermediate(_parts, []),
  join("\n",
      map((String intermediate)
        |->
        "public type "+intermediate+":..."
      ,
      final_list
      )
  )+"\n"
.

define String
  concat_declaration
  (
    List(Type_File_Parts) _parts
  )=
  join("\n\n",
      map((Type_File_Parts part)
        |->
        since part is type_file_part(_, _, declaration_str, _),
        declaration_str
      ,
      _parts
      )
  )
.

define String
  concat_message
  (
    List(Type_File_Parts) _parts
  )=
  join("\n\n",
      map((Type_File_Parts part)
        |->
        since part is type_file_part(_, _, _, message_str),
        message_str
      ,
      _parts
      )
  )
.

define Maybe(One)
  write_type_file
  (
    String  file_name,
    String  header,
    List(Type_File_Parts) file_parts
  )=
  
  //with file_name = dest_dir+to_lower(name)+".anubis",  
  //create all necessary directories if doesn't exist.
  make_directories(file_name);
  if file(file_name, new) is
  {
    failure     then  println("can't create file "+file_name);failure,
    success(fd) then 
      with strm = make_stream(fd),
      write_string(strm,
        header +
        concat_transmit(file_parts) + "\n" +
        concat_intermediate(file_parts) + "\n" +
        concat_declaration(file_parts) +
        concat_message(file_parts)
      )
  }
.

define Type_File_Parts
  make_type_file
  (
    DS_Type  ds_type,   //type description
    String   dest_dir,
    List(DS_Type) same_file_types
  )
  =
  since ds_type is ds_type(name, generation, alternatives),
  println("Generating file for type ["+name+"]");
      with transmit_foreign = generate_transmit_foreign_type(ds_type, same_file_types),
      with intermediate_type= get_all_intermediate_type(same_file_types),
      with type_declaration = dump_DS_Type(ds_type),
      with message_str      = 
        if generation is
        {
          no_message  then "",
          message     then         
            generate_type_message_description(ds_type) +
            generate_type_to_message(ds_type)          +
            generate_type_from_message(ds_type)        
        },
    type_file_part(transmit_foreign, intermediate_type, type_declaration, message_str)
.
  
public define One
  make_all_types
  (
    DS_Type_File_Output_Dir   out_dir,
    DS_Type_File_Output_Name  output_file_name,
    List(DS_Type)             types,
    String                    destination_dir
  )=
  with final_destination_dir = 
    if out_dir is
    {
      auto                  then  destination_dir + "types/generated/",
      relative_path(path)   then  destination_dir + path + "/",
      absolute_path(path)   then  path
    },
    
  if output_file_name is 
  {
    auto            then
      map_forget((DS_Type type) 
        |-> 
        since type is ds_type(type_name, _, ds_alts),
        
        with file_name = final_destination_dir+to_lower(type_name)+".anubis",
        println("generating type file for Type ["+type_name+"] to "+file_name);

        with header = generate_header,
        with file_parts = make_type_file(type, final_destination_dir, []),

        write_type_file(file_name, header, [file_parts])
      ,
      types)
                       
    name(_file_name) then
      with file_name = final_destination_dir+_file_name+".anubis",

      with header = generate_header,      
      with file_parts =
        map((DS_Type type) 
          |-> 
          since type is ds_type(type_name, _, ds_alts),
          println("generating type file for Type ["+type_name+"] to "+file_name);
          make_type_file(type, final_destination_dir, types)
        ,
        types),

      forget(write_type_file(file_name, header, file_parts))

  }

.