changeset 55:8e32696380f3

added expression evaluation and checking function
author lost
date Sun, 04 Jan 2009 21:42:54 +0000
parents 360d53062bb9
children 55260a178667
files src/lwasm.c src/lwasm.h
diffstat 2 files changed, 56 insertions(+), 1 deletions(-) [+]
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;
--- a/src/lwasm.h	Sun Jan 04 20:38:12 2009 +0000
+++ b/src/lwasm.h	Sun Jan 04 21:42:54 2009 +0000
@@ -116,6 +116,15 @@
 
 __lwasm_E__ lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp);
 
+
+// also throw an error on expression eval failure
+// return 0 on ok, -1 on error
+#define EXPR_NOFLAG			0
+#define EXPR_PASS1CONST		1
+#define EXPR_PASS2CONST		2
+#define EXPR_BYTE			4
+__lwasm_E__ int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val);
+
 #undef __lwasm_E__