Graphite  Version 3
An experimental 3D geometry processing program
interpreter.h
Go to the documentation of this file.
1 /*
2  * GXML/Graphite: Geometry and Graphics Programming Library + Utilities
3  * Copyright (C) 2000 Bruno Levy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * If you modify this software, you should include a notice giving the
20  * name of the person performing the modification, the date of modification,
21  * and the reason for such modification.
22  *
23  * Contact: Bruno Levy
24  *
25  * levy@loria.fr
26  *
27  * ISA Project
28  * LORIA, INRIA Lorraine,
29  * Campus Scientifique, BP 239
30  * 54506 VANDOEUVRE LES NANCY CEDEX
31  * FRANCE
32  *
33  * Note that the GNU General Public License does not permit incorporating
34  * the Software into proprietary programs.
35  */
36 
37 #ifndef H_OGF_GOM_INTERPRETER_H
38 #define H_OGF_GOM_INTERPRETER_H
39 
40 #include <OGF/gom/common/common.h>
41 #include <OGF/gom/types/object.h>
42 #include <geogram/basic/numeric.h>
43 #include <string>
44 #include <vector>
45 #include <map>
46 
52 namespace OGF {
53 
54  class Interpreter;
55  class Object;
56  class MetaClass;
57  class MetaMethod;
58  class ArgList;
59  class Request;
60  class Callable;
61  class Connection;
62 
63  /*************************************************************************/
64 
68  gom_attribute(abstract,"true")
69  gom_class GOM_API Scope : public Object {
70  public:
76  Scope(Object* object);
77 
81  ~Scope() override;
82 
89  virtual Any resolve(const std::string& name) = 0;
90 
95  virtual void list_names(std::vector<std::string>& names) const;
96 
100  void search(
101  const std::string& needle, const std::string& path=""
102  ) override;
103 
104  protected:
105  Object* object_;
106  };
107 
109 
110  /*************************************************************************/
111 
115  gom_class GOM_API GlobalScope : public Scope {
116  public:
122  GlobalScope(Interpreter* interpreter);
123 
127  ~GlobalScope() override;
128 
132  Any resolve(const std::string& name) override;
133 
137  void list_names(std::vector<std::string>& names) const override;
138  };
139 
140  /************************************************************************/
141 
147  gom_class GOM_API InterfaceScope : public Scope {
148  public:
155 
159  ~InterfaceScope() override;
160 
164  Any resolve(const std::string& name) override;
165 
169  void list_names(std::vector<std::string>& names) const override;
170  };
171 
172  /*************************************************************************/
173 
179  gom_class GOM_API MetaTypesScope : public Scope {
180  public:
186  MetaTypesScope(const std::string& prefix = "");
187 
191  ~MetaTypesScope() override;
192 
196  Any resolve(const std::string& name) override;
197 
201  void list_names(std::vector<std::string>& names) const override;
202 
203  gom_slots:
204  MetaTypesScope* create_subscope(const std::string& name);
205 
206  private:
207  std::string prefix_;
208  std::map<std::string, Scope_var> subscopes_;
209  };
210 
211 
212  /*************************************************************************/
213 
214 
221  gom_attribute(abstract,"true")
222  gom_class GOM_API Interpreter : public Object {
223 
224  public:
225 
230 
234  virtual void reset()=0;
235 
250  virtual Any resolve(const std::string& id, bool quiet=true) const;
251 
259  virtual void bind(const std::string& id, const Any& value) = 0;
260 
266  virtual void list_names(std::vector<std::string>& names) const;
267 
274  virtual Any eval(
275  const std::string& expression, bool quiet=true
276  ) const;
277 
278 
279  gom_slots:
286  void inspect(Object* object);
287 
293  void inspect_meta_type(MetaType* meta_type);
294 
299  void list_classes();
300 
308  void bind_object(const std::string& id, Object* object);
309 
322  Object* resolve_object(const std::string& id, bool quiet=true) const;
323 
324 
337  const std::string& id, bool quiet=true
338  ) const;
339 
348  const std::string& expression, bool quiet=true
349  ) const;
350 
357  std::string eval_string(
358  const std::string& expression, bool quiet=true
359  ) const;
360 
368  virtual MetaType* resolve_meta_type(const std::string& type_name) const;
369 
377  virtual bool bind_meta_type(MetaType* mtype);
378 
387  virtual Object* create(const ArgList& args);
388 
395  virtual std::string get_environment_value(const std::string& name);
396 
402  virtual void set_environment_value(
403  const std::string& name, const std::string& value
404  );
405 
416  virtual bool execute(
417  const std::string& command,
418  bool save_in_history = true,
419  bool log = true
420  ) = 0;
421 
429  virtual bool execute_file(const std::string& file_name);
430 
436  virtual void out(
437  const std::string& message, const std::string& tag = "GOM"
438  );
439 
445  virtual void err(
446  const std::string& message, const std::string& tag = "GOM"
447  );
448 
454  virtual void warn(
455  const std::string& message, const std::string& tag = "GOM"
456  );
457 
462  virtual void status(const std::string& message);
463 
470  virtual void append_dynamic_libraries_path(const std::string& path);
471 
478  virtual bool load_module(const std::string& module_name);
479 
488  virtual Connection* connect(Request* from, Callable* to);
489 
490 
495  Interpreter* interpreter(const std::string& language) {
496  return instance_by_language(language);
497  }
498 
504  const std::string& extension
505  ) {
506  return instance_by_file_extension(extension);
507  }
508 
512  void search(const std::string& needle, const std::string& path="");
513 
514  public:
520  return default_interpreter_;
521  }
522 
523  gom_properties:
528  virtual std::string get_history() const;
529 
535  const std::string& get_language() const {
536  return language_;
537  }
538 
544  const std::string& get_filename_extension() const {
545  return extension_;
546  }
547 
552  Scope* get_globals() const {
553  return globals_;
554  }
555 
561  return meta_types_;
562  }
563 
564  public:
565 
572  static Interpreter* instance_by_language(const std::string& language);
573 
580  const std::string& extension
581  );
582 
586  virtual ~Interpreter();
587 
600  static void initialize(
601  Interpreter* instance, const std::string& language,
602  const std::string& extension
603  );
604 
605 
613  static void terminate(
614  const std::string& language, const std::string& extension
615  );
616 
623  static void terminate();
624 
631  Object* create(const std::string& classname, const ArgList& args);
632 
641  virtual void automatic_completion(
642  const std::string& line, index_t startw, index_t endw,
643  const std::string& cmpword, std::vector<std::string>& matches
644  );
645 
646 
652  virtual void save_history(const std::string& file_name) const;
653 
657  virtual void clear_history();
658 
663  void add_to_history(const std::string& command);
664 
669  size_t history_size() const {
670  return history_.size();
671  }
672 
678  std::string history_line(unsigned int l) const {
679  return l < history_size() ? history_[l] : std::string("");
680  }
681 
682  protected:
692  const std::string& prefix,
693  std::vector<std::string>& completions
694  );
695 
704  const std::string& prefix, std::vector<std::string>& completions
705  );
706 
713  virtual void get_keys(
714  const std::string& context, std::vector<std::string>& keys
715  );
716 
723  virtual void get_keys(
724  Any& context, std::vector<std::string>& keys
725  );
726 
733  void inspect_method(Object* object, MetaMethod* mmethod);
734 
741  void inspect_meta_class(Object* object, MetaClass* mclass);
742 
748 
754  void set_language(const std::string& language) {
755  language_ = language;
756  }
757 
762  void set_filename_extension(const std::string& extension) {
763  extension_ = extension;
764  }
765 
773  virtual std::string stringify(const std::string& str) const;
774 
784  virtual std::string name_value_pair_call(const std::string& args) const;
785 
786  protected:
787  std::vector<std::string> history_;
788  std::string language_;
789  std::string extension_;
790 
791  private:
792  static std::map<
793  std::string, SmartPointer<Interpreter>
794  > instance_;
795  static std::map<std::string, Interpreter*>
796  instance_by_file_extension_;
797  static Interpreter* default_interpreter_;
798 
799  Scope_var globals_;
800  Scope_var meta_types_;
801  };
802 
804 
805  /*************************************************************************/
806 
807 }
808 
809 #endif
A class that stores a variable of arbitrary type.
Definition: any.h:62
Represents a list of name-value pairs.
Definition: arg_list.h:65
A Callable object.
Definition: callable.h:50
The Scope that contains all global variables of an Interpreter.
Definition: interpreter.h:115
Any resolve(const std::string &name) override
Finds a variable by id.
GlobalScope(Interpreter *interpreter)
GlobalScope constructor.
void list_names(std::vector< std::string > &names) const override
Lists all the variable names available in this scope.
~GlobalScope() override
GlobalScope destructor.
The Scope that contains all interfaces of an object.
Definition: interpreter.h:147
~InterfaceScope() override
InterfaceScope destructor.
InterfaceScope(Object *object)
InterfaceScope constructor.
void list_names(std::vector< std::string > &names) const override
Lists all the variable names available in this scope.
Any resolve(const std::string &name) override
Finds a variable by id.
Abstract base class for the GOM interpreter.
Definition: interpreter.h:222
static void initialize(Interpreter *instance, const std::string &language, const std::string &extension)
Initializes the interpreter subsystem, and defines the interpreter to be used.
Object * resolve_object(const std::string &id, bool quiet=true) const
Finds an objet by id.
virtual void automatic_completion(const std::string &line, index_t startw, index_t endw, const std::string &cmpword, std::vector< std::string > &matches)
Gets the possible automatic completions from a partial command entered by the user in the command lin...
virtual void append_dynamic_libraries_path(const std::string &path)
Adds a path where dynamic libraries can be loaded.
virtual void list_names(std::vector< std::string > &names) const
Lists the global variable names in this Interpreter..
virtual void save_history(const std::string &file_name) const
Saves the history to a file.
virtual Any eval(const std::string &expression, bool quiet=true) const
Evaluates a string with an expression.
virtual ~Interpreter()
Interpreter destructor.
void set_filename_extension(const std::string &extension)
Sets the filename extensions for the interpreted language.
Definition: interpreter.h:762
virtual bool bind_meta_type(MetaType *mtype)
Binds a MetaType.
virtual void warn(const std::string &message, const std::string &tag="GOM")
Displays a warning message in the terminal or console.
void filter_completion_candidates(const std::string &prefix, std::vector< std::string > &completions)
Keeps in a list of completion only those that start with a given prefix.
Object * create(const std::string &classname, const ArgList &args)
Creates an object from a classname and arguments list.
virtual Any resolve(const std::string &id, bool quiet=true) const
Finds a variable by id.
void inspect_meta_method(MetaMethod *mmethod)
Outputs to the logger the signature of a method.
static void terminate()
Terminates the interpreter subsystem, and deallocates the interpreter.
virtual std::string stringify(const std::string &str) const
Transforms a string into a string constant in the interpreted language.
std::string eval_string(const std::string &expression, bool quiet=true) const
Evaluates a string with an expression.
Scope * get_globals() const
Gets the Scope with the global variables.
Definition: interpreter.h:552
virtual void out(const std::string &message, const std::string &tag="GOM")
Displays a message in the terminal or console.
const std::string & get_filename_extension() const
Gets the filename extensions for the interpreted language.
Definition: interpreter.h:544
virtual void get_keys(const std::string &context, std::vector< std::string > &keys)
Gets all possible keys in a certain context.
Interpreter()
Interpreter constructor.
const std::string & get_language() const
Gets the name of the interpreted language.
Definition: interpreter.h:535
void inspect_meta_type(MetaType *meta_type)
Outputs to the logger the methods, slots, properties of a given MetaClass.
virtual bool execute_file(const std::string &file_name)
Executes commands from a given file.
virtual void bind(const std::string &id, const Any &value)=0
Binds a value to a variable.
virtual void clear_history()
Clears the history.
virtual void reset()=0
Clears all variables, restarts from initial state.
virtual std::string get_environment_value(const std::string &name)
Gets the value of a Geogram environment value.
Interpreter * interpreter(const std::string &language)
Gets an interpreter for a given language.
Definition: interpreter.h:495
static Interpreter * default_interpreter()
Gets the default interpreter.
Definition: interpreter.h:519
virtual void status(const std::string &message)
Displays a status message.
static Interpreter * instance_by_file_extension(const std::string &extension)
Gets the instance of the interpreter by file extension.
void inspect_method(Object *object, MetaMethod *mmethod)
Displays the prototype of a given method of an object.
void bind_object(const std::string &id, Object *object)
Binds an object to a variable.
Interpreter * interpreter_by_file_extension(const std::string &extension)
Gets an interpreter for a given language by file extension.
Definition: interpreter.h:503
void filename_completion(const std::string &prefix, std::vector< std::string > &completions)
Gets all possible filenames starting from a certain prefix.
virtual bool load_module(const std::string &module_name)
Loads a plug-in.
virtual MetaType * resolve_meta_type(const std::string &type_name) const
Finds a MetaType by name.
size_t history_size() const
Gets the size of the history.
Definition: interpreter.h:669
virtual void err(const std::string &message, const std::string &tag="GOM")
Displays an error message in the terminal or console.
void search(const std::string &needle, const std::string &path="")
Displays the names of all objects that contain a substring.
virtual std::string name_value_pair_call(const std::string &args) const
Transforms a list of name-value pairs arguments.
Scope * get_meta_types() const
Gets the Scope with the meta types.
Definition: interpreter.h:560
void add_to_history(const std::string &command)
Adds a command line to the history.
Object * resolve_object_by_global_id(const std::string &id, bool quiet=true) const
Finds an objet by global id.
void list_classes()
Outputs to the logger the names of all classes registered to GOM.
void inspect_meta_class(Object *object, MetaClass *mclass)
Displays a meta class.
static Interpreter * instance_by_language(const std::string &language)
Gets the instance of the interpreter that interprets a given language.
virtual Connection * connect(Request *from, Callable *to)
Connects a signal to a callable.
std::string history_line(unsigned int l) const
Gets one of the commands in the history by line index.
Definition: interpreter.h:678
Object * eval_object(const std::string &expression, bool quiet=true) const
Evaluates a string with an expression.
virtual void get_keys(Any &context, std::vector< std::string > &keys)
Gets all possible keys in a certain context.
virtual Object * create(const ArgList &args)
Creates an object.
void set_language(const std::string &language)
Sets the name of the interpreted language.
Definition: interpreter.h:754
virtual bool execute(const std::string &command, bool save_in_history=true, bool log=true)=0
Executes a single line of code in the interpreted language.
static void terminate(const std::string &language, const std::string &extension)
Terminates an interpreter and removes a given language from the list of interpreters.
virtual void set_environment_value(const std::string &name, const std::string &value)
Sets the value of an environment variable.
The representation of a class in the Meta repository.
Definition: meta_class.h:64
The representation of a method in the Meta repository.
Definition: meta_method.h:72
The representation of a type in the Meta repository.
Definition: meta_type.h:221
A Scope that contains MetaTypes.
Definition: interpreter.h:179
Any resolve(const std::string &name) override
Finds a variable by id.
~MetaTypesScope() override
MetaTypesScope destructor.
MetaTypesScope(const std::string &prefix="")
MetaTypesScope constructor.
void list_names(std::vector< std::string > &names) const override
Lists all the variable names available in this scope.
Base class for all objects in the GOM system.
Definition: object.h:65
A pointer to an object and to a meta-method of that object (a very limited notion of "closure").
Definition: callable.h:110
A naming scope in an Interpreter.
Definition: interpreter.h:69
~Scope() override
Scope destructor.
virtual void list_names(std::vector< std::string > &names) const
Lists all the variable names available in this scope.
Scope(Object *object)
Scope constructor.
void search(const std::string &needle, const std::string &path="") override
Displays the names of all objects that contain a substring.
virtual Any resolve(const std::string &name)=0
Finds a variable by id.
std::string extension(const std::string &path)
Gets a path extension.
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Global Graphite namespace.
Definition: common.h:76
Types and functions for numbers manipulation.
The base class for all objects in the GOM system.
Definitions common to all include files in the gom library.