mycompile_everything.anubis 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

   This file is the development version of 'compile_everything.anubis'. 
   It is never released. 

read official_library.anubis

read tools/basis.anubis
read lexical_analysis/fast_lexer_2.anubis


   Extract the file name from a line like this: 
   
     "\nread   <name>  "
     "\ntransmit   <name>  "
     
   where the number of blancs can vary and where anything can be on the right of the file name (except '\n')
   
   
define String
  extract_file_name_2
  (
    Int            skip,
    String         s,
    List(Word8)    so_far 
  ) =   
  if nth(skip,s) is 
  {
    failure then implode(reverse(so_far)), 
    success(c) then 
      if c:[' ','\"','\r']
      then implode(reverse(so_far))                     // at end of file name
      else extract_file_name_2(skip+1,s,[c . so_far])   // not at end of file name
  }.
 
define List(Word8)
  normalize_string
  (
    List(Word8)   l
  ) =
  if l is 
  {
    [ ] then [ ], 
    [h . t] then 
           if h = '\n' then ['\\', 'n' . normalize_string(t)] 
      else if h = '\r' then ['\\', 'r' . normalize_string(t)] 
      else if h = '\"' then ['\\', '\"' . normalize_string(t)]
      else [h . normalize_string(t)] 
  }.
 
define String
  extract_file_name
  (
    Int            skip,    // characters to skip at the beginning
    String         s
  ) = implode(normalize_string(explode(s))). 
   
   implode(normalize_string(explode(to_string(to_text_mode(to_byte_array(s)))))). 
   
define String
  extract_file_name_1
  (
    Int            skip,    // characters to skip at the beginning
    String         s
  ) =
  if nth(skip,s) is 
  {
    failure then print("Cannot understand this: '"+s+"'.\n"); "", 
    success(c) then 
      if (c = ' ' | c = '\"') 
      then extract_file_name_1(skip+1,s)         // skip blanks (and double quote if any)
      else extract_file_name_2(skip+1,s,[c])     // go to state 2 of this extractor
  }.  

 define String 
  extract_file_name
  (
    Int     skip, 
    String  s
  ) =
  if nth(skip,s) is 
  {
    failure then print("Cannot understand this: '"+s+"'.\n"); "", 
    success(c) then 
      if ('a' +=< c & c +=< 'z') 
      then extract_file_name(skip+1,s)       // skip until first blank
      else extract_file_name_1(skip+1,s)     // go to state 1 of this extractor
  }.  
  

define List(LexerItem(String,One))
  read_lexer
  =
  [
    lexer_item("#nread(# )+.+",
               return((ByteArray b, Int l, Int c, One u) |-> token(extract_file_name(1,to_string(b))))),
    lexer_item("#ntransmit(# )+.+",
               return((ByteArray b, Int l, Int c, One u) |-> token(extract_file_name(1,to_string(b))))),
    lexer_item(".", ignore), 
    lexer_item("#n[a-z]*", ignore) 
  ].


define List(String)
  dependancies_2
  (
    One -> LexerOutput(String)   lexer,
    List(String)                 so_far
  ) =
  if lexer(unique) is
  {
    end_of_input        then reverse(so_far), 
    error(_0,line,col)  then print("Lexer error ["+to_string(_0)+"] at "+line+"/"+col+".\n"); reverse(so_far), 
    token(_0)           then dependancies_2(lexer,[_0 . so_far])
  }. 

define List(String)
  dependancies
  (
    String                                             dir_path, 
    String                                             name, 
    LexingStream(One) -> One -> LexerOutput(String)    prelexer
  ) =
  if file(dir_path+name,read) is 
  {
    failure then print("Cannot open file '"+dir_path+name+"'.\n"); [], 
    success(f) then if make_lexing_stream("\n",f,10000,1,unique) is 
    {
      failure then print("Cannot read file '"+dir_path+name+"'.\n"); [], 
      success(ls) then with lexer = prelexer(ls), 
        dependancies_2(lexer,[])
    }
  }. 


