functional: Added functional lib
[nit.git] / lib / functional / functional_gen.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2019 Louis-Vincent Boudreault <lv.boudreault95@gmail.com>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # This module is only used to generate `functional_types.nit`
18 module functional_gen
19
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]
23 do
24 var args = new Array[String]
25 for i in [0..nargs[ do
26 args.push("A" + i.to_s)
27 end
28 return args
29 end
30
31 class RoutineTemplate
32 var classname: String
33 var nb_generics: Int
34 var supers: Array[String]
35 var has_return: Bool
36 var annotation = "is abstract"
37
38 redef fun to_s
39 do
40 var generics = gen_generics(nb_generics)
41 var params = new Array[String]
42 for g in generics do
43 params.push(g.to_lower + ": " + g)
44 end
45 var signature = ""
46 if params.length > 0 then signature = "({params.join(",")})"
47 if has_return then
48 signature += ": RESULT"
49 generics.push("RESULT")
50 end
51 var classparam = ""
52 if generics.length > 0 then
53 classparam = "[{generics.join(",")}]"
54 end
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}")
61 classdef.add("end")
62 return classdef.join("\n")
63 end
64 end
65
66 # Writes all functional types
67 fun generate_functypes(n: Int, writer: Writer)
68 do
69 writer.write("""
70 # This file is part of NIT ( http://www.nitlanguage.org ).
71 #
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
75 #
76 # http://www.apache.org/licenses/LICENSE-2.0
77 #
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.
83
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
89
90 interface Routine
91 end
92 interface Fun
93 super Routine
94 end
95 interface Proc
96 super Routine
97 end
98 """)
99 var templates = new Array[String]
100 for i in [0..n[ do
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)
105 end
106 writer.write(templates.join("\n"))
107 end
108
109 var fw = new FileWriter.open("functional_types.nit")
110 generate_functypes(20, fw)