Go to the first, previous, next, last section, table of contents.

### module

Function names and variables in a library may be encapsulated by module. Let us see an example of using module

    module stack;

    static Sp $
    Sp = 0$
    static Ssize$
    Ssize = 100$
    static Stack $
    Stack = newvect(Ssize)$
    localf push $
    localf pop $

    def push(A) {
      if (Sp >= Ssize) {print("Warning: Stack overflow\nDiscard the top"); pop();}
      Stack[Sp] = A;
      Sp++;
    }
    def pop() {
      local A;
      if (Sp <= 0) {print("Stack underflow"); return 0;}
      Sp--;
      A = Stack[Sp];
      return A;
    }
    endmodule;

    def demo() {
      stack.push(1);
      stack.push(2);
      print(stack.pop());
      print(stack.pop());
    }

Module is encapsulated by the sentences `module` module name and `endmodule`. A variable of a module is declared with the key word `static`. The static variables cannot be refered nor changed out of the module, but it can be refered and changed in any functions in the module. A global variable which can be refered and changed at any place is declared with the key word `extern`.

Any function defined in a module must be declared forward with the keyword `localf`. In the example above, `push` and `pop` are declared. This declaration is necessary.

A function `functionName` defined in a module `moduleName` can be called by the expression `moduleName.functioName(arg1, arg2, ...)` out of the module. Inside the module, `moduleName.` is not necessary. In the example below, the functions `push` and `pop` defined in the module `stack` are called out of the module.

     stack.push(2);
     print( stack.pop() );
     2

Any function name defined in a module is local. In other words, the same function name may be used out of the module to define a different function.

The module structure of asir is introduced to develop large libraries. In order to load libraries on demand, the command `module_definedp` will be useful. The below is an example of demand loading.

    if (!module_definep("stack")) load("stack.rr") $

It is not necessary to declare local variables in asir. As you see in the example of the stack module, we may declare local variables by the key word `local`. Once this key word is used, asir requires to declare all the variables. In order to avoid some troubles to develop a large libraries, it is recommended to use `local` declarations.

When we need to call a function in a module before the module is defined, we must make a prototype declaration as the example below.

    /* Prototype declaration of the module stack */
    module stack;
    localf push $
    localf pop $
    endmodule;

    def demo() {
      print("----------------");
      stack.push(1);
      print(stack.pop());
      print("---------------");
    }

    module stack;
      /* The body of the module stack */
    endmodule;

In order to call functions defined in the top level from the inside of a module, we use `::` as in the example below.

    def afo() {
      S = "afo, afo";
      return S;
    }
    module abc;
    localf foo,afo $

    def foo() {
      G = ::afo();
      return G;
    }
    def afo() {
      return "afo, afo in abc";
    }
    endmodule;
    end$

    [1200] abc.foo();
    afo, afo
    [1201] abc.afo();
    afo, afo in abc

`References`
  section `module_list`, section `module_definedp`, section `remove_module`.

Go to the first, previous, next, last section, table of contents.