diff options
author | David Walter Seikel | 2012-01-15 23:47:53 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-15 23:47:53 +1000 |
commit | 737e7f59330532755167a222d4d52bb8cc2d6009 (patch) | |
tree | f17e8f1179e2c7f6e5f155287c33ce8e18288b49 /LuaSL/src/LuaSL_LSL_tree.c | |
parent | Re-arrange lemon to run before frex due to dependencies. (diff) | |
download | SledjHamr-737e7f59330532755167a222d4d52bb8cc2d6009.zip SledjHamr-737e7f59330532755167a222d4d52bb8cc2d6009.tar.gz SledjHamr-737e7f59330532755167a222d4d52bb8cc2d6009.tar.bz2 SledjHamr-737e7f59330532755167a222d4d52bb8cc2d6009.tar.xz |
Get multi file parsing working.
Diffstat (limited to '')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 214 |
1 files changed, 101 insertions, 113 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 5a082bd..169a699 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c | |||
@@ -672,66 +672,74 @@ static void convertLeaf2Lua(FILE *file, LSL_Leaf *leaf) | |||
672 | } | 672 | } |
673 | } | 673 | } |
674 | 674 | ||
675 | int nextFile(LuaSL_yyparseExtra *extra) | 675 | static void doneParsing(LuaSL_yyparseParam *param) |
676 | { | 676 | { |
677 | if (NULL != extra->file) | 677 | if (param->ast) |
678 | fclose(extra->file); | ||
679 | if (--(extra->argc) > 0 && *++(extra->argv) != '\0') | ||
680 | { | 678 | { |
681 | strncpy(extra->fileName, *(extra->argv), PATH_MAX - 1); | 679 | FILE *out; |
682 | extra->fileName[PATH_MAX - 1] = '\0'; | 680 | char buffer[PATH_MAX]; |
683 | extra->file = fopen(extra->fileName, "r"); | 681 | char outName[PATH_MAX]; |
684 | if (NULL == extra->file) | 682 | char luaName[PATH_MAX]; |
683 | int count; | ||
684 | |||
685 | outputLeaf(stdout, param->ast); | ||
686 | printf("\n"); | ||
687 | evaluateLeaf(param->ast, NULL, NULL); | ||
688 | printf("\n"); | ||
689 | |||
690 | strcpy(outName, param->fileName); | ||
691 | strcat(outName, "2"); | ||
692 | strcpy(luaName, param->fileName); | ||
693 | strcat(luaName, ".lua"); | ||
694 | out = fopen(outName, "w"); | ||
695 | if (out) | ||
696 | { | ||
697 | outputLeaf(out, param->ast); | ||
698 | fclose(out); | ||
699 | sprintf(buffer, "diff %s %s", param->fileName, outName); | ||
700 | count = system(buffer); | ||
701 | printf("Return value of %s is %d\n", buffer, count); | ||
702 | if (0 != count) | ||
703 | fprintf(stderr, "%s says they are different!\n", buffer); | ||
704 | } | ||
705 | else | ||
706 | fprintf(stderr, "Unable to open file %s for writing!\n", outName); | ||
707 | out = fopen(luaName, "w"); | ||
708 | if (out) | ||
685 | { | 709 | { |
686 | fprintf(stderr, "Error opening file %s.\n", extra->fileName); | 710 | convertLeaf2Lua(out, param->ast); |
687 | return 1; | 711 | fclose(out); |
688 | } | 712 | } |
689 | printf("Opened %s.\n", extra->fileName); | 713 | else |
690 | return(0); | 714 | fprintf(stderr, "Unable to open file %s for writing!\n", luaName); |
691 | } | 715 | } |
692 | return(1); | ||
693 | } | 716 | } |
694 | 717 | ||
695 | int main(int argc, char **argv) | 718 | static int nextFile(LuaSL_yyparseParam *param) |
696 | { | 719 | { |
697 | // char *programName = argv[0]; | 720 | if (NULL != param->file) |
698 | int i; | ||
699 | |||
700 | // Figure out what numbers yacc gave to our tokens. | ||
701 | for (i = 0; LSL_Tokens[i].token != NULL; i++) | ||
702 | { | 721 | { |
703 | if (lowestToken > LSL_Tokens[i].type) | 722 | fclose(param->file); |
704 | lowestToken = LSL_Tokens[i].type; | 723 | param->file = NULL; |
705 | } | 724 | } |
706 | tokens = calloc(i + 1, sizeof(LSL_Token *)); | 725 | if (--(param->argc) > 0 && *++(param->argv) != '\0') |
707 | if (tokens) | ||
708 | { | 726 | { |
709 | char buffer[PATH_MAX]; | 727 | strncpy(param->fileName, *(param->argv), PATH_MAX - 1); |
710 | char fileName[PATH_MAX]; | 728 | param->fileName[PATH_MAX - 1] = '\0'; |
711 | LuaSL_yyparseParam param; | 729 | param->file = fopen(param->fileName, "r"); |
712 | LuaSL_yyparseExtra extra; | 730 | if (NULL == param->file) |
713 | int count; | ||
714 | |||
715 | // Sort the token table. | ||
716 | for (i = 0; LSL_Tokens[i].token != NULL; i++) | ||
717 | { | 731 | { |
718 | int j = LSL_Tokens[i].type - lowestToken; | 732 | fprintf(stderr, "Error opening file %s.\n", param->fileName); |
719 | 733 | return FALSE; | |
720 | tokens[j] = &(LSL_Tokens[i]); | ||
721 | } | 734 | } |
722 | 735 | printf("Opened %s.\n", param->fileName); | |
723 | fileName[0] = '\0'; | 736 | burnLeaf(param->ast); |
724 | param.ast = NULL; | 737 | param->ast = NULL; |
725 | param.lval = calloc(1, sizeof(LSL_Leaf)); | 738 | param->lval = calloc(1, sizeof(LSL_Leaf)); |
726 | memset(&extra, 0, sizeof(extra)); | 739 | param->line = 0; |
727 | extra.argc = argc; | 740 | param->column = 0; |
728 | extra.argv = argv; | 741 | return TRUE; |
729 | extra.fileName = fileName; | 742 | } |
730 | extra.file = NULL; | ||
731 | // Grab the first file name, if any. | ||
732 | if (1 == nextFile(&extra)) | ||
733 | return 1; | ||
734 | |||
735 | /* | 743 | /* |
736 | if ('\0' == fileName[0]) | 744 | if ('\0' == fileName[0]) |
737 | { | 745 | { |
@@ -757,87 +765,67 @@ int main(int argc, char **argv) | |||
757 | } | 765 | } |
758 | */ | 766 | */ |
759 | 767 | ||
760 | if (yylex_init_extra(&extra, &(param.scanner))) | 768 | return FALSE; |
761 | return 1; | 769 | } |
762 | 770 | ||
763 | #ifdef LUASL_DEBUG | 771 | int main(int argc, char **argv) |
764 | // yydebug= 5; | 772 | { |
765 | #endif | 773 | // char *programName = argv[0]; |
774 | int i; | ||
766 | 775 | ||
776 | // Figure out what numbers yacc gave to our tokens. | ||
777 | for (i = 0; LSL_Tokens[i].token != NULL; i++) | ||
778 | { | ||
779 | if (lowestToken > LSL_Tokens[i].type) | ||
780 | lowestToken = LSL_Tokens[i].type; | ||
781 | } | ||
782 | tokens = calloc(i + 1, sizeof(LSL_Token *)); | ||
783 | if (tokens) | ||
784 | { | ||
785 | LuaSL_yyparseParam param; | ||
767 | 786 | ||
768 | #ifdef LUASL_DEBUG | 787 | // Sort the token table. |
769 | yyset_debug(1, param.scanner); | 788 | for (i = 0; LSL_Tokens[i].token != NULL; i++) |
770 | #endif | 789 | { |
790 | int j = LSL_Tokens[i].type - lowestToken; | ||
771 | 791 | ||
772 | #ifdef LUASL_FILES | 792 | tokens[j] = &(LSL_Tokens[i]); |
773 | yyset_in(extra.file, &(param.scanner)); | 793 | } |
774 | #endif | 794 | |
795 | // First time setup. | ||
796 | memset(¶m, 0, sizeof(param)); | ||
797 | param.argc = argc; | ||
798 | param.argv = argv; | ||
799 | |||
800 | // Loop through the files. | ||
801 | while (nextFile(¶m)) | ||
775 | { | 802 | { |
776 | void *pParser = ParseAlloc(malloc); | 803 | void *pParser = ParseAlloc(malloc); |
777 | int yv; | 804 | int yv; |
778 | 805 | ||
779 | ParseTrace(stdout, "LSL_lemon "); | 806 | #ifdef LUASL_DEBUG |
780 | 807 | // yydebug= 5; | |
781 | #ifndef LUASL_FILES | ||
782 | while ((i = fread(buffer, 1, PATH_MAX - 1, extra.file)) > 0) | ||
783 | #endif | 808 | #endif |
784 | { | 809 | if (yylex_init_extra(¶m, &(param.scanner))) |
785 | #ifndef LUASL_FILES | 810 | return 1; |
786 | buffer[i] = '\0'; | 811 | #ifdef LUASL_DEBUG |
787 | yy_scan_string(buffer, param.scanner); | 812 | yyset_debug(1, param.scanner); |
788 | #endif | 813 | #endif |
789 | // on EOF yylex will return 0 | 814 | yyset_in(param.file, param.scanner); |
790 | while((yv = yylex(param.lval, param.scanner)) != 0) | 815 | ParseTrace(stdout, "LSL_lemon "); |
791 | { | 816 | // on EOF yylex will return 0 |
792 | Parse(pParser, yv, param.lval, ¶m); | 817 | while((yv = yylex(param.lval, param.scanner)) != 0) |
793 | if (LSL_SCRIPT == yv) | 818 | { |
794 | break; | 819 | Parse(pParser, yv, param.lval, ¶m); |
795 | param.lval = calloc(1, sizeof(LSL_Leaf)); | 820 | if (LSL_SCRIPT == yv) |
796 | } | 821 | break; |
822 | param.lval = calloc(1, sizeof(LSL_Leaf)); | ||
797 | } | 823 | } |
798 | 824 | ||
799 | yylex_destroy(param.scanner); | 825 | yylex_destroy(param.scanner); |
800 | Parse (pParser, 0, param.lval, ¶m); | 826 | Parse (pParser, 0, param.lval, ¶m); |
801 | ParseFree(pParser, free); | 827 | ParseFree(pParser, free); |
802 | 828 | doneParsing(¶m); | |
803 | if (param.ast) | ||
804 | { | ||
805 | FILE *out; | ||
806 | char outName[PATH_MAX]; | ||
807 | char luaName[PATH_MAX]; | ||
808 | |||
809 | outputLeaf(stdout, param.ast); | ||
810 | printf("\n"); | ||
811 | evaluateLeaf(param.ast, NULL, NULL); | ||
812 | printf("\n"); | ||
813 | |||
814 | strcpy(outName, fileName); | ||
815 | strcat(outName, "2"); | ||
816 | strcpy(luaName, fileName); | ||
817 | strcat(luaName, ".lua"); | ||
818 | out = fopen(outName, "w"); | ||
819 | if (out) | ||
820 | { | ||
821 | outputLeaf(out, param.ast); | ||
822 | fclose(out); | ||
823 | sprintf(buffer, "diff %s %s", fileName, outName); | ||
824 | count = system(buffer); | ||
825 | printf("Return value of %s is %d\n", buffer, count); | ||
826 | if (0 != count) | ||
827 | fprintf(stderr, "%s says they are different!\n", buffer); | ||
828 | } | ||
829 | else | ||
830 | fprintf(stderr, "Unable to open file %s for writing!\n", outName); | ||
831 | out = fopen(luaName, "w"); | ||
832 | if (out) | ||
833 | { | ||
834 | convertLeaf2Lua(out, param.ast); | ||
835 | fclose(out); | ||
836 | } | ||
837 | else | ||
838 | fprintf(stderr, "Unable to open file %s for writing!\n", luaName); | ||
839 | burnLeaf(param.ast); | ||
840 | } | ||
841 | } | 829 | } |
842 | } | 830 | } |
843 | else | 831 | else |