add runvisitor hierarchy
[scilab.git] / scilab / modules / core / src / cpp / scilab.cpp
1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2006-2008 - DIGITEO - Bruno JOFRET
4 *
5 *  This file must be used under the terms of the CeCILL.
6 *  This source file is licensed as described in the file COPYING, which
7 *  you should have received as part of this distribution.  The terms
8 *  are also available at
9 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12
13 #pragma comment(lib,"../../bin/libintl.lib")
14
15 #include <cstdio>
16 #include <iostream>
17 #include <string.h>
18
19 extern "C"
20 {
21 #ifndef _MSC_VER
22 #include <unistd.h>
23 #endif
24
25         //#include "SetScilabEnvironment.h"
26 #include "prompt.h"
27 #include "localization.h"
28 #include "InitializeLocalization.h"
29 #include "MALLOC.h"
30 #include "setgetSCIpath.h"
31 #include "inisci-c.h"
32 #include "scilabmode.h"
33 #ifdef _MSC_VER
34 #include "../src/c/scilab_windows/getScilabDirectory.h"
35 #endif
36 #include "ConsoleRead.h"
37 #include "../../../console/includes/InitializeConsole.h"
38 #include "../../../jvm/includes/InitializeJVM.h"
39 #include "InitializeCore.h"
40 #include "../../../shell/includes/InitializeShell.h"
41 #include "../../../console/includes/InitializeConsole.h"
42 #include "../../../tclsci/includes/InitializeTclTk.h"
43 #include "../../../localization/includes/InitializeLocalization.h"
44 #include "../../../graphics/includes/graphicModuleLoad.h"
45 #include "../../../jvm/includes/InitializeJVM.h"
46 #ifdef _MSC_VER
47 #include "../../../windows_tools/includes/InitializeWindows_tools.h"
48 #endif
49 #include "../../../gui/includes/InitializeGUI.h"
50 #include "../../../string/includes/InitializeString.h"
51 #include "scilabmode.h"
52 #include "SetScilabEnvironment.h"
53 #include "../../../jvm/includes/loadBackGroundClassPath.h"
54         /*
55         ** HACK HACK HACK
56         */
57         extern char *TermReadAndProcess(void);
58 }
59
60 #include "yaspio.hxx"
61 #include "tasks.hxx"
62 #include "exit_status.hxx"
63 #include "parser.hxx"
64 #include "context.hxx"
65 #include "configvariable.hxx"
66 #include "context.hxx"
67 //#include "setenvvar.hxx"
68 #include "funcmanager.hxx"
69
70 #define INTERACTIVE     -1
71
72 const char*     prog_name;
73 const char*     file_name;
74
75 bool printAst = false;
76 bool execAst = true;
77 bool dumpAst = false;
78 bool dumpStack = false;
79 bool timed = false;
80 bool ASTtimed = false;
81 bool consoleMode = false;
82
83 using symbol::Context;
84 using std::string;
85
86 void Add_i(void);
87 void Add_pi(void);
88 void Add_s(void);
89 void Add_z(void);
90 void Add_true(void);
91 void Add_false(void);
92 void Add_All_Variables(void);
93
94 void Add_Double_Constant(string _szName, double _dblReal, double _dblImg, bool _bComplex);
95 void Add_Poly_Constant(string _szName, string _szPolyVar, int _iRank, Double *_pdblReal);
96 void Add_Boolean_Constant(string _szName, bool _bBool);
97
98 int InitializeEnvironnement(void);
99 bool execScilabStart(void);
100
101 /*
102 ** Usage
103 **
104 ** Display usage : options available in YaSp
105 */
106 static void usage (void)
107 {
108         std::cerr << "Usage: "<< prog_name << " [--parse-trace] [--display-tree] [--exec] [-f file] | --help" << std::endl;
109         std::cerr << "--parse-trace : Display bison state machine evolution." << std::endl;
110         std::cerr << "--display-tree : Display Syntax tree formated as understood scilab code." << std::endl;
111         std::cerr << "--context-dump : Display what is stored in scilab at the end." << std::endl;
112         std::cerr << "--timed : Enable timer." << std::endl;
113         std::cerr << "--AST-timed : Enable AST timer." << std::endl;
114         std::cerr << "--no-exec : Do not run the scilab code." << std::endl;
115         std::cerr << "--debug : Print the AST nodes." << std::endl;
116         std::cerr << "-f file : Batch mode on the given file." << std::endl;
117         std::cerr << "-l lang : Change the language of scilab ( default : en_US )" << std::endl;
118         std::cerr << "-nw : Enable console mode" << std::endl;
119         std::cerr << "-nwni : Enable console mode" << std::endl;
120         std::cerr << "--help : Display this help." << std::endl;
121 }
122
123
124 /*
125 ** Get Options
126 **
127 **
128 */
129 static int      get_option (const int argc, char *argv[], int *_piFileIndex, int *_piLangIndex)
130 {
131         int     i = 0;
132
133 #ifdef DEBUG
134         std::cerr << "-*- Getting Options -*-"<< std::endl;
135 #endif
136
137         for (i = 1; i < argc; ++i) {
138                 if (!strcmp("--parse-trace", argv[i])) {
139                         Parser::getInstance()->enableParseTrace();
140                 }
141                 else if (!strcmp("--display-tree", argv[i])) {
142                         printAst = true;
143                 }
144                 else if (!strcmp("--help", argv[i])) {
145                         usage ();
146                         exit (WELL_DONE);
147                 }
148                 else if (!strcmp("--debug", argv[i])) {
149                         dumpAst = true;
150                 }
151                 else if (!strcmp("--no-exec", argv[i])) {
152                         execAst = false;
153                 }
154                 else if (!strcmp("--context-dump", argv[i])) {
155                         dumpStack = true;
156                 }
157                 else if (!strcmp("--timed", argv[i])) {
158                         timed = true;
159                 }
160                 else if (!strcmp("--AST-timed", argv[i])) {
161                         ASTtimed = true;
162                 }
163                 else if (!strcmp("-f", argv[i])) {
164                         i++;
165                         *_piFileIndex = i;
166                 }
167                 else if (!strcmp("-l", argv[i])) {
168                         i++;
169                         *_piLangIndex = i;
170                 }
171                 else if (!strcmp("-nw", argv[i]) || !strcmp("-nwni", argv[i])) {
172                         consoleMode = true;
173                         setScilabMode(SCILAB_NWNI);
174                 }
175         }
176
177 #ifdef DEBUG
178         std::cerr << "File : " << argv[good] << std::endl;
179 #endif
180
181         return 0;
182 }
183
184 /*
185 ** HACK HACK HACK
186 */
187
188 extern "C"
189 {
190 #include <stdio.h>
191
192 #ifndef _MSC_VER
193 #include <unistd.h>
194 #endif
195 #include "scilabmode.h"
196         extern char *TermReadAndProcess(void);
197         extern void ConsolePrintf(char*);
198 }
199
200
201 /*
202 ** -*- Batch Main -*-
203 */
204 static int batchMain (void)
205 {
206         /*
207         ** -*- PARSING -*-
208         */
209         Parser::ParserStatus parseResult = parseFileTask(timed, file_name, prog_name);
210
211         /*
212         ** -*- DUMPING TREE -*-
213         */
214         if (dumpAst == true) { dumpAstTask(timed); }
215
216         if (parseResult != Parser::Succeded)
217         {
218                 YaspWrite(Parser::getInstance()->getErrorMessage());
219                 return PARSE_ERROR;
220         }
221
222         /*
223         ** -*- PRETTY PRINT TREE -*-
224         */
225         if (printAst == true) { printAstTask(timed); }
226
227         /*
228         ** -*- EXECUTING TREE -*-
229         */
230         if (execAst == true) { execAstTask(timed, ASTtimed); }
231
232         /*
233         ** -*- DUMPING STACK AFTER EXECUTION -*-
234         */
235         if (dumpStack == true) { dumpStackTask(timed); }
236
237 #ifdef DEBUG
238         std::cerr << "To end program press [ENTER]" << std::endl;
239 #endif
240
241         return WELL_DONE;
242 }
243
244 static void banner()
245 {
246 #define SCI_VERSION_STRING "scilab-branch-YaSp"
247         int i;
248         char *line = "        ___________________________________________        ";
249         int startVersion = (int)(floor((double)(strlen(line)/2)) - floor((double)(strlen(SCI_VERSION_STRING)/2)));
250
251         YaspWrite(line);
252         YaspWrite("\n");
253
254         /* To center the version name */
255         for( i=0 ; i<startVersion ; i++ )
256         {
257                 YaspWrite(" ");
258         }
259
260         YaspWrite(SCI_VERSION_STRING);
261         YaspWrite("\n\n");
262
263         YaspWrite(_("                 Consortium Scilab (DIGITEO)\n"));
264
265         YaspWrite(_("               Copyright (c) 1989-2009 (INRIA)\n"));
266         YaspWrite(_("               Copyright (c) 1989-2007 (ENPC)\n"));
267         YaspWrite(line);
268         YaspWrite("\n");
269         YaspWrite("\n");
270         YaspWrite("           -*- THIS IS SCILAB 6.0 aka YaSp -*-\n");
271         YaspWrite("\n");
272         YaspWrite(line);
273         YaspWrite("\n");
274 }
275
276 /*
277 ** -*- stateView
278 ** Used to show parser state.
279 ** Find if we are stuck within some control structure.
280 */
281
282 static void stateShow(Parser::ControlStatus status)
283 {
284         switch (status)
285         {
286         case Parser::WithinFor :                        SetTemporaryPrompt("-for      ->"); break;
287         case Parser::WithinWhile :              SetTemporaryPrompt("-while    ->"); break;
288         case Parser::WithinIf :                         SetTemporaryPrompt("-if       ->"); break;
289         case Parser::WithinElse :                       SetTemporaryPrompt("-else     ->"); break;
290         case Parser::WithinElseIf :             SetTemporaryPrompt("-elseif   ->"); break;
291         case Parser::WithinTry :                        SetTemporaryPrompt("-try      ->"); break;
292         case Parser::WithinCatch :              SetTemporaryPrompt("-catch    ->"); break;
293         case Parser::WithinFunction : SetTemporaryPrompt("-function ->"); break;
294         case Parser::WithinSelect :             SetTemporaryPrompt("-select   ->"); break;
295         case Parser::WithinCase :                       SetTemporaryPrompt("-case     ->"); break;
296         case Parser::AllControlClosed : break;
297         }
298 }
299
300 /*
301 ** -*- Interactive Main -*-
302 */
303 static int interactiveMain (void)
304 {
305         Parser::ParserStatus parseResult;
306         bool exit = false;
307         int pause = 0;
308         char *command = NULL;
309         Parser* pParser = Parser::getInstance();
310
311         banner();
312
313         while (!exit)
314         {
315           // Show Parser Sate before prompt
316           stateShow(pParser->getControlStatus());
317
318                 //set prompt value
319                 C2F(setprlev)(&pause);
320
321                 if (pParser->getControlStatus() == Parser::AllControlClosed) 
322                 {
323                         if(command)
324                         {
325                                 FREE(command);
326                                 command = NULL;
327                         }
328                         command = YaspRead();
329                 }
330                 else
331                 {
332                         char* pstRead = YaspRead();
333                         //+1 for null termination and +1 for '\n'
334                         size_t iLen = strlen(command) + strlen(pstRead) + 2;
335                         char* pstNewCommand = (char*)MALLOC(iLen * sizeof(char));
336 #ifdef _MSC_VER
337                         sprintf_s(pstNewCommand, iLen, "%s\n%s", command, pstRead);
338 #else
339                         sprintf(pstNewCommand, "%s\n%s", command, pstRead);
340 #endif
341                         FREE(pstRead);
342                         FREE(command);
343                         command = pstNewCommand;
344                 }
345
346                 //std::cout << "---" << std::endl << "Command = " << std::endl;
347                 //std::cout << command << std::endl;
348                 //std::cout << "---" << std::endl;
349                 if (strcmp(command, "quit") == 0 || strcmp(command, "exit") == 0)
350                 {
351                         exit = true;
352                         return 0;
353                 }
354
355                 if (strcmp(command, "") != 0)
356                 {
357                         /*
358                         ** -*- PARSING -*-
359                         */
360                         parseResult = parseCommandTask(timed, command);
361
362                         /*
363                         ** -*- DUMPING TREE -*-
364                         */
365                         if (dumpAst == true) { dumpAstTask(timed); }
366
367                         if (parseResult == Parser::Succeded)
368                         {
369                                 /*
370                                 ** -*- PRETTY PRINT TREE -*-
371                                 */
372                                 if (printAst == true) { printAstTask(timed); }
373
374                                 /*
375                                 ** -*- EXECUTING TREE -*-
376                                 */
377                                 if (execAst == true) { execAstTask(timed, ASTtimed); }
378
379                                 /*
380                                 ** -*- DUMPING STACK AFTER EXECUTION -*-
381                                 */
382                                 if (dumpStack == true) { dumpStackTask(timed); }
383                         }
384                         else if(parseResult == Parser::Failed && pParser->getControlStatus() == Parser::AllControlClosed)
385                         {
386                                 YaspWrite(pParser->getErrorMessage());
387                                 //std::cerr << "Parser control : " << pParser->getControlStatus() << std::endl;
388                         }
389                 }
390         }
391 #ifdef DEBUG
392         std::cerr << "To end program press [ENTER]" << std::endl;
393 #endif
394         return WELL_DONE;
395 }
396
397 static void TermPrintf(char *text)
398 {
399         std::cout << text;
400 }
401
402 /*
403 ** -*- MAIN -*-
404 */
405 int main(int argc, char *argv[])
406 {
407         int iFileIndex = INTERACTIVE;
408         int iLangIndex = 0;
409         int iMainRet = 0;
410         prog_name = argv[0];
411
412         setScilabMode(SCILAB_STD);
413         Parser::getInstance()->disableParseTrace();
414         get_option(argc, argv, &iFileIndex, &iLangIndex);
415
416         if (consoleMode)
417         {
418                 setYaspInputMethod(&TermReadAndProcess);
419                 setYaspOutputMethod(&TermPrintf);
420         }
421         else
422         {
423                 setYaspInputMethod(&ConsoleRead);
424                 setYaspOutputMethod(&ConsolePrintf);
425         }
426
427         /* Scilab Startup */
428         InitializeEnvironnement();
429
430         InitializeString();
431
432 #ifdef _MSC_VER
433         InitializeWindows_tools();
434 #endif
435
436         InitializeCore();
437
438         InitializeShell();
439
440         if ( 
441                 !consoleMode ) 
442         {
443                 /* bug 3702 */
444                 /* tclsci creates a TK window on Windows */
445                 /* it changes focus on previous windows */
446                 /* we put InitializeTclTk before InitializeGUI */
447
448                 //InitializeTclTk();
449                 InitializeJVM();
450                 InitializeGUI();
451
452                 /* create needed data structure if not already created */
453                 loadGraphicModule() ;
454
455                 /* Standard mode -> init Java Console */
456                 //if ( !consoleMode ) 
457                 {
458                         /* Initialize console: lines... */
459                         InitializeConsole();
460                 }
461
462                 loadBackGroundClassPath();
463         }
464
465         /* set current language of scilab */
466         FuncManager *pFM = new FuncManager();
467
468         //execute scilab.start
469         //execScilabStartTask();
470
471
472
473         if (iFileIndex == INTERACTIVE)
474         {
475                 file_name = "prompt";
476                 iMainRet = interactiveMain();
477         }
478         else
479         {
480                 file_name = argv[iFileIndex];
481                 iMainRet = batchMain();
482         }
483
484         //close main scope
485         symbol::Context::getInstance()->scope_end();
486         delete pFM;
487         return iMainRet;
488 }
489
490 int InitializeEnvironnement(void)
491 {
492         char *ScilabDirectory = NULL;
493 #ifdef _MSC_VER
494         ScilabDirectory = getScilabDirectory(FALSE);
495 #else
496         SetSci();
497         ScilabDirectory = getSCIpath();
498 #endif
499         if(ScilabDirectory)
500         {
501                 ConfigVariable::getInstance()->set("SCI", ScilabDirectory);
502                 FREE(ScilabDirectory);
503         }
504         SetScilabEnvironment();
505         InitializeLocalization();
506         Add_All_Variables();
507         return 0;
508 }
509
510 void Add_All_Variables(void)
511 {
512         Add_pi();
513         Add_i();
514         Add_s();
515         Add_z();
516         Add_true();
517         Add_false();
518 }
519
520 void Add_false(void)
521 {
522         Add_Boolean_Constant("%f", false);
523 }
524
525 void Add_true(void)
526 {
527         Add_Boolean_Constant("%t", true);
528 }
529
530 void Add_pi(void)
531 {
532         Add_Double_Constant("%pi", 3.1415926535897931159980, 0, false);
533 }
534
535 void Add_i(void)
536 {
537         Add_Double_Constant("%i", 0, 1, true);
538 }
539
540 void Add_s(void)
541 {
542         Double dblCoef(1,2);
543         dblCoef.val_set(0, 0, 0);
544         dblCoef.val_set(0, 1, 1);
545
546         Add_Poly_Constant("%s","s", 2, &dblCoef);
547 }
548
549 void Add_z(void)
550 {
551         Double dblCoef(1,2);
552         dblCoef.val_set(0, 0, 0);
553         dblCoef.val_set(0, 1, 1);
554
555         Add_Poly_Constant("%z","z", 2, &dblCoef);
556 }
557
558 void Add_Poly_Constant(string _szName, string _szPolyVar, int _iRank, Double *_pdbl)
559 {
560         types::MatrixPoly *pVar = new types::MatrixPoly(_szPolyVar, 1, 1, &_iRank);
561         Poly *poPoly = pVar->poly_get(0,0);
562         poPoly->coef_set(_pdbl);
563         Context::getInstance()->put(_szName, *pVar);
564 }
565
566 void Add_Double_Constant(string _szName, double _dblReal, double _dblImg, bool _bComplex)
567 {
568         types::Double* pVal = new types::Double(1,1,_bComplex);
569         pVal->val_set(0,0,_dblReal,_dblImg);
570         symbol::Context::getInstance()->put(_szName, *pVal);
571 }
572
573 void Add_Boolean_Constant(string _szName, bool _bBool)
574 {
575         types::Bool* pVal = new types::Bool(_bBool);
576         symbol::Context::getInstance()->put(_szName, *pVal);
577 }