#
#  File: globals.k3
#  Maintains the set of global variables, collected by parsing options
#  and pragmats or set by the parser.
#
#  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: globals.k3,v 1.5 2006/08/09 11:13:09 marcs Exp $"
#
MODULE globals = TIME_OPTION
                 DIRECTORS_OPTION
                 FACTORIZE_OPTION
                 LOG_OPTION
                 WARNINGS_OPTION
                 NO_ANALYSIS_OPTION
                 ANALYSIS_OPTION
                 VERBOSE_OPTION
                 COUNTERS_OPTION
                 NEGATIVE_MEMO_OPTION
		 POSITIVE_MEMO_OPTION
                 TRACE_OPTION
                 PROFILE_OPTION
                 DEBUG_OPTION
                 LEXICON_USED
                 UNDERSCORE_OPTION
                 DETAILS_OPTION
        	 VISIBLE_OPTION
	         FREQ_OPTION
                 INCLUDE_PATH
#                BITS_LENGTH

                 LABRACKET_OPTION
                 NO_OUTPUT_OPTION
                 TRANSDUCE_OPTION
                 MAX_TIME_OPTION
                 MAX_PEN_OPTION
                 TRANSLATE_OPTION
                 WORD_TERM_OPTION
                 INV_CHARS_OPTION
                 LIST_DELIM_OPTION
                 GRAPH_OPTION
                 VERBOSE_PARSER_OPTION
                 CLIENT_OPTION
		 TOKEN_FILE_OPTION
                 SEGMENT_MODE_OPTION
                 GENERATIVE_GRAMMAR_OPTION

                 LEXICON_FILE_EXT
                 GRAMMAR_FILE_EXT
		 FN_BASE.

DEFINES	BOOL,
	PRAGMATS,
	FN_BASE,

	FUNCTION or (>BOOL1>, >BOOL2),
	FUNCTION and not (>BOOL1>, >BOOL2),
        FUNCTION convert >BOOL to TEXT>,
        FUNCTION convert >PRAGMATS to TEXT>,
        FUNCTION empty (PRAGMATS>),
        FUNCTION current pragmats (PRAGMATS>),
        FUNCTION add (>PRAGMATS1>, >PRAGMATS2),
        TEST if warnings (>PRAGMATS),
        TEST if profile (>PRAGMATS),
        TEST if details (>PRAGMATS),

	ACTION set basename (>TEXT),
	FUNCTION get basename (TEXT>),
        FUNCTION get code file (TEXT>),
        FUNCTION get log file (TEXT>),
        FUNCTION get prof file (TEXT>),
	FUNCTION get lexicon interface file name (TEXT>),
        FUNCTION get grammar file extension (TEXT>),
        FUNCTION get lexicon file extension (TEXT>),

        ACTION set lexicon used,
        ACTION set counters option,
        ACTION set profile option,
        ACTION reset profile option,
        ACTION increase verbose option,
        ACTION set warnings option,
        ACTION reset warnings option,
        ACTION set details option,
        ACTION reset details option,
	ACTION set log option,
	ACTION set directors option,
	ACTION set factorize option,
	ACTION set negative memo option,
	ACTION set positive memo option,
	ACTION set trace option,
	ACTION set debug option,
	ACTION set no analysis option,
        ACTION set full analysis option,
        ACTION set labelled bracket option,
        ACTION set suppress output option,
	ACTION set time option,
	ACTION set underscore option,
        ACTION set transduce option,
        ACTION set translate option off,
        ACTION set translate option default,
        ACTION set translate option (>TEXT),
        ACTION set word terminators option (>TEXT),
        ACTION set invisible characters option (>TEXT),
        ACTION set graph option,
        ACTION set verbose parser option (>INT),
        ACTION set client option,
	ACTION set token file option (>TEXT),
        ACTION set frequence option,
        ACTION set segment mode option,
        ACTION set generative grammar option,

        TEST if warnings option,
        TEST if no analysis option,
        TEST if full analysis option,
        TEST if verbose option,
        TEST if verbose option (INT>),
        TEST if very verbose option,
        TEST if time option,
        TEST if log option,
        TEST if counters option,
        TEST if directors option,
        TEST if factorize option,
        TEST if negative memo option,
	TEST if positive memo option,
        TEST if trace option,
        TEST if profile option,
        TEST if debug option,
        TEST if lexicon used,
        TEST if underscore option,
        TEST if frequence option,

	ACTION set include path (>TEXT),
        FUNCTION get include path (TEXT>),

        TEST if labelled bracket option,
        TEST if suppress output option,
        TEST if transduce option,
        TEST if translate option (TEXT>),
        TEST if word terminators option (TEXT>),
        TEST if invisible characters option (TEXT>),
        TEST if graph option,
        TEST if verbose parser option (INT>),
        TEST if client option,
	TEST if token file option (TEXT>),
        TEST if segment mode option,
        TEST if generative grammar option,

        FUNCTION get agfl version (TEXT>),
        FUNCTION get grammar version (INT>).

