functional: Added functional lib
[nit.git] / lib / functional / functional_gen.nit
diff --git a/lib/functional/functional_gen.nit b/lib/functional/functional_gen.nit
new file mode 100644 (file)
index 0000000..2bfa266
--- /dev/null
@@ -0,0 +1,110 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2019 Louis-Vincent Boudreault <lv.boudreault95@gmail.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This module is only used to generate `functional_types.nit`
+module functional_gen
+
+# Generates of an array of formal type as strings.
+# The size of the array equals the arity of the class.
+fun gen_generics(nargs: Int): Array[String]
+do
+        var args = new Array[String]
+        for i in [0..nargs[ do
+                args.push("A" + i.to_s)
+        end
+        return args
+end
+
+class RoutineTemplate
+        var classname: String
+        var nb_generics: Int
+        var supers: Array[String]
+        var has_return: Bool
+        var annotation = "is abstract"
+
+        redef fun to_s
+        do
+                var generics = gen_generics(nb_generics)
+                var params = new Array[String]
+                for g in generics do
+                        params.push(g.to_lower + ": " + g)
+                end
+                var signature = ""
+                if params.length > 0 then signature = "({params.join(",")})"
+                if has_return then
+                        signature += ": RESULT"
+                        generics.push("RESULT")
+                end
+                var classparam = ""
+                if generics.length > 0 then
+                        classparam = "[{generics.join(",")}]"
+                end
+                var superdecls = new Array[String]
+                for s in supers do superdecls.add("\tsuper {s}")
+                var classdef = new Array[String]
+                classdef.add("class {classname}{classparam}")
+                classdef.add("{superdecls.join("\n")}")
+                classdef.add("\tfun call{signature} {annotation}")
+                classdef.add("end")
+                return classdef.join("\n")
+        end
+end
+
+# Writes all functional types
+fun generate_functypes(n: Int, writer: Writer)
+do
+        writer.write("""
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This module provides functional type to represents various function forms.
+# Function types can hold up to 20 arguments. The type `Fun` is for function
+# (input and output) and `Proc` is for procedure (input but no output).
+# This file is automatically generated, do not edit it manually.
+module functional_types
+
+interface Routine
+end
+interface Fun
+        super Routine
+end
+interface Proc
+        super Routine
+end
+""")
+        var templates = new Array[String]
+        for i in [0..n[ do
+                var t1 = new RoutineTemplate("Fun{i}", i, ["Fun"], true)
+                var t2 = new RoutineTemplate("Proc{i}", i, ["Proc"], false)
+                templates.push(t1.to_s)
+                templates.push(t2.to_s)
+        end
+        writer.write(templates.join("\n"))
+end
+
+var fw = new FileWriter.open("functional_types.nit")
+generate_functypes(20, fw)