From 8f486613be58ced269db1d437e560c16558604e8 Mon Sep 17 00:00:00 2001 From: becquet Date: Thu, 10 May 2007 18:49:20 +0000 Subject: Création de chuck, le programme du robot 2007. --- i/chuck/src/interpreter/Makefile.defs | 6 ++ i/chuck/src/interpreter/interpreter.cc | 127 +++++++++++++++++++++++ i/chuck/src/interpreter/interpreter.hh | 90 ++++++++++++++++ i/chuck/src/interpreter/interpreter.tcc | 142 ++++++++++++++++++++++++++ i/chuck/src/interpreter/interpreter_parser.cc | 43 ++++++++ i/chuck/src/interpreter/interpreter_parser.hh | 43 ++++++++ i/chuck/src/interpreter/test_interpreter.cc | 139 +++++++++++++++++++++++++ 7 files changed, 590 insertions(+) create mode 100644 i/chuck/src/interpreter/Makefile.defs create mode 100644 i/chuck/src/interpreter/interpreter.cc create mode 100644 i/chuck/src/interpreter/interpreter.hh create mode 100644 i/chuck/src/interpreter/interpreter.tcc create mode 100644 i/chuck/src/interpreter/interpreter_parser.cc create mode 100644 i/chuck/src/interpreter/interpreter_parser.hh create mode 100644 i/chuck/src/interpreter/test_interpreter.cc (limited to 'i/chuck/src/interpreter') diff --git a/i/chuck/src/interpreter/Makefile.defs b/i/chuck/src/interpreter/Makefile.defs new file mode 100644 index 0000000..32c9e48 --- /dev/null +++ b/i/chuck/src/interpreter/Makefile.defs @@ -0,0 +1,6 @@ +PROGRAMS += test_interpreter + +interpreter_OBJECTS = interpreter.o interpreter_parser.o \ + $(utils_OBJECTS) $(parser_OBJECTS) + +test_interpreter_OBJECTS = test_interpreter.o $(interpreter_OBJECTS) diff --git a/i/chuck/src/interpreter/interpreter.cc b/i/chuck/src/interpreter/interpreter.cc new file mode 100644 index 0000000..bbf4767 --- /dev/null +++ b/i/chuck/src/interpreter/interpreter.cc @@ -0,0 +1,127 @@ +// interpreter.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.hh" +#include "interpreter_parser.hh" + +/// Destructor. +Interpreter::~Interpreter (void) +{ + for (Funcs::iterator i = funcs_.begin (); i != funcs_.end (); ++i) + { + delete i->second.func; + } +} + +/// Add a function, Interpreter owns f. +void +Interpreter::add (const std::string &s, Func *f, const std::string &desc) +{ + if (!funcs_.insert (Funcs::value_type (s, FuncDesc (f, desc))).second) + { + // Interpreter owns f, therefore, f must be deleted. + delete f; + throw std::runtime_error ("function \'" + s + + "\' inserted two times"); + } +} + +/// Add a function without description, Interpreter owns f. +void +Interpreter::add (const std::string &s, Func *f) +{ + add (s, f, ""); +} + +/// Test if a function is defined. +bool +Interpreter::exists (const std::string &s) const +{ + Funcs::const_iterator i; + i = funcs_.find (s); + return i != funcs_.end (); +} + +/// Call a function by name. +void +Interpreter::call (const std::string &s, const Args &a, + bool dryrun/*false*/) const +{ + Funcs::const_iterator i; + i = funcs_.find (s); + if (i == funcs_.end ()) + throw std::runtime_error ("function \'" + s + "\' does not exist"); + try + { + (*i->second.func) (a, dryrun); + } + catch (const std::exception &e) + { + throw std::runtime_error ("while calling \'" + s + "\', " + + e.what ()); + } +} + +/// 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); +} + +/// Return an help string. +std::string +Interpreter::help (void) const +{ + std::string ret; + for (Funcs::const_iterator i = funcs_.begin (); i != funcs_.end (); ++i) + { + ret += i->first; + if (!i->second.desc.empty ()) + { + ret += " : "; + // Well, if you really want to implement this better, please do... + for (std::string::const_iterator j = i->second.desc.begin (); + j != i->second.desc.end (); j++) + { + if (*j == '\n') + ret += "\n "; + else + ret += *j; + } + } + ret += '\n'; + } + return ret; +} + diff --git a/i/chuck/src/interpreter/interpreter.hh b/i/chuck/src/interpreter/interpreter.hh new file mode 100644 index 0000000..3341ea1 --- /dev/null +++ b/i/chuck/src/interpreter/interpreter.hh @@ -0,0 +1,90 @@ +#ifndef interpreter_hh +#define interpreter_hh +// interpreter.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 "utils/any.hh" + +#include + +class Interpreter +{ + public: + /// Function arguments. + typedef any Arg; + /// Function arguments list. + typedef std::list Args; + /// Functions must inherit from this type. + class Func + { + public: + /// Mandatory virtual destructor. + virtual ~Func (void) + { } + /// Called on function invocation, getting the argument list and + /// returning true on success. + virtual void operator() (const Args &, bool dryrun) = 0; + }; + private: + /// Func class construction helper. + template + class MemFunc; + private: + struct FuncDesc + { + Func *func; + std::string desc; + FuncDesc (Func *func_, const std::string &desc_) + : func (func_), desc (desc_) { } + }; + /// Type of the private function map. + typedef std::map Funcs; + /// The function map itself. + Funcs funcs_; + public: + /// Destructor. + ~Interpreter (void); + /// Add a function, Interpreter owns f. + void add (const std::string &s, Func *f, const std::string &desc); + /// Add a function without description, Interpreter owns f. + void add (const std::string &s, Func *f); + /// Test if a function is defined. + bool exists (const std::string &s) const; + /// 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); + /// Return an help string. + std::string help (void) const; + /// Take all the template sophistications out of the programmer hands. + template + static Func *memFunc (T &i, F f); +}; + +#include "interpreter.tcc" + +#endif // interpreter_hh diff --git a/i/chuck/src/interpreter/interpreter.tcc b/i/chuck/src/interpreter/interpreter.tcc new file mode 100644 index 0000000..5af0e0a --- /dev/null +++ b/i/chuck/src/interpreter/interpreter.tcc @@ -0,0 +1,142 @@ +#ifndef interpreter_tcc +#define interpreter_tcc +// interpreter.tcc +// 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 "utils/meta/remove_reference.hh" + +#include + +/// Func class construction helper. +template +class Interpreter::MemFunc +{ +}; + +/// Partially specialised for member functions taking an arguments list. +template +class Interpreter::MemFunc +: public Func +{ + T &i_; + typedef void (T::*F) (const Interpreter::Args &, bool); + F f_; + public: + MemFunc (T &i, F f) : i_ (i), f_ (f) { } + void operator() (const Args &a, bool dryrun) + { + (i_.*f_) (a, dryrun); + } +}; + +/// Partially specialised for members functions taking no argument. +template +class Interpreter::MemFunc : public Func +{ + T &i_; + typedef R (T::*F) (void); + F f_; + public: + MemFunc (T &i, F f) : i_ (i), f_ (f) { } + void operator() (const Args &a, bool dryrun) + { + if (!a.empty ()) + throw std::runtime_error ("no argument expected"); + if (!dryrun) + (i_.*f_) (); + } +}; + +/// Partially specialised for members functions taking one argument. +template +class Interpreter::MemFunc : public Func +{ + T &i_; + typedef R (T::*F) (A1); + F f_; + public: + MemFunc (T &i, F f) : i_ (i), f_ (f) { } + void operator() (const Args &a, bool dryrun) + { + Args::const_iterator i = a.begin (); + if (a.size () != 1) + throw std::runtime_error ("one argument expected"); + A1 a1 = any_cast::type> (*i); + if (!dryrun) + (i_.*f_) (a1); + } +}; + +/// Partially specialised for members functions taking two arguments. +template +class Interpreter::MemFunc : public Func +{ + T &i_; + typedef R (T::*F) (A1, A2); + F f_; + public: + MemFunc (T &i, F f) : i_ (i), f_ (f) { } + void operator() (const Args &a, bool dryrun) + { + Args::const_iterator i = a.begin (); + if (a.size () != 2) + throw std::runtime_error ("two arguments expected"); + A1 a1 = any_cast::type> (*i); + A2 a2 = any_cast::type> (*++i); + if (!dryrun) + (i_.*f_) (a1, a2); + } +}; + +/// Partially specialised for members functions taking three arguments. +template +class Interpreter::MemFunc : public Func +{ + T &i_; + typedef R (T::*F) (A1, A2, A3); + F f_; + public: + MemFunc (T &i, F f) : i_ (i), f_ (f) { } + void operator() (const Args &a, bool dryrun) + { + Args::const_iterator i = a.begin (); + if (a.size () != 3) + throw std::runtime_error ("three arguments expected"); + A1 a1 = any_cast::type> (*i); + A2 a2 = any_cast::type> (*++i); + A3 a3 = any_cast::type> (*++i); + if (!dryrun) + (i_.*f_) (a1, a2, a3); + } +}; + +/// Take all the template sophistications out of the programmer hands. +template +Interpreter::Func * +Interpreter::memFunc (T &i, F f) +{ + return new MemFunc (i, f); +} + +#endif // interpreter_tcc diff --git a/i/chuck/src/interpreter/interpreter_parser.cc b/i/chuck/src/interpreter/interpreter_parser.cc new file mode 100644 index 0000000..f10e8ec --- /dev/null +++ b/i/chuck/src/interpreter/interpreter_parser.cc @@ -0,0 +1,43 @@ +// 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_); + if (interpreter_.exists ("_postcall")) + interpreter_.call ("_postcall", AnyList (), dryrun_); +} + diff --git a/i/chuck/src/interpreter/interpreter_parser.hh b/i/chuck/src/interpreter/interpreter_parser.hh new file mode 100644 index 0000000..3bd256d --- /dev/null +++ b/i/chuck/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/chuck/src/interpreter/test_interpreter.cc b/i/chuck/src/interpreter/test_interpreter.cc new file mode 100644 index 0000000..631d10d --- /dev/null +++ b/i/chuck/src/interpreter/test_interpreter.cc @@ -0,0 +1,139 @@ +// test_interpreter.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.hh" + +#include +#include +#include + +class TestInterpreter +{ + Interpreter t; + public: + void funcA (const Interpreter::Args &a, bool dryrun) + { + if (!dryrun) + std::cout << " a " << a << std::endl; + } + bool funcB (void) + { + std::cout << " b ( )" << std::endl; + return true; + } + int funcC (int i) + { + std::cout << " c ( " << i << " )" << std::endl; + return 0; + } + void funcD (const std::string &s) + { + std::cout << " d ( " << s << " )" << std::endl; + } + void funcE (int i, const std::string &s, double d) + { + std::cout << " e ( " << i << ' ' << s << ' ' << d << " )" << std::endl; + } + void call (const std::string &s, const Interpreter::Args &a) + { + try + { + std::cout << "call " << s << ' ' << a << std::endl; + t.call (s, a); + } + catch (const std::exception &e) + { + // Do not use std::cerr as there should be only normal errors. + 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) + { + // Add functions. + t.add ("a", Interpreter::memFunc (*this, &TestInterpreter::funcA), + "a ARGS...\n" + "the wonderful `a' function taking any number of any arguments"); + t.add ("b", Interpreter::memFunc (*this, &TestInterpreter::funcB), + "b\ntakes no argument"); + t.add ("c", Interpreter::memFunc (*this, &TestInterpreter::funcC), + "c THE_MIGHTY_NUMBER\n" + "takes one integer"); + t.add ("d", Interpreter::memFunc (*this, &TestInterpreter::funcD), + "d THE_STRING\n" + "takes one string"); + t.add ("e", Interpreter::memFunc (*this, &TestInterpreter::funcE), + "e INT STRING DOUBLE\n" + "takes one integer, one string and one double"); + // Make argument lists. + Interpreter::Args a[4]; + a[1].push_back (71117); + a[2].push_back (std::string ("robert")); + a[3].push_back (42); + a[3].push_back (std::string ("merguez")); + a[3].push_back (51.1664); + // Need help? + std::cout << t.help () << std::endl; + // Call all those wonderful functions. + call ("unknown", a[0]); + for (unsigned int i = 0; i < sizeof (a) / sizeof (a[0]); ++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; + } +}; + +int main (void) +{ + TestInterpreter tt; + return tt.main (); +} -- cgit v1.2.3