Are You Going to Write A Script Interpreter for the Language Design Module ?

typedef struct TokenNode {
    char *token;              
    struct TokenNode *next;  
} TokenNode;


TokenNode *new_token_node(const char *token) {
    TokenNode *node = (TokenNode *)malloc(sizeof(TokenNode));
    node->token = strdup(token);
    node->next = NULL;
    return node;
}


TokenNode *tokenize_expression(const char *expr_src) {
    TokenNode *head = NULL, *tail = NULL;
    char token[EXPR_MAX_LEN];
    int token_len = 0;

    for (int i = 0; expr_src[i] != '\0'; i++) {
        if (isspace(expr_src[i])) {
            // Skip spaces
            if (token_len > 0) {
                token[token_len] = '\0'; 
                TokenNode *new_node = new_token_node(token);
                if (!head) {
                    head = new_node;
                    tail = head;
                } else {
                    tail->next = new_node;
                    tail = new_node;
                }
                token_len = 0;
            }
        } else {
            
            token[token_len++] = expr_src[i];
        }
    }
    if (token_len > 0) {
        token[token_len] = '\0';
        TokenNode *new_node = new_token_node(token);
        if (!head) {
            head = new_node;
            tail = head;
        } else {
            tail->next = new_node;
        }
    }
    return head;
}


int evaluate_postfix(TokenNode *head) {
    Stack *stack = new_stack();
    TokenNode *current = head;

    while (current != NULL) {
        char *token = current->token;

        if (isdigit(token[0])) {
            
            stack->push(stack, atoi(token));
        } else {
            
            int operand2 = stack->pop(stack);
            int operand1 = stack->pop(stack);

            if (strcmp(token, "+") == 0) {
                stack->push(stack, operand1 + operand2);
            } else if (strcmp(token, "-") == 0) {
                stack->push(stack, operand1 - operand2);
            } else if (strcmp(token, "*") == 0) {
                stack->push(stack, operand1 * operand2);
            } else if (strcmp(token, "/") == 0) {
                if (operand2 != 0) {
                    stack->push(stack, operand1 / operand2);
                } else {
                    printf("Error: Division by zero\n");
                    return -1;
                }
            }
        }

        
        current = current->next;
    }

    
    int result = stack->pop(stack);
    stack->destroy(stack);
    free(stack);

    return result;
}


int postfix_eval(const char *expr_src) {
    printf("====The expression %s is being evaluated====\n", expr_src);

    
    TokenNode *tokens = tokenize_expression(expr_src);

    
    int result = evaluate_postfix(tokens);

    // Print the result
    printf("Result: %d\n", result);

    // Free the linked list
    TokenNode *current = tokens;
    while (current != NULL) {
        TokenNode *temp = current;
        current = current->next;
        free(temp->token);
        free(temp);
    }

    return result;
}

Last updated