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 Union
13
14 class Abstract < Object
15 def self.base_type_sym
16 raise X::InternalSubclassResponsibility
17 end
18
19
20 def self.order_num
21 raise X::InternalSubclassResponsibility
22 end
23
24
25 def base_type_sym
26 self.class.base_type_sym
27 end
28
29
30 def order_num
31 self.class.order_num
32 end
33
34
35 def to_s
36 format("&%s %s", self.type_sym, self.contents.to_s)
37 end
38
39
40 def pretty_print(q)
41 q.text format("&%s ", self.type_sym.to_s)
42 q.pp self.contents
43 end
44
45
46 define_instance_method(
47 :meth_to_string,
48 :'to-s', [],
49 [], VCA::String
50 )
51 def meth_to_string(loc, env, event)
52 VC.make_string(
53 format("&%s %s",
54 self.type_sym,
55 self.meth_contents(
56 loc, env, event
57 ).meth_to_string(
58 loc, env, event
59 ).val
60 )
61 )
62 end
63
64
65 define_instance_method(
66 :meth_is_equal,
67 :'==', [],
68 [VC::Top], VCA::Bool
69 )
70 def meth_is_equal(loc, env, event, other)
71 ASSERT.kind_of other, VC::Top
72
73 VC.make_bool(
74 (
75 other.kind_of?(self.class) &&
76 self.contents.meth_is_equal(
77 loc, env, event, other.contents
78 ).true?
79 )
80 )
81 end
82
83
84 define_instance_method(
85 :meth_is_less_than,
86 :'<', [],
87 [self], VCA::Bool
88 )
89 def meth_is_less_than(loc, env, event, other)
90 ASSERT.kind_of other, VCU::Abstract
91
92 unless other.base_type_sym == self.base_type_sym
93 raise X::TypeError.new(
94 loc,
95 env,
96 "Expected a %s, but %s : %s",
97 self.base_type_sym,
98 other.to_s,
99 other.base_type_sym
100 )
101 end
102
103 VC.make_bool(
104 if self.order_num == other.order_num
105 unless other.contents.kind_of?(self.contents.class)
106 raise X::TypeError.new(
107 loc,
108 env,
109 "Expected a %s, but %s : %s",
110 self.contents.type_sym,
111 other.contents.to_s,
112 other.contents.type_sym
113 )
114 end
115
116 self.contents.meth_is_less_than(
117 loc, env, event, other.contents
118 ).true?
119 else
120 self.order_num < other.order_num
121 end
122 )
123 end
124 end
125 Abstract.freeze
126
127 end # Umu::Value::Core::Union
128
129 end # Umu::Value::Core
130
131 end # Umu::Value
132
133 end # Umu