Skip to content

Commit

Permalink
fix(parser): fix std::invalid_argument thrown by std::stoi
Browse files Browse the repository at this point in the history
  • Loading branch information
nullptr0x committed Aug 28, 2023
1 parent 9ee1172 commit 633f8cd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Swirl/include/tokenizer/Tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class TokenStream {
return ret;
}


std::string readEscaped(char _end, char apnd = 0) {
uint8_t is_escaped = false;
std::string ret;
Expand Down Expand Up @@ -183,6 +184,7 @@ class TokenStream {
};
}


Token next(bool _showTNw = false, bool _showTWs = false, bool _modifyCurTk = true) {
Token cur_tk = readNextTok();

Expand Down
15 changes: 9 additions & 6 deletions Swirl/src/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ std::unique_ptr<FuncCall> Parser::parseCall() {

parseExpr( call_node->ident);

std::cout << "first arg parsed" << std::endl;
// std::function<void()> parseArgs = [this, &call_node, &parseArgs]() -> void {
// if (m_Stream.p_CurTk.value == "," && m_Stream.p_CurTk.type == PUNC) {
// m_Stream.next();
Expand Down Expand Up @@ -192,11 +191,15 @@ void Parser::parseExpr(const std::string id) {

case NUMBER:
std::cout << "num: " << m_Stream.p_CurTk.value << std::endl;
if (m_Stream.p_CurTk.value.find('.') != std::string::npos)
push_to_expr(std::make_shared<Double>(std::stod(m_Stream.p_CurTk.value)));
else
push_to_expr(std::make_shared<Int>(std::stoi(m_Stream.p_CurTk.value)));
m_Stream.next();

try {
if (m_Stream.p_CurTk.value.find('.') != std::string::npos)
push_to_expr(std::make_shared<Double>(std::stod(m_Stream.p_CurTk.value)));
else
push_to_expr(std::make_shared<Int>(std::stoi(m_Stream.p_CurTk.value)));
m_Stream.next();
} catch (const std::invalid_argument& _) { return; }

continue;

case IDENT:
Expand Down
2 changes: 1 addition & 1 deletion tests/test1.sw
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

t_func("a" + "b", 123)
t_func("322" + 32, 123)

0 comments on commit 633f8cd

Please sign in to comment.