Thread: Modify a Function in C

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    Modify a Function in C

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abhinav3
    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.
    From what I understand, if you could change the implementation of execute_program, you would simply add another case for switch (pc->stmt_type).

    I may be mistaken, but I don't think you can accomplish what you want to do without changing the implementation of execute_program. If this were C++, the polymorphic behaviour would still require changing the implementation of execute_program, after which no further changes would be required to cater to new statement (sub)types.

    If you were allowed to change the implementation of execute_program, then you could indeed implement inheritance/polymorphism in C to accomplish what you want, similiar to how it might be done in C++, e.g., the statementNode object might have a function pointer member that pointed to the right function for the subtype to do the specific action that you want to take.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You're not allowed to (or cannot) change execute_program() at all? The problems, that I see, are that it's a void function and there are calls to exit() throughout it (although if the default case didn't call exit() I could see a way to do it)

    Edit: actually, you could (sort of) do it. Just have a new function that parses your new values and if they fail then fallback onto execute_program(). It wouldn't be "morphism" in the way it's normally implement by, say, a C++ compiler but it'd work.

    Actually, no. The silly while is in the function. Hmm.
    Last edited by Hodor; 12-04-2013 at 03:35 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you have the source code for your compiler, why are you not permitted to modify it for your purposes?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > 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.
    So what's the problem?

    You can synthesise any loop with an if statement and a goto statement, both of which are provided for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to modify array in a shared library function
    By JulietBoy in forum C Programming
    Replies: 8
    Last Post: 05-24-2013, 05:39 AM
  2. Modify string in function
    By Ducky in forum C++ Programming
    Replies: 75
    Last Post: 06-19-2012, 04:59 PM
  3. modify function pass it array
    By a.mlw.walker in forum C Programming
    Replies: 12
    Last Post: 08-01-2011, 04:03 AM
  4. how to modify strcmp function
    By asteroid1122 in forum C Programming
    Replies: 6
    Last Post: 08-23-2009, 12:24 AM
  5. modify has function from string parameter to templates...
    By rusty0412 in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2005, 08:02 PM

Tags for this Thread