csv parse numbers

This commit is contained in:
VaclavT 2021-02-09 18:58:15 +01:00
parent 7cd3155397
commit 22f9410d17
1 changed files with 24 additions and 2 deletions

26
ml.cpp
View File

@ -18,6 +18,7 @@
#include <iostream>
#include <fstream>
#include <ctime>
#include <regex>
std::string read_file_contents(std::string filename) {
std::ifstream f;
@ -107,6 +108,21 @@ bool is_symbol(char ch) {
return (isalpha(ch) || ispunct(ch)) && ch != '(' && ch != ')' && ch != '"' && ch != '\'';
}
// std::regex int_underscored_regex("[0-9][0-9_]+[0-9]");
std::regex int_regex("[0-9]+");
std::regex double_regex("[0-9]+\\.[0-9]+");
// Is string representing int value
bool is_string_int(const std::string &str) {
return std::regex_match(str, int_regex);
}
// Is string representing float value
bool is_string_float(const std::string &str) {
return std::regex_match(str, double_regex);
}
////////////////////////////////////////////////////////////////////////////////
/// LISP CONSTRUCTS ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@ -889,7 +905,7 @@ Value Value::eval(Environment &env) {
}
}
void skip_whitespace(std::string &s, int &ptr) {
void skip_whitespace(const std::string &s, int &ptr) {
while (isspace(s[ptr])) { ptr++; }
}
@ -1214,7 +1230,13 @@ namespace builtin {
std::vector<Value> row;
for (int c = 0; c < cols; c++) {
std::string value = parsed_data[r][c];
row.push_back(Value::string(value));
if (is_string_int(value)) {
row.push_back(Value(stoi(value)));
} if (is_string_float(value)) {
row.push_back(Value(std::stod(value)));
} else {
row.push_back(Value::string(value));
}
}
result.push_back(row);
}