USES io.

OPTION :: true;
          true with INT;
          true with text TEXT;
          false.

WARNINGS_OPTION,
NO_ANALYSIS_OPTION,
ANALYSIS_OPTION,
VERBOSE_OPTION,
TIME_OPTION,
DIRECTORS_OPTION,
FACTORIZE_OPTION,
LOG_OPTION,
NEGATIVE_MEMO_OPTION,
POSITIVE_MEMO_OPTION,
TRACE_OPTION,
PROFILE_OPTION,
DEBUG_OPTION,
COUNTERS_OPTION,
LEXICON_USED,
UNDERSCORE_OPTION,
DETAILS_OPTION,
FREQ_OPTION,
VISIBLE_OPTION = OPTION.

# BITS_LENGTH = INT.

INCLUDE_PATH = TEXT.

LABRACKET_OPTION,
NO_OUTPUT_OPTION,
TRANSDUCE_OPTION,
TRANSLATE_OPTION,
MAX_TIME_OPTION,
MAX_PEN_OPTION,
WORD_TERM_OPTION,
INV_CHARS_OPTION,
LIST_DELIM_OPTION,
GRAPH_OPTION,
VERBOSE_PARSER_OPTION,
CLIENT_OPTION, 
TOKEN_FILE_OPTION,
SEGMENT_MODE_OPTION,
GENERATIVE_GRAMMAR_OPTION = OPTION.

LEXICON_FILE_EXT,
GRAMMAR_FILE_EXT,
FN_BASE = TEXT.

PRELUDE init options:
    [ ".lex" -> LEXICON_FILE_EXT ],
    [ ".gra" -> GRAMMAR_FILE_EXT ],
    [ "" -> FN_BASE ],
    [ false -> WARNINGS_OPTION ],
    [ true -> VISIBLE_OPTION ],
    [ false -> NO_ANALYSIS_OPTION ],
    [ false -> ANALYSIS_OPTION ],
    [ false -> VERBOSE_OPTION ],
    [ false -> LOG_OPTION ],
    [ false -> COUNTERS_OPTION ],
    [ false -> DIRECTORS_OPTION ],
    [ false -> FACTORIZE_OPTION ],
    [ false -> TIME_OPTION ],
    [ false -> NEGATIVE_MEMO_OPTION ],
    [ false -> POSITIVE_MEMO_OPTION ],
    [ false -> TRACE_OPTION ],
    [ false -> PROFILE_OPTION ],
    [ false -> DEBUG_OPTION ],
    [ false -> LEXICON_USED ],
    [ false -> DETAILS_OPTION ],
    [ false -> UNDERSCORE_OPTION ],
    [ "/usr/local/include" -> INCLUDE_PATH ],
    [ false -> FREQ_OPTION ],
