1 # coding: utf-8
2 # frozen_string_literal: true
3
4
5
6 module Umu
7
8 module ConcreteSyntax
9
10 module Core
11
12 module Expression
13
14 module MemoStream
15
16 class Abstract < Expression::Abstract; end
17
18
19
20
21 class Nil < Abstract
22 def to_s
23 '&{}'
24 end
25
26
27 def pretty_print(q)
28 q.text '&{}'
29 end
30
31
32 private
33
34 def __desugar__(env, event)
35 ASCE.make_memo_stream_nil self.loc
36 end
37 end
38
39
40
41 class Cons < Abstract
42 attr_reader :head_expr, :tail_expr
43
44 def initialize(loc, head_expr, tail_expr)
45 ASSERT.kind_of head_expr, CSCE::Abstract
46 ASSERT.kind_of tail_expr, CSCE::Abstract
47
48 super(loc)
49
50 @head_expr = head_expr
51 @tail_expr = tail_expr
52 end
53
54
55 def to_s
56 format("&{%s | %s}",
57 self.head_expr.to_s,
58 self.tail_expr.to_s
59 )
60 end
61
62
63 def pretty_print(q)
64 PRT.group q, bb:'&{', eb:'}' do
65 q.pp self.head_expr
66
67 q.breakable
68
69 q.text '|'
70
71 q.breakable
72
73 q.pp self.tail_expr
74 end
75 end
76
77
78 private
79
80 def __desugar__(env, event)
81 new_env = env.enter event
82
83 ASCE.make_memo_stream_cons(
84 self.loc,
85 self.head_expr.desugar(new_env),
86 self.tail_expr.desugar(new_env)
87 )
88 end
89 end
90
91 end # Umu::ConcreteSyntax::Core::Expression::MemoStream
92
93
94 module_function
95
96 def make_memo_stream_nil(loc)
97 ASSERT.kind_of loc, LOC::Entry
98
99 MemoStream::Nil.new(loc).freeze
100 end
101
102
103 def make_memo_stream_cons(loc, head_expr, tail_expr)
104 ASSERT.kind_of loc, LOC::Entry
105 ASSERT.kind_of head_expr, CSCE::Abstract
106 ASSERT.kind_of tail_expr, CSCE::Abstract
107
108 MemoStream::Cons.new(loc, head_expr, tail_expr).freeze
109 end
110
111 end # Umu::ConcreteSyntax::Core::Expression
112
113 end # Umu::ConcreteSyntax::Core
114
115 end # Umu::ConcreteSyntax
116
117 end # Umu