File: value/core/morph/stream/cell.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Umu#6
  module: Value#8
  module: Core#10
  module: Morph#12
  module: Stream#14
  module: Cell#16
has properties
constant: NIL #59
function: make_nil #125
function: make_cons / 2 #130
  class: Abstract#18
inherits from
  Top ( Umu::Value::Core )
has properties
constant: TYPE_SYM #19
method: nil? #22
method: step / 1 #27
method: force / 3 #32
  class: Nil#39
inherits from
  Abstract ( Umu::Value::Core::Morph::Stream::Cell )
has properties
constant: TYPE_SYM #40
method: nil? #43
method: to_s #48
method: force / 3 #53
  class: Cons#63
inherits from
  Abstract ( Umu::Value::Core::Morph::Stream::Cell )
has properties
constant: TYPE_SYM #64
attribute: head_expr [R] #68
attribute: tail_stream [R] #68
method: initialize / 2 #70
method: nil? #79
method: to_s #84
method: force / 3 #92

Class Hierarchy

Object ( Builtin-Module )
Top ( Umu::Value::Core )
Abstract ( Umu::Value::Core::Morph::Stream::Cell ) — #18
  Nil    #39
  Cons    #63

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 Morph
  13 
  14  module Stream
  15 
  16  module Cell
  17 
  18  class Abstract < Top
  19      TYPE_SYM = :StreamCell
  20 
  21 
  22      def nil?
  23          raise X::InternalSubclassResponsibility
  24      end
  25 
  26 
  27      def step(env)
  28          VC.make_cell_stream_entry self, env.va_context
  29      end
  30 
  31 
  32      def force(_loc, _env, _event)
  33          raise X::InternalSubclassResponsibility
  34      end
  35  end
  36 
  37 
  38 
  39  class Nil < Abstract
  40      TYPE_SYM = :StreamNil
  41 
  42 
  43      def nil?
  44          true
  45      end
  46 
  47 
  48      def to_s
  49          '&{}'
  50      end
  51 
  52 
  53      def force(_loc, _env, _event)
  54          VC.make_none
  55      end
  56  end
  57  Nil.freeze
  58 
  59  NIL = Nil.new.freeze
  60 
  61 
  62 
  63  class Cons < Abstract
  64      TYPE_SYM = :StreamCons
  65 
  66 
  67 
  68      attr_reader :head_expr, :tail_stream
  69 
  70      def initialize(head_expr, tail_stream)
  71          ASSERT.kind_of head_expr,   ASCE::Abstract
  72          ASSERT.kind_of tail_stream, Stream::Entry::Abstract
  73 
  74          @head_expr   = head_expr
  75          @tail_stream = tail_stream
  76      end
  77 
  78 
  79      def nil?
  80          false
  81      end
  82 
  83 
  84      def to_s
  85          format("&{ %s | %s }",
  86                   self.head_expr.to_s,
  87                   self.tail_stream.to_s
  88          )
  89      end
  90 
  91 
  92      def force(loc, env, event)
  93          new_env = env.enter event
  94 
  95          head_value  = self.head_expr.evaluate(new_env).value
  96 
  97          tail_stream  = self.tail_stream.step(new_env)
  98          unless tail_stream.kind_of? Stream::Entry::Abstract
  99              raise X::TypeError.new(
 100                  loc,
 101                  new_env,
 102                  "force: Expected a Stream, but %s : %s",
 103                  tail_stream.to_s,
 104                  tail_stream.type_sym.to_s
 105              )
 106          end
 107 
 108          if (
 109              head_value.kind_of?(Stream::Entry::Abstract) &&
 110              head_value.cell.nil? &&
 111              tail_stream.cell.nil?
 112          )
 113              VC.make_none
 114          else
 115              VC.make_some VC.make_tuple(head_value, tail_stream)
 116          end
 117      end
 118  end
 119  Cons.freeze
 120 
 121 
 122 
 123  module_function
 124 
 125      def make_nil
 126          NIL
 127      end
 128 
 129 
 130      def make_cons(head_expr, tail_stream)
 131          ASSERT.kind_of head_expr,   ASCE::Abstract
 132          ASSERT.kind_of tail_stream, Stream::Entry::Abstract
 133 
 134          Cons.new(head_expr, tail_stream).freeze
 135      end
 136 
 137  end # Umu::Value::Morph::Core::Stream::Cell
 138 
 139  end # Umu::Value::Morph::Core::Stream
 140 
 141  end # Umu::Value::Morph::Core
 142 
 143  end # Umu::Value::Morph
 144 
 145  end # Umu::Value
 146 
 147  end # Umu