From aa023b1a9f1a84323a81c8674d46a1d5a0d39629 Mon Sep 17 00:00:00 2001 From: schodet Date: Tue, 11 Apr 2006 19:08:35 +0000 Subject: Correction d'une erreur dans le bad_any_cast. Ajout du support de la notation exponentielle dans le parser. Ajout du support du parser dans l'interpreter. --- i/marvin/src/interpreter/Makefile.defs | 3 +- i/marvin/src/interpreter/interpreter.cc | 17 ++++++++++ i/marvin/src/interpreter/interpreter.hh | 4 +++ i/marvin/src/interpreter/interpreter_parser.cc | 41 ++++++++++++++++++++++++ i/marvin/src/interpreter/interpreter_parser.hh | 43 ++++++++++++++++++++++++++ i/marvin/src/interpreter/test_interpreter.cc | 36 +++++++++++++++++---- i/marvin/src/parser/yylexer.ll | 8 +++-- i/marvin/src/utils/any.hh | 2 +- i/marvin/src/utils/any.tcc | 4 +-- 9 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 i/marvin/src/interpreter/interpreter_parser.cc create mode 100644 i/marvin/src/interpreter/interpreter_parser.hh (limited to 'i') diff --git a/i/marvin/src/interpreter/Makefile.defs b/i/marvin/src/interpreter/Makefile.defs index d1aac01..32c9e48 100644 --- a/i/marvin/src/interpreter/Makefile.defs +++ b/i/marvin/src/interpreter/Makefile.defs @@ -1,5 +1,6 @@ PROGRAMS += test_interpreter -interpreter_OBJECTS = interpreter.o $(utils_OBJECTS) +interpreter_OBJECTS = interpreter.o interpreter_parser.o \ + $(utils_OBJECTS) $(parser_OBJECTS) test_interpreter_OBJECTS = test_interpreter.o $(interpreter_OBJECTS) diff --git a/i/marvin/src/interpreter/interpreter.cc b/i/marvin/src/interpreter/interpreter.cc index a7f905c..bdc918a 100644 --- a/i/marvin/src/interpreter/interpreter.cc +++ b/i/marvin/src/interpreter/interpreter.cc @@ -23,6 +23,7 @@ // // }}} #include "interpreter.hh" +#include "interpreter_parser.hh" /// Destructor. Interpreter::~Interpreter (void) @@ -66,3 +67,19 @@ Interpreter::call (const std::string &s, const Args &a, } } +/// Interpret a string. +void +Interpreter::interpretString (const std::string &s, bool dryrun/*false*/) +{ + InterpreterParser p (*this, dryrun); + p.parseString (s); +} + +/// Interpret a file. +void +Interpreter::interpretFile (const std::string &file, bool dryrun/*false*/) +{ + InterpreterParser p (*this, dryrun); + p.parseFile (file); +} + diff --git a/i/marvin/src/interpreter/interpreter.hh b/i/marvin/src/interpreter/interpreter.hh index 169e0f2..4164c28 100644 --- a/i/marvin/src/interpreter/interpreter.hh +++ b/i/marvin/src/interpreter/interpreter.hh @@ -63,6 +63,10 @@ class Interpreter /// Call a function by name. void call (const std::string &s, const Args &a, bool dryrun = false) const; + /// Interpret a string. + void interpretString (const std::string &s, bool dryrun = false); + /// Interpret a file. + void interpretFile (const std::string &file, bool dryrun = false); /// Take all the template sophistications out of the programmer hands. template static Func *memFunc (T &i, F f); diff --git a/i/marvin/src/interpreter/interpreter_parser.cc b/i/marvin/src/interpreter/interpreter_parser.cc new file mode 100644 index 0000000..b81eba3 --- /dev/null +++ b/i/marvin/src/interpreter/interpreter_parser.cc @@ -0,0 +1,41 @@ +// interpreter_parser.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "interpreter_parser.hh" +#include "interpreter.hh" + +/// Constructor. +InterpreterParser::InterpreterParser (Interpreter &interpreter, + bool dryrun/*false*/) + : interpreter_ (interpreter), dryrun_ (dryrun) +{ +} + +/// Function called by the parser to make a call. +void +InterpreterParser::call (const std::string &id, AnyList &args) +{ + interpreter_.call (id, args, dryrun_); +} + diff --git a/i/marvin/src/interpreter/interpreter_parser.hh b/i/marvin/src/interpreter/interpreter_parser.hh new file mode 100644 index 0000000..3bd256d --- /dev/null +++ b/i/marvin/src/interpreter/interpreter_parser.hh @@ -0,0 +1,43 @@ +#ifndef interpreter_parser_hh +#define interpreter_parser_hh +// interpreter_parser.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "parser/parser.hh" + +class Interpreter; + +/// Parser used by Interpreter. +class InterpreterParser : public Parser +{ + Interpreter &interpreter_; + bool dryrun_; + public: + /// Constructor. + InterpreterParser (Interpreter &interpreter, bool dryrun = false); + /// Function called by the parser to make a call. + void call (const std::string &id, AnyList &args); +}; + +#endif // interpreter_parser_hh diff --git a/i/marvin/src/interpreter/test_interpreter.cc b/i/marvin/src/interpreter/test_interpreter.cc index 8c2eb88..d51357d 100644 --- a/i/marvin/src/interpreter/test_interpreter.cc +++ b/i/marvin/src/interpreter/test_interpreter.cc @@ -68,9 +68,23 @@ class TestInterpreter std::cout << ' ' << e.what () << std::endl; } } + void interpretString (const std::string &s) + { + try + { + std::cout << "interpret dry \"" << s << "\"" << std::endl; + t.interpretString (s, true); + std::cout << "interpret \"" << s << "\"" << std::endl; + t.interpretString (s); + } + catch (const std::exception &e) + { + // Do not use std::cerr as there should be only normal errors. + std::cout << ' ' << e.what () << std::endl; + } + } int main (void) { - Interpreter::Args a[4]; // Add functions. t.add ("a", Interpreter::memFunc (*this, &TestInterpreter::funcA)); t.add ("b", Interpreter::memFunc (*this, &TestInterpreter::funcB)); @@ -78,6 +92,7 @@ class TestInterpreter t.add ("d", Interpreter::memFunc (*this, &TestInterpreter::funcD)); t.add ("e", Interpreter::memFunc (*this, &TestInterpreter::funcE)); // Make argument lists. + Interpreter::Args a[4]; a[1].push_back (71117); a[2].push_back (std::string ("robert")); a[3].push_back (42); @@ -87,11 +102,20 @@ class TestInterpreter call ("unknown", a[0]); for (unsigned int i = 0; i < sizeof (a) / sizeof (a[0]); ++i) { - call ("a", a[i]); - call ("b", a[i]); - call ("c", a[i]); - call ("d", a[i]); - call ("e", a[i]); + for (char f = 'a'; f <= 'e'; f++) + { + call (std::string (1, f), a[i]); + } + } + // Now, with the interpreter. + interpretString ("a = 42"); + interpretString ("42"); + for (char f = 'a'; f <= 'e'; f++) + { + interpretString (std::string (1, f)); + interpretString (std::string (1, f) + " 42 "); + interpretString (std::string (1, f) + " \"robert\";; "); + interpretString (std::string (1, f) + " 757 \"tintin\" 0.3141516e+1"); } return 0; } diff --git a/i/marvin/src/parser/yylexer.ll b/i/marvin/src/parser/yylexer.ll index 34157b9..084de8f 100644 --- a/i/marvin/src/parser/yylexer.ll +++ b/i/marvin/src/parser/yylexer.ll @@ -37,9 +37,11 @@ INTDEC [+-]?[0-9]+ INTHEX "0x"[0-9a-fA-F]+ INTNUM {INTDEC}|{INTHEX} -DOUBLE1 [+-]?\.[0-9]+ -DOUBLE2 [+-]?[0-9]+\.[0-9]* -DOUBLENUM {DOUBLE1}|{DOUBLE2} +DOUBLEEX [eE]{INTDEC} +DOUBLE1 [+-]?\.[0-9]+{DOUBLEEX}? +DOUBLE2 [+-]?[0-9]+\.[0-9]*{DOUBLEEX}? +DOUBLE3 {INTDEC}{DOUBLEEX} +DOUBLENUM {DOUBLE1}|{DOUBLE2}|{DOUBLE3} %% diff --git a/i/marvin/src/utils/any.hh b/i/marvin/src/utils/any.hh index 55c8ee7..dc6204e 100644 --- a/i/marvin/src/utils/any.hh +++ b/i/marvin/src/utils/any.hh @@ -107,7 +107,7 @@ class bad_any_cast : public std::bad_cast std::string what_; public: /// Constructor. - bad_any_cast (const std::type_info &from, const std::type_info &to); + bad_any_cast (const std::type_info &to, const std::type_info &from); /// Destructor which should throw nothing. virtual ~bad_any_cast() throw() { } diff --git a/i/marvin/src/utils/any.tcc b/i/marvin/src/utils/any.tcc index 0d36d6e..70d1084 100644 --- a/i/marvin/src/utils/any.tcc +++ b/i/marvin/src/utils/any.tcc @@ -164,8 +164,8 @@ any_cast (const any &rhs) /// Constructor. inline -bad_any_cast::bad_any_cast (const std::type_info &from, - const std::type_info &to) +bad_any_cast::bad_any_cast (const std::type_info &to, + const std::type_info &from) { what_ = "illegal conversion from \'"; what_ += from.name (); -- cgit v1.2.3