This module is only used to generate functional_types.nit

Introduced classes

Redefined classes

redef class Sys

functional :: functional_gen $ Sys

The main class of the program.

All class definitions

redef class Sys

functional :: functional_gen $ Sys

The main class of the program.
package_diagram functional::functional_gen functional_gen core core functional::functional_gen->core a_star-m a_star-m a_star-m->functional::functional_gen

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.

Children

module a_star-m

a_star-m

# 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 classkind: String
        var classname: String
        var nb_generics: Int
        var supers: Array[String]
        var has_return: Bool
        var is_redef = false
        var annotation = "is abstract"

        fun callparams: Array[String]
        do
                var generics = gen_generics(nb_generics)
                var params = new Array[String]
                for g in generics do
                        if is_redef then
                                params.push(g.to_lower)
                        else
                                params.push(g.to_lower + ": " + g)
                        end
                end
                return params
        end

        fun classparams: Array[String]
        do
                var res = gen_generics(nb_generics)
                if has_return then res.add("RESULT")
                return res
        end

        redef fun to_s
        do
                var signature = ""
                var callparams = self.callparams
                var classparams = self.classparams

                if callparams.length > 0 then signature = "({callparams.join(",")})"
                if has_return then signature += ": RESULT"
                var classdecl = "{classkind} {classname}"
                if classparams.length > 0 then classdecl += "[{classparams.join(",")}]"
                var superdecls = new Array[String]
                for s in supers do superdecls.add("\tsuper {s}")

                var classdef = new Array[String]
                var redefkw = ""
                if is_redef then redefkw = "redef "
                classdef.add("{classdecl}")
                classdef.add("{superdecls.join("\n")}")
                classdef.add("\t{redefkw}fun 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]
        var templates2 = new Array[String]
        for i in [0..n[ do
                var t1 = new RoutineTemplate("interface", "Fun{i}", i, ["Fun"], true)
                var t2 = new RoutineTemplate("interface", "Proc{i}", i, ["Proc"], false)
                templates.push(t1.to_s)
                templates.push(t2.to_s)

                # We want routine ref to be at the end of the file
                var t3 = new RoutineTemplate("universal", "FunRef{i}", i, ["Fun{i}{t1.classparams}"], true)
                var procsuper = "Proc{i}"
                if i > 0 then procsuper = "Proc{i}{t2.classparams}"
                var t4 = new RoutineTemplate("universal", "ProcRef{i}", i, [procsuper], false)
                t3.annotation = "is intern"
                t3.is_redef = true
                t4.annotation = "is intern"
                t4.is_redef = true
                templates2.push(t3.to_s)
                templates2.push(t4.to_s)
        end
        templates.add_all(templates2)
        templates.add(
"""
universal RoutineRef
end
""")
        writer.write(templates.join("\n"))
end

var fw = new FileWriter.open("functional_types.nit")
generate_functypes(20, fw)
lib/functional/functional_gen.nit:17,1--148,26