typed.rb 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +26 -0
  3. data/bin/typed.rb +110 -0
  4. data/lib/typed/language.rb +131 -0
  5. data/lib/typed/model/tm_abs.rb +104 -0
  6. data/lib/typed/model/tm_array_literal.rb +25 -0
  7. data/lib/typed/model/tm_boolean.rb +15 -0
  8. data/lib/typed/model/tm_boolean_operator.rb +34 -0
  9. data/lib/typed/model/tm_break.rb +24 -0
  10. data/lib/typed/model/tm_case_when.rb +38 -0
  11. data/lib/typed/model/tm_class.rb +63 -0
  12. data/lib/typed/model/tm_const.rb +29 -0
  13. data/lib/typed/model/tm_defined.rb +19 -0
  14. data/lib/typed/model/tm_error.rb +16 -0
  15. data/lib/typed/model/tm_float.rb +15 -0
  16. data/lib/typed/model/tm_for.rb +42 -0
  17. data/lib/typed/model/tm_fun.rb +165 -0
  18. data/lib/typed/model/tm_global_var.rb +22 -0
  19. data/lib/typed/model/tm_global_var_assignment.rb +20 -0
  20. data/lib/typed/model/tm_hash_literal.rb +32 -0
  21. data/lib/typed/model/tm_if_else.rb +24 -0
  22. data/lib/typed/model/tm_instance_var.rb +23 -0
  23. data/lib/typed/model/tm_instance_var_assignment.rb +32 -0
  24. data/lib/typed/model/tm_int.rb +15 -0
  25. data/lib/typed/model/tm_local_var_asgn.rb +35 -0
  26. data/lib/typed/model/tm_mass_asgn.rb +60 -0
  27. data/lib/typed/model/tm_mlhs.rb +87 -0
  28. data/lib/typed/model/tm_module.rb +51 -0
  29. data/lib/typed/model/tm_next.rb +24 -0
  30. data/lib/typed/model/tm_nil.rb +14 -0
  31. data/lib/typed/model/tm_range_literal.rb +30 -0
  32. data/lib/typed/model/tm_regexp.rb +27 -0
  33. data/lib/typed/model/tm_rescue.rb +27 -0
  34. data/lib/typed/model/tm_return.rb +24 -0
  35. data/lib/typed/model/tm_s_class.rb +30 -0
  36. data/lib/typed/model/tm_self.rb +22 -0
  37. data/lib/typed/model/tm_send.rb +300 -0
  38. data/lib/typed/model/tm_sequencing.rb +53 -0
  39. data/lib/typed/model/tm_string.rb +15 -0
  40. data/lib/typed/model/tm_string_interpolation.rb +21 -0
  41. data/lib/typed/model/tm_super.rb +27 -0
  42. data/lib/typed/model/tm_symbol.rb +15 -0
  43. data/lib/typed/model/tm_symbol_interpolation.rb +21 -0
  44. data/lib/typed/model/tm_try.rb +29 -0
  45. data/lib/typed/model/tm_var.rb +28 -0
  46. data/lib/typed/model/tm_while.rb +43 -0
  47. data/lib/typed/model.rb +48 -0
  48. data/lib/typed/prelude.rb +939 -0
  49. data/lib/typed/prelude_existential_registry.bin +0 -0
  50. data/lib/typed/prelude_generic_registry.bin +0 -0
  51. data/lib/typed/prelude_registry.bin +0 -0
  52. data/lib/typed/runtime/ast_parser.rb +589 -0
  53. data/lib/typed/runtime/method_signature_processor.rb +72 -0
  54. data/lib/typed/runtime/normalization/validations.rb +47 -0
  55. data/lib/typed/runtime/normalization.rb +196 -0
  56. data/lib/typed/runtime/parser_context.rb +36 -0
  57. data/lib/typed/runtime/type_parser.rb +215 -0
  58. data/lib/typed/runtime/type_registry.rb +170 -0
  59. data/lib/typed/runtime/type_signature_processor.rb +34 -0
  60. data/lib/typed/runtime.rb +33 -0
  61. data/lib/typed/type_signature/parser.rb +240 -0
  62. data/lib/typed/types/polymorphism/existential_type_variable.rb +13 -0
  63. data/lib/typed/types/polymorphism/generic_comparisons.rb +134 -0
  64. data/lib/typed/types/polymorphism/generic_variables.rb +24 -0
  65. data/lib/typed/types/polymorphism/type_variable.rb +138 -0
  66. data/lib/typed/types/polymorphism/type_variable_register.rb +298 -0
  67. data/lib/typed/types/polymorphism/unification.rb +579 -0
  68. data/lib/typed/types/ty_boolean.rb +15 -0
  69. data/lib/typed/types/ty_dynamic.rb +39 -0
  70. data/lib/typed/types/ty_either.rb +168 -0
  71. data/lib/typed/types/ty_error.rb +18 -0
  72. data/lib/typed/types/ty_existential_type.rb +22 -0
  73. data/lib/typed/types/ty_function.rb +144 -0
  74. data/lib/typed/types/ty_generic_function.rb +115 -0
  75. data/lib/typed/types/ty_generic_object.rb +180 -0
  76. data/lib/typed/types/ty_generic_singleton_object.rb +238 -0
  77. data/lib/typed/types/ty_object.rb +256 -0
  78. data/lib/typed/types/ty_singleton_object.rb +78 -0
  79. data/lib/typed/types/ty_stack_jump.rb +44 -0
  80. data/lib/typed/types/ty_top_level_object.rb +38 -0
  81. data/lib/typed/types.rb +60 -0
  82. data/lib/typed/typing_context.rb +206 -0
  83. data/lib/typed/version.rb +3 -0
  84. data/lib/typed.rb +161 -0
  85. data/spec/lib/ast_parser_spec.rb +101 -0
  86. data/spec/lib/examples/animals.rb +44 -0
  87. data/spec/lib/examples/counter.rb +16 -0
  88. data/spec/lib/examples/if.rb +31 -0
  89. data/spec/lib/language_spec.rb +36 -0
  90. data/spec/lib/model/tm_abs_spec.rb +66 -0
  91. data/spec/lib/model/tm_array_literal_spec.rb +36 -0
  92. data/spec/lib/model/tm_case_when_spec.rb +39 -0
  93. data/spec/lib/model/tm_class_spec.rb +67 -0
  94. data/spec/lib/model/tm_defined_spec.rb +10 -0
  95. data/spec/lib/model/tm_for_spec.rb +150 -0
  96. data/spec/lib/model/tm_fun_spec.rb +11 -0
  97. data/spec/lib/model/tm_hash_literal_spec.rb +40 -0
  98. data/spec/lib/model/tm_mass_asgn_spec.rb +104 -0
  99. data/spec/lib/model/tm_module_spec.rb +42 -0
  100. data/spec/lib/model/tm_regexp_spec.rb +9 -0
  101. data/spec/lib/model/tm_return_spec.rb +47 -0
  102. data/spec/lib/model/tm_s_class_spec.rb +27 -0
  103. data/spec/lib/model/tm_self_spec.rb +19 -0
  104. data/spec/lib/model/tm_string_interpolation_spec.rb +10 -0
  105. data/spec/lib/model/tm_symbol_interpolation_spec.rb +10 -0
  106. data/spec/lib/model/tm_symbol_spec.rb +9 -0
  107. data/spec/lib/model/tm_while_spec.rb +141 -0
  108. data/spec/lib/polymorphism/type_variable_spec.rb +14 -0
  109. data/spec/lib/polymorphism/unification_spec.rb +328 -0
  110. data/spec/lib/prelude/array_spec.rb +263 -0
  111. data/spec/lib/prelude/class_spec.rb +12 -0
  112. data/spec/lib/prelude/enumerable_spec.rb +278 -0
  113. data/spec/lib/prelude/enumerator_spec.rb +101 -0
  114. data/spec/lib/prelude/hash_spec.rb +361 -0
  115. data/spec/lib/prelude/kernel_spec.rb +23 -0
  116. data/spec/lib/prelude/object_spec.rb +22 -0
  117. data/spec/lib/prelude/pair_spec.rb +16 -0
  118. data/spec/lib/prelude/showable_spec.rb +31 -0
  119. data/spec/lib/prelude/string_spec.rb +98 -0
  120. data/spec/lib/runtime/normalization_spec.rb +29 -0
  121. data/spec/lib/runtime/validations_spec.rb +56 -0
  122. data/spec/lib/runtime_spec.rb +503 -0
  123. data/spec/lib/type_signature/parser_spec.rb +239 -0
  124. data/spec/lib/types/comparisons_spec.rb +35 -0
  125. data/spec/lib/types/polymorphism/generic_comparisons_spec.rb +492 -0
  126. data/spec/lib/types/polymorphism/type_variable_register_spec.rb +128 -0
  127. data/spec/lib/types/ty_dynamic_spec.rb +103 -0
  128. data/spec/lib/types/ty_either_spec.rb +288 -0
  129. data/spec/lib/types/ty_error_spec.rb +18 -0
  130. data/spec/lib/types/ty_generic_object_spec.rb +78 -0
  131. data/spec/lib/types/ty_generic_singleton_object_spec.rb +288 -0
  132. data/spec/lib/types/typing_context_spec.rb +86 -0
  133. data/spec/lib/types_spec.rb +174 -0
  134. data/spec/lib/typing/boolean_asgn_spec.rb +134 -0
  135. data/spec/lib/typing/break_spec.rb +79 -0
  136. data/spec/lib/typing/generics_spec.rb +191 -0
  137. data/spec/lib/typing/instance_vars_spec.rb +103 -0
  138. data/spec/lib/typing/next_spec.rb +29 -0
  139. data/spec/lib/typing/op_asgn_spec.rb +104 -0
  140. data/spec/lib/typing/overriden_methods_spec.rb +31 -0
  141. data/spec/lib/typing/subtyping_spec.rb +112 -0
  142. data/spec/lib/typing/tm_boolean_operator_spec.rb +100 -0
  143. data/spec/lib/typing/tm_boolean_spec.rb +61 -0
  144. data/spec/lib/typing/tm_const_spec.rb +28 -0
  145. data/spec/lib/typing/tm_defined_spec.rb +12 -0
  146. data/spec/lib/typing/tm_fun_spec.rb +347 -0
  147. data/spec/lib/typing/tm_global_var_spec.rb +33 -0
  148. data/spec/lib/typing/tm_if_else_spec.rb +104 -0
  149. data/spec/lib/typing/tm_ignore_spec.rb +24 -0
  150. data/spec/lib/typing/tm_instance_vars_spec.rb +117 -0
  151. data/spec/lib/typing/tm_local_var_asgn_spec.rb +134 -0
  152. data/spec/lib/typing/tm_mlhs_spec.rb +164 -0
  153. data/spec/lib/typing/tm_module_spec.rb +89 -0
  154. data/spec/lib/typing/tm_raise_spec.rb +31 -0
  155. data/spec/lib/typing/tm_range_literal_spec.rb +25 -0
  156. data/spec/lib/typing/tm_regexp_spec.rb +14 -0
  157. data/spec/lib/typing/tm_return_spec.rb +45 -0
  158. data/spec/lib/typing/tm_send_casting_spec.rb +26 -0
  159. data/spec/lib/typing/tm_send_class_methods_spec.rb +42 -0
  160. data/spec/lib/typing/tm_send_generic_apply_spec.rb +103 -0
  161. data/spec/lib/typing/tm_send_generic_methods_spec.rb +77 -0
  162. data/spec/lib/typing/tm_send_initialize_spec.rb +68 -0
  163. data/spec/lib/typing/tm_send_lambda_spec.rb +135 -0
  164. data/spec/lib/typing/tm_send_spec.rb +217 -0
  165. data/spec/lib/typing/tm_send_yield_block_spec.rb +308 -0
  166. data/spec/lib/typing/tm_sequencing_spec.rb +174 -0
  167. data/spec/lib/typing/tm_string_interpolation_spec.rb +19 -0
  168. data/spec/lib/typing/tm_super_spec.rb +63 -0
  169. data/spec/lib/typing/tm_symbol_interpolation_spec.rb +19 -0
  170. data/spec/lib/typing/tm_symbol_spec.rb +14 -0
  171. data/spec/lib/typing/tm_try_spec.rb +73 -0
  172. data/spec/spec_helper.rb +140 -0
  173. metadata +216 -0
