Commits

PioneerAxon committed b9e07a1

Fixed add/subtract percent

Added: In parserfunc.c function pf_do_add_perrcent() and pf_do_subtract_percent() with working code.
Fixed: In parser.c, expression_2() now handles add/subtract percentage.
Fixed: In parser.c, wrong dependency on mp-equation-private.h.

  • Participants
  • Parent commits 435ee5b

Comments (0)

Files changed (2)

File src/parser.c

 #include<parserfunc.h>
 
 #include "mp-equation.h"
-#include "mp-equation-private.h"
 
 /* Converts LexerTokenType to Precedence value. */
 static guint
 
 		token = l_get_next_token (state->lexer);
 		l_roll_back (state->lexer);
+
 		if (token->token_type == T_FUNCTION
 		 || token->token_type == T_VARIABLE
 		 || token->token_type == T_SUB_NUMBER
 			return 0;
 		token = l_get_next_token (state->lexer);
 		if (token->token_type == T_PERCENTAGE) {
-		//FIXME: not really sure how to handle this. :(
+			//FIXME: This condition needs to be rechecked.. :(
+			if (node->right->precedence > P_Percentage) {
+				node->precedence = P_Percentage;
+				node->evaluate = pf_do_add_percent;
+				return 1;
+			}
+			else {
+				/* Assume '%' to be part of 'expression T_PERCENTAGE' statement. */
+				l_roll_back (state->lexer);
+				if (!expression_2 (state))
+					return 1;
+			}
 		}
 		else {
 			l_roll_back (state->lexer);
 			return 0;
 		token = l_get_next_token (state->lexer);
 		if (token->token_type == T_PERCENTAGE) {
-		//FIXME: not really sure how to handle this. :(
+			//FIXME: This condition needs to be rechecked.. :(
+			if (node->right->precedence > P_Percentage) {
+				node->precedence = P_Percentage;
+				node->evaluate = pf_do_subtract_percent;
+				return 1;
+			}
+			else {
+				/* Assume '%' to be part of 'expression T_PERCENTAGE' statement. */
+				l_roll_back (state->lexer);
+				if (!expression_2 (state))
+					return 1;
+			}
 		}
 		else {
 			l_roll_back (state->lexer);

File src/parserfunc.c

 
 void* pf_do_add_percent (ParseNode* self)
 {
-	return NULL;
+	MPNumber* ans;
+	MPNumber* val;
+	MPNumber* per;
+	ans = (MPNumber*) malloc (sizeof (MPNumber));
+	val = (MPNumber*) (*(self->left->evaluate)) (self->left);
+	per = (MPNumber*) (*(self->right->evaluate)) (self->right);
+	if (!val || !per) {
+		if (val) free (val);
+		if (per) free (per);
+		free (ans);
+		return NULL;
+	}
+	mp_add_integer (per, 100, per);
+	mp_divide_integer (per, 100, per);
+	mp_multiply (val, per, ans);
+	free (val);
+	free (per);
+	return ans;
 }
 
 void* pf_do_subtract_percent (ParseNode* self)
 {
-	return NULL;
+	MPNumber* ans;
+	MPNumber* val;
+	MPNumber* per;
+	ans = (MPNumber*) malloc (sizeof (MPNumber));
+	val = (MPNumber*) (*(self->left->evaluate)) (self->left);
+	per = (MPNumber*) (*(self->right->evaluate)) (self->right);
+	if (!val || !per) {
+		if (val) free (val);
+		if (per) free (per);
+		free (ans);
+		return NULL;
+	}
+	mp_add_integer (per, -100, per);
+	mp_divide_integer (per, -100, per);
+	mp_multiply (val, per, ans);
+	free (val);
+	free (per);
+	return ans;
 }
 
 /* Converts a constant into percentage. */