Hey Guys,
Is it possible to inheritance in C??
I want to do the following thing.
I have a compiler.c file in which I have the following function:
Code:
void execute_program(struct statementNode * program)
{
struct statementNode * pc = program;
int op1, op2, result;
while (pc != NULL)
{
switch (pc->stmt_type)
{
case NOOPSTMT:
pc = pc->next;
break;
case PRINTSTMT:
if (pc->print_stmt == NULL)
{
print_debug("Error: pc points to a print statement but pc->print_stmt is null.\n");
exit(1);
}
if (pc->print_stmt->id == NULL)
{
print_debug("Error: print_stmt->id is null.\n");
exit(1);
}
printf("%d\n", pc->print_stmt->id->value);
pc = pc->next;
break;
case ASSIGNSTMT:
if (pc->assign_stmt == NULL)
{
print_debug("Error: pc points to an assignment statement but pc->assign_stmt is null.\n");
exit(1);
}
if (pc->assign_stmt->op1 == NULL)
{
print_debug("Error: assign_stmt->op1 is null.\n");
exit(1);
}
if (pc->assign_stmt->op == PLUS || pc->assign_stmt->op == MINUS
|| pc->assign_stmt->op == MULT || pc->assign_stmt->op == DIV)
{
if (pc->assign_stmt->op2 == NULL)
{
print_debug("Error: right-hand-side of assignment is an expression but assign_stmt->op2 is null.\n");
exit(1);
}
}
if (pc->assign_stmt->lhs == NULL)
{
print_debug("Error: assign_stmt->lhs is null.\n");
exit(1);
}
switch (pc->assign_stmt->op)
{
case PLUS:
op1 = pc->assign_stmt->op1->value;
op2 = pc->assign_stmt->op2->value;
result = op1 + op2;
break;
case MINUS:
op1 = pc->assign_stmt->op1->value;
op2 = pc->assign_stmt->op2->value;
result = op1 - op2;
break;
case MULT:
op1 = pc->assign_stmt->op1->value;
op2 = pc->assign_stmt->op2->value;
result = op1 * op2;
break;
case DIV:
op1 = pc->assign_stmt->op1->value;
op2 = pc->assign_stmt->op2->value;
result = op1 / op2;
break;
case 0:
op1 = pc->assign_stmt->op1->value;
result = op1;
break;
default:
print_debug("Error: invalid value for assign_stmt->op (%d).\n", pc->assign_stmt->op);
exit(1);
break;
}
pc->assign_stmt->lhs->value = result;
pc = pc->next;
break;
case IFSTMT:
if (pc->if_stmt == NULL)
{
print_debug("Error: pc points to an if statement but pc->if_stmt is null.\n");
exit(1);
}
if (pc->if_stmt->true_branch == NULL)
{
print_debug("Error: if_stmt->true_branch is null.\n");
exit(1);
}
if (pc->if_stmt->false_branch == NULL)
{
print_debug("Error: if_stmt->false_branch is null.\n");
exit(1);
}
if (pc->if_stmt->op1 == NULL)
{
print_debug("Error: if_stmt->op1 is null.\n");
exit(1);
}
if (pc->if_stmt->op2 == NULL)
{
print_debug("Error: if_stmt->op2 is null.\n");
exit(1);
}
op1 = pc->if_stmt->op1->value;
op2 = pc->if_stmt->op2->value;
switch (pc->if_stmt->relop)
{
case GREATER:
if (op1 > op2)
pc = pc->if_stmt->true_branch;
else
pc = pc->if_stmt->false_branch;
break;
case LESS:
if (op1 < op2)
pc = pc->if_stmt->true_branch;
else
pc = pc->if_stmt->false_branch;
break;
case NOTEQUAL:
if (op1 != op2)
pc = pc->if_stmt->true_branch;
else
pc = pc->if_stmt->false_branch;
break;
default:
print_debug("Error: invalid value for if_stmt->relop (%d).\n", pc->if_stmt->relop);
exit(1);
break;
}
break;
case GOTOSTMT:
if (pc->goto_stmt == NULL)
{
print_debug("Error: pc points to a goto statement but pc->goto_stmt is null.\n");
exit(1);
}
if (pc->goto_stmt->target == NULL)
{
print_debug("Error: goto_stmt->target is null.\n");
exit(1);
}
pc = pc->goto_stmt->target;
break;
default:
print_debug("Error: invalid value for stmt_type (%d).\n", pc->stmt_type);
exit(1);
break;
}
}
}
I have to make another .c file in which I have to use this function.
In the above given function I just need to add a new case for while and repeat statements. I can't change the above function in compiler.c file.
Please suggest me alternative for the above problem.
Thanks in advance