summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/parser.c16
-rw-r--r--src/parserfunc.c19
2 files changed, 17 insertions, 18 deletions
diff --git a/src/parser.c b/src/parser.c
index 8ab4c3c..c9d711b 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -82,7 +82,7 @@ p_make_precedence_t(ParserState* state, LexerTokenType type)
return (p_get_precedence(type) + (state->depth_level * P_Depth));
}
-/* Allocate and create a new node. */
+/* Allocate and create a new node. `value` must be free()able */
static ParseNode*
p_create_node(ParserState* state, LexerToken* token, guint precedence, Associativity associativity, void* value, void* (*function)(ParseNode*))
{
@@ -343,7 +343,7 @@ p_check_variable(ParserState* state, gchar* name)
if(utf8_next_char(name)[0] != '\0')
{
result = 1;
- buffer = (gchar*) malloc(sizeof(gchar) * strlen(name));
+ buffer = (gchar*) malloc(sizeof(gchar) * (strlen(name) + 1));
for(c = name; *c != '\0'; c = next)
{
next = utf8_next_char(c);
@@ -1055,9 +1055,8 @@ variable(ParserState* state)
if(token->token_type == T_SUP_NUMBER)
{
/* FUNCTION SUP_NUMBER expression */
- /* Pass power as void * value. That will be taken care in pf_apply_func_with_powre. */
- node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), token, pf_apply_func_with_power);
+ node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), g_strdup(token->string), pf_apply_func_with_power);
p_insert_into_tree_unary(state, node);
if(!expression(state))
@@ -1067,9 +1066,8 @@ variable(ParserState* state)
else if(token->token_type == T_NSUP_NUMBER)
{
/* FUNCTION NSUP_NUMBER expression */
- /* Pass power as void * value. That will be taken care in pf_apply_func_with_npowre. */
- node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), token, pf_apply_func_with_npower);
+ node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), g_strdup(token->string), pf_apply_func_with_npower);
p_insert_into_tree_unary(state, node);
if(!expression(state))
@@ -1096,9 +1094,8 @@ variable(ParserState* state)
if(token->token_type == T_ROOT)
{
/* SUB_NUM ROOT expression */
- /* Pass SUB_NUM as void* value in node. pf_do_nth_root will take care of it. */
- node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), token_old, pf_do_nth_root);
+ node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), g_strdup(token_old->string), pf_do_nth_root);
p_insert_into_tree_unary(state, node);
if(!expression (state))
@@ -1177,9 +1174,8 @@ term(ParserState* state)
if(token->token_type == T_SUP_NUMBER)
{
/* VARIABLE SUP_NUMBER */
- /* Pass power as void* value. pf_get_variable_with_power will take care of it. */
- node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), token, pf_get_variable_with_power);
+ node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), g_strdup(token->string), pf_get_variable_with_power);
p_insert_into_tree(state, node);
}
diff --git a/src/parserfunc.c b/src/parserfunc.c
index 6f27daf..1d45467 100644
--- a/src/parserfunc.c
+++ b/src/parserfunc.c
@@ -191,7 +191,7 @@ pf_get_variable(ParseNode* self)
if(utf8_next_char(self->token->string)[0] != '\0')
{
result = 1;
- buffer = (gchar*) malloc(sizeof(gchar) * strlen(self->token->string));
+ buffer = (gchar*) malloc(sizeof(gchar) * (strlen(self->token->string) + 1));
mp_set_from_integer(1, &value);
for(c = self->token->string; *c != '\0'; c = next)
{
@@ -230,9 +230,9 @@ pf_get_variable_with_power(ParseNode* self)
MPNumber t = mp_new();
MPNumber* ans = mp_new_ptr();
- pow = super_atoi(((LexerToken*) self->value)->string);
+ pow = super_atoi(self->value);
- /* No need to free the memory. It is allocated and freed somewhere else. */
+ free(self->value);
self->value = NULL;
if(!(self->state->get_variable))
@@ -252,7 +252,7 @@ pf_get_variable_with_power(ParseNode* self)
if(utf8_next_char(self->token->string)[0] != '\0')
{
result = 1;
- buffer = (gchar*) malloc(sizeof(gchar) * strlen(self->token->string));
+ buffer = (gchar*) malloc(sizeof(gchar) * (strlen(self->token->string) + 1));
mp_set_from_integer(1, &value);
for(c = self->token->string; *c != '\0'; c = next)
{
@@ -344,10 +344,11 @@ pf_apply_func_with_power(ParseNode* self)
set_error(self->state, PARSER_ERR_UNKNOWN_FUNCTION, self->token->string);
return NULL;
}
- pow = super_atoi(((LexerToken*) self->value)->string);
+ pow = super_atoi(self->value);
mp_xpowy_integer(tmp, pow, ans);
mp_free(val);
mp_free(tmp);
+ free(self->value);
self->value = NULL;
return ans;
}
@@ -361,7 +362,7 @@ pf_apply_func_with_npower(ParseNode* self)
MPNumber* ans = mp_new_ptr();
gint pow;
gchar* inv_name;
- inv_name = (gchar*) malloc(sizeof(gchar) * strlen(self->token->string) + strlen("⁻¹") + 1);
+ inv_name = (gchar*) malloc(sizeof(gchar) * (strlen(self->token->string) + strlen("⁻¹") + 1));
strcpy(inv_name, self->token->string);
strcat(inv_name, "⁻¹");
val = (MPNumber*) (*(self->right->evaluate))(self->right);
@@ -391,11 +392,12 @@ pf_apply_func_with_npower(ParseNode* self)
set_error(self->state, PARSER_ERR_UNKNOWN_FUNCTION, self->token->string);
return NULL;
}
- pow = super_atoi(((LexerToken*) self->value)->string);
+ pow = super_atoi(self->value);
mp_xpowy_integer(tmp, -pow, ans);
mp_free(val);
mp_free(tmp);
free(inv_name);
+ free(self->value);
self->value = NULL;
return ans;
}
@@ -407,7 +409,8 @@ pf_do_nth_root(ParseNode* self)
MPNumber* val;
gint pow;
MPNumber* ans = mp_new_ptr();
- pow = sub_atoi(((LexerToken*) self->value)->string);
+ pow = sub_atoi(self->value);
+ free(self->value);
self->value = NULL;
val = (MPNumber*) (*(self->right->evaluate))(self->right);
if(!val)