File: value/core/io.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Umu#6
  module: Value#8
  module: Core#10
has properties
function: make_input / 1 #161
function: make_output / 1 #168
  module: IO#12
  class: Abstract#14
inherits from
  Object ( Umu::Value::Core )
has properties
attribute: io [R] #15
method: initialize / 1 #18
method: to_s #27
  class: Input#35
inherits from
  Abstract ( Umu::Value::Core::IO )
has properties
method: meth_seen / 3 #41
method: meth_get_string / 3 #53
constant: FN_DEST #64
method: meth_each_line / 3 #93
  class: Output#101
inherits from
  Abstract ( Umu::Value::Core::IO )
has properties
method: meth_told / 3 #107
method: meth_put_string / 4 #119
method: meth_flush / 3 #133
method: meth_pretty_print / 4 #145

Class Hierarchy

Object ( Builtin-Module )
Top ( Umu::Value::Core )
Object ( Umu::Value::Core )
Abstract ( Umu::Value::Core::IO ) — #14
  Input    #35
  Output    #101

Code

   1  # coding: utf-8
   2  # frozen_string_literal: true
   3 
   4 
   5 
   6  module Umu
   7 
   8  module Value
   9 
  10  module Core
  11 
  12  module IO
  13 
  14  class Abstract < Object
  15      attr_reader :io
  16 
  17 
  18      def initialize(io)
  19          ASSERT.kind_of io, ::IO
  20 
  21          super()
  22 
  23          @io = io
  24      end
  25 
  26 
  27      def to_s
  28          self.io.inspect
  29      end
  30  end
  31  Abstract.freeze
  32 
  33 
  34 
  35  class Input  < Abstract
  36      define_instance_method(
  37          :meth_seen,
  38          :seen, [],
  39          [], VC::Unit
  40      )
  41      def meth_seen(_loc, _env, _event)
  42          self.io.close unless self.io.tty?
  43 
  44          VC.make_unit
  45      end
  46 
  47 
  48      define_instance_method(
  49          :meth_get_string,
  50          :gets, [],
  51          [], VCU::Option::Abstract
  52      )
  53      def meth_get_string(_loc, _env, _event)
  54          s = self.io.gets
  55 
  56          if s
  57              VC.make_some VC.make_string(s.chomp)
  58          else
  59              VC.make_none
  60          end
  61      end
  62 
  63 
  64      FN_DEST = lambda { |input|
  65              result = (
  66                  if input.io.eof?
  67                      VC.make_none
  68                  else
  69                      s = input.io.gets
  70                      if s
  71                          str_val = VC.make_string s.chomp
  72 
  73                          VC.make_some(
  74                              VC.make_tuple(
  75                                  str_val,
  76                                  VC.make_enumerator(input, FN_DEST)
  77                              )
  78                          )
  79                      else
  80                         VC.make_none
  81                      end
  82                  end
  83              )
  84 
  85              ASSERT.kind_of result, VCU::Option::Abstract
  86          }
  87 
  88      define_instance_method(
  89          :meth_each_line,
  90          :'each-line', [],
  91          [], VCM::Enum::Abstract
  92      )
  93      def meth_each_line(_loc, _env, _event)
  94          VC.make_enumerator self, FN_DEST
  95      end
  96  end
  97  Input.freeze
  98 
  99 
 100 
 101  class Output < Abstract
 102      define_instance_method(
 103          :meth_told,
 104          :told, [],
 105          [], VC::Unit
 106      )
 107      def meth_told(_loc, _env, _event)
 108          self.io.close unless self.io.tty?
 109 
 110          VC.make_unit
 111      end
 112 
 113 
 114      define_instance_method(
 115          :meth_put_string,
 116          :puts, [],
 117          [VCA::String], VC::Unit
 118      )
 119      def meth_put_string(_loc, _env, _event, value)
 120          ASSERT.kind_of value, VCA::String
 121 
 122          self.io.print value.val
 123 
 124          VC.make_unit
 125      end
 126 
 127 
 128      define_instance_method(
 129          :meth_flush,
 130          :flush, [],
 131          [], VC::Unit
 132      )
 133      def meth_flush(_loc, _env, _event)
 134          self.io.flush
 135 
 136          VC.make_unit
 137      end
 138 
 139 
 140      define_instance_method(
 141          :meth_pretty_print,
 142          :pp, [],
 143          [VC::Top], VC::Unit
 144      )
 145      def meth_pretty_print(_loc, _env, event, value)
 146          ASSERT.kind_of value, VC::Top
 147 
 148          PP.pp value, self.io
 149 
 150          VC.make_unit
 151      end
 152  end
 153  Output.freeze
 154 
 155  end # Umu::Value::Core::IO
 156 
 157 
 158 
 159  module_function
 160 
 161      def make_input(io)
 162          ASSERT.kind_of io, ::IO
 163 
 164          IO::Input.new(io).freeze
 165      end
 166 
 167 
 168      def make_output(io)
 169          ASSERT.kind_of io, ::IO
 170 
 171          IO::Output.new(io).freeze
 172      end
 173 
 174  end # Umu::Value::Core
 175 
 176  end # Umu::Value
 177 
 178  end # Umu