1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2019 Louis-Vincent Boudreault <lv.boudreault95@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # This module is only used to generate `functional_types.nit`
20 # Generates of an array of formal type as strings.
21 # The size of the array equals the arity of the class.
22 fun gen_generics
(nargs
: Int): Array[String]
24 var args
= new Array[String]
25 for i
in [0..nargs
[ do
26 args
.push
("A" + i
.to_s
)
34 var supers
: Array[String]
36 var annotation
= "is abstract"
40 var generics
= gen_generics
(nb_generics
)
41 var params
= new Array[String]
43 params
.push
(g
.to_lower
+ ": " + g
)
46 if params
.length
> 0 then signature
= "({params.join(",")})"
48 signature
+= ": RESULT"
49 generics
.push
("RESULT")
52 if generics
.length
> 0 then
53 classparam
= "[{generics.join(",")}]"
55 var superdecls
= new Array[String]
56 for s
in supers
do superdecls
.add
("\tsuper {s}")
57 var classdef
= new Array[String]
58 classdef
.add
("class {classname}{classparam}")
59 classdef
.add
("{superdecls.join("\n")}")
60 classdef
.add
("\tfun call{signature} {annotation}")
62 return classdef
.join
("\n")
66 # Writes all functional types
67 fun generate_functypes
(n
: Int, writer
: Writer)
70 # This file is part of NIT ( http://www.nitlanguage.org ).
72 # Licensed under the Apache License, Version 2.0 (the "License");
73 # you may not use this file except in compliance with the License.
74 # You may obtain a copy of the License at
76 # http://www.apache.org/licenses/LICENSE-2.0
78 # Unless required by applicable law or agreed to in writing, software
79 # distributed under the License is distributed on an "AS IS" BASIS,
80 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81 # See the License for the specific language governing permissions and
82 # limitations under the License.
84 # This module provides functional type to represents various function forms.
85 # Function types can hold up to 20 arguments. The type `Fun` is for function
86 # (input and output) and `Proc` is for procedure (input but no output).
87 # This file is automatically generated, do not edit it manually.
88 module functional_types
99 var templates
= new Array[String]
101 var t1
= new RoutineTemplate("Fun{i}", i
, ["Fun"], true)
102 var t2
= new RoutineTemplate("Proc{i}", i
, ["Proc"], false)
103 templates
.push
(t1
.to_s
)
104 templates
.push
(t2
.to_s
)
106 writer
.write
(templates
.join
("\n"))
109 var fw
= new FileWriter.open
("functional_types.nit")
110 generate_functypes
(20, fw
)