Graphite Version 3
An experimental 3D geometry processing program
Loading...
Searching...
No Matches
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
43#include <string>
44#include <vector>
45#include <map>
46
52namespace 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 virtual index_t get_nb_elements() const override;
105
106 virtual void get_element(index_t i, Any& value) const override;
107
108
109 protected:
110 Object* object_;
111 };
112
114
115 /*************************************************************************/
116
120 gom_class GOM_API GlobalScope : public Scope {
121 public:
128
132 ~GlobalScope() override;
133
137 Any resolve(const std::string& name) override;
138
142 void list_names(std::vector<std::string>& names) const override;
143 };
144
145 /************************************************************************/
146
152 gom_class GOM_API InterfaceScope : public Scope {
153 public:
160
164 ~InterfaceScope() override;
165
169 Any resolve(const std::string& name) override;
170
174 void list_names(std::vector<std::string>& names) const override;
175 };
176
177 /*************************************************************************/
178
184 gom_class GOM_API MetaTypesScope : public Scope {
185 public:
191 MetaTypesScope(const std::string& prefix = "");
192
196 ~MetaTypesScope() override;
197
201 Any resolve(const std::string& name) override;
202
206 void list_names(std::vector<std::string>& names) const override;
207
208 gom_slots:
209 MetaTypesScope* create_subscope(const std::string& name);
210
211 private:
212 std::string prefix_;
213 std::map<std::string, Scope_var> subscopes_;
214 };
215
216
217 /*************************************************************************/
218
219
226 gom_attribute(abstract,"true")
227 gom_class GOM_API Interpreter : public Object {
228
229 public:
230
235
239 virtual void reset()=0;
240
255 virtual Any resolve(const std::string& id, bool quiet=true) const;
256
264 virtual void bind(const std::string& id, const Any& value) = 0;
265
271 virtual void list_names(std::vector<std::string>& names) const;
272
279 virtual Any eval(
280 const std::string& expression, bool quiet=true
281 ) const;
282
283
284 gom_slots:
291 void inspect(Object* object);
292
298 void inspect_meta_type(MetaType* meta_type);
299
305
313 void bind_object(const std::string& id, Object* object);
314
327 Object* resolve_object(const std::string& id, bool quiet=true) const;
328
329
342 const std::string& id, bool quiet=true
343 ) const;
344
353 const std::string& expression, bool quiet=true
354 ) const;
355
362 std::string eval_string(
363 const std::string& expression, bool quiet=true
364 ) const;
365
373 virtual MetaType* resolve_meta_type(const std::string& type_name) const;
374
382 virtual bool bind_meta_type(MetaType* mtype);
383
392 virtual Object* create(const ArgList& args);
393
400 virtual std::string get_environment_value(const std::string& name);
401
408 const std::string& name, const std::string& value
409 );
410
421 virtual bool execute(
422 const std::string& command,
423 bool save_in_history = true,
424 bool log = true
425 ) = 0;
426
434 virtual bool execute_file(const std::string& file_name);
435
441 virtual void out(
442 const std::string& message, const std::string& tag = "GOM"
443 );
444
450 virtual void err(
451 const std::string& message, const std::string& tag = "GOM"
452 );
453
459 virtual void warn(
460 const std::string& message, const std::string& tag = "GOM"
461 );
462
467 virtual void status(const std::string& message);
468
475 virtual void append_dynamic_libraries_path(const std::string& path);
476
483 virtual void append_to_ogf_path(const std::string& path);
484
491 virtual bool load_module(const std::string& module_name);
492
501 virtual Connection* connect(Request* from, Callable* to);
502
503
508 Interpreter* interpreter(const std::string& language) {
509 return instance_by_language(language);
510 }
511
517 const std::string& extension
518 ) {
519 return instance_by_file_extension(extension);
520 }
521
525 void search(
526 const std::string& needle, const std::string& path=""
527 ) override;
528
534 virtual void save_history(const std::string& file_name) const;
535
539 virtual void clear_history();
540
545 void add_to_history(const std::string& command);
546
553 virtual std::string back_resolve(Object* object) const;
554
555 public:
561 return default_interpreter_;
562 }
563
564 gom_properties:
569 virtual std::string get_history() const;
570
576 const std::string& get_language() const {
577 return language_;
578 }
579
585 const std::string& get_filename_extension() const {
586 return extension_;
587 }
588
594 return globals_;
595 }
596
602 return meta_types_;
603 }
604
611 return record_set_property_;
612 }
613
614
621 record_set_property_ = x;
622 }
623
630 return show_add_to_history_;
631 }
632
639 show_add_to_history_ = x;
640 }
641
642 public:
643
650 static Interpreter* instance_by_language(const std::string& language);
651
658 const std::string& extension
659 );
660
664 ~Interpreter() override;
665
678 static void initialize(
679 Interpreter* instance, const std::string& language,
680 const std::string& extension
681 );
682
683
691 static void terminate(
692 const std::string& language, const std::string& extension
693 );
694
701 static void terminate();
702
709 Object* create(const std::string& classname, const ArgList& args);
710
720 const std::string& line, index_t startw, index_t endw,
721 const std::string& cmpword, std::vector<std::string>& matches
722 );
723
724
732 Object* target, const std::string& slot_name, const ArgList& args
733 );
734
742 Object* target, const std::string& prop_name, const Any& value
743 );
744
749 size_t history_size() const {
750 return history_.size();
751 }
752
758 std::string history_line(unsigned int l) const {
759 return l < history_size() ? history_[l] : std::string("");
760 }
761
762
769 virtual std::string back_parse(
770 const Any& any, MetaType* mtype=nullptr
771 ) const;
772
773 protected:
783 const std::string& prefix,
784 std::vector<std::string>& completions
785 );
786
795 const std::string& prefix, std::vector<std::string>& completions
796 );
797
804 virtual void get_keys(
805 const std::string& context, std::vector<std::string>& keys
806 );
807
814 virtual void get_keys(
815 Any& context, std::vector<std::string>& keys
816 );
817
824 void inspect_method(Object* object, MetaMethod* mmethod);
825
832 void inspect_meta_class(Object* object, MetaClass* mclass);
833
839
845 void set_language(const std::string& language) {
846 language_ = language;
847 }
848
853 void set_filename_extension(const std::string& extension) {
854 extension_ = extension;
855 }
856
864 virtual std::string stringify(const std::string& str) const;
865
875 virtual std::string name_value_pair_call(const std::string& args) const;
876
883 virtual bool is_keyword(const std::string& name) const;
884
894 virtual bool name_needs_quotes(const std::string& name) const;
895
896 protected:
897 std::vector<std::string> history_;
898 std::string language_;
899 std::string extension_;
900 // If not set, record_set_property_in_history()) is ignored.
901 bool record_set_property_;
902 // If set, commands added to history are shown in the terminal
903 bool show_add_to_history_;
904
905 private:
906 static std::map<
907 std::string, SmartPointer<Interpreter>
908 > instance_;
909 static std::map<std::string, Interpreter*>
910 instance_by_file_extension_;
911 static Interpreter* default_interpreter_;
912
913 Scope_var globals_;
914 Scope_var meta_types_;
915 };
916
918
919 /*************************************************************************/
920
921}
922
923#endif
A smart pointer with reference-counted copy semantics.
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.
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.
~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.
static void initialize(Interpreter *instance, const std::string &language, const std::string &extension)
Initializes the interpreter subsystem, and defines the interpreter to be used.
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.
Interpreter * interpreter(const std::string &language)
Gets an interpreter for a given language.
const std::string & get_language() const
Gets the name of the interpreted language.
virtual void record_set_property_in_history(Object *target, const std::string &prop_name, const Any &value)
Records a property modification.
bool get_show_add_to_history() const
Gets history verbosity.
virtual Object * create(const ArgList &args)
Creates an object.
virtual Any eval(const std::string &expression, bool quiet=true) const
Evaluates a string with an expression.
Object * create(const std::string &classname, const ArgList &args)
Creates an object from a classname and arguments list.
static Interpreter * default_interpreter()
Gets the default interpreter.
const std::string & get_filename_extension() const
Gets the filename extensions for the interpreted language.
void set_filename_extension(const std::string &extension)
Sets the filename extensions for the interpreted language.
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.
void set_record_set_property(bool x)
Sets property change recording mode.
virtual bool name_needs_quotes(const std::string &name) const
Tests whether a name needs quotes when recorded in history.
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.
Object * resolve_object(const std::string &id, bool quiet=true) const
Finds an objet by id.
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.
virtual void record_invoke_in_history(Object *target, const std::string &slot_name, const ArgList &args)
Record an object invokation to history.
virtual void out(const std::string &message, const std::string &tag="GOM")
Displays a message in the terminal or console.
virtual Connection * connect(Request *from, Callable *to)
Connects a signal to a callable.
void set_show_add_to_history(bool x)
Sets verbose history.
virtual void get_keys(const std::string &context, std::vector< std::string > &keys)
Gets all possible keys in a certain context.
Interpreter()
Interpreter constructor.
void inspect_meta_type(MetaType *meta_type)
Outputs to the logger the methods, slots, properties of a given MetaClass.
Object * eval_object(const std::string &expression, bool quiet=true) const
Evaluates a string with an expression.
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.
Interpreter * interpreter_by_file_extension(const std::string &extension)
Gets an interpreter for a given language by file extension.
virtual std::string back_resolve(Object *object) const
finds how to refer to an object in the interpreter
virtual void clear_history()
Clears the history.
Scope * get_meta_types() const
Gets the Scope with the meta types.
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.
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.
static Interpreter * instance_by_language(const std::string &language)
Gets the instance of the interpreter that interprets a given language.
void inspect_method(Object *object, MetaMethod *mmethod)
Displays the prototype of a given method of an object.
bool get_record_set_property() const
Gets property change recording mode.
virtual void append_to_ogf_path(const std::string &path)
Adds a path to the OGF path.
void bind_object(const std::string &id, Object *object)
Binds an object to a variable.
~Interpreter() override
Interpreter destructor.
void filename_completion(const std::string &prefix, std::vector< std::string > &completions)
Gets all possible filenames starting from a certain prefix.
virtual MetaType * resolve_meta_type(const std::string &type_name) const
Finds a MetaType by name.
void search(const std::string &needle, const std::string &path="") override
Displays the names of all objects that contain a substring.
virtual bool load_module(const std::string &module_name)
Loads a plug-in.
size_t history_size() const
Gets the size of the history.
virtual void err(const std::string &message, const std::string &tag="GOM")
Displays an error message in the terminal or console.
virtual std::string name_value_pair_call(const std::string &args) const
Transforms a list of name-value pairs arguments.
void add_to_history(const std::string &command)
Adds a command line to the history.
virtual std::string back_parse(const Any &any, MetaType *mtype=nullptr) const
finds a scriptable representation of a value
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.
std::string history_line(unsigned int l) const
Gets one of the commands in the history by line index.
Scope * get_globals() const
Gets the Scope with the global variables.
virtual bool is_keyword(const std::string &name) const
Tests whether a string is a keyword in the language.
virtual void get_keys(Any &context, std::vector< std::string > &keys)
Gets all possible keys in a certain context.
void set_language(const std::string &language)
Sets the name of the interpreted language.
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:222
A Scope that contains MetaTypes.
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
virtual void get_element(index_t i, Any &value) const override
Gets an element by index.
~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.
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:340
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.