#    [ 32 -> BITS_LENGTH ],

    [ false -> LABRACKET_OPTION ],
    [ false -> NO_OUTPUT_OPTION ],
    [ false -> TRANSDUCE_OPTION ],
    [ true with text "" -> TRANSLATE_OPTION ],
    [ false -> MAX_TIME_OPTION ],
    [ false -> MAX_PEN_OPTION ],
    [ false -> WORD_TERM_OPTION ],
    [ false -> INV_CHARS_OPTION ],
    [ false -> LIST_DELIM_OPTION ],
    [ false -> GRAPH_OPTION ],
    [ false -> VERBOSE_PARSER_OPTION ],
    [ false -> CLIENT_OPTION ],
    [ false -> TOKEN_FILE_OPTION ],
    [ false -> SEGMENT_MODE_OPTION ],
    [ false -> GENERATIVE_GRAMMAR_OPTION ].

#
# File name handling
# Option parsing will provide the basename of the root grammar module
# The output filenames are derived from this basename.
#
ACTION set basename (>TEXT):
  [ TEXT -> FN_BASE ].

FUNCTION get basename (TEXT>):
  [ FN_BASE -> TEXT ].

FUNCTION get grammar file extension (TEXT>):
  [ GRAMMAR_FILE_EXT -> TEXT ].

FUNCTION get lexicon file extension (TEXT>):
  [ LEXICON_FILE_EXT -> TEXT ].

FUNCTION get input file (TEXT>):
  [ FN_BASE -> TEXT ].

FUNCTION get code file (TEXT>):
  [ FN_BASE + ".S" -> TEXT ].

FUNCTION get log file (TEXT>):
  [ FN_BASE + ".log" -> TEXT ].

FUNCTION get prof file (TEXT>):
  [ FN_BASE + ".pri" -> TEXT ].

FUNCTION get lexicon interface file name (TEXT>):
  [ FN_BASE + ".lif" -> TEXT ].

#
# option handling
# Most option are simple true/false options
# although some carry an extra INT or TEXT with the true value
#
ACTION set warnings option:
  [ true -> WARNINGS_OPTION ].

ACTION reset warnings option:
  [ false -> WARNINGS_OPTION ].

ACTION set no analysis option:
  [ true -> NO_ANALYSIS_OPTION ].

ACTION set full analysis option:
  [ true -> ANALYSIS_OPTION ].

ACTION increase verbose option:
  if verbose option (INT),
    [ INT + 1 -> INT1 ], [ true with INT1 -> VERBOSE_OPTION ];  
  [ true with 1 -> VERBOSE_OPTION ].

ACTION set log option:
  [ true -> LOG_OPTION ].

ACTION set counters option:
  [ true -> COUNTERS_OPTION ].

ACTION set directors option:
  [ true -> DIRECTORS_OPTION ].

ACTION set factorize option:
  [ true -> FACTORIZE_OPTION ].

ACTION set negative memo option:
  [ true -> NEGATIVE_MEMO_OPTION ].

ACTION set positive memo option:
  [ true -> POSITIVE_MEMO_OPTION ].

ACTION set trace option:
  [ true -> TRACE_OPTION ].

ACTION set profile option:
  [ true -> PROFILE_OPTION ].

ACTION reset profile option:
  [ false -> PROFILE_OPTION ].

ACTION set debug option:
  [ true -> DEBUG_OPTION ].

ACTION set time option:
  [ true -> TIME_OPTION ].

ACTION set lexicon used:
  [ true -> LEXICON_USED ].

ACTION set underscore option:
  [ true -> UNDERSCORE_OPTION ].

ACTION set include path (>TEXT):
  [ TEXT -> INCLUDE_PATH ].

ACTION set frequence option:
  [ true -> FREQ_OPTION ].

# 8 bits per byte assumed
#
#ACTION set bits per word (>TEXT):
#  decbin (TEXT, INT),
#    greatereq (INT, 32),	/ at least 32 bits in long
#    [ INT %% 8 -> INT1 ],
#    [ INT1 -> 0 ],
#    [ INT -> BITS_LENGTH ];
#  abort ("invalid number of bits per word '" + TEXT + "'"),
#    !.

ACTION set details option:
  [ true -> DETAILS_OPTION ].