@@ -0,0 +1,239 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe TypedRb::TypeSignature::Parser do
4
+
5
+ it 'parses a unit type' do
6
+ result = described_class.parse('unit')
7
+ expect(result).to eq(:unit)
8
+ end
9
+
10
+ it 'parses an atomic type' do
11
+ result = described_class.parse('Bool')
12
+ expect(result).to eq('Bool')
13
+ end
14
+
15
+ it 'parses a generic type' do
16
+ result = described_class.parse('Array[Bool]')
17
+ expect(result).to eq({:type => 'Array',
18
+ :parameters => [{:type => 'Bool', :kind => :type_var}],
19
+ :kind => :generic_type})
20
+ end
21
+
22
+ it 'parses a nested generic type' do
23
+ result = described_class.parse('Array[Array[Integer]]')
24
+ expect(result).to eq({:type=>'Array',
25
+ :parameters => [
26
+ {:type=>'Array',
27
+ :parameters => [{
28
+ :type => 'Integer',
29
+ :kind => :type_var
30
+ }],
31
+ :kind => :generic_type }
32
+ ],
33
+ :kind => :generic_type})
34
+ end
35
+
36
+ it 'parses a nested generic type with multiple type arguments' do
37
+ result = described_class.parse('Array[Hash[Symbol][String]]')
38
+ expect(result).to eq({:type=>'Array',
39
+ :parameters => [
40
+ {:type=>'Hash',
41
+ :parameters => [{
42
+ :type => 'Symbol',
43
+ :kind => :type_var
44
+ }, {
45
+ :type => 'String',
46
+ :kind => :type_var
47
+ }],
48
+ :kind => :generic_type }
49
+ ],
50
+ :kind => :generic_type})
51
+ end
52
+
53
+ it 'parses an atomic rest type' do
54
+ result = described_class.parse('Bool...')
55
+ expect(result).to eq({:type => 'Array',
56
+ :parameters => ['Bool'],
57
+ :kind => :rest})
58
+ end
59
+
60
+ it 'parses a type var rest type' do
61
+ result = described_class.parse('[T]...')
62
+ expect(result).to eq({:type => 'Array',
63
+ :parameters => [{:type=>"T", :kind => :type_var}],
64
+ :kind => :rest})
65
+
66
+ end
67
+
68
+ it 'parses a function type' do
69
+ result = described_class.parse('Bool -> Int')
70
+ expect(result).to eq(['Bool', 'Int'])
71
+ end
72
+
73
+ it 'parses applied type parameters in signatures' do
74
+ result = described_class.parse('Bool... -> Array[Bool]')
75
+ expect(result[0]).to eq({:type => 'Array',
76
+ :parameters => ['Bool'],
77
+ :kind => :rest})
78
+ expect(result[1]).to eq({:type => 'Array',
79
+ :parameters => [{:type=>"Bool", :kind=>:type_var}],
80
+ :kind => :generic_type})
81
+ end
82
+
83
+ it 'parses applied type parameters in signatures' do
84
+ result = described_class.parse('Bool... -> Array[T < Bool]')
85
+ expect(result[0]).to eq({:type => 'Array',
86
+ :parameters => ['Bool'],
87
+ :kind => :rest})
88
+ expect(result[1]).to eq({:type => 'Array',
89
+ :parameters => [{:type =>"T", :kind =>:type_var, :bound => 'Bool', :binding => '<'}],
90
+ :kind => :generic_type})
91
+ end
92
+
93
+ it 'parses applied type parameters in signatures' do
94
+ result = described_class.parse('Bool... -> Array[T > Bool]')
95
+ expect(result[0]).to eq({:type => 'Array',
96
+ :parameters => ['Bool'],
97
+ :kind => :rest})
98
+ expect(result[1]).to eq({:type => 'Array',
99
+ :parameters => [{:type =>"T", :kind =>:type_var, :bound => 'Bool', :binding => '>'}],
100
+ :kind => :generic_type})
101
+ end
102
+
103
+ it 'parses a complex type' do
104
+ result = described_class.parse('Bool -> Int -> Bool')
105
+ expect(result).to eq(['Bool', 'Int', 'Bool'])
106
+ end
107
+
108
+ it 'parses a complex type using unit' do
109
+ result = described_class.parse('Bool -> Int -> unit')
110
+ expect(result).to eq(['Bool', 'Int', :unit])
111
+ end
112
+
113
+ it 'parses a types with parentheses' do
114
+ result = described_class.parse('(Bool -> Int) -> Bool')
115
+ expect(result).to eq([['Bool', 'Int'], 'Bool'])
116
+ end
117
+
118
+ it 'parses a types with parentheses in the return type' do
119
+ result = described_class.parse('Bool -> (Int -> Bool)')
120
+ expect(result).to eq(['Bool', ['Int', 'Bool']])
121
+ end
122
+
123
+ it 'parses a types with parentheses in the complex return type' do
124
+ result = described_class.parse('Bool -> (Int -> (Bool -> Int))')
125
+ expect(result).to eq(['Bool', ['Int', ['Bool', 'Int']]])
126
+ end
127
+
128
+ it 'parses a types with complex parentheses' do
129
+ result = described_class.parse('(Bool -> Bool) -> (Bool -> Int)')
130
+ expect(result).to eq([['Bool', 'Bool'], ['Bool', 'Int']])
131
+ end
132
+
133
+ it 'parses a types with complex parentheses' do
134
+ result = described_class.parse('(Bool -> Bool) -> (Bool -> Int) -> (Int -> Int)')
135
+ expect(result).to eq([['Bool', 'Bool'], ['Bool', 'Int'], ['Int', 'Int']])
136
+ end
137
+
138
+ it 'parses a types with complex compound parentheses' do
139
+ result = described_class.parse('((Bool -> Bool) -> (Bool -> Int)) -> (Bool -> Int)')
140
+ expect(result).to eq([[['Bool','Bool'], ['Bool', 'Int']], ['Bool', 'Int']])
141
+ end
142
+
143
+ it 'parses unbalanced type expressions' do
144
+ result = described_class.parse('Bool -> Int -> (Bool -> Int) -> Int')
145
+ expect(result).to eq(['Bool','Int', ['Bool','Int'], 'Int'])
146
+ end
147
+
148
+ it 'parses unbalanced type expressions with just return types' do
149
+ result = described_class.parse('Bool -> Int -> (-> Int) -> Int')
150
+ expect(result).to eq(['Bool','Int', ['Int'], 'Int'])
151
+ end
152
+
153
+ it 'parses expressions with only return type' do
154
+ result = described_class.parse(' -> Int')
155
+ expect(result).to eq(['Int'])
156
+ end
157
+
158
+ it 'parses type variables' do
159
+ result = described_class.parse('[X]')
160
+ expect(result).to eq({:type => 'X', :kind => :type_var })
161
+ end
162
+
163
+ it 'parses type variables with lower binding' do
164
+ result = described_class.parse('[X < Numeric]')
165
+ expect(result).to eq({:type => 'X', :kind => :type_var, :bound => 'Numeric', :binding => '<' })
166
+ end
167
+
168
+ it 'parses type variables with lower binding' do
169
+ result = described_class.parse('[X > Numeric]')
170
+ expect(result).to eq({:type => 'X', :kind => :type_var, :bound => 'Numeric', :binding => '>' })
171
+ end
172
+
173
+ it 'parses return type variables' do
174
+ result = described_class.parse(' -> [X]')
175
+ expect(result).to eq([{:type => 'X', :kind => :type_var }])
176
+ end
177
+
178
+ it 'parses type variables in both sides' do
179
+ result = described_class.parse('[X<String] -> [Y]')
180
+ expect(result).to eq([{:type => 'X', :bound => 'String', :kind => :type_var, :binding => '<' },
181
+ {:type => 'Y', :kind => :type_var }])
182
+ end
183
+
184
+ it 'parses type variables in complex expressions' do
185
+ result = described_class.parse('[X] -> ([Y] -> Integer)')
186
+ expect(result).to eq([{:type => 'X', :kind => :type_var },
187
+ [{:type => 'Y', :kind => :type_var },
188
+ 'Integer']])
189
+ end
190
+
191
+ it 'parses a block' do
192
+ result = described_class.parse('Int -> unit -> &(String -> Integer)')
193
+ expect(result).to eq(['Int', :unit, {:block => ['String', 'Integer'], :kind => :block_arg}])
194
+ end
195
+
196
+ it 'parses parametric types' do
197
+ result = described_class.parse('Array[X] -> [X]')
198
+ expect(result).to eq([{:type => "Array",
199
+ :parameters => [{:type => "X", :kind => :type_var}],
200
+ :kind => :generic_type},
201
+ {:type => "X",
202
+ :kind => :type_var}])
203
+ end
204
+
205
+ it 'parses parametric types with multiple var types' do
206
+ result = described_class.parse('Int -> (Bool -> Array[X][Y][Z])')
207
+ expect(result).to eq(["Int", ["Bool", {:type => "Array",
208
+ :parameters => [{:type => "X", :kind => :type_var},
209
+ {:type => "Y", :kind => :type_var},
210
+ {:type => "Z", :kind => :type_var}],
211
+ :kind => :generic_type}]])
212
+ end
213
+
214
+ it 'parses parametric types with bounds' do
215
+ result = described_class.parse('Array[X<Int] -> Hash[T<String][U<Object]')
216
+ expect(result).to eq([{:type => "Array",
217
+ :parameters => [{:type => "X", :bound => "Int", :binding => '<', :kind => :type_var}],
218
+ :kind => :generic_type},
219
+ {:type => "Hash",
220
+ :parameters => [{:type => "T", :bound => "String", :binding => '<', :kind => :type_var},
221
+ {:type => "U", :bound => "Object", :binding => '<', :kind => :type_var}],
222
+ :kind => :generic_type}])
223
+ end
224
+
225
+ it 'parses parametric rest arguments' do
226
+ result = described_class.parse('Array[X]... -> String')
227
+ expect(result).to eq([{:kind=>:rest,
228
+ :type=>"Array",
229
+ :parameters=>[{:type=>"Array",
230
+ :parameters=>[{:type=>"X", :kind=>:type_var}],
231
+ :kind=>:generic_type}]},
232
+ "String"])
233
+ end
234
+
235
+ it 'parses multiple type variables in sequence' do
236
+ result = described_class.parse('[X][Y][Z]')
237
+ expect(result).to eq([{:type=>"X", :kind=>:type_var}, {:type=>"Y", :kind=>:type_var}, {:type=>"Z", :kind=>:type_var}])
238
+ end
239
+ end
@@ -0,0 +1,35 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe TypedRb::Types::TyObject do
4
+ context 'comparisons' do
5
+ it 'can compare tyobjects correctly' do
6
+ expect(described_class.new(Integer) < described_class.new(Numeric)).to eq(true)
7
+ expect(described_class.new(Integer) <= described_class.new(Numeric)).to eq(true)
8
+ expect(described_class.new(Integer) == described_class.new(Integer)).to eq(true)
9
+ expect(described_class.new(Numeric) == described_class.new(Numeric)).to eq(true)
10
+ expect(described_class.new(Integer) != described_class.new(Numeric)).to eq(true)
11
+ expect(described_class.new(Numeric) != described_class.new(Integer)).to eq(true)
12
+ expect(described_class.new(Numeric) > described_class.new(Integer)).to eq(true)
13
+ expect(described_class.new(Numeric) >= described_class.new(Integer)).to eq(true)
14
+ end
15
+
16
+ it 'can compare tyobjects non ordered' do
17
+ expect {
18
+ described_class.new(Integer) < described_class.new(String)
19
+ }.to raise_error(ArgumentError)
20
+ expect {
21
+ described_class.new(Integer) <= described_class.new(String)
22
+ }.to raise_error(ArgumentError)
23
+
24
+ expect(described_class.new(Integer) != described_class.new(String)).to eq(true)
25
+ expect(described_class.new(String) != described_class.new(Integer)).to eq(true)
26
+
27
+ expect {
28
+ described_class.new(Integer) > described_class.new(String)
29
+ }.to raise_error(ArgumentError)
30
+ expect {
31
+ described_class.new(Integer) >= described_class.new(String)
32
+ }.to raise_error(ArgumentError)
33
+ end
34
+ end
35
+ end