@@ -71,19 +71,25 @@ std::string handleBasicExpression(const nlohmann::json& expression)
7171std::string handleOperatorExpression (const std::string& operation, const std::string& leftStr,
7272 const std::string& rightStr)
7373{
74- static const std::unordered_set<std::string> validOperators = { " +" , " -" , " *" , " /" };
74+ if (operation.size () > 1 )
75+ {
76+ throw std::logic_error (" Invalid operator length!" );
77+ }
7578
76- if (validOperators. find ( operation) == validOperators. end () )
79+ switch ( operation[ 0 ] )
7780 {
81+ case ' +' :
82+ case ' -' :
83+ case ' *' :
84+ case ' /' :
85+ return " (" + leftStr + " " + operation + " " + rightStr + " )" ;
86+ default :
7887 throw std::runtime_error (" Invalid operator: " + operation);
7988 }
80-
81- return operation + (leftStr.empty () ? " " : " " + leftStr) +
82- (rightStr.empty () ? " " : " " + rightStr);
8389}
8490
85- std::string handleAggregateExpression (const std::string& operation,
86- const std::vector<std::string>& inputs)
91+ std::string handleCombinationExpression (const std::string& operation,
92+ const std::vector<std::string>& inputs)
8793{
8894 static const std::unordered_set<std::string> validAggregates = { " sum" , " min" , " max" };
8995
@@ -100,10 +106,10 @@ std::string handleAggregateExpression(const std::string& operation,
100106 auto input = std::accumulate (std::next (inputs.begin ()), inputs.end (), inputs[0 ],
101107 [](std::string a, const std::string& b) { return a + " , " + b; });
102108
103- return operation + " : [" + input + " ]" ;
109+ return operation + " [" + input + " ]" ;
104110}
105111
106- std::string displayExpression (const nlohmann::json& expression)
112+ std::string Combinator:: displayExpression (const nlohmann::json& expression)
107113{
108114 if (expression.is_number () || expression.is_string ())
109115 {
@@ -119,15 +125,17 @@ std::string displayExpression(const nlohmann::json& expression)
119125
120126 if (operation == " throttle" )
121127 {
122- return operation;
128+ if (!expression.contains (" input" ))
129+ {
130+ throw std::logic_error (" Throttle does not contain a input" );
131+ }
132+ return handleBasicExpression (expression[" input" ]);
123133 }
124134
125- if (expression.contains (" left" ) || expression.contains (" right" ))
135+ if (expression.contains (" left" ) && expression.contains (" right" ))
126136 {
127- std::string leftStr =
128- expression.contains (" left" ) ? displayExpression (expression[" left" ]) : " " ;
129- std::string rightStr =
130- expression.contains (" right" ) ? displayExpression (expression[" right" ]) : " " ;
137+ std::string leftStr = displayExpression (expression[" left" ]);
138+ std::string rightStr = displayExpression (expression[" right" ]);
131139 return handleOperatorExpression (operation, leftStr, rightStr);
132140 }
133141
@@ -143,7 +151,7 @@ std::string displayExpression(const nlohmann::json& expression)
143151 {
144152 inputStrings.push_back (displayExpression (input));
145153 }
146- return handleAggregateExpression (operation, inputStrings);
154+ return handleCombinationExpression (operation, inputStrings);
147155 }
148156
149157 throw std::runtime_error (" Unsupported operation type: " + operation);
0 commit comments