Hi! I'm trying to implementing shunting yard algorithm and I'm done converting it to postfix. I have some problem on evaluating it.
Here's my code:
Code:
Tree* evaluateExpr(Tree** right, Tree** oper, Tree** left) {
Tree* result = NULL;
char* rightType = (*right) -> type;
char* leftType = (*left) -> type;
char* type = !strcmp("float_typ", rightType) || !strcmp("float_typ", leftType) ? "float_typ" : "int_typ";
void *rightVal, *leftVal, *value;
if(!strcmp("int_typ", rightType)) {
rightVal = (int*)malloc(sizeof(int));
rightVal = atoi((*right) -> value);
}
else if(!strcmp("float_typ", rightType)) {
rightVal = (float*)malloc(sizeof(float));
rightVal = atof((*right) -> value);
}
if(!strcmp("int_typ", leftType)) {
leftVal = (int*)malloc(sizeof(int));
leftVal = atoi((*left) -> value);
}
else if(!strcmp("float_typ", leftType)) {
leftVal = (float*)malloc(sizeof(float));
leftVal = atof((*left) -> value);
}
value = strcmp("float_typ", type) ? (int*)malloc(sizeof(int)) : (float*)malloc(sizeof(float));
if(!strcmp("+", (*oper) -> value))
value = rightVal + leftVal;
else if(!strcmp("-", (*oper) -> value))
value = rightVal - leftVal;
else if(!strcmp("*", (*oper) -> value))
value = rightVal * leftVal;
else if(!strcmp("%", (*oper) -> value))
value = rightVal % leftVal;
else if(!strcmp("/", (*oper) -> value))
value = rightVal / leftVal;
result = newNode(type, 0, (*oper) -> line);
result -> type = type;
result -> value = convertToChar(value, type);
return result;
}
Based on the code above I'm trying to use a generic function to evaluate it and I got some error:
Code:
include/rules.c: In function ‘evaluateExpr’:include/rules.c:241:12: warning: assignment makes pointer from integer without a cast [enabled by default]
rightVal = atoi((*right) -> value);
^
include/rules.c:245:12: error: incompatible types when assigning to type ‘void *’ from type ‘double’
rightVal = atof((*right) -> value);
^
include/rules.c:250:11: warning: assignment makes pointer from integer without a cast [enabled by default]
leftVal = atoi((*left) -> value);
^
include/rules.c:254:11: error: incompatible types when assigning to type ‘void *’ from type ‘double’
leftVal = atof((*left) -> value);
^
include/rules.c:257:64: warning: pointer type mismatch in conditional expression [enabled by default]
value = strcmp("float_typ", type) ? (int*)malloc(sizeof(int)) : (float*)malloc(sizeof(float));
^
include/rules.c:260:20: error: invalid operands to binary + (have ‘void *’ and ‘void *’)
value = rightVal + leftVal;
^
include/rules.c:263:9: warning: assignment makes pointer from integer without a cast [enabled by default]
value = rightVal - leftVal;
^
include/rules.c:266:20: error: invalid operands to binary * (have ‘void *’ and ‘void *’)
value = rightVal * leftVal;
^
include/rules.c:269:20: error: invalid operands to binary % (have ‘void *’ and ‘void *’)
value = rightVal % leftVal;
^
include/rules.c:272:20: error: invalid operands to binary / (have ‘void *’ and ‘void *’)
value = rightVal / leftVal;
Hope you can help me with this, Thanks