ACTION reset details option:
  [ false -> DETAILS_OPTION ].

ACTION set labelled bracket option:
  [ true -> LABRACKET_OPTION ].

ACTION set suppress output option:
  [ true -> NO_OUTPUT_OPTION ].

ACTION set transduce option:
  [ true -> TRANSDUCE_OPTION ].

ACTION set translate option off:
  [ false -> TRANSLATE_OPTION ].

ACTION set translate option default:
  [ true with text "" -> TRANSLATE_OPTION ].

ACTION set translate option (>TEXT):
  [ true with text TEXT -> TRANSLATE_OPTION ].

ACTION set word terminators option (>TEXT):
  [ true with text TEXT -> WORD_TERM_OPTION ].

ACTION set invisible characters option (>TEXT):
  [ true with text TEXT -> INV_CHARS_OPTION ].

ACTION set graph option:
  [ true -> GRAPH_OPTION ].

ACTION set verbose parser option (>INT):
  [ true with INT -> VERBOSE_PARSER_OPTION ].

ACTION set client option:
  [ true -> CLIENT_OPTION ].

ACTION set token file option (>TEXT):
  [ true with text TEXT -> TOKEN_FILE_OPTION ].

TEST if warnings option:
  [ WARNINGS_OPTION -> true ].

TEST if no analysis option:
  [ NO_ANALYSIS_OPTION -> true ].

TEST if full analysis option:
  [ ANALYSIS_OPTION -> true ].

TEST if verbose option:
  if verbose option (INT).

TEST if very verbose option:
  if verbose option (INT), greatereq (INT, 2).

TEST if verbose option (INT>):
  [ VERBOSE_OPTION -> true with INT ].

TEST if log option:
  [ LOG_OPTION -> true ].

TEST if counters option:
  [ COUNTERS_OPTION -> true ].

TEST if directors option:
  [ DIRECTORS_OPTION -> true ].

TEST if factorize option:
  [ FACTORIZE_OPTION -> true ].

TEST if negative memo option:
  [ NEGATIVE_MEMO_OPTION -> true ].

TEST if positive memo option:
  [ POSITIVE_MEMO_OPTION -> true ].

TEST if trace option:
  [ TRACE_OPTION -> true ].

TEST if profile option:
  [ PROFILE_OPTION -> true ].

TEST if debug option:
  [ DEBUG_OPTION -> true ].

TEST if time option:
  [ TIME_OPTION -> true ].

TEST if lexicon used:
  [ LEXICON_USED -> true ].

TEST if underscore option:
  [ UNDERSCORE_OPTION -> true ].

TEST if frequence option:
  [ FREQ_OPTION -> true ].

FUNCTION get include path (TEXT>):
  [ INCLUDE_PATH -> TEXT ].

# FUNCTION get bits per word (INT>):
#   [ BITS_LENGTH -> INT ].
# 
# 8 bits per byte assumed
# 
# FUNCTION size of long (INT>):
#   get bits per word (INT),
#     [ INT % 8 -> INT ].

TEST if labelled bracket option:
  [ LABRACKET_OPTION -> true ].

TEST if suppress output option:
  [ NO_OUTPUT_OPTION -> true ].

TEST if transduce option:
  [ TRANSDUCE_OPTION -> true ].

TEST if translate option (TEXT>):
  [ TRANSLATE_OPTION -> true with text TEXT ].

TEST if word terminators option (TEXT>):
  [ WORD_TERM_OPTION -> true with text TEXT ].

TEST if invisible characters option (TEXT>):
  [ INV_CHARS_OPTION -> true with text TEXT ].

TEST if graph option:
  [ GRAPH_OPTION -> true ].

TEST if verbose parser option (INT>):
  [ VERBOSE_PARSER_OPTION -> true with INT ].

TEST if client option:
  [ CLIENT_OPTION -> true ].

TEST if token file option (TEXT>):
  [ TOKEN_FILE_OPTION -> true with text TEXT ].

FUNCTION get warnings option (BOOL>):
  convert WARNINGS_OPTION to BOOL.

