packages feed

egison-4.1.3: lib/core/shell.egi

-- Get input and parse them into the format specified with sopts and copts.
-- Used in --map and --filter options.
def SH.genInput sopts copts :=
  map (TSV.parseLine sopts copts) (io (readLines ()))
  where
    -- Read multiple lines from stdin until EOF.
    readLines () := do
      let eof := isEof ()
      if eof
        then return []
        else do let line := readLine ()
                let lines := readLines ()
                return (line :: lines)

def TSV.parseLine sopts copts line :=
  readTsv (S.intercalate "\t" (fnC copts (fnS sopts (S.split "\t" line))))
    where
      fnS sopts xs :=
        match sopts as list (list integer) with
        | [$m] :: $opts' ->
          let (hs, ts) := splitAt (m - 1) xs
           in fnS opts' (hs ++ map (\t -> S.concat ["\"", t, "\""]) ts)
        | [$m, #m] :: $opts' ->
          let (hs, ts') := splitAt (m - 1) xs
              (mf, ts)  := uncons ts'
           in fnS opts' (hs ++ S.concat ["\"", mf, "\""] :: ts)
        | [$m, $n & ?(> m)] :: $opts' ->
          let (hs, ts') := splitAt (m - 1) xs
              (ms, ts)  := splitAt (n - m + 1) ts'
           in fnS opts' (hs ++ map (\m -> S.concat ["\"", m, "\""]) ms ++ ts)
        | [$m, _] :: $opts' -> fnS ([m] :: opts') xs
        | _ -> xs
      fnC copts xs :=
        match copts as list (list integer) with
        | [$m] :: $opts' ->
          let (hs, ts) := splitAt (m - 1) xs
           in fnC opts' (hs ++ [S.concat ["[", S.intercalate ", " ts, "]"]])
        | [$m, #m] :: $opts' ->
          let (hs, ts') := splitAt (m - 1) xs
              (mf, ts)  := uncons ts'
           in fnC opts' (hs ++ S.concat ["[", mf, "]"] :: ts)
        | [$m, $n & ?(> m)] :: $opts' ->
          let (hs, ts') := splitAt (m - 1) xs
              (ms, ts)  := splitAt (n - m + 1) ts'
           in fnC opts' (hs ++ S.concat ["[", S.intercalate ", " ms, "]"] :: ts)
        | [$m, _] :: $opts' -> fnC ([m] :: opts') xs
        | _ -> xs

def TSV.show := showTsv