#
#  File: modules.k3
#  Maintains the list of grammars and lexica to be compiled
#
#  Copyright 2005 Radboud University of Nijmegen
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Library General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#  CVS ID: "$Id: modules.k3,v 1.12 2005/10/26 12:39:43 marcs Exp $"
#
MODULE modules = GRAMMAR_MODULE_LIST
                 LEXICON_MODULE_LIST.

DEFINES ACTION add unread grammar module (>TEXT),
        ACTION mark grammar module as read (>TEXT),
        TEST try get unread grammar module (TEXT>),
        ACTION add lexicon module (>TEXT),
        ACTION write lexicon modules to lif,
        ACTION log modules.

USES globals, io.

#
# For a module we must maintain its name and whether it has been parsed
#
GRAMMAR_MODULE_LIST = MODULE_LIST.
LEXICON_MODULE_LIST = MODULE_LIST.
MODULE_LIST :: name TEXT parsed BOOL and MODULE_LIST;
               nil.

PRELUDE init modules:
  [ nil -> GRAMMAR_MODULE_LIST ],
    [ nil -> LEXICON_MODULE_LIST ].

#
# Adding grammar and lexicon modules
#
ACTION add unread grammar module (>TEXT):
  add unread module (TEXT, GRAMMAR_MODULE_LIST).

ACTION add lexicon module (>TEXT):
  add unread module (TEXT, LEXICON_MODULE_LIST).

FUNCTION add unread module (>TEXT, >MODULE_LIST>):
  [ MODULE_LIST -> name TEXT1 parsed BOOL1 and MODULE_LIST1 ],
    ( [ TEXT = TEXT1 ];
      add unread module (TEXT, MODULE_LIST1),
       [ name TEXT1 parsed BOOL1 and MODULE_LIST1 -> MODULE_LIST ]
    );
  [ MODULE_LIST -> nil ],
    [ name TEXT parsed false and nil -> MODULE_LIST ];
  ?.

#
# Marking modules
#
ACTION mark grammar module as read (>TEXT):
  mark module as read (TEXT, GRAMMAR_MODULE_LIST).

FUNCTION mark module as read (>TEXT, >MODULE_LIST>):
  [ MODULE_LIST -> name TEXT1 parsed BOOL1 and MODULE_LIST1 ],
    ( [ TEXT = TEXT1],
        ( [ BOOL1 -> false],
            [ name TEXT1 parsed true and MODULE_LIST1 -> MODULE_LIST ];
          ?
        );
      mark module as read (TEXT, MODULE_LIST1),
        [ name TEXT1 parsed BOOL1 and MODULE_LIST1 -> MODULE_LIST ]
    );
  ?.

#
# Locating unread modules
#
TEST try get unread grammar module (TEXT>):
  try get unread grammar module (GRAMMAR_MODULE_LIST, TEXT).

TEST try get unread grammar module (>MODULE_LIST, TEXT>):
  [ MODULE_LIST -> name TEXT parsed BOOL and MODULE_LIST1 ],
    ( [ BOOL -> false ];
      try get unread grammar module (MODULE_LIST1, TEXT)
    ).

#
# Logging
#
ACTION log modules:
  log no nl ("Grammar modules: "), log modules (GRAMMAR_MODULE_LIST, ""), log ("."),
  log no nl ("Lexicon modules: "), log modules (LEXICON_MODULE_LIST, ""), log (".").

ACTION log modules (>MODULE_LIST, >TEXT1):
  [ MODULE_LIST -> name TEXT parsed BOOL and MODULE_LIST1 ],
    log no nl (TEXT1), log no nl (TEXT), log modules (MODULE_LIST1, ", ");
  +.

#
# Write the lexicon names to the lif
#
ACTION write lexicon modules to lif:
  write modules to lif (LEXICON_MODULE_LIST), write separator to lif.

ACTION write modules to lif (>MODULE_LIST):
  [ MODULE_LIST -> name TEXT parsed BOOL and MODULE_LIST1 ],
    write to lif (TEXT + "\n"), write modules to lif (MODULE_LIST1);
  +.
