1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // https://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26 
27 #include <Context.h>
28 #include <CmdCalc.h>
29 #include <Filter.h>
30 #include <Eval.h>
31 
32 ////////////////////////////////////////////////////////////////////////////////
CmdCalc()33 CmdCalc::CmdCalc ()
34 {
35   _keyword               = "calc";
36   _usage                 = "task          calc <expression>";
37   _description           = "Calculator";
38   _read_only             = true;
39   _displays_id           = false;
40   _needs_gc              = false;
41   _uses_context          = false;
42   _accepts_filter        = false;
43   _accepts_modifications = false;
44   _accepts_miscellaneous = true;
45   _category              = Command::Category::misc;
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
execute(std::string & output)49 int CmdCalc::execute (std::string& output)
50 {
51   // Configurable infix/postfix
52   bool infix {true};
53   if (Context::getContext ().config.get ("expressions") == "postfix")
54     infix = false;
55 
56   // Create an evaluator with DOM access.
57   Eval e;
58   e.addSource (domSource);
59   e.debug (Context::getContext ().config.getBoolean ("debug"));
60 
61   // Compile all the args into one expression.
62   std::string expression;
63   for (const auto& word : Context::getContext ().cli2.getWords ())
64     expression += word + ' ';
65 
66   // Evaluate according to preference.
67   Variant result;
68   if (infix)
69     e.evaluateInfixExpression (expression, result);
70   else
71     e.evaluatePostfixExpression (expression, result);
72 
73   output = (std::string) result + '\n';
74   return 0;
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78