define One
  show_dependancies
  (
    String                                             dir_path, 
    AnubisLibraryItem                                  i,
    LexingStream(One) -> One -> LexerOutput(String)    prelexer
  ) = 
  if i is 
  {
    anubis_file(rel,name)        then
      print(dir_path+name+" :\n");  
      map_forget((String s) |-> print("  "+s+"\n"), dependancies(dir_path,name,prelexer)),
    anubis_c_file(rel,name)      then 
      print(dir_path+name+" :\n");  
      map_forget((String s) |-> print("  "+s+"\n"), dependancies(dir_path,name,prelexer)),
    anubis_font(rel,name)        then unique, 
    apg_file(rel,name)           then unique, 
    image(rel,name)              then unique, 
    text_file(rel,name)          then unique, 
    pdf_file(rel,name)           then unique, 
    directory(rel,name,content)  then
      map_forget((AnubisLibraryItem j) |-> show_dependancies(dir_path+name+"/",j,prelexer), content) 
  }. 

global define One
  show_dependancies
  (
    List(String) args
  ) =
  if make_lexer(read_lexer,'#') is 
  {
    error(e) then print(to_English(e)+".\n"), 
    ok(prelexer) then print("Lexer ok.\n"); 
      show_dependancies("",the_official_Anubis_library,prelexer)
  }. 


   Finding a file in the official library. The file B is refered to by a 'read'
   or 'transmit' from within a file A, which is in the path P relative to 
   'anubis_dev'. We search first relative to P, and then relative to 'library'. 
   Warning: the path of B can contain "..". 


define List(String)
  simplify   // simplifying a path (handling "..")
  (
    List(String)  path
  ) =
  if path is 
  {
    [ ] then [ ], 
    [h . t] then if t is 
      {
        [ ] then path, 
        [u . v] then 
          if u = ".."
          then simplify(v)
          else [h . simplify(t)]
      }
  }.
  
define Bool
  find_file_here
  (
    String                     filename, 
    List(AnubisLibraryItem)    files
  ) =
  if files is 
  {
    [ ] then false, 
    [h . t] then if h is directory(_,_,_) 
      then find_file_here(filename,t)
      else if filename = name(h) & (rel(h):[r,e])
           then true
           else find_file_here(filename,t)
  }.  
  
define Bool
  find_file_here
  (
    String              name,  // name of file 
    AnubisLibraryItem   dir    // must be a directory
  ) =  
  if dir is directory(r,n,c) 
  then find_file_here(name,c)
  else false. 
  
define Maybe(AnubisLibraryItem)
  find_subdir
  (
    String                    dir_name, 
    List(AnubisLibraryItem)   files       
  ) =
  if files is 
  {
    [ ] then failure, 
    [h . t] then if h is directory(rel,name,_)
      then if dir_name = name & (rel:[r,e])
           then success(h) 
           else find_subdir(dir_name,t)
      else find_subdir(dir_name,t)
  }.
  
  
define Maybe(AnubisLibraryItem)
  find_subdir
  (
    String              dir_name, 
    AnubisLibraryItem   dir         // must be a directory
  ) =
  if dir is directory(_,_,c) 
  then find_subdir(dir_name,c)
  else should_not_happen(failure). 
  
define Bool
  find_file_abs
  (
    List(String)        path,    // path of file relative to 'dir'
    String              name,    // name of file
    AnubisLibraryItem   dir      // must be a directory
  ) =  
  if dir is directory(_,_,_)
  then if path is 
  {
    [ ] then find_file_here(name,dir),
    [h . t] then if find_subdir(h,dir) is 
      {
        failure     then false,  
        success(d)  then find_file_abs(t,name,d)
      }
  }
  else should_not_happen(false). 
  
define Bool
  find_file_from_pathA
  (
    List(String)        pathA,   // path of file A                (e.g. ["library","tools"])   (in natural order)
    List(String)        pathB,   // path of file B                (e.g. ["..","system"])       
    String              _B,      // name of file B                (e.g. "string.anubis")
    AnubisLibraryItem   the_lib  // the whole library
  ) =
  if the_lib is directory(_,_,_) 
  then with path = simplify(pathA + reverse(pathB)),   // compute path of B relative to 'anubis_dev'
       find_file_abs(path,_B,the_lib)
  else should_not_happen(false).  


define Bool
  find_file_from_library
  (
    List(String)        pathA,   // path of file A                (e.g. ["tools","library"])
    List(String)        pathB,   // path of file B                (e.g. ["system",".."])
    String              _B,      // name of file B                (e.g. "string.anubis")
    AnubisLibraryItem   the_lib  // the whole library
  ) =
  find_file_from_pathA(["library"],pathB,_B,the_lib). 
  

