-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsgsvm.c
157 lines (138 loc) · 3.79 KB
/
sgsvm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <assert.h>
#include "sgscript.h"
#include "sgs_dbgserver.h"
#include "sgs_prof.h"
#define EPFX "SGSVM Error: "
#define EPRINT( x ) printf( EPFX x "\n" )
void readme()
{
puts( "syntax:" );
puts( " sgsvm [files|options]" );
puts( " sgsvm [options] -p <file>[, <arguments>]" );
puts( "" );
puts( "options:" );
puts( " -h, --help: print this text" );
puts( " -v, --version: print version info" );
puts( " -s, --separate: restart the engine between scripts" );
puts( " -d, --debug: enable interactive debugging on errors" );
puts( " -dsp, --debug-start-paused: start from debugger before running any code" );
puts( " -p, --program: translate the following arguments into a SGS program call" );
puts( " --stats: print VM stats after running the scripts" );
puts( " --profile: enable profiling by collecting call stack timings" );
puts( " --profile-ops: enable low-level VM instruction profiling" );
puts( " --profile-mem: enable memory usage profiling" );
}
int sep = 0, v = 0;
sgs_Context* C;
sgs_DebugServer* D;
sgs_Prof P;
int idbg = 0;
int idbg_break_on_start = 0;
int prof = 0;
int stats = 0;
void sgs_init()
{
C = sgs_CreateEngine();
if( idbg )
{
D = sgs_CreateDebugServer( C, 0 );
if( idbg_break_on_start )
sgs_DebugServerCmd( D, NULL );
}
if( prof ) sgs_ProfInit( C, &P, prof );
}
void sgs_close()
{
if( idbg ) sgs_CloseDebugServer( D );
if( prof )
{
sgs_ProfDump( C, &P );
sgs_ProfClose( C, &P );
}
if( stats )
sgs_Stat( C, SGS_STAT_DUMP_STATS );
sgs_DestroyEngine( C );
}
void sgs_dofile( const char* name, int incl )
{
int rv = incl ? sgs_Include( C, name ) : sgs_ExecFile( C, name );
if( rv < 0 )
{
if( rv == SGS_ENOTFND ) printf( EPFX "file was not found: %s\n", name );
else if( rv == SGS_EINPROC ) printf( EPFX "failed to load file: %s\n", name );
else printf( EPFX "failed to run file: %s\n", name );
}
}
void sgs_printversion()
{
if( v )
printf( "SGSVM [SGScript v%s]\n", SGS_VERSION );
}
int main( int argc, char** argv )
{
int i, j;
if( argc < 2 )
{
EPRINT( "need to specify at least one file" );
readme();
return 1;
}
for( i = 1; i < argc; ++i )
{
if( strcmp( argv[ i ], "--separate" ) == 0 ||
strcmp( argv[ i ], "-s" ) == 0 ){ sep = 1; argv[ i ] = 0; }
else if( strcmp( argv[ i ], "--debug" ) == 0 ||
strcmp( argv[ i ], "-d" ) == 0 ){ idbg = 1; argv[ i ] = 0; }
else if( strcmp( argv[ i ], "--debug-start-paused" ) == 0 ||
strcmp( argv[ i ], "-dsp" ) == 0 ){ idbg = 1; idbg_break_on_start = 1; argv[ i ] = 0; }
else if( strcmp( argv[ i ], "--profile" ) == 0 )
{ prof = 1; argv[ i ] = 0; }
else if( strcmp( argv[ i ], "--profile-ops" ) == 0 )
{ prof = 2; argv[ i ] = 0; }
else if( strcmp( argv[ i ], "--profile-mem" ) == 0 )
{ prof = 3; argv[ i ] = 0; }
else if( strcmp( argv[ i ], "--help" ) == 0 ||
strcmp( argv[ i ], "-h" ) == 0 ){ readme(); return 0; }
else if( strcmp( argv[ i ], "--version" ) == 0 ||
strcmp( argv[ i ], "-v" ) == 0 ){ v = 1; argv[ i ] = 0; }
else if( strcmp( argv[ i ], "--stats" ) == 0 ){ stats = 1; argv[ i ] = 0; }
else if( strcmp( argv[ i ], "--program" ) == 0 ||
strcmp( argv[ i ], "-p" ) == 0 )
{
i++;
if( i == argc )
{
EPRINT( "file name expected" );
return 1;
}
sgs_printversion();
sgs_init();
for( j = i; j < argc; ++j )
sgs_PushString( C, argv[ j ] );
sgs_CreateArray( C, NULL, argc - i );
sgs_SetGlobalByName( C, "argv", sgs_StackItem( C, -1 ) );
sgs_Pop( C, 1 );
sgs_SetGlobalByName( C, "argc", sgs_MakeInt( argc - i ) );
sgs_dofile( argv[ i ], 1 );
sgs_close();
return 0;
}
}
sgs_printversion();
sgs_init();
for( i = 1; i < argc; ++i )
{
if( argv[ i ] )
{
sgs_dofile( argv[ i ], 0 );
if( sep )
{
sgs_close();
sgs_init();
}
}
}
sgs_close();
return 0;
}