package alba
Alba compiler
Install
Dune Dependency
Authors
Maintainers
Sources
0.4.2.tar.gz
sha256=203ee151ce793a977b2d3e66f8b3a0cd7a82cc7f15550c63d88cb30c71eb5f95
md5=64367c393f80ca784f88d07155da4fb0
doc/src/alba.fmlib_native/native_io.ml.html
Source file native_io.ml
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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
open Fmlib open Module_types module Buffer: sig type t val make: int -> (Bytes.t -> int -> int -> int) -> (Bytes.t -> int -> int -> int) -> t val is_ok: t -> bool val is_full: t -> bool val flush: t -> unit module Read: functor (W:WRITABLE) -> sig end module Write: functor (R:READABLE) -> sig val write: t -> R.t -> R.t end end = struct type t = { mutable rp: int; (* The content of the buffer is between the read and the write pointer. *) mutable wp: int; mutable flag: bool; (* ok flag, set to false if (a) refilling a buffer adds 0 bytes, (b) flushing a nonempty buffer does not write anything to the filesystem. *) read: Bytes.t -> int -> int -> int; (* refill function *) write: Bytes.t -> int -> int -> int; (* flush function *) bytes: Bytes.t} let make (n:int) (read:Bytes.t -> int -> int -> int) (write:Bytes.t -> int -> int -> int) : t = assert (n > 0); assert (n <= Sys.max_string_length); {rp = 0; wp = 0; bytes = Bytes.create n; flag = true; read; write} let is_ok (b:t): bool = b.flag let is_empty (b:t): bool = b.rp = b.wp let reset (b:t): unit = b.rp <- 0; b.wp <- 0 let is_full (b:t): bool = b.wp = Bytes.length b.bytes let flush (b:t): unit = if not (is_empty b) && is_ok b then let n = b.write b.bytes b.rp (b.wp - b.rp) in if n = 0 then b.flag <- false else (* BUG: what if n > 0 && n <> b.wp - b.rp ?? Only a part of the buffer has been written!! *) reset b (*let get (b:t): char = assert (not (is_empty b)); let c = Bytes.get b.bytes b.rp in b.rp <- b.rp + 1; c*) let putc (b:t) (c:char): unit = assert (is_ok b); if is_full b then flush b; assert (not (is_full b)); Bytes.set b.bytes b.wp c; b.wp <- b.wp + 1 module Read (W:WRITABLE) = struct (*let read (b:t) (w:W.t): W.t = let rec read w = if not (is_empty b) && W.needs_more w then read (W.putc w (get b)) else w in read w*) end module Write (R:READABLE) = struct let write (b:t) (r:R.t): R.t = let rec write r = if not (is_full b) && R.has_more r then (putc b (R.peek r); write (R.advance r)) else r in write r end end (* Buffer *) module File_system: sig type t val make: unit -> t val flush_all: t -> unit type in_file type out_file val stdin: in_file val stdout: out_file val stderr: out_file (*val getc: t -> in_file -> char option val getline: t -> in_file -> string option val putc: t -> out_file -> char -> unit val open_for_read: t -> string -> in_file option val open_for_write: t -> string -> out_file option val create: t -> string -> out_file option val close_in: t -> in_file -> unit val close_out: t -> out_file -> unit*) val flush: t -> out_file -> unit module Read: functor (W:WRITABLE) -> sig val read_buffer: t -> in_file -> W.t -> W.t val read: t -> in_file -> W.t -> W.t end module Write: functor (R:READABLE) -> sig val write_buffer: t -> out_file -> R.t -> R.t val write: t -> out_file -> R.t -> R.t end end = struct type file = | Read of Unix.file_descr * Buffer.t | Write of Unix.file_descr * Buffer.t (*| Free of int*) type t = {mutable files: file array; mutable first_free: int; line_buf: Buffer.t} type in_file = int type out_file = int let buffer_size = 4096 let unix_read (fd:Unix.file_descr) (b:Bytes.t) (ofs:int) (n:int): int = try Unix.read fd b ofs n with Unix.Unix_error _ -> 0 let unix_write (fd:Unix.file_descr) (b:Bytes.t) (ofs:int) (n:int): int = try Unix.write fd b ofs n with Unix.Unix_error _ -> 0 let readable_file (fd:Unix.file_descr): file = Read (fd, Buffer.make buffer_size (unix_read fd) (fun _ _ _ -> assert false)) let writable_file (fd:Unix.file_descr): file = Write (fd, Buffer.make buffer_size (fun _ _ _ -> assert false) (unix_write fd)) let make (): t = {first_free = -1; files = [| readable_file Unix.stdin; writable_file Unix.stdout; writable_file Unix.stderr |]; line_buf = let fr _ _ _ = assert false in let fw _ _ _ = assert false in Buffer.make 200 fr fw } (*let put_to_files (fs:t) (file:file): int option = if fs.first_free >= 2 then begin let fd = fs.first_free in match fs.files.(fd) with | Free n -> fs.first_free <- n; fs.files.(fd) <- file; Some fd | _ -> assert false (* Cannot happen, must be free! *) end else begin let nfiles = Array.length fs.files in let files = Array.make (nfiles + 1) file in Array.blit fs.files 0 files 0 nfiles; fs.files <- files; Some nfiles end*) let writable_buffer (fs:t) (fd:int): Buffer.t = assert (fd < Array.length fs.files); match fs.files.(fd) with | Write (_,b) -> b | _ -> assert false (*let readable_buffer (fs:t) (fd:int): Buffer.t = assert (fd < Array.length fs.files); match fs.files.(fd) with | Read (_,b) -> b | _ -> assert false*) (*let getc (fs:t) (fd:in_file): char option = Buffer.getc (readable_buffer fs fd) let putc (fs:t) (fd:out_file) (c:char): unit = Buffer.putc (writable_buffer fs fd) c let open_for_read (fs:t) (path:string): in_file option = try put_to_files fs (readable_file (Unix.openfile path [Unix.O_RDONLY] 0o640)) with Unix.Unix_error _ -> None let open_for_write (fs:t) (path:string): out_file option = try put_to_files fs (writable_file (Unix.openfile path [Unix.O_WRONLY] 0o640)) with Unix.Unix_error _ -> None let create (fs:t) (path:string): out_file option = try put_to_files fs (writable_file (Unix.openfile path [Unix.O_CREAT] 0o640)) with Unix.Unix_error _ -> None*) (*let unix_file_descriptor (fs:t) (fd:int): Unix.file_descr = assert (fd < Array.length fs.files); match fs.files.(fd) with | Read (fd,_) -> fd | Write (fd,_) -> fd*) (*let close_file (fs:t) (fd:int): unit = assert (fd < Array.length fs.files); match fs.files.(fd) with | Read (fd,_) -> Unix.close fd | Write (fd,b) -> Buffer.flush b; Unix.close fd | Free _ -> () let close_in (fs:t) (fd:in_file): unit = close_file fs fd let close_out (fs:t) (fd:out_file): unit = close_file fs fd*) let flush (fs:t) (fd:out_file) : unit = assert (fd < Array.length fs.files); match fs.files.(fd) with | Write (_,b) -> Buffer.flush b | _ -> () let flush_all (fs:t): unit = for i = 0 to Array.length fs.files - 1 do flush fs i done let stdin: in_file = 0 let stdout: out_file = 1 let stderr: out_file = 2 (*let stdin_buffer (fs:t): Buffer.t = readable_buffer fs stdin let stdout_buffer (fs:t): Buffer.t = writable_buffer fs stdout let stderr_buffer (fs:t): Buffer.t = writable_buffer fs stderr let getline (fs:t) (fd:in_file): string option = assert (fd < Array.length fs.files); let b = readable_buffer fs fd in Buffer.reset fs.line_buf; let content () = Some (Buffer.content fs.line_buf) in let len = Buffer.size fs.line_buf in let rec read (i:int): string option = if i = len then content () else begin match Buffer.getc b with | None -> if i = 0 then None else content () | Some c -> if c = '\n' then content () else begin Buffer.putc fs.line_buf c; read (i+1) end end in read 0 *) module Read (W:WRITABLE) = struct let read_buffer (_:t) (_:in_file) (_:W.t): W.t = assert false let read (_:t) (_:in_file) (_:W.t): W.t = assert false end module Write (R:READABLE) = struct module BW = Buffer.Write (R) let write_buffer (fs:t) (fd:out_file) (r:R.t): R.t = assert (fd < Array.length fs.files); BW.write (writable_buffer fs fd) r let write (fs:t) (fd:out_file) (r:R.t): R.t = assert (fd < Array.length fs.files); let b = writable_buffer fs fd in let rec write r = let more = R.has_more r in if not (Buffer.is_full b) && more then write @@ BW.write b r else if Buffer.is_ok b && more then (Buffer.flush b; write r) else r in write r end end (* File_system *) module IO0: Make_io.SIG = struct type program = | More of (File_system.t * (File_system.t -> program)) | Done let rec execute_program: program -> unit = function | Done -> () | More (fs, f) -> execute_program (f fs) (* [f fs] does one execution step and returns the remainder of the program. *) (* The same as an iteration ======================== let execute_program (p:program): unit = let pref = ref p in while !pref <> Done do match !pref with | Done -> assert false (* cannot happen *) | More (fs, f) -> pref := f fs done *) type 'a cont = 'a -> File_system.t -> program module M = Monad.Of_sig_min( struct type 'a t = File_system.t -> 'a cont -> program let return (a:'a): 'a t = fun fs k -> k a fs let (>>=) (m:'a t) (f:'a -> 'b t): 'b t = fun fs k -> m fs (fun a fs -> More (fs, fun fs -> f a fs k)) end) include M type in_file = File_system.in_file type out_file = File_system.out_file let stdin: in_file = File_system.stdin let stdout: out_file = File_system.stdout let stderr: out_file = File_system.stderr let exit (code:int): 'a t = fun fs _ -> File_system.flush_all fs; Stdlib.exit code let execute (p:unit t): unit = let fs = File_system.make () in let _ = try execute_program (p fs (fun () _ -> Done)) with e -> File_system.flush_all fs; raise e in File_system.flush_all fs; Stdlib.exit 0 let command_line: string array t = fun fs k -> k Sys.argv fs let current_working_directory: string t = fun fs k -> k (Sys.getcwd ()) fs let path_separator: char = if Sys.win32 then '\\' else '/' let path_delimiter: char = if Sys.win32 then ';' else ':' let read_directory (path:string): string array option t = fun fs k -> k (try Some (Sys.readdir path) with _ -> None) fs let cli_prompt (prompt: string): string option t = fun fs k -> File_system.flush fs stdout; k (let res = LNoise.linenoise prompt in match res with | None -> res | Some str -> ignore (LNoise.history_add str); res) fs let cli_loop (state: 'a) (get_prompt: 'a -> string option) (next: 'a -> string -> 'a t) (stop: 'a -> 'a t) : 'a t = let rec loop state = match get_prompt state with | None -> return state | Some prompt_string -> cli_prompt prompt_string >>= function | None -> stop state | Some line -> next state line >>= loop in loop state module Read (W:WRITABLE) = struct module FS_Read = File_system.Read (W) let read_buffer (fd:in_file) (w:W.t): W.t t = fun fs k -> k (FS_Read.read_buffer fs fd w) fs let read (fd:in_file) (w:W.t): W.t t = fun fs k -> k (FS_Read.read fs fd w) fs end module Write (R:READABLE) = struct module FS_Write = File_system.Write (R) let write_buffer (fd:out_file) (r:R.t): R.t t = fun fs k -> k (FS_Write.write_buffer fs fd r) fs let write (fd:out_file) (r:R.t): R.t t = fun fs k -> k (FS_Write.write fs fd r) fs end end module IO: Io.SIG = Make_io.Make (IO0)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>