| 839 | } |
| 840 | |
| 841 | void _AR_EXP_ToString(const AR_ExpNode *root, char **str, size_t *str_size, |
| 842 | size_t *bytes_written) { |
| 843 | /* Make sure there are at least 64 bytes in str. */ |
| 844 | if(*str == NULL) { |
| 845 | *bytes_written = 0; |
| 846 | *str_size = 128; |
| 847 | *str = rm_calloc(*str_size, sizeof(char)); |
| 848 | } |
| 849 | |
| 850 | if((*str_size - strlen(*str)) < 64) { |
| 851 | *str_size += 128; |
| 852 | *str = rm_realloc(*str, sizeof(char) * *str_size); |
| 853 | } |
| 854 | /* Concat Op. */ |
| 855 | if(AR_EXP_IsOperation(root)) { |
| 856 | /* Binary operation? */ |
| 857 | char binary_op = 0; |
| 858 | |
| 859 | const char *func_name = AR_EXP_GetFuncName(root); |
| 860 | if(strcmp(func_name, "ADD") == 0) binary_op = '+'; |
| 861 | else if(strcmp(func_name, "SUB") == 0) binary_op = '-'; |
| 862 | else if(strcmp(func_name, "MUL") == 0) binary_op = '*'; |
| 863 | else if(strcmp(func_name, "DIV") == 0) binary_op = '/'; |
| 864 | |
| 865 | if(binary_op) { |
| 866 | _AR_EXP_ToString(root->op.children[0], str, str_size, bytes_written); |
| 867 | |
| 868 | /* Make sure there are at least 64 bytes in str. */ |
| 869 | if((*str_size - strlen(*str)) < 64) { |
| 870 | *str_size += 128; |
| 871 | *str = rm_realloc(*str, sizeof(char) * *str_size); |
| 872 | } |
| 873 | |
| 874 | *bytes_written += sprintf((*str + *bytes_written), " %c ", binary_op); |
| 875 | |
| 876 | _AR_EXP_ToString(root->op.children[1], str, str_size, bytes_written); |
| 877 | } else { |
| 878 | /* Operation isn't necessarily a binary operation, use function call representation. */ |
| 879 | *bytes_written += sprintf((*str + *bytes_written), "%s(", AR_EXP_GetFuncName(root)); |
| 880 | |
| 881 | for(int i = 0; i < root->op.child_count ; i++) { |
| 882 | _AR_EXP_ToString(root->op.children[i], str, str_size, bytes_written); |
| 883 | |
| 884 | /* Make sure there are at least 64 bytes in str. */ |
| 885 | if((*str_size - strlen(*str)) < 64) { |
| 886 | *str_size += 128; |
| 887 | *str = rm_realloc(*str, sizeof(char) * *str_size); |
| 888 | } |
| 889 | if(i < (root->op.child_count - 1)) { |
| 890 | *bytes_written += sprintf((*str + *bytes_written), ","); |
| 891 | } |
| 892 | } |
| 893 | *bytes_written += sprintf((*str + *bytes_written), ")"); |
| 894 | } |
| 895 | } else { |
| 896 | // Concat Operand node. |
| 897 | if(root->operand.type == AR_EXP_CONSTANT) { |
| 898 | SIValue_ToString(root->operand.constant, str, str_size, bytes_written); |
no test coverage detected