summaryrefslogtreecommitdiff
path: root/i
diff options
context:
space:
mode:
Diffstat (limited to 'i')
-rw-r--r--i/marvin/src/interpreter/Makefile.defs3
-rw-r--r--i/marvin/src/interpreter/interpreter.cc17
-rw-r--r--i/marvin/src/interpreter/interpreter.hh4
-rw-r--r--i/marvin/src/interpreter/interpreter_parser.cc41
-rw-r--r--i/marvin/src/interpreter/interpreter_parser.hh43
-rw-r--r--i/marvin/src/interpreter/test_interpreter.cc36
-rw-r--r--i/marvin/src/parser/yylexer.ll8
-rw-r--r--i/marvin/src/utils/any.hh2
-rw-r--r--i/marvin/src/utils/any.tcc4
9 files changed, 145 insertions, 13 deletions
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<class T, typename F>
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 ();