view src/lwval.h @ 15:1f598d89b9b0

Started creating expression parser
author lost
date Thu, 23 Oct 2008 03:37:48 +0000
parents src/expr.h@b28d7cb60779
children 4f14eae64d38
line wrap: on
line source

/*
lwval.h
Copyright © 2008 William Astle

This file is part of LWASM.

LWASM 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 3 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 General Public License for
more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
This file contains definitions associated with the expression evaluator used
by LWASM.

The core of the entire expression handler is the opaque type LWVAL, pointers
to which are passed around to keep track of values. A value may be a simple
integer or it could be a more complex expression linked by operators or it
could be a polynomial expression. Simple integers are merely a degenerate
case of polynomials.

The package understands the following operations:

addition
subtraction
multiplication
division
modulus
parentheses
unary negation
unary "positive"
bitwise and
bitwise or
bitwise not (1's complement)
bitwise exclusive or

Infix operators can be expressed as LWVAL <op> LWVAL. Thus, the order of
operations is only relevant when initially parsing the expression. The order
of evaluation is determined by what appears on either side of the <op> as
an LWVAL may be an expression.
*/

#ifndef __lwval_h_seen__
#define __lwval_h_seen__

typedef struct lwval LWVAL;

struct lwval_dt_expr
{
	LWVAL *v1;						// first value
	LWVAL *v2;						// second value
	int op;							// operator
};

union lwval_dt
{
	int lwval_int;					// integer type data
	char *lwval_var;				// pointer to variable name
	struct lwval_dt_expr expr;		// expression
};

enum
{
	LWVAL_TYPE_UNDEF,				// undefined
	LWVAL_TYPE_NAN,					// not a number
	LWVAL_TYPE_INT,					// integer
	LWVAL_TYPE_VAR,					// variable (symbol)
	LWVAL_TYPE_EXPR,				// expression
	LWVAL_TYPE_ERR					// error
};

struct lwval
{
	int lwval_type;					// data type
	union lwval_dt dt;				// type specific stuff
};

#ifndef __lwval_c_seen__
#define EE extern
#else
#define EE
#endif

EE LWVAL *lwval_construct_int(int value);
EE LWVAL *lwval_construct_err(int errno);
EE LWVAL *lwval_construct_nan(void);
EE LWVAL *lwval_construct_expr(LWVAL *v1, LWVAL *v2, int op);
EE LWVAL *lwval_construct_undef(void);
EE void lwval_clear(LWVAL *value);
EE void lwval_destroy(LWVAL *value);
EE void lwval_dup(LWVAL *v1, LWVAL *v2);

// operators - operate on v1 and v2 in order, result goes into
// v1; return v1
EE LWVAL *lwval_add(LWVAL *v1, LWVAL *v2);
EE LWVAL *lwval_sub(LWVAL *v1, LWVAL *v2);
EE LWVAL *lwval_mul(LWVAL *v1, LWVAL *v2);
EE LWVAL *lwval_div(LWVAL *v1, LWVAL *v2);
EE LWVAL *lwval_mod(LWVAL *v1, LWVAL *v2);
EE LWVAL *lwval_neg(LWVAL *v1);

#endif //__lwval_h_seen__