FUNCTION get profile option (BOOL>):
  convert PROFILE_OPTION to BOOL.

FUNCTION get visibility option (BOOL>):
  convert VISIBLE_OPTION to BOOL.

FUNCTION get details option (BOOL>):
  convert DETAILS_OPTION to BOOL.

FUNCTION convert >OPTION to BOOL>:
  [ OPTION -> true ], [ true -> BOOL ];
  [ false -> BOOL ].

#
# Segment mode option/pragmat
#
ACTION set segment mode option:
  [ true -> SEGMENT_MODE_OPTION ].

TEST if segment mode option:
  [ SEGMENT_MODE_OPTION -> true ].

#
#  Generative grammar:
#
ACTION set generative grammar option:
  [ true -> GENERATIVE_GRAMMAR_OPTION ].

TEST if generative grammar option:
  [ GENERATIVE_GRAMMAR_OPTION -> true ].

#
#  Version control
#
FUNCTION get agfl version (TEXT>):
  get config version (TEXT).

FUNCTION get grammar version (INT>):
  get input file (TEXT1),
    get grammar file extension (TEXT2),
      ( file date (TEXT1 + TEXT2, INT);
        abort ("Can't get file date of '" + TEXT1 + TEXT2 + "'"),
        !
      ).

#
#  Pragmats
#
PRAGMATS :: WARNINGS and DETAILS and PROFILE.
WARNINGS, DETAILS, PROFILE :: BOOL.

FUNCTION empty (PRAGMATS>):
  [ false and false and false -> PRAGMATS ].

FUNCTION current pragmats (PRAGMATS>):
  get warnings option (BOOL1),
    get details option (BOOL2),
    get profile option (BOOL3),
    [ BOOL1 and BOOL2 and BOOL3 -> PRAGMATS ].

FUNCTION add (>PRAGMATS1>, >PRAGMATS2):
  [ PRAGMATS1 -> BOOL1 and BOOL3 and BOOL5 ],
    [ PRAGMATS2 -> BOOL2 and BOOL4 and BOOL6 ],
    or (BOOL1, BOOL2),
    or (BOOL3, BOOL4),
    or (BOOL5, BOOL6),
    [ BOOL1 and BOOL3 and BOOL5 -> PRAGMATS1 ].

TEST if warnings (>PRAGMATS):
  [ PRAGMATS -> WARNINGS and DETAILS and PROFILE ], [ WARNINGS -> true ].

TEST if details (>PRAGMATS):
  [ PRAGMATS -> WARNINGS and DETAILS and PROFILE ], [ DETAILS -> true ].

TEST if profile (>PRAGMATS):
  [ PRAGMATS -> WARNINGS and DETAILS and PROFILE ], [ PROFILE -> true ].

FUNCTION convert >PRAGMATS to TEXT>:
  [ PRAGMATS -> BOOL1 and BOOL2 and BOOL3 ],
    convert BOOL1 to on or off TEXT1,
    convert BOOL2 to on or off TEXT2,
    convert BOOL3 to on or off TEXT3,
    [ "warnings " + TEXT1 + ", details " + TEXT2 + ", profile " + TEXT3 -> TEXT ].

#
# Perhaps this is the wrong location to define BOOL
#
BOOL :: true; false.

FUNCTION or (>BOOL1>, >BOOL2):
  [ BOOL2 -> true ], [ BOOL2 -> BOOL1 ];
  +.

FUNCTION and not (>BOOL1>, >BOOL2):
  [ BOOL2 -> true ], [ false -> BOOL1 ];
  +.

FUNCTION convert >BOOL to TEXT>:
  [ BOOL -> true ], [ "TRUE" -> TEXT ];
  [ BOOL -> false ], [ "FALSE" -> TEXT ];
  ?.

FUNCTION convert >BOOL to on or off TEXT>:
  [ BOOL -> true ], [ "on" -> TEXT ];
  [ "off" -> TEXT ].

EXTERNAL FUNCTION get config version (TEXT>): 300
