diff options
author | David Walter Seikel | 2012-01-08 00:20:28 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-08 00:20:28 +1000 |
commit | 428e27426d9e2503b475c4e608ee780bbb9e72e6 (patch) | |
tree | 2fed615d391c3c24d42d481df1742fd4a28f9029 /LuaSL/src/LuaSL_LSL_tree.c | |
parent | Forget to save first. lol (diff) | |
download | SledjHamr-428e27426d9e2503b475c4e608ee780bbb9e72e6.zip SledjHamr-428e27426d9e2503b475c4e608ee780bbb9e72e6.tar.gz SledjHamr-428e27426d9e2503b475c4e608ee780bbb9e72e6.tar.bz2 SledjHamr-428e27426d9e2503b475c4e608ee780bbb9e72e6.tar.xz |
Parse an actual file.
Diffstat (limited to '')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 153 |
1 files changed, 109 insertions, 44 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 011339f..c7fc1bb 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c | |||
@@ -322,35 +322,9 @@ int yyerror(const char *msg) | |||
322 | return 0; | 322 | return 0; |
323 | } | 323 | } |
324 | 324 | ||
325 | static LSL_AST *newTree(const char *expr) | 325 | int main(int argc, char **argv) |
326 | { | ||
327 | LuaSL_yyparseParam param; | ||
328 | YY_BUFFER_STATE state; | ||
329 | |||
330 | #ifdef LUASL_DEBUG | ||
331 | yydebug= 5; | ||
332 | #endif | ||
333 | |||
334 | param.ast = NULL; | ||
335 | if (yylex_init(&(param.scanner))) | ||
336 | return NULL; | ||
337 | |||
338 | #ifdef LUASL_DEBUG | ||
339 | yyset_debug(1, param.scanner); | ||
340 | #endif | ||
341 | |||
342 | state = yy_scan_string(expr, param.scanner); | ||
343 | if (yyparse(¶m)) | ||
344 | return NULL; | ||
345 | |||
346 | yy_delete_buffer(state, param.scanner); | ||
347 | yylex_destroy(param.scanner); | ||
348 | |||
349 | return param.ast; | ||
350 | } | ||
351 | |||
352 | int main(void) | ||
353 | { | 326 | { |
327 | char *programName = argv[0]; | ||
354 | int i; | 328 | int i; |
355 | 329 | ||
356 | // Figure out what numbers yacc gave to our tokens. | 330 | // Figure out what numbers yacc gave to our tokens. |
@@ -362,8 +336,12 @@ int main(void) | |||
362 | tokens = calloc(i + 1, sizeof(LSL_Token *)); | 336 | tokens = calloc(i + 1, sizeof(LSL_Token *)); |
363 | if (tokens) | 337 | if (tokens) |
364 | { | 338 | { |
365 | const char test[] = " 4 + 2 * 10 + 3 * ( 5 + 1 )"; | 339 | char buffer[4096]; |
366 | LSL_AST *ast; | 340 | LSL_AST *ast; |
341 | LuaSL_yyparseParam param; | ||
342 | int count; | ||
343 | FILE *file; | ||
344 | boolean badArgs = FALSE; | ||
367 | 345 | ||
368 | // Sort the token table. | 346 | // Sort the token table. |
369 | for (i = 0; LSL_Tokens[i].token != NULL; i++) | 347 | for (i = 0; LSL_Tokens[i].token != NULL; i++) |
@@ -373,26 +351,113 @@ int main(void) | |||
373 | tokens[j] = &(LSL_Tokens[i]); | 351 | tokens[j] = &(LSL_Tokens[i]); |
374 | } | 352 | } |
375 | 353 | ||
376 | // Run the parser on a test. | 354 | buffer[0] = '\0'; |
377 | if ((ast = newTree(test))) | 355 | |
356 | // get the arguments passed in | ||
357 | while (--argc > 0 && *++argv != '\0') | ||
378 | { | 358 | { |
379 | LSL_Value left, right; | 359 | if (*argv[0] == '-') |
360 | { | ||
361 | // point to the characters after the '-' sign | ||
362 | char *s = argv[0] + 1; | ||
363 | |||
364 | switch (*s) | ||
365 | { | ||
366 | case 'f': // file | ||
367 | { | ||
368 | if (--argc > 0 && *++argv != '\0') | ||
369 | { | ||
370 | strncpy(buffer, *argv, 4095); | ||
371 | buffer[4095] = '\0';; | ||
372 | } | ||
373 | else | ||
374 | badArgs = TRUE; | ||
375 | break; | ||
376 | } | ||
377 | default: | ||
378 | badArgs = TRUE; | ||
379 | } | ||
380 | } | ||
381 | else | ||
382 | badArgs = TRUE; | ||
383 | } | ||
380 | 384 | ||
381 | left.content.integerValue = 0; | 385 | if (badArgs) |
382 | left.type = LSL_INTEGER; | 386 | { |
383 | right.content.integerValue = 0; | 387 | printf("Usage: %s [-f filename]\n", programName); |
384 | right.type = LSL_INTEGER; | 388 | printf(" -f: Script file to run.\n"); |
385 | evaluateAST(ast, &left, &right); | 389 | printf("Or pass filenames in stdin.\n"); |
390 | return 1; | ||
391 | } | ||
392 | |||
393 | if ('\0' == buffer[0]) | ||
394 | { | ||
395 | count = read(STDIN_FILENO, buffer, sizeof(buffer)); | ||
396 | if (0 > count) | ||
397 | { | ||
398 | printf("Error in stdin!\n"); | ||
399 | return 1; | ||
400 | } | ||
401 | else if (0 == count) | ||
402 | { | ||
403 | printf("No bytes in stdin!\n"); | ||
404 | return 1; | ||
405 | } | ||
406 | else | ||
407 | { | ||
408 | buffer[count] = '\0'; | ||
409 | printf("Filename %s in stdin.\n", buffer); | ||
410 | } | ||
411 | } | ||
412 | else | ||
413 | printf("Filename %s in argument.\n", buffer); | ||
414 | |||
415 | file = fopen(buffer, "r"); | ||
416 | if (NULL == file) | ||
417 | { | ||
418 | printf("Error opening file %s.\n", buffer); | ||
419 | return 1; | ||
420 | } | ||
421 | |||
422 | #ifdef LUASL_DEBUG | ||
423 | yydebug= 5; | ||
424 | #endif | ||
425 | |||
426 | param.ast = NULL; | ||
427 | if (yylex_init(&(param.scanner))) | ||
428 | return 1; | ||
386 | 429 | ||
387 | #ifdef LUASL_DEBUG | 430 | #ifdef LUASL_DEBUG |
388 | printf("\n"); | 431 | yyset_debug(1, param.scanner); |
389 | #endif | 432 | #endif |
390 | printf("Result of '%s' is %d %d\n", test, left.content.integerValue, right.content.integerValue); | 433 | yyset_in(file, param.scanner); |
391 | outputAST(ast); | 434 | |
392 | printf("\n"); | 435 | if (!yyparse(¶m)) |
393 | convertAST2Lua(ast); | 436 | { |
394 | printf("\n"); | 437 | yylex_destroy(param.scanner); |
395 | burnAST(ast); | 438 | ast = param.ast; |
439 | |||
440 | if (ast) | ||
441 | { | ||
442 | LSL_Value left, right; | ||
443 | |||
444 | left.content.integerValue = 0; | ||
445 | left.type = LSL_INTEGER; | ||
446 | right.content.integerValue = 0; | ||
447 | right.type = LSL_INTEGER; | ||
448 | evaluateAST(ast, &left, &right); | ||
449 | |||
450 | #ifdef LUASL_DEBUG | ||
451 | printf("\n"); | ||
452 | #endif | ||
453 | printf("Result of -\n"); | ||
454 | outputAST(ast); | ||
455 | printf("\n"); | ||
456 | printf("is %d %d. And converted to Lua it is -\n", left.content.integerValue, right.content.integerValue); | ||
457 | convertAST2Lua(ast); | ||
458 | printf("\n"); | ||
459 | burnAST(ast); | ||
460 | } | ||
396 | } | 461 | } |
397 | } | 462 | } |
398 | else | 463 | else |