diff src/lwasm.c @ 55:8e32696380f3

added expression evaluation and checking function
author lost
date Sun, 04 Jan 2009 21:42:54 +0000
parents b9856da2674a
children 035b95a3690f
line wrap: on
line diff
--- a/src/lwasm.c	Sun Jan 04 20:38:12 2009 +0000
+++ b/src/lwasm.c	Sun Jan 04 21:42:54 2009 +0000
@@ -1,6 +1,6 @@
 /*
 lwasm.c
-Copyright © 2008 William Astle
+Copyright © 2009 William Astle
 
 This file is part of LWASM.
 
@@ -195,6 +195,52 @@
 	return(lwasm_expr_eval(inp, outp, lwasm_expr_lookup_symbol, &st));
 }
 
+int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val)
+{
+	lwasm_expr_stack_t *s;
+	char *ep;
+	int rval;
+		
+	s = lwasm_evaluate_expr(as, l, *inp, &ep);
+	if (!s)
+	{
+		register_error(as, l, 1, "Bad expression");
+		*val = 0;
+		return -1;
+	}
+	*inp = ep;
+	
+	if (flag & EXPR_PASS1CONST && as -> passnum == 1 && !lwasm_expr_is_constant(s))
+	{
+		register_error(as, l, 1, "Illegal incomplete reference (pass 1)");
+		*val = 0;
+		lwasm_expr_stack_free(s);
+		return -1;
+	}
+	if (flag & EXPR_PASS2CONST && as -> passnum == 2 && !lwasm_expr_is_constant(s))
+	{
+		register_error(as, l, 2, "Incomplete reference (pass 2)");
+		*val = 0;
+		lwasm_expr_stack_free(s);
+		return -1;
+	}
+	*val = lwasm_expr_get_value(s);
+	lwasm_expr_stack_free(s);
+
+	if (flag & EXPR_BYTE && as -> passnum == 2 && (*val < -128 || *val > 255))
+	{
+		register_error(as, l, 2, "Byte overflow");
+		*val &= 0xff;
+		return -1;
+	}
+	if (flag & EXPR_BYTE)
+	{
+		*val &= 0xff;
+	}
+
+	return 0;
+}
+
 void debug_message(int level, const char *fmt, ...)
 {
 	va_list args;