From cd3de49e76305a50ae61696210ce10804a59cef1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 2 Sep 2016 15:34:15 +0300 Subject: [PATCH] Move token-to-argument conversion to Statement --- source/statement.cpp | 22 ++++++++++++++++++++++ source/statement.h | 4 ++++ source/textparser.cpp | 17 ++--------------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/source/statement.cpp b/source/statement.cpp index b1ff058..9dfb544 100644 --- a/source/statement.cpp +++ b/source/statement.cpp @@ -1,5 +1,6 @@ #include #include "statement.h" +#include "token.h" #include "type.h" using namespace std; @@ -36,6 +37,27 @@ string Statement::get_signature() const return result; } +Statement &Statement::append_from_token(const Token &token) +{ + if(token.type==Token::INTEGER) + return append(lexical_cast(token.str)); + else if(token.type==Token::FLOAT) + return append(lexical_cast(token.str)); + else if(token.type==Token::STRING) + return append(token.str); + else if(token.type==Token::IDENTIFIER) + { + if(token.str=="true") + return append(true); + else if(token.str=="false") + return append(false); + else + return append(Symbol(token.str)); + } + else + throw invalid_argument("Statement::append_from_token"); +} + StatementInfo::StatementInfo(): args_size(0) diff --git a/source/statement.h b/source/statement.h index 184ef99..9aad48d 100644 --- a/source/statement.h +++ b/source/statement.h @@ -7,6 +7,8 @@ namespace Msp { namespace DataFile { +struct Token; + class Statement { public: @@ -32,6 +34,8 @@ public: return *this; } + Statement &append_from_token(const Token &); + template Statement &operator,(const T &v) { return append(v); } diff --git a/source/textparser.cpp b/source/textparser.cpp index a7e8ee9..7dc4bde 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -66,21 +66,8 @@ Statement TextParser::parse_statement(const Token *t) sub = 1; else if(token.str==";") break; - else if(token.type==Token::INTEGER) - result.append(lexical_cast(token.str)); - else if(token.type==Token::FLOAT) - result.append(lexical_cast(token.str)); - else if(token.type==Token::STRING) - result.append(token.str); - else if(token.type==Token::IDENTIFIER) - { - if(token.str=="true") - result.append(true); - else if(token.str=="false") - result.append(false); - else - result.append(Symbol(token.str)); - } + else if(token.type!=Token::SPECIAL) + result.append_from_token(token); else throw syntax_error(token.str); } -- 2.45.2