-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsgsc.c
216 lines (195 loc) · 4.28 KB
/
sgsc.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include "sgscript.h"
#define EPFX "SGSC Error: "
#define EPRINT( x ) printf( EPFX x "\n" )
int action = 0;
const char* infile = NULL;
const char* outfile = NULL;
const char* outext = ".sgc";
void print_help()
{
printf( "Available options:\n"
" -h\t- print help info\n"
" -c\t- compile a file\n"
" -d\t- dump bytecode to STDOUT\n"
" -o\t- set compiled output file name (optional)\n"
"note: either -c or -d must be specified\n"
"example usage:\n"
"> sgsc -c script.sgs -o compiled.sgc\n"
"> sgsc -d compiled.sgc\n"
"\n" );
}
int loadfile( const char* file, char** out, size_t* outsize )
{
char* data;
size_t len;
long ftrv;
FILE* f;
f = fopen( file, "rb" );
if( !f )
return SGS_ENOTFND;
fseek( f, 0, SEEK_END );
ftrv = ftell( f );
if( ftrv < 0 )
{
fclose( f );
return SGS_EINPROC;
}
len = (size_t) ftrv;
fseek( f, 0, SEEK_SET );
data = (char*) malloc( len );
if( fread( data, 1, len, f ) != len )
{
fclose( f );
free( data );
return SGS_EINPROC;
}
fclose( f );
*out = data;
*outsize = len;
return SGS_SUCCESS;
}
int main( int argc, char** argv )
{
int i;
printf( "SGSC [SGScript v%s]\n", SGS_VERSION );
/* parse */
for( i = 1; i < argc; ++i )
{
if( 0 == strcmp( argv[ i ], "-h" ) )
{
print_help();
return 0;
}
else if( 0 == strcmp( argv[ i ], "-c" ) )
action = 1;
else if( 0 == strcmp( argv[ i ], "-d" ) )
action = 2;
else if( 0 == strcmp( argv[ i ], "-o" ) )
{
i++;
if( i >= argc )
{
EPRINT( "expected file name after -o" );
return 1;
}
outfile = argv[ i ];
}
else if( argv[ i ][ 0 ] == '-' )
{
EPRINT( "unrecognized option" );
print_help();
return 1;
}
else if( infile )
{
EPRINT( "can only process one file at a time" );
return 1;
}
else
infile = argv[ i ];
}
/* validate */
if( !action )
{
EPRINT( "action (-c or -d) not specified" );
print_help();
return 1;
}
else if( !infile )
{
EPRINT( "no input file" );
print_help();
return 1;
}
/* do */
{
int ret;
FILE* f;
char of[ 270 ];
size_t size;
char* data = NULL;
sgs_Context* C = sgs_CreateEngine(); /* RSRC: sgs_CreateEngine -> C */
ret = loadfile( infile, &data, &size );
if( ret != SGS_SUCCESS )
{
printf( EPFX "failed to read the file, error %d\n", ret );
sgs_DestroyEngine( C );
return ret;
}
/* RSRC: loadfile -> data */
if( action == 1 )
{
char* data2 = NULL;
size_t size2;
ret = sgs_Compile( C, data, size, &data2, &size2 );
free( data );
/* FREE: loadfile */
if( ret != SGS_SUCCESS )
{
printf( EPFX "failed to compile, error %d\n", ret );
sgs_DestroyEngine( C );
return ret;
}
/* RSRC: sgs_Compile -> data2 */
if( outfile )
strncpy( of, outfile, 260 );
else
{
char* sp, *pp = NULL;
strncpy( of, infile, 260 );
sp = strstr( of, ".sgs" );
while( sp )
{
pp = sp;
sp = strstr( sp + 1, ".sgs" );
}
if( pp - of + 4 == (ptrdiff_t) strlen( of ) )
memcpy( pp, ".sgc", 4 );
else
strcat( of, ".sgc" );
}
f = fopen( of, "wb" );
if( !f )
{
sgs_Free( C, data2 );
sgs_DestroyEngine( C );
printf( EPFX "failed to open output file for writing\n" );
return errno;
}
/* RSRC: fopen -> f */
if( fwrite( data2, size2, 1, f ) < 1 )
{
fclose( f );
sgs_Free( C, data2 );
sgs_DestroyEngine( C );
printf( EPFX "failed to write to '%s'\n", of );
return errno;
}
sgs_Free( C, data2 ); /* FREE: sgs_Compile */
fclose( f ); /* FREE: fopen */
printf( "successfully wrote bytecode to '%s'\n", of );
}
else if( action == 2 )
{
/* MAINTAIN
- RSRC: loadfile -> data
- RSRC: sgs_CreateEngine -> C
*/
ret = sgs_DumpCompiled( C, data, size );
free( data );
/* FREE: loadfile */
if( ret != SGS_SUCCESS )
{
printf( EPFX "failed to dump input file, error %d\n", ret );
sgs_DestroyEngine( C );
return ret;
}
}
sgs_DestroyEngine( C );
}
return 0;
}