Added lexing logic for doubles
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful

This commit is contained in:
2025-09-30 20:30:05 +03:00
committed by Emin Arslan
parent ea9c6a0144
commit f93b2deda2
2 changed files with 10 additions and 5 deletions

View File

@@ -11,13 +11,14 @@ enum TokenType {
Symbol, Symbol,
String, String,
Int, Int,
Double,
End End
}; };
// Plain Old Data // Plain Old Data
struct Token { struct Token {
enum TokenType type; enum TokenType type;
std::variant<int64_t, std::string> value; std::variant<int64_t, double, std::string> value;
}; };
std::ostream &operator<<(std::ostream &os, Token const &t); std::ostream &operator<<(std::ostream &os, Token const &t);

View File

@@ -1,10 +1,8 @@
#include <cstdlib>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <lex.hpp> #include <lex.hpp>
#include <string> #include <string>
#include <cctype> #include <cctype>
#include <cstring>
using namespace std; using namespace std;
@@ -17,6 +15,7 @@ std::ostream &operator<<(std::ostream &os, Token const &t) {
case TokenType::Symbol: os << "Symbol, " << get<string>(t.value) << ")"; break; case TokenType::Symbol: os << "Symbol, " << get<string>(t.value) << ")"; break;
case TokenType::String: os << "String, \"" << get<string>(t.value) << "\")"; break; case TokenType::String: os << "String, \"" << get<string>(t.value) << "\")"; break;
case TokenType::Int: os << "Int, " << get<int64_t>(t.value) << ")"; break; case TokenType::Int: os << "Int, " << get<int64_t>(t.value) << ")"; break;
case TokenType::Double: os << "Double, " << get<double>(t.value) << ")"; break;
case TokenType::End: os << "END)"; break; case TokenType::End: os << "END)"; break;
default: default:
os << ")"; os << ")";
@@ -71,6 +70,7 @@ Token Lexer::lexNumOrSym() {
break; break;
} }
dot_seen = true; dot_seen = true;
continue;
} }
if (!isdigit(c)) { if (!isdigit(c)) {
is_number = false; is_number = false;
@@ -78,8 +78,12 @@ Token Lexer::lexNumOrSym() {
} }
} }
if (is_number) { if (is_number && dot_seen) {
return {TokenType::Int, atoll(s.c_str())}; if (s == ".")
return {TokenType::Symbol, s};
return {TokenType::Double, stod(s)};
} else if (is_number) {
return {TokenType::Int, stoll(s)};
} }
return {TokenType::Symbol, s}; return {TokenType::Symbol, s};
} }