~/bend-docscommunity

impl.bend source

impl.bend on the hub · documented module

# THIS MODULE IS PROJECT-AGNOSTIC. KEEP IT THAT WAY.# A POLYFILL: Bend has no pipes to a started program and no stdin handle yet. Delete this module# when Base has them, and call Base instead.# This program's stdin, and a started program's stdin and stdout, as Files; and the UTF-8 lines# that come through them. A line has at most max bytes with its LF or CRLF: a longer one fails with# E2BIG (7).import Base# Code# ====# This program's stdin. A File owns its descriptor: ask for it once.def stdin() -> IO(File):  import "./std.c"  import "./std.js"# Start a program, with its stdin and stdout as pipes: its process id, the File that writes its# stdin, and the File that reads its stdout. Its stderr is ours. Closing the stdin File gives it# EOF; nothing waits for it to exit. On JS, it runs on Bun on Linux and macOS only; elsewhere it# fails with ENOSYS.def open(  program: String,  args: List<String>) -> IO(Result<&1, &1, U32 & String, U32 & (File & File)>):  import "./open.c"  import "./open.js"# A line with no LF that ends the input is the last line; one that does not is too long.def read_line.peeked(  next: File & Result<&1, &1, U32 & String, String>,  text: String) -> File  & Result<&1, &1, U32 & String, Maybe<String>>:  match next:    case (file, Fail{error}):      (file, Fail{error})    case (file, Done{SNil{}}):      (file, Done{Some{text}})    case (file, Done{SCon{_, _}}):      (file, Fail{(7, "a line is longer than its cap")})# The line of a piece that ends with its LF: the piece without the LF, and the CR before it, if any.def read_line.unended(+piece: String) -> String:  +line = String.take(piece, Nat.sub(String.length(piece), 1n))  Bool.pick(    String,    String.ends_with(line, "\r"),    String.take(line, Nat.sub(String.length(line), 1n)),    line  )def read_line.if(  file: File,  ended: Bool,  piece: String) -> IO(File & Result<&1, &1, U32 & String, Maybe<String>>):  match ended:    case True{}:      IO.pure(        File & Result<&1, &1, U32 & String, Maybe<String>>,        (file, Done{Some{read_line.unended(piece)}})      )    case False{}:      do IO<File & Result<&1, &1, U32 & String, Maybe<String>>>:        next: File & Result<&1, &1, U32 & String, String> <- File.read_line(file, 1)        return read_line.peeked(next, piece)def read_line.got(  got: File & Result<&1, &1, U32 & String, String>) -> IO(File & Result<&1, &1, U32 & String, Maybe<String>>):  match got:    case (file, Fail{error}):      IO.pure(File & Result<&1, &1, U32 & String, Maybe<String>>, (file, Fail{error}))    case (file, Done{SNil{}}):      IO.pure(File & Result<&1, &1, U32 & String, Maybe<String>>, (file, Done{None{}}))    case (file, Done{SCon{h, t}}):      +piece: String = SCon{h, t}      read_line.if(file, String.ends_with(piece, "\n"), piece)# The next line of file, as it comes, without its LF or CRLF; None at the end of the file.def read_line(file: File, max: U32) -> IO(File & Result<&1, &1, U32 & String, Maybe<String>>):  IO.bind(    File & Result<&1, &1, U32 & String, String>,    File & Result<&1, &1, U32 & String, Maybe<String>>,    File.read_line(file, max),    read_line.got  )def write_lines.text(lines: List<String>) -> String:  match lines:    case Nil{}:      ""    case Con{line, rest}:      line ++ "\n" ++ write_lines.text(rest)# Writes each line to file, with its LF.def write_lines(file: File, lines: List<String>) -> IO(File & Result<&1, &1, U32 & String, Unit>):  File.write(file, write_lines.text(lines))