view src/expr.c @ 15:1f598d89b9b0

Started creating expression parser
author lost
date Thu, 23 Oct 2008 03:37:48 +0000
parents b28d7cb60779
children df0c4a46af8f
line wrap: on
line source

/*
expr.c
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 the actual expression evaluator which uses the LWVAL
mechanism to store results.
*/

#define __expr_c_seen__

#include <ctype.h>
#include <stdlib.h>

#include "expr.h"
#include "lwval.h"

// parse a single term out of the expression; return NULL if
// end of expression; return LWVAL_TYPE_ERR if error
/*
The following is handled by lwasm_parse_term:

- constants
- parsing a symbol
- unary -
- unary +
- ()

*/
LWVAL *lwasm_parse_term(char **ptr)
{
	int sign = 1;
	int s = 0;
	LWVAL *rval;
	
start_term:
	if (!**ptr || isspace(**ptr) || **ptr == ')')
		return s ? lwval_construct_err(1) : NULL;

	s = 1;	
	// unary + - NOOP
	if (**ptr == '+')
	{
		(*ptr)++;
		goto start_term;
	}
	
	// unary - - applied once the rest of the term is worked out
	if (**ptr == '-')
	{
		(*ptr)++;
		sign = -sign;
		goto start_term;
	}
	
	// parens
	if (**ptr == '(')
	{
		LWVAL *v;
		(*ptr)++;
		rval = lwasm_parse_expr(ptr);
		if (**ptr != ')')
		{
			lwval_destroy(rval);
			return lwval_construct_err(1);
		}
		(*ptr)++;
		goto ret;
	}

	// parse an actual term here; no more futzing with expressions

ret:
	// apply negation if appropriate
	if (sign < 0)
		lwval_neg(rval);
	return rval;
}

// parse an expression
LWVAL *lwasm_parse_expr(char **ptr)
{
}

// attempt to evaluate/simplify expression
int lwasm_eval_expr(LWVAL *expr)
{
}