/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident	"$Header: expression.h 1.1 89/10/25 $"

/* Define the various arithmetic operations */
#define	OPadd		0		/* add expressions */
#define	OPsub		1		/* subtract expressions */
#define	OPmul		2		/* multiply expressions */
#define	OPdiv		3		/* divide expression */
#define	OPneg		4		/* negate left exprsn: unary minus */
#define	OPsym		5		/* operand is symbol */
#define	OPconst		6		/* operand is constant */

/*
 * Define the various expression node types:
 *  NOTE: Each expression operation includes both the
 *	  arithmetic operation (in the low 4 bits), and its
 *	  relative precendence in the upper 3 bits).
 */
#define	EXOPadd		((0<<4)|OPadd)	/* add expressions */
#define	EXOPsub		((0<<4)|OPsub)	/* subtract expressions */
#define	EXOPmul		((1<<4)|OPmul)	/* multiply expressions */
#define	EXOPdiv		((1<<4)|OPdiv)	/* divide expression */
#define	EXOPneg		((2<<4)|OPneg)	/* negate left exprsn: unary minus */
#define	EXOPsym		((2<<4)|OPsym)	/* operand is symbol */
#define	EXOPconst	((2<<4)|OPconst)/* operand is constant */

#define	ARITH_OP(x)	(((int)x)&0x0f)	/* extract arithmetic operation */
#define	ARITH_PREC(x)	(((int)x)&0x70)	/* extract arithmetic precedence */

typedef double expr_type;		/* expression arithmetic type */

/*
 *	Expression node:
 */
struct expr {
	char ex_op;			/* EXOP{add,sub,mul,div,neg,sym, */
	union {				/*	const} */
		expr_type constant;	/* constant if ex_op = EXOPconst */
		struct symbol *sym;	/* symbol if ex_op = EXOPsym */
		struct {		/* .....else expressions */
			struct expr *lexpr;
			struct expr *rexpr;
		} expr;
	} ex_u;
};

/*
 *	Expression list:
 */
struct exprlist {
	struct exprlist *exl_next;	/* next expression in list */
	struct expr *exl_expr;		/* expression */
	long exl_val;			/* value of expression */
	char *exl_file;			/* file of expression origin */
	char *exl_product;		/* product of expression origin */
};

/* Relational operators */
#define	RELOPless	0		/* <  */
#define	RELOPlesseq	1		/* <= */
#define	RELOPeq		2		/* = */
#define	RELOPgtreq	3		/* >= */
#define	RELOPgtr	4		/* >  */

/*
 *  Evaluate an expression.
 */
extern expr_type
evaluate(
	/* struct expr *expr;		-- this expression is being evaluated */
	/* struct symbol *forpar;	-- for thie configuration parameter */
);

/*
 *  Function used to print an expression (left to right).
 */
extern void
print_expr(
	/* FILE *fp;			-- output file stream */
	/* struct expr *expr;		-- expression tree to be printed */
);
