/* %Z%%M% %I% %E% */

/*
 * Copyright (c) 1992, 1993, 1995, 2000 by Wayne C. Gramlich.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/*
 * This routine will install generate routines into the routine table.
 */

#ifndef FLAGS_DEFS_H
#include "flags_defs.h"
#endif

#ifndef GEN_EXPORTS_H
#include "gen_exports.h"
#endif

#ifndef GENERATE_DEFS_H
#include "generate_defs.h"
#endif

#ifndef HEAP_EXPORTS_H
#include "heap_exports.h"
#endif

#ifndef LINT_H
#include "lint.h"
#endif

#ifndef MSG_EXPORTS_H
#include "msg_exports.h"
#endif

#ifndef OUT_EXPORTS_H
#include "out_exports.h"
#endif

#ifndef PARSER_DEFS_H
#include "parser_defs.h"
#endif

#ifndef ROUTINE_DEFS_H
#include "routine_defs.h"
#endif

#ifndef STR_EXPORTS_H
#include "str_exports.h"
#endif

#ifndef STRVEC_EXPORTS_H
#include "strvec_exports.h"
#endif

#ifndef TABLE_EXPORTS_H
#include "table_exports.h"
#endif

#ifndef TOKEN_DEFS_H
#include "token_defs.h"
#endif

#ifndef TYPE_DEFS_H
#include "type_defs.h"
#endif

#ifndef VECTOR_DEFS_H
#include "vector_defs.h"
#endif

#ifndef UNIX_ASSERT_H
#include "unix_assert.h"
#endif

#ifndef UNIX_UNISTD_H
#include "unix_unistd.h"
#endif

/*
 * Generate_parse(parser)
 *	This routine will parse a generate routine type from "parser".
 */
Generate
generate_parse(
	Parser		parser)
{
	Generate	generate;
	Generate_kind	kind;
	Str		symbol;
	Token		token;

	token = parser_token_peek(parser);
	if (token->type != Token_type_symbol) {
		msg_out(parser->msg, token->position,
			"Missing generator name");
		return (Generate)0;
	}
	symbol = token->value.symbol;
	if (strequal(symbol, "compare")) {
		kind = Generate_compare;
	} else if (strequal(symbol, "address_get")) {
		kind = Generate_address_get;
	} else if (strequal(symbol, "allocate")) {
		kind = Generate_allocate;
	} else if (strequal(symbol, "copy")) {
		kind = Generate_copy;
	} else if (strequal(symbol, "equal")) {
		kind = Generate_equal;
	} else if (strequal(symbol, "erase")) {
		kind = Generate_erase;
	} else if (strequal(symbol, "hash")) {
		kind = Generate_hash;
	} else if (strequal(symbol, "identical")) {
		kind = Generate_identical;
	} else if (strequal(symbol, "input")) {
		kind = Generate_input;
	} else if (strequal(symbol, "integer_convert")) {
		kind = Generate_integer_convert;
	} else if (strequal(symbol, "new")) {
		kind = Generate_new;
	} else if (strequal(symbol, "output")) {
		kind = Generate_output;
	} else if (strequal(symbol, "parse")) {
		kind = Generate_parse;
	} else if (strequal(symbol, "print")) {
		kind = Generate_print;
	} else if (strequal(symbol, "save")) {
		kind = Generate_save;
	} else if (strequal(symbol, "unsigned_convert")) {
		kind = Generate_unsigned_convert;
	} else {
		msg_out(parser->msg, token->position,
			"%s is not a valid generator", symbol);
		return (Generate)0;
	}
	(void)parser_token_read(parser);
	generate = heap_allocate(parser->heap, Generate);
	generate->kind = kind;
	generate->routine = (Routine)0;
	return generate;
}

/*
 * generate_print(generate, out_file)
 *	This will print "generate" out to "out_file".
 */
void
generate_print(
	Generate	generate,
	Stdio		out_file)
{
	out(out_file, "%s", generate_unparse(generate->kind));
}

/*
 * generate_unparse(generate_kind)
 *	This routine will return the unparsed string for "generate_kind".
 */
Str
generate_unparse(
	Generate_kind	generate_kind)
{
	switch (generate_kind) {
	    case Generate_address_get:
		return "address_get";
	    case Generate_allocate:
		return "allocate";
	    case Generate_compare:
		return "compare";
	    case Generate_copy:
		return "copy";
	    case Generate_equal:
		return "equal";
	    case Generate_erase:
		return "erase";
	    case Generate_hash:
		return "hash";
	    case Generate_identical:
		return "identical";
	    case Generate_input:
		return "input";
	    case Generate_integer_convert:
		return "integer_convert";
	    case Generate_new:
		return "new";
	    case Generate_output:
		return "output";
	    case Generate_parse:
		return "parse";
	    case Generate_print:
		return "print";
	    case Generate_save:
		return "save";
	    case Generate_unsigned_convert:
		return "unsigned_convert";
	    default:
		assert_fail();
	}
	/* NOTREACHED */
}
