interpreter.h
1 //interpreter.h
2 //----------------------------------------------------------------------
3 /*
4  * Fco. Javier Bohórquez Ogalla
5  * 75766599-E
6  * Puerto Real (Cádiz)
7  * powersgame@gmail.com
8  */
9 //----------------------------------------------------------------------
10 /*
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24  * MA 02110-1301, USA.
25  */
26 //----------------------------------------------------------------------
27 
28 //----------------------------------------------------------------------
29 #ifndef _INTERPRETER_
30 #define _INTERPRETER_
31 #include <iostream>
32 #include <string>
33 #include <fstream>
34 #include <vector>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <getopt.h>
38 #include <readline/readline.h>
39 #include <readline/history.h>
40 #include "run/tree/runTree.h"
41 #include "run/table/sTable.h"
42 
43 #define INTERPRETER_NAME "omi"
44 #define INTERPRETER_DESCRIPTION "Open Modular Interpreter"
45 #define INTERPRETER_VERSION "0.1"
46 #define INTERPRETER_LICENSE "GNU General Public License version 2"
47 #define HISTORY_FILE string(getenv("HOME")) + "/." + string(INTERPRETER_NAME) + "_history"
48 #define PS1 INTERPRETER_NAME + string(" |> ")
49 #define PS2 INTERPRETER_NAME + string(" {> ")
50 #define PS2_BRACKET INTERPRETER_NAME + string(" (> ")
51 
52 using namespace std;
53 
54 extern bool inline_param;
55 extern string infile;
56 //----------------------------------------------------------------------
57 
58 //----------------------------------------------------------------------
68 class interpreter {
69  public:
74  void run ();
79  void scan_string(const char* str);
88  static string omi (string str, vector<string>args);
96  static interpreter *init (int argc, char *argv[]) {
97  if (!interpreter_)
98  interpreter_ = new interpreter (argc, argv);
99  return interpreter_;
100  }
105  static interpreter *get () {
106  return interpreter_;
107  }
108 #if JSON==1
109  bool json;
110  string json_file;
111  ofstream json_stream;
112  bool json_init;
113  string step_content;
114  string step_extra;
115  unsigned int steps;
116  unsigned int steps_count;
117  static void jsonInit();
118  static void to_json(string value, string end = "");
119  static void to_jsonRun (runNode * r, string msg = "");
120  static void to_jsonRunClass (runNode * r);
121  static void to_jsonEndClass (runNode * r);
122  static void to_jsonSetParent (runNode *r, runNode* val);
123  static void to_jsonSetValue (runNode *r, runNode* val);
124  static void to_jsonSetValue (runNode *r, bool val);
125  static void to_jsonSetValue (runNode *r, string val);
126  static void to_jsonSetValue (runNode *r, num val);
127  static void to_jsonSetRef (runNode *r, runNode* ref, runNode* obj);
128  static void to_jsonReturn (runNode *r, runNode* val);
129  static void to_jsonSleep (runNode *r, int seg);
130  static void to_jsonNewNode (runNode *node, bool implicit = false);
131  static void to_jsonError (string error);
132  static void to_jsonRemoveLevel (unsigned int level);
133  static void to_jsonAccessVar (sTable *r, int level, runNode* str, runNode* ref, runNode*val);
134  static void to_jsonAccessFunction (sTable *r, runNode* str, runNode* ref, runNode *val);
135  static void to_jsonAccessClass (sTable *r, runNode* str, runNode* ref, runNode *val);
136  static void to_jsonClone (runNode * elem, runNode * of, string name, string type);
137  static void to_jsonChangeRef (runNode* ref, runNode* val);
138  static void to_jsonChangeValue (runNode* node, string val);
139  static void to_jsonDelete (runNode * elem);
140  static void to_jsonOut (runNode * elem, string out);
141  static void to_jsonRunStatic (runNode * elem);
142  static void to_jsonEndStatic (runNode * elem);
143  static void to_jsonRunPrivate (runNode * elem);
144  static void to_jsonEndPrivate (runNode * elem);
145  static void to_jsonRunGlobal (runNode * elem);
146  static void to_jsonEndGlobal (runNode * elem);
147  static string to_jsonInput (runNode * elem);
148  static void to_jsonSymbols (runNode *elem, runNode* key, runNode * value);
149  static void jsonEnd(string error = "");
150 #endif
151 #if SERVER==1
152  bool server;
153  string port;
154  ofstream *s_out;
155  void serverRun ();
156 #endif
157  private:
158  interpreter (int argc, char *argv[]);
159  void generateClass ();
160  void parseOptions (int argc, char *argv[]);
161  void parseArgs ();
162  void caseScript ();
163  void caseCode ();
164  void caseInline ();
165  void caseStdin ();
166  void printVersion ();
167  void loadModule (string module);
168  void printHelp ();
169  string getLine ();
170  vector<string> args_;
171  string file_;
172  string code_;
173  bool inl_, error_opt_;
174  static interpreter* interpreter_;
175 
176 };
177 #endif
178 //----------------------------------------------------------------------
179 
180 //----------------------------------------------------------------------
181 //Fco. Javier Bohórquez Ogalla
Árbol de ejecución.
Nodo ejecutable.
Definition: runTree.h:123
Contexto de tablas de símbolos.
Definition: sTable.h:121
Tabla de símbolos.
Interprete OMI.
Definition: interpreter.h:68
static interpreter * init(int argc, char *argv[])
Definition: interpreter.h:96