NEST  2.6.0,not_revisioned_source_dir@0
interpret.h
Go to the documentation of this file.
1 /*
2  * interpret.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef INTERPRETER_H
24 #define INTERPRETER_H
25 /*
26  interpret.h defines the SLI Interpreter class
27 */
28 
29 #include <typeinfo>
30 #include <list>
31 
32 #include "token.h"
33 #include "slitype.h"
34 #include "tokenstack.h"
35 #include "sliactions.h"
36 #include "slibuiltins.h"
37 #include "slimodule.h"
38 
53 class Parser;
54 class Scanner;
55 class DictionaryStack;
56 class Dictionary;
57 class FunctionDatum;
58 class BoolDatum;
59 class DynModule;
60 
61 namespace nest {
62  class Network;
63 }
64 
65 extern "C"{
66  void SLIthrowsignal(int s);
67 }
68 
70 {
71  std::list<SLIModule *> modules;
72 
73  /* Flags and variables to control debugging and
74  * optimizations.
75  */
76  bool debug_mode_;
77  bool show_stack_;
83 
84 
85  unsigned long cycle_count;
87  unsigned long cycle_restriction;
88 
89 
90 
92  void inittypes(void);
93  void initdictionaries(void);
94  void initbuiltins(void);
95  void initexternals(void);
96 
97 public:
98  unsigned long code_accessed; // for code coverage analysis.
99  unsigned long code_executed; // ration should be coverage
100 
101 
104 
107 
108 
109  // Names of basics functions
122 
125 
129 
130  // Names of symbols and objects
150 
151  // Names of basic errors
166 
168 
169  // Debug Message levels as static consts
174  static const int M_ALL;
175  static const int M_DEBUG;
177  static const int M_STATUS;
178  static const int M_INFO;
179  static const int M_WARNING;
180  static const int M_ERROR;
181  static const int M_FATAL;
182  static const int M_QUIET;
183 
185  private:
186  static char const * const M_ALL_NAME;
187  static char const * const M_DEBUG_NAME;
188  static char const * const M_STATUS_NAME;
189  static char const * const M_INFO_NAME;
190  static char const * const M_WARNING_NAME;
191  static char const * const M_ERROR_NAME;
192  static char const * const M_FATAL_NAME;
193  static char const * const M_QUIET_NAME;
194 
195  public:
196 
197  // These static members must be accessible from
198  // the Datum constructors
199 
220 
221  // SLIType default actions
230 
231  // Basic Operations needed to run the default actions
243 
244  // State variables of the Interpreter
245 
246 
247  Token ct; // callback; see comments in execute(void)
248 
251 
252  // public member functions:
253  SLIInterpreter(void);
254  ~SLIInterpreter();
255 
256  int startup();
257 
261  int execute(const std::string &);
262 
266  int execute(const Token &);
267 
271  int execute(int v=0);
272 
273  // int execute_protected(void);
274 
279  int execute_(size_t exitlevel=0);
280  int execute_debug_(size_t exitlevel=0);
281 
282  void createdouble(Name const&, double);
283  void createcommand(Name const&, SLIFunction const *);
284  void createcommand(Name const&, Name const&, SLIFunction const *);
285  void createconstant(Name const&, const Token&);
286 
287 
292  const Token & lookup(const Name &n) const;
293 
294 
299  const Token & lookup2(const Name &n) const;
300 
305  const Token & baselookup(const Name &n) const; // lookup in a specified
306 
309  bool known(const Name &n) const;
310 
313  bool baseknown(const Name &n) const;
314 
320  void def(Name const&, Token const &);
321 
325  void undef(Name const&);
326 
330  void basedef(const Name &n, const Token &t);
331 
336  void def_move(Name const&, Token &);
337 
341  void basedef_move(const Name &n, Token &t);
342 
343  void setcycleguard(Index);
344  void removecycleguard(void);
345 
346 
356  {
357  ++call_depth_;
358  }
359 
369  {
370  --call_depth_;
371  }
372 
381  void set_call_depth(int l)
382  {
383  call_depth_=l;
384  }
385 
394  int get_call_depth() const
395  {
396  return call_depth_;
397  }
398 
408  {
410  }
411 
420  int get_max_call_depth() const
421  {
422  return max_call_depth_;
423  }
424 
430  bool step_mode() const
431  {
433  }
434 
438  bool get_debug_mode() const
439  {
440  return debug_mode_;
441  }
442 
447  {
448  debug_mode_=true;
449  }
450 
455  {
456  debug_mode_=false;
457  }
458 
462  void toggle_stack_display();
463 
464 
465 
466 
470  void debug_options() const;
471 
477  char debug_commandline(Token &);
478 
479 
484  {
485  return opt_tailrecursion_;
486  }
487 
500  {
501  opt_tailrecursion_=true;
502  }
503 
516  {
517  opt_tailrecursion_=false;
518  }
519 
531  bool show_backtrace() const
532  {
533  return show_backtrace_;
534  }
535 
547  void backtrace_on();
548 
549 
561  void backtrace_off();
562 
563 
564  bool catch_errors() const
565  {
566  return catch_errors_;
567  }
568 
570  {
571  catch_errors_=true;
572  }
573 
575  {
576  catch_errors_=false;
577  }
578 
579  void stack_backtrace(int n);
580 
620  void raiseerror(const char* err) { raiseerror(Name(err));}
621 
660  void raiseerror(Name err);
661 
672  void raiseerror(std::exception &err);
673 
711  void raiseerror(Name cmd, Name err);
712 
725  void print_error(Token cmd);
726 
737  void raiseagain(void);
738 
744  void raisesignal(int);
745 
746 
747  // Message loging mechanism
748 
757  void verbosity(int);
758 
781  int verbosity(void) const;
782 
809  void message(int level, const char from[], const char text[],
810  const char errorname[] = "") const;
811 
817  void message(std::ostream& out, const char levelname[],
818  const char from[], const char text[],
819  const char errorname[] = "") const;
820 
821  void terminate(int returnvalue=-1);
822 
823  //*******************************************************
824  Name getcurrentname(void) const;
825 
826  unsigned long cycles(void) const
827  {
828  return cycle_count;
829  }
830 
831 
832  template<class T> void addmodule(void);
833  void addmodule(SLIModule *);
834 
835  /*
836  * Add a linked dynamic module to the interpreter.
837  * Initializers (commandstrings) for linked dynamic modules are executed
838  * by sli-init.sli after all C++ initialization is done.
839  * Do not use this for modules loaded at runtime!
840  */
842 
843  FunctionDatum * Ilookup(void) const;
844  FunctionDatum * Iiterate(void) const;
845 
851  void assert_stack_load(size_t n);
852 
853 };
854 
855 // This function template is a workaround for the parameterless
856 // template member function, given below. As of
857 // egcs-2.91.16 980328 (gcc-2.8.0 release)
858 // the compiler is not able to parse a call like
859 // engine.addmodule<ModuleX>();
860 // (Stroustrup97), Sec 13.3.1 (p335)
861 
862 template<class T>
864 {
865  i.addmodule(new T);
866 }
867 
868 template<class T>
870 {
871  SLIModule *m=new T();
872 
873  modules.push_back(m);
874  m->install(std::cout,this);
875 }
876 
877 inline
879 {
880  if ( OStack.load() < n)
881  throw StackUnderflow(n, OStack.load());
882 }
883 
884 
885 #endif
static SLIType Arraytype
Definition: interpret.h:206
void inc_call_depth()
Increment call depth level.
Definition: interpret.h:355
static SLIType Litproceduretype
Definition: interpret.h:208
void backtrace_off()
Switch stack backtrace off.
Definition: interpret.cc:968
unsigned long cycle_restriction
Definition: interpret.h:87
void debug_options() const
Show Debug options.
Definition: interpret.cc:1012
Name dstack_name
Definition: interpret.h:147
unsigned long code_accessed
Definition: interpret.h:98
Name StackUnderflowError
Definition: interpret.h:153
static const IforallarrayFunction iforallarrayfunction
Definition: interpret.h:238
void addlinkeddynmodule(DynModule *, nest::Network *)
Definition: interpret.cc:567
void addmodule(void)
Definition: interpret.h:869
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
static const int M_INFO
Predefined error level for informational messages.
Definition: interpret.h:178
void dec_call_depth()
Decrement call depth level.
Definition: interpret.h:368
Definition: slibuiltins.h:102
Name e_name
Definition: interpret.h:124
Name interpreter_name
Definition: interpret.h:149
Name ArgumentTypeError
Definition: interpret.h:152
Definition: slibuiltins.h:52
static char const *const M_WARNING_NAME
Definition: interpret.h:190
static SLIType Callbacktype
Definition: interpret.h:213
int verbosity(void) const
Retrieve the current verbosity level of the SLI messaging mechanism.
Definition: interpret.cc:786
void initexternals(void)
Definition: interpret.cc:227
static const IforallindexedarrayFunction iforallindexedarrayfunction
Definition: interpret.h:240
Name BadIOError
Definition: interpret.h:159
Exception to be thrown if an error occured while accessing the stack.
Definition: sliexceptions.h:303
Definition: sliactions.h:87
bool opt_tailrecursion_
Optimize tailing recursion.
Definition: interpret.h:80
FunctionDatum * Ilookup(void) const
Definition: interpret.cc:251
static NametypeFunction nametypefunction
Definition: interpret.h:223
Definition: sliactions.h:38
static const int M_ALL
Predefined error level for turning on the display of all messages; for use with verbosity(int).
Definition: interpret.h:174
Definition: sliactions.h:66
Name iforallstring_name
Definition: interpret.h:121
Name getcurrentname(void) const
Definition: interpret.cc:930
static CallbacktypeFunction callbacktypefunction
Definition: interpret.h:228
Definition: slibuiltins.h:110
void initdictionaries(void)
Definition: interpret.cc:185
Definition: slifunction.h:35
int max_call_depth_
Depth until which procedure calls are debugged.
Definition: interpret.h:82
void inittypes(void)
Definition: interpret.cc:141
Name iiterate_name
Definition: interpret.h:113
Name mark_name
Definition: interpret.h:134
Definition: slibuiltins.h:38
Token execbarrier_token
Definition: interpret.h:167
static const IforallstringFunction iforallstringfunction
Definition: interpret.h:242
Definition: tokenstack.h:38
void optimize_tailrecursion_on()
Enable tail-recursion optimization.
Definition: interpret.h:499
static const int M_WARNING
Predefined error level for warning messages.
Definition: interpret.h:179
Represent strings by ints to facilitate fast comparison.
Definition: name.h:53
Name signo_name
Definition: interpret.h:143
void terminate(int returnvalue=-1)
Definition: interpret.cc:791
Name errorname_name
Definition: interpret.h:141
bool debug_mode_
True, if SLI level debugging is enabled.
Definition: interpret.h:76
Name estack_name
Definition: interpret.h:145
void addmodule(SLIInterpreter &i)
Definition: interpret.h:863
SLIInterpreter(void)
Definition: interpret.cc:373
static const int M_FATAL
Predefined error level for failure messages.
Definition: interpret.h:181
void raiseagain(void)
Re-raise the last error.
Definition: interpret.cc:756
void undef(Name const &)
Unbind a previously bound Token from a Name.
Definition: interpret.cc:353
TokenStack OStack
Definition: interpret.h:249
Dictionary * errordict
Definition: interpret.h:103
void toggle_stack_display()
Switch stack display on or off in debug mode.
Definition: interpret.cc:952
static SLIType DoubleVectortype
Definition: interpret.h:218
static const IsetcallbackFunction isetcallbackfunction
Definition: interpret.h:233
bool show_stack_
Show stack in debug mode.
Definition: interpret.h:77
bool show_backtrace_
Show stack-backtrace on error.
Definition: interpret.h:78
Definition: sliactions.h:73
void raisesignal(int)
TO BE DOCUMENTED.
Definition: interpret.cc:772
static char const *const M_STATUS_NAME
Definition: interpret.h:188
bool step_mode() const
Returns true, if step mode is active.
Definition: interpret.h:430
std::list< SLIModule * > modules
Definition: interpret.h:71
void message(int level, const char from[], const char text[], const char errorname[]="") const
Display a message.
Definition: interpret.cc:805
void def_move(Name const &, Token &)
Bind a Token to a Name.
Definition: interpret.cc:363
bool known(const Name &n) const
Test for a name searching all dictionaries on the stack.
Definition: interpret.cc:338
Name pi_name
Definition: interpret.h:123
void set_max_call_depth(int d)
Set maximal call depth level to a specific value.
Definition: interpret.h:407
static SLIType Integertype
Definition: interpret.h:200
unsigned long Index
Definition: token.h:372
Name end_name
Definition: interpret.h:128
static const IiterateFunction iiteratefunction
Definition: interpret.h:234
static SLIType Literaltype
Definition: interpret.h:205
Name DivisionByZeroError
Definition: interpret.h:156
TokenStack EStack
Definition: interpret.h:250
void createdouble(Name const &, double)
Definition: interpret.cc:261
Name ostack_name
Definition: interpret.h:146
A class that associates names and tokens.
Definition: dict.h:45
void optimize_tailrecursion_off()
Disable tail-recursion optimization.
Definition: interpret.h:515
int verbositylevel
Definition: interpret.h:91
int startup()
Initialise the interpreter by reading in the startup files.
Definition: interpret.cc:1199
Name ilookup_name
Definition: interpret.h:110
void set_call_depth(int l)
Set call depth level to a specific value.
Definition: interpret.h:381
void install(std::ostream &, SLIInterpreter *)
Print installation message via interpreter message command.
Definition: slimodule.cc:26
static char const *const M_ALL_NAME
Definition: interpret.h:186
static const IforallindexedstringFunction iforallindexedstringfunction
Definition: interpret.h:241
void setcycleguard(Index)
Definition: interpret.cc:941
Name ifor_name
Definition: interpret.h:116
Definition: scanner.h:38
Definition: sliactions.h:59
bool baseknown(const Name &n) const
Test for a name in the bottom level dictionary.
Definition: interpret.cc:343
static DatatypeFunction datatypefunction
Definition: interpret.h:222
static SLIType Dictionarytype
Definition: interpret.h:209
Name isetcallback_name
Definition: interpret.h:112
~SLIInterpreter()
Definition: interpret.cc:583
int get_call_depth() const
Return current call depth level.
Definition: interpret.h:394
Name istopped_name
Definition: interpret.h:135
Name stop_name
Definition: interpret.h:127
void stack_backtrace(int n)
List the execution stack from level n-1 downwards to level 0.
Definition: interpret.cc:980
bool get_debug_mode() const
Returns true, if debug mode is turned on.
Definition: interpret.h:438
static FunctiontypeFunction functiontypefunction
Definition: interpret.h:226
bool cycle_guard
Definition: interpret.h:86
FunctionDatum * Iiterate(void) const
Definition: interpret.cc:256
DictionaryStack * DStack
Definition: interpret.h:105
static char const *const M_INFO_NAME
Definition: interpret.h:189
char debug_commandline(Token &)
Prompt user for commands during debug mode.
Definition: interpret.cc:1038
Name CycleGuardError
Definition: interpret.h:161
Name iforallarray_name
Definition: interpret.h:117
Name KernelError
Definition: interpret.h:164
Name newerror_name
Definition: interpret.h:140
void createcommand(Name const &, SLIFunction const *)
Define a function in the current dictionary.
Definition: interpret.cc:274
Definition: interpret.h:69
bool show_backtrace() const
True, if a stack backtrace should be shown on error.
Definition: interpret.h:531
const Token & lookup2(const Name &n) const
Lookup a name searching all dictionaries on the stack.
Definition: interpret.cc:328
void assert_stack_load(size_t n)
Throw StackUnderflow exception if too few elements on stack.
Definition: interpret.h:878
Main administrative interface to the network.
Definition: network.h:135
int execute_debug_(size_t exitlevel=0)
Definition: interpret.cc:1250
Definition: slitype.h:49
void raiseerror(const char *err)
Cause the SLI interpreter to raise an error.
Definition: interpret.h:620
Definition: dictstack.h:70
static TrietypeFunction trietypefunction
Definition: interpret.h:227
static SLIType IntVectortype
Definition: interpret.h:217
Parser * parse
Definition: interpret.h:106
Definition: slibuiltins.h:68
static char const *const M_DEBUG_NAME
Definition: interpret.h:187
Definition: functiondatum.h:42
Name userdict_name
Definition: interpret.h:137
static SLIType Booltype
Definition: interpret.h:204
static char const *const M_ERROR_NAME
Definition: interpret.h:191
int execute_(size_t exitlevel=0)
Run the interpreter with a prepared execution stack.
Definition: interpret.cc:1306
void print_error(Token cmd)
Print a description of a raised error.
Definition: interpret.cc:687
static SLIType Symboltype
Definition: interpret.h:210
static const int M_QUIET
An error level above all others. Use to turn off messages completely.
Definition: interpret.h:182
Definition: slibuiltins.h:60
void initbuiltins(void)
Definition: interpret.cc:205
bool catch_errors() const
Definition: interpret.h:564
static SLIType XIstreamtype
Definition: interpret.h:215
unsigned long cycles(void) const
Definition: interpret.h:826
static SLIType Iteratortype
Definition: interpret.h:219
Name BadErrorHandler
Definition: interpret.h:163
Definition: slibuiltins.h:118
static SLIType Stringtype
Definition: interpret.h:202
Name ipop_name
Definition: interpret.h:111
static const IloopFunction iloopfunction
Definition: interpret.h:235
void catch_errors_on()
Definition: interpret.h:569
void basedef(const Name &n, const Token &t)
Bind a Token to a Name in the bottom level dictionary.
Definition: interpret.cc:358
Name commandstring_name
Definition: interpret.h:148
Name iforallindexedarray_name
Definition: interpret.h:119
Name null_name
Definition: interpret.h:131
int get_max_call_depth() const
Return value of maximal call depth level.
Definition: interpret.h:420
const Token & baselookup(const Name &n) const
Lookup a name searching only the bottom level dictionary.
Definition: interpret.cc:333
static const int M_STATUS
Predefined error level for status messages.
Definition: interpret.h:177
Name iparse_name
Definition: interpret.h:126
Name errordict_name
Definition: interpret.h:138
Name iforalliter_name
Definition: interpret.h:118
Base class for dynamically loadable SLI interpreter modules.
Definition: dynmodule.h:39
void SLIthrowsignal(int s)
Name StringStreamExpectedError
Definition: interpret.h:160
void def(Name const &, Token const &)
Bind a Token to a Name.
Definition: interpret.cc:348
Definition: slibuiltins.h:77
void catch_errors_off()
Definition: interpret.h:574
Name iloop_name
Definition: interpret.h:114
static const IforFunction iforfunction
Definition: interpret.h:237
Name irepeat_name
Definition: interpret.h:115
Base class for all SLI Interpreter modules.
Definition: slimodule.h:34
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
static SLIType Proceduretype
Definition: interpret.h:207
Definition: booldatum.h:35
Definition: slibuiltins.h:93
int call_depth_
Current depth of procedure calls.
Definition: interpret.h:81
Index load(void) const
Definition: tokenstack.h:156
Definition: slibuiltins.h:85
static const IforalliterFunction iforalliterfunction
Definition: interpret.h:239
static LitproceduretypeFunction litproceduretypefunction
Definition: interpret.h:225
static SLIType Nametype
Definition: interpret.h:203
unsigned long code_executed
Definition: interpret.h:99
Name WriteProtectedError
Definition: interpret.h:155
bool optimize_tailrecursion() const
Returns true, if tailing recursion optimization is done.
Definition: interpret.h:483
Name commandname_name
Definition: interpret.h:142
bool catch_errors_
Enter debugger on error.
Definition: interpret.h:79
void debug_mode_off()
Turn debug mode off.
Definition: interpret.h:454
int execute(const std::string &)
Execute the supplied command string.
Definition: interpret.cc:1212
static const int M_ERROR
Predefined error level for error messages.
Definition: interpret.h:180
Definition: sliactions.h:45
Definition: sliactions.h:31
void createconstant(Name const &, const Token &)
Define a constant in the current dictionary.
Definition: interpret.cc:292
void removecycleguard(void)
Definition: interpret.cc:947
A type-independent container for C++-types.
Definition: token.h:68
static SLIType Ostreamtype
Definition: interpret.h:216
static const IlookupFunction ilookupfunction
Definition: interpret.h:232
Name quitbyerror_name
Definition: interpret.h:139
static SLIType Istreamtype
Definition: interpret.h:214
Name true_name
Definition: interpret.h:132
static SLIType Functiontype
Definition: interpret.h:211
const Token & lookup(const Name &n) const
Lookup a name searching all dictionaries on the stack.
Definition: interpret.cc:323
void debug_mode_on()
Turn debug mode on.
Definition: interpret.h:446
static SLIType Trietype
Definition: interpret.h:212
void basedef_move(const Name &n, Token &t)
Bind a Token to a Name in the bottom level dictionary.
Definition: interpret.cc:368
static XIstreamtypeFunction xistreamtypefunction
Definition: interpret.h:229
Name RangeCheckError
Definition: interpret.h:157
Name PositiveIntegerExpectedError
Definition: interpret.h:158
static ProceduretypeFunction proceduretypefunction
Definition: interpret.h:224
Name SystemSignal
Definition: interpret.h:162
void backtrace_on()
Switch stack backtrace on.
Definition: interpret.cc:960
static const IrepeatFunction irepeatfunction
Definition: interpret.h:236
Name iforallindexedstring_name
Definition: interpret.h:120
Definition: slibuiltins.h:45
Token ct
Definition: interpret.h:247
static char const *const M_QUIET_NAME
Definition: interpret.h:193
Definition: parser.h:37
Name UndefinedNameError
Definition: interpret.h:154
Name false_name
Definition: interpret.h:133
Name InternalKernelError
Definition: interpret.h:165
Definition: sliactions.h:52
static const int M_DEBUG
Predefined error level for debugging messages.
Definition: interpret.h:176
static SLIType Doubletype
Definition: interpret.h:201
Name systemdict_name
Definition: interpret.h:136
Dictionary * statusdict
Definition: interpret.h:102
Name recordstacks_name
Definition: interpret.h:144
static char const *const M_FATAL_NAME
Definition: interpret.h:192
unsigned long cycle_count
Definition: interpret.h:85