<commit_before>#include <iostream>
#include <tuple>
#include <cstring>
#include <string>
#include <ctype.h>
#include <iomanip>

using std::cout;
using std::cerr;
using std::endl;
using std::string;

std::tuple<int, double> getNumber(string &, int);
std::tuple<int, double> getPrim(string &, int);
std::tuple<int, double> getTerm(string &, int);
double getExpr(string &, int);

/*
    Grammar
    expr = term | expr + term | expr - term
    term = prim | term * prim | term / prim
    prim = number | -number
    number = [0-9]+
*/


// getExpr - return value of expression written in {str} from 0 to {end_pos}
double getExpr(string & str, int end_pos) {
    int pos, str_len = str.size();
    double right_value;

    // Value of empty string is 0    
    if (str_len == end_pos) return 0;

    //get right term (look to grammar) and receive the index before this term (pos)
    std::tie(pos, right_value) = getTerm(str, end_pos);

    //return if term is first 
    if (pos == -1) {
        return right_value;
    }

    // look to grammar
    if (str[pos] == '+') {
        return getExpr(str, pos - 1) + right_value;
    } else if (str[pos] == '-') {
        return getExpr(str, pos - 1) - right_value;
    } else {
        return right_value;
    }
}

// getTerm - return value of term and return the index before this term
std::tuple<int, double> getTerm(string & str, int end_pos) {
    int pos;
    double right_value;
    // look to grammar
    std::tie(pos, right_value) = getPrim(str, end_pos);
    
    // return if term is first
    if (pos == -1) {
        return std::make_tuple(pos, right_value);
    }

    double left_value = right_value;
    int pos_before_term = pos;
    // look to grammar
    if (str[pos] == '*') {
        std::tie(pos_before_term, left_value) = getTerm(str, pos - 1);
        left_value *= right_value;
    } else if (str[pos] == '/') {
        std::tie(pos_before_term, left_value) = getTerm(str, pos - 1);
        if (right_value == 0) {<commit_msg>fix error expr<commit_after>#include <iostream>
#include <tuple>
#include <cstring>
#include <string>
#include <ctype.h>
#include <iomanip>

using std::cout;
using std::cerr;
using std::endl;
using std::string;

std::tuple<int, double> getNumber(string &, int);
std::tuple<int, double> getPrim(string &, int);
std::tuple<int, double> getTerm(string &, int);
double getExpr(string &, int);

/*
    Grammar
    expr = term | expr + term | expr - term
    term = prim | term * prim | term / prim
    prim = number | -number
    number = [0-9]+
*/


// getExpr - return value of expression written in {str} from 0 to {end_pos}
double getExpr(string & str, int end_pos) {
    int pos, str_len = str.size();
    double right_value;

    // Value of empty string is 0    
    if (str_len == end_pos) return 0;

    //get right term (look to grammar) and receive the index before this term (pos)
    std::tie(pos, right_value) = getTerm(str, end_pos);

    //return if term is first 
    if (pos == -1) {
        return right_value;
    }

    // look to grammar
    if (str[pos] == '+') {
        return getExpr(str, pos - 1) + right_value;
    } else if (str[pos] == '-') {
        return getExpr(str, pos - 1) - right_value;
    } else {
        std::string error = "Error is expr, position: " + std::to_string(end_pos) + " find next char: ";
        error += str[pos];
        error += " expected [0-9] or -";
        throw error;
    }
}

// getTerm - return value of term and return the index before this term
std::tuple<int, double> getTerm(string & str, int end_pos) {
    int pos;
    double right_value;
    // look to grammar
    std::tie(pos, right_value) = getPrim(str, end_pos);
    
    // return if term is first
    if (pos == -1) {
        return std::make_tuple(pos, right_value);
    }

    double left_value = right_value;
    int pos_before_term = pos;
    // look to grammar
    if (str[pos] == '*') {
        std::tie(pos_before_term, left_value) = getTerm(str, pos - 1);
        left_value *= right_value;
    } else if (str[pos] == '/') {
        std::tie(pos_before_term, left_value) = getTerm(str, pos - 1);
        if (right_value == 0) {
{'fraction_non_alphanumeric': 0.1433389544688027, 'fraction_numerical': 0.006263550951577933, 'mean_word_length': 2.7170993733213966, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4913866', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1397, 'n_words': 428}