root/trunk/orca/console.c

Revision 297, 3.9 kB (checked in by krobillard, 2 years ago)

Orca - Added /return refinement to 'quit.

Line 
1 /*============================================================================
2     ORCA Interpreter
3     Copyright (C) 2005-2006  Karl Robillard
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library 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 GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 ===========================================================================*/
19
20
21 #include <stdio.h>
22 #include "ovalue.h"
23
24
25 #define ESC     27
26
27
28 /*
29   Returns pointer in 'to' buffer at end of copy.
30 */
31 char* strCopy( char* to, const char* from )
32 {
33     while( *from )
34         *to++ = *from++;
35     return to;
36 }
37
38
39 int main( int argc, char** argv )
40 {
41     OEnv env;
42     char cmd[ 512 ];
43     int ret = 0;
44
45
46     orInitEnv( &env, 0, 0 );
47
48     if( argc > 1 )
49     {
50         char* pos;
51         if( argc > 2 )
52         {
53             int i;
54             pos = strCopy( cmd, "do/args %" );
55             pos = strCopy( pos, argv[1] );
56             *pos++ = ' ';
57             *pos++ = '[';
58             for( i = 2; i < argc; ++i )
59             {
60                 if( i > 2 )
61                     *pos++ = ' ';
62                 *pos++ = '"';
63                 pos = strCopy( pos, argv[i] );
64                 *pos++ = '"';
65             }
66             *pos++ = ']';
67         }
68         else
69         {
70             pos = strCopy( cmd, "do %" );
71             pos = strCopy( pos, argv[1] );
72         }
73
74         orEvalCStr( cmd, pos - cmd );
75
76         if( orErrorThrown )
77         {
78             if( orErrorIsType(OR_ERROR_QUIT) )
79             {
80                 ret = orErrorThrown->error.msg;
81             }
82             else
83             {
84                 if( ! orErrorIsType(OR_ERROR_HALT) )
85                     orPrintNative( orErrorThrown );
86                 orResetEnv( &env );
87                 goto halt;
88                 //ret = -1;
89             }
90         }
91     }
92     else
93     {
94         printf( "ORCA %s (%s)\n", OR_VERSION_STR, __DATE__ );
95 halt:
96         while( 1 )
97         {
98             printf( "O> " );
99             fflush( stdout );   /* Required on Windows. */
100             fgets( cmd, 128, stdin );
101
102 #if 0
103             {
104                 char* cp = cmd;
105                 while( *cp != '\n' )
106                     printf( " %d", (int) *cp++ );
107                 printf( "\n" );
108             }
109 #endif
110
111             if( cmd[0] == ESC )
112             {
113                 // Up   27 91 65
114                 // Down 27 91 66
115                 printf( "\n" );
116             }
117             else if( cmd[0] != '\n' )
118             {
119                 orEvalCStr( cmd, -1 );
120
121                 if( orErrorThrown )
122                 {
123                     if( orErrorIsType(OR_ERROR_QUIT) )
124                     {
125                         ret = orErrorThrown->error.msg;
126                         break;
127                     }
128                     if( ! orErrorIsType(OR_ERROR_HALT) )
129                         orPrintNative( orErrorThrown );
130                     orResetEnv( &env );
131                 }
132                 else
133                 {
134                     OValue* val;
135                     val = orTOS;
136                     if( (val->type != OT_UNSET) &&
137                         (val->type != OT_OBJECT) &&
138                         (val->type != OT_FUNCTION) )
139                     {
140                         printf( "== " );
141                         orProbe( val );
142                     }
143                 }
144             }
145         }
146     }
147
148     orFreeEnv( &env );
149
150     return ret;
151 }
152
153
154 /*EOF*/
Note: See TracBrowser for help on using the browser.