view src/lwval.h @ 125:f9bfc2986023

Actually allow script file to be specified and fix segfault on parsing script file
author lost
date Fri, 23 Jan 2009 04:53:01 +0000
parents 4f14eae64d38
children
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 __lwval_extern__ extern
#else
#define __lwval_extern__
#endif

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

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

#undef __lwval_extern__

#endif //__lwval_h_seen__