-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXScript.c
More file actions
319 lines (288 loc) · 5.61 KB
/
Copy pathXScript.c
File metadata and controls
319 lines (288 loc) · 5.61 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#include <strings.h>
#include "types.h"
#include "xscript.h"
#define MAX_STRING_SIZE 1024
extern char message[255];
TToken *tokenList[MAX_TOKENS];
int lookPos;
TToken *look;
int main(int argc, char *argv[])
{
char *source = NULL;
if (argc < 2) {
printf("You must especify a script file!");
return 0;
}
if ((source = loadSource(argv[1]))== NULL)
{
printf("Error reading %s", argv[1]);
return -1;
}
printf("\nSource loaded..");
if (tokenize(tokenList, source))
{
showTokens(tokenList);
printf("\nXScript->Running %s", argv[1]);
run();
printf("\nXSCript->Finished");
} else
{
printf("Error: %-30.30s", message);
}
printf("\nXScript->freeing token memory...");
freeTokens(tokenList);
// Libera memoria gasta pelo codigo fonte
//------------------------------------------------
printf("\nXScript->freeing source memory...");
free(source);
return 0;
}
int run()
{
lookPos = 0;
next();
while (look != NULL)
{
switch(look->tokenType)
{
case TK_COMMAND:
runCommands();
break;
default:
showError("Expected command in line %d col %d", look->sourceLine, look->sourceCol);
break;
}
}
return true;
}
int runCommands()
{
switch(look->tokenCode)
{
case TK_PRINT:
runPrint();
break;
default:
showError("Unexpected command:%s in line %d col %d", look->tokenSource, look->sourceLine, look->sourceCol);
}
}
int runPrint()
{
next();
if (look->tokenType == TK_STRING || look->tokenType == TK_STRFUNC)
puts(strExpression());
else
if (look->tokenType == TK_NUMBER || look->tokenType == TK_NUMFUNC || look->tokenType == TK_OPERATOR)
printf("%10.2f\n", numExpression());
else
showError("Print: String or numeric expression expected in line %d col %d", look->sourceLine, look->sourceCol);
}
double numExpression()
{
double result = getFactor();
//printf("%f", result);
while (look != NULL)
{
switch(look->tokenSource[0]){
case '+':
next();
result = result + getFactor();
continue;
case '-':
next();
result = result - getFactor();
continue;
default:
break;
}
break;
}
return result;
}
double getFactor()
{
double result = getNum();
while (look != NULL){
switch(look->tokenSource[0]) {
case '*':
next();
result = result * getNum();
continue;
case '/':
next();
result = result / getNum();
continue;
default:
break;
}
break;
}
return result;
}
double getNum()
{
double result = 0;
if (look->tokenSource[0] == '(')
{
matchChar('(');
result = result + numExpression();
matchChar(')');
return result;
}
switch(look->tokenType) {
case TK_NUMBER:
result = look->tokenValue;
next();
break;
case TK_NUMFUNC:
result = result + getNumFunc();
break;
default:
showError("Number or numeric function expected in line %d col %d", look->sourceLine, look->sourceCol);
break;
}
return result;
}
double getNumFunc()
{
double result = 0;
double n1, n2;
switch(look->tokenCode) {
case TK_SIN:
next();
matchChar('(');
result = result + sin(M_PI * getNum() / 180);
matchChar(')');
break;
case TK_ASIN:
next();
matchChar('(');
result = result + asin(getNum());
matchChar(')');
break;
case TK_COS:
next();
matchChar('(');
result = result + cos(M_PI * getNum() / 180);
matchChar(')');
break;
case TK_ACOS:
next();
matchChar('(');
result = result + 180 * acos(getNum()) / M_PI;
matchChar(')');
break;
case TK_TAN:
next();
matchChar('(');
result = result + tan(M_PI * getNum() / 180);
matchChar(')');
break;
case TK_ATAN:
next();
matchChar('(');
result = result + 180 * atan(getNum()) / M_PI;
matchChar(')');
break;
case TK_POW:
next();
matchChar('(');
n1 = getNum();
matchChar(',');
n2 = getNum();
matchChar(')');
result = result + pow(n1, n2);
break;
default:
showError("Numeric function %s unimplemented in line %d col %d", look->tokenSource,
look->sourceLine, look->sourceCol);
break;
}
return result;
}
char *strExpression()
{
char *buffer;
buffer = malloc(512);
memset(buffer, 0, 512);
strcat(buffer, getStr());
while (look != NULL)
{
switch(look->tokenType) {
case TK_OPERATOR:
if (look->tokenSource[0] == '+')
{
next();
strcat(buffer, getStr());
}
break;
default:
break;
}
if (look->tokenSource[0] != '+') break;
}
return buffer;
}
char *getStr()
{
char *buffer = NULL;
switch(look->tokenType) {
case TK_STRING:
buffer = malloc(strlen(look->tokenSource) + 1);
strcpy(buffer, look->tokenSource);
next();
break;
default:
showError("String or String function expected in line %d col %d", look->sourceLine, look->sourceCol);
break;
}
return buffer;
}
int next()
{
look = tokenList[lookPos++];
}
int matchChar(char c)
{
if (look == NULL)
{
showError("Unexpected end of script");
}
if (look->tokenSource[0] != c)
{
showError("Expected %c found %c in line %d col %d", c, look->tokenSource[0], look->sourceLine, look->sourceCol);
}
next();
}
char *loadSource(char *file)
{
FILE *f = fopen(file, "rb");
char *source;
if (f == NULL)
{
return NULL;
}
// Verifica o tamanho
fseek(f, 0L, SEEK_END);
int size = ftell(f);
// Volta ao inicio do arquivo
fseek(f, 0L, SEEK_SET);
source = (char *) malloc(size + 1);
fread(source, size, 1, f);
fclose(f);
source[size] = '\0';
return source;
}
void showError(const char *error, ...)
{
va_list args;
va_start(args, error);
vprintf(error, args);
va_end(args);
exit(-1);
}