Commits

Lionel Flandrin  committed 8c47ad2

Added commands showvals, unset and exit

  • Participants
  • Parent commits f297b65

Comments (0)

Files changed (5)

 
   MK_SYMBOL(ABS),
   MK_SYMBOL(INT),
+  MK_SYMBOL(SHOWVARS),
+  MK_SYMBOL(UNSET),
+  MK_SYMBOL(EXIT),
 };
 
 int Lexer::parse_symbol()
   }
 }
 
+void Lexer::unset_variable(const std::string &s)
+{
+  var_map_t::iterator it;
+
+  it = this->variables.find(s);
+
+  if (it != this->variables.end()) {
+    delete it->second;
+    this->variables.erase(it);
+  }
+}
+
+void Lexer::show_variables(const std::string &p)
+{
+  printer_map_t::iterator  pit;
+  Printer                 *printer;
+  size_t                   max_sz = 0;
+
+  pit = this->printers.find(p);
+
+  if (pit == this->printers.end()) {
+    std::cout << "Unknown printer " << p << std::endl;
+    return;
+  }
+
+  printer = pit->second;
+
+  for (var_map_t::const_iterator it = this->variables.begin();
+       it != this->variables.end();
+       ++it)
+    max_sz = std::max(max_sz, it->first.length());
+
+  for (var_map_t::const_iterator it = this->variables.begin();
+       it != this->variables.end();
+       ++it) {
+    std::cout << it->first
+              << std::string(max_sz - it->first.length(), ' ')
+              << " = ";
+    printer->print(std::cout, *it->second);
+    std::cout << std::endl;
+  }
+}
+
 void Lexer::result(Value *v, const std::string &p)
 {
   printer_map_t::iterator it;
 
     Value *get_variable(const std::string &);
     void   set_variable(const std::string &, const Value &);
+    void   unset_variable(const std::string &);
+    void   show_variables(const std::string & = "");
 
     void result(Value *, const std::string & = "");
 

File tests/expected-output.txt

 43
 4.2
 103.64
+G    = 1000000000
+GB   = 1073741824
+M    = 1000000
+MB   = 1048576
+T    = 1000000000000
+TB   = 1099511627776
+k    = 1000
+kB   = 1024
+m    = 0.001
+n    = 0.000000001
+p    = 0.000000000001
+u    = 0.000001
+var  = 43
+var1 = 43
+var2 = 43
+var3 = 4.2
+var4 = 4.2
+G   = 1000000000
+GB  = 1073741824
+M   = 1000000
+MB  = 1048576
+T   = 1000000000000
+TB  = 1099511627776
+k   = 1000
+kB  = 1024
+m   = 0.001
+n   = 0.000000001
+p   = 0.000000000001
+u   = 0.000001
+var = 43
           ^^^^^ division by 0
           ^^^^^^^^^^^^^^^^ division by 0
 5

File tests/tests.txt

 var1 = var2 = var
 var3 = var4 = 4.2
 var1 + var2 + var3 * var4
+showvars
+unset var1
+unset var2 var3 var4
+showvars
 
 # Error handling
 42 + 1 / 0
 0.1 & 3
 3 & 0.1
 1 << -1
+exit
+2+2
 
 %token EOS BAD
 %token ABS INT
+%token SHOWVARS UNSET EXIT
 
 %type<value> exp
 
 
 line:
     EOS
-  | exp EOS { lexer.result($1); }
-  | exp ':' SYMBOL EOS { lexer.result($1, *$3); delete $3; }
+  | exp EOS                 { lexer.result($1); }
+  | exp ':' SYMBOL EOS      { lexer.result($1, *$3); delete $3; }
+  | SHOWVARS EOS            { lexer.show_variables(); }
+  | SHOWVARS ':' SYMBOL EOS { lexer.show_variables(*$3); delete $3; }
+  | UNSET unsetsymbols EOS
+  | EXIT EOS                { YYACCEPT; }
   /* We recover from an error at the end of the statement */
   | error EOS { yyerrok; }
 ;
 
+unsetsymbols:
+    unsetsymbols unsetsymbol
+  | unsetsymbol
+
+unsetsymbol:
+    SYMBOL { lexer.unset_variable(*$1); delete $1; }
+
 exp:
     '(' exp ')'  { $$ = $2; }
   | exp '+' exp  { homogenize($1, $3); $$ = $1; *$$ += *$3; delete $3; }