aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c115
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h7
2 files changed, 76 insertions, 46 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c
index b441f0b..2b4d882 100644
--- a/LuaSL/src/LuaSL_LSL_tree.c
+++ b/LuaSL/src/LuaSL_LSL_tree.c
@@ -8,9 +8,9 @@ static void evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
8static void evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 8static void evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
9static void eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 9static void eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
10static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 10static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
11static void outputIntegerToken(LSL_Leaf *content); 11static void outputIntegerToken(FILE *file, LSL_Leaf *content);
12static void outputParenthesisToken(LSL_Leaf *content); 12static void outputParenthesisToken(FILE *file, LSL_Leaf *content);
13static void outputStatementToken(LSL_Leaf *content); 13static void outputStatementToken(FILE *file, LSL_Leaf *content);
14 14
15LSL_Token LSL_Tokens[] = 15LSL_Token LSL_Tokens[] =
16{ 16{
@@ -335,62 +335,62 @@ static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *
335 } 335 }
336} 336}
337 337
338static void outputLeaf(LSL_Leaf *leaf) 338static void outputLeaf(FILE *file, LSL_Leaf *leaf)
339{ 339{
340 if (leaf) 340 if (leaf)
341 { 341 {
342 outputLeaf(leaf->left); 342 outputLeaf(file, leaf->left);
343 if ((!(LSL_NOIGNORE & leaf->token->flags)) && (leaf->ignorableText)) 343 if ((!(LSL_NOIGNORE & leaf->token->flags)) && (leaf->ignorableText))
344 printf("%s", leaf->ignorableText); 344 fprintf(file, "%s", leaf->ignorableText);
345 if (leaf->token->output) 345 if (leaf->token->output)
346 leaf->token->output(leaf); 346 leaf->token->output(file, leaf);
347 else 347 else
348 printf("%s", leaf->token->token); 348 fprintf(file, "%s", leaf->token->token);
349 outputLeaf(leaf->right); 349 outputLeaf(file, leaf->right);
350 } 350 }
351} 351}
352 352
353static void outputIntegerToken(LSL_Leaf *content) 353static void outputIntegerToken(FILE *file, LSL_Leaf *content)
354{ 354{
355 if (content) 355 if (content)
356 printf("%d", content->value.integerValue); 356 fprintf(file, "%d", content->value.integerValue);
357} 357}
358 358
359static void outputParenthesisToken(LSL_Leaf *content) 359static void outputParenthesisToken(FILE *file, LSL_Leaf *content)
360{ 360{
361 if (content) 361 if (content)
362 { 362 {
363 printf("%s", content->token->token); 363 fprintf(file, "%s", content->token->token);
364 outputLeaf(content->value.parenthesis->expression); 364 outputLeaf(file, content->value.parenthesis->expression);
365 outputLeaf(content->value.parenthesis->right); 365 outputLeaf(file, content->value.parenthesis->right);
366 } 366 }
367} 367}
368 368
369static void outputStatementToken(LSL_Leaf *content) 369static void outputStatementToken(FILE *file, LSL_Leaf *content)
370{ 370{
371 if (content) 371 if (content)
372 { 372 {
373 outputLeaf(content->value.statementValue->expressions); 373 outputLeaf(file, content->value.statementValue->expressions);
374 if (content->ignorableText) 374 if (content->ignorableText)
375 printf("%s", content->ignorableText); 375 fprintf(file, "%s", content->ignorableText);
376 printf("%s", content->token->token); 376 fprintf(file, "%s", content->token->token);
377 } 377 }
378} 378}
379 379
380static void convertLeaf2Lua(LSL_Leaf *leaf) 380static void convertLeaf2Lua(FILE *file, LSL_Leaf *leaf)
381{ 381{
382 if (leaf) 382 if (leaf)
383 { 383 {
384 convertLeaf2Lua(leaf->left); 384 convertLeaf2Lua(file, leaf->left);
385 if ((!(LSL_NOIGNORE & leaf->token->flags)) && (leaf->ignorableText)) 385 if ((!(LSL_NOIGNORE & leaf->token->flags)) && (leaf->ignorableText))
386 printf("%s", leaf->ignorableText); 386 fprintf(file, "%s", leaf->ignorableText);
387 if (leaf->token->convert) 387 if (leaf->token->convert)
388 leaf->token->convert(leaf); 388 leaf->token->convert(file, leaf);
389 else if (leaf->token->output) 389 else if (leaf->token->output)
390 leaf->token->output(leaf); 390 leaf->token->output(file, leaf);
391 else 391 else
392 printf("%s", leaf->token->token); 392 fprintf(file, "%s", leaf->token->token);
393 convertLeaf2Lua(leaf->right); 393 convertLeaf2Lua(file, leaf->right);
394 } 394 }
395} 395}
396 396
@@ -408,7 +408,8 @@ int main(int argc, char **argv)
408 tokens = calloc(i + 1, sizeof(LSL_Token *)); 408 tokens = calloc(i + 1, sizeof(LSL_Token *));
409 if (tokens) 409 if (tokens)
410 { 410 {
411 char buffer[4096]; 411 char buffer[PATH_MAX];
412 char fileName[PATH_MAX];
412 LuaSL_yyparseParam param; 413 LuaSL_yyparseParam param;
413 int file; 414 int file;
414 int count; 415 int count;
@@ -422,7 +423,7 @@ int main(int argc, char **argv)
422 tokens[j] = &(LSL_Tokens[i]); 423 tokens[j] = &(LSL_Tokens[i]);
423 } 424 }
424 425
425 buffer[0] = '\0'; 426 fileName[0] = '\0';
426 427
427 // get the arguments passed in 428 // get the arguments passed in
428 while (--argc > 0 && *++argv != '\0') 429 while (--argc > 0 && *++argv != '\0')
@@ -438,8 +439,8 @@ int main(int argc, char **argv)
438 { 439 {
439 if (--argc > 0 && *++argv != '\0') 440 if (--argc > 0 && *++argv != '\0')
440 { 441 {
441 strncpy(buffer, *argv, 4095); 442 strncpy(fileName, *argv, PATH_MAX - 1);
442 buffer[4095] = '\0';; 443 fileName[PATH_MAX - 1] = '\0';;
443 } 444 }
444 else 445 else
445 badArgs = TRUE; 446 badArgs = TRUE;
@@ -461,11 +462,11 @@ int main(int argc, char **argv)
461 return 1; 462 return 1;
462 } 463 }
463 464
464 if ('\0' == buffer[0]) 465 if ('\0' == fileName[0])
465 { 466 {
466//strcpy(buffer, "test.lsl"); 467//strcpy(fileName, "test.lsl");
467 468
468 count = read(STDIN_FILENO, buffer, sizeof(buffer)); 469 count = read(STDIN_FILENO, fileName, PATH_MAX - 1);
469 if (0 > count) 470 if (0 > count)
470 { 471 {
471 printf("Error in stdin!\n"); 472 printf("Error in stdin!\n");
@@ -478,18 +479,18 @@ int main(int argc, char **argv)
478 } 479 }
479 else 480 else
480 { 481 {
481 buffer[count] = '\0'; 482 fileName[count] = '\0';
482 printf("Filename %s in stdin.\n", buffer); 483 printf("Filename %s in stdin.\n", fileName);
483 } 484 }
484 485
485 } 486 }
486 else 487 else
487 printf("Filename %s in argument.\n", buffer); 488 printf("Filename %s in argument.\n", fileName);
488 489
489 file = open(buffer, 0); 490 file = open(fileName, 0);
490 if (-1 == file) 491 if (-1 == file)
491 { 492 {
492 printf("Error opening file %s.\n", buffer); 493 printf("Error opening file %s.\n", fileName);
493 return 1; 494 return 1;
494 } 495 }
495#ifdef LUASL_DEBUG 496#ifdef LUASL_DEBUG
@@ -512,7 +513,7 @@ int main(int argc, char **argv)
512 513
513 ParseTrace(stdout, "LSL_lemon "); 514 ParseTrace(stdout, "LSL_lemon ");
514 515
515 while ((i = read(file, buffer, 4095)) > 0) 516 while ((i = read(file, buffer, PATH_MAX - 1)) > 0)
516 { 517 {
517 buffer[i] = '\0'; 518 buffer[i] = '\0';
518 yy_scan_string(buffer, param.scanner); 519 yy_scan_string(buffer, param.scanner);
@@ -532,6 +533,10 @@ int main(int argc, char **argv)
532 533
533 if (param.ast) 534 if (param.ast)
534 { 535 {
536 FILE *out;
537 char outName[PATH_MAX];
538 char luaName[PATH_MAX];
539
535 LSL_Leaf left, right; 540 LSL_Leaf left, right;
536 541
537 left.value.integerValue = 0; 542 left.value.integerValue = 0;
@@ -539,13 +544,37 @@ int main(int argc, char **argv)
539 right.value.integerValue = 0; 544 right.value.integerValue = 0;
540 right.token = tokens[LSL_INTEGER - lowestToken]; 545 right.token = tokens[LSL_INTEGER - lowestToken];
541 546
542 outputLeaf(param.ast); 547 outputLeaf(stdout, param.ast);
543 printf("\n"); 548 printf("\n");
544 evaluateLeaf(param.ast, &left, &right); 549 evaluateLeaf(param.ast, &left, &right);
545
546 printf("\nAnd converted to Lua it is -\n");
547 convertLeaf2Lua(param.ast);
548 printf("\n"); 550 printf("\n");
551
552 strcpy(outName, fileName);
553 strcat(outName, "2");
554 strcpy(luaName, fileName);
555 strcat(luaName, ".lua");
556 out = fopen(outName, "w");
557 if (out)
558 {
559 outputLeaf(out, param.ast);
560 fclose(out);
561 sprintf(buffer, "diff %s %s", fileName, outName);
562 count = system(buffer);
563 printf("Return value of %s is %d\n", buffer, count);
564 if (0 != count}
565 printf(stderr, "%s says they are different!\n", buffer);
566
567 }
568 else
569 fprintf(stderr, "Unable to open file %s for writing!\n", outName);
570 out = fopen(luaName, "w");
571 if (out)
572 {
573 convertLeaf2Lua(out, param.ast);
574 fclose(out);
575 }
576 else
577 fprintf(stderr, "Unable to open file %s for writing!\n", luaName);
549 burnLeaf(param.ast); 578 burnLeaf(param.ast);
550 } 579 }
551 580
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
index 35d0470..bb29399 100644
--- a/LuaSL/src/LuaSL_LSL_tree.h
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -16,7 +16,8 @@
16#include <sys/stat.h> 16#include <sys/stat.h>
17#include <fcntl.h> 17#include <fcntl.h>
18#include <stdlib.h> 18#include <stdlib.h>
19//#define BUFS 1024 19#include <stdio.h>
20#include <limits.h> // For PATH_MAX.
20 21
21#include "LuaSL_lemon_yaccer.h" 22#include "LuaSL_lemon_yaccer.h"
22 23
@@ -43,8 +44,8 @@ extern int lowestToken;
43 44
44typedef int LSL_Type; 45typedef int LSL_Type;
45 46
46typedef void (*convertToken2Lua) (LSL_Leaf *content); 47typedef void (*convertToken2Lua) (FILE *file, LSL_Leaf *content);
47typedef void (*outputToken) (LSL_Leaf *content); 48typedef void (*outputToken) (FILE *file, LSL_Leaf *content);
48typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 49typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
49 50
50#ifndef FALSE 51#ifndef FALSE