define Bool
  find_file    // find file among released files only !
  (
    List(String)        pathA,   // path of file A                (e.g. ["tools","library"])
    List(String)        pathB,   // path of file B                (e.g. ["system",".."])
    String              _B,      // name of file B                (e.g. "string.anubis")
    AnubisLibraryItem   the_lib  // the whole library
  ) =
  find_file_from_pathA(pathA,pathB,_B,the_lib)
  | find_file_from_library(pathA,pathB,_B,the_lib).

define List(String)
  explode_path
  (
    List(Word8)         l,
    List(Word8)         acc, 
    List(String)        so_far
  ) =
  if l is 
  {
    [ ] then if acc is 
      {
        [ ]      then reverse(so_far),
        [_ . _]  then reverse([implode(reverse(acc)) . so_far])
      }, 
    [c . o] then 
      if c = '/' | c = '\\'
      then explode_path(o,[],[implode(reverse(acc)) . so_far])
      else explode_path(o,[c . acc],so_far)
  }. 

define One
  check_dependancy
  (
    Released           rel,
    String             _A,      // name of first file (containing 'read')
    String             dir_path, 
    String             dep,     // reference as found after 'read' or 'transmit'
    AnubisLibraryItem  the_lib
  ) =
  with   pathA = explode_path(explode(dir_path),[],[]),
       pathB_B = ["library" . explode_path(explode(dep),[],[])],
  if reverse(pathB_B) is 
  {
    [ ] then should_not_happen(unique), 
    [_B . rpathB] then with pathB = reverse(rpathB),
      if find_file(pathA,pathB,_B,the_lib) 
      then unique
      else print("released file '"+concat(pathA,"/")+"/"+_A+"'\n"+
                 "  refers to non released file '"+concat(pathB,"/")+"/"+_B+"'.\n")
  }. 
    

define One
  check_dependancies
  (
    String                                             dir_path, 
    AnubisLibraryItem                                  i,
    LexingStream(One) -> One -> LexerOutput(String)    prelexer,
    AnubisLibraryItem                                  the_lib
  ) = 
  if i is 
  {
    anubis_file(rel,name)        then
      if rel:[r,e]
      then map_forget((String s) |-> check_dependancy(rel,name,dir_path,s,the_lib), dependancies(dir_path,name,prelexer))
      else unique, 
    anubis_c_file(rel,name)      then 
      if rel:[r,e]
      then map_forget((String s) |-> check_dependancy(rel,name,dir_path,s,the_lib), dependancies(dir_path,name,prelexer))
      else unique, 
    anubis_font(rel,name)        then unique, 
    apg_file(rel,name)           then unique, 
    image(rel,name)              then unique, 
    text_file(rel,name)          then unique, 
    pdf_file(rel,name)           then unique, 
    directory(rel,name,content)  then
      map_forget((AnubisLibraryItem j) |-> check_dependancies(dir_path+name+"/",j,prelexer,the_lib), content) 
  }. 




global define One
  check_dependancies
  (
    List(String) args
  ) =
  if make_lexer(read_lexer,'#') is 
  {
    error(e) then print(to_English(e)+".\n"), 
    ok(prelexer) then print("Lexer ok.\n"); 
      check_dependancies("",the_official_Anubis_library,prelexer,the_official_Anubis_library)
  }. 



define One
   compile
   (
     String              dir_path,
     AnubisLibraryItem   item, 
   ) =
   if item is
   {
     anubis_file(rel, name)        then 
       if rel:[r,e] then forget((Maybe(Word8))execute(success(dir_path),"anubis",["-nocolor", name])) else unique,  
     anubis_c_file(rel, name)      then 
       if rel:[r,e] then forget((Maybe(Word8))execute(success(dir_path),"anubis",["-nocolor", name])) else unique,  
     anubis_font(rel, name)        then unique,
     apg_file(rel, name)           then unique,
     image(rel, name)              then unique,
     text_file(rel, name)          then unique,
     pdf_file(rel, name)           then unique,
     directory(rel, name, content) then 
       map_forget((AnubisLibraryItem i) |-> compile(dir_path+"/"+name,i),content)
   }. 


global define One
   compile_everything
   (
     List(String) args
   ) =
   compile(anubis_directory,the_official_Anubis_library).