aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-01-15 23:47:53 +1000
committerDavid Walter Seikel2012-01-15 23:47:53 +1000
commit737e7f59330532755167a222d4d52bb8cc2d6009 (patch)
treef17e8f1179e2c7f6e5f155287c33ce8e18288b49 /LuaSL
parentRe-arrange lemon to run before frex due to dependencies. (diff)
downloadSledjHamr-737e7f59330532755167a222d4d52bb8cc2d6009.zip
SledjHamr-737e7f59330532755167a222d4d52bb8cc2d6009.tar.gz
SledjHamr-737e7f59330532755167a222d4d52bb8cc2d6009.tar.bz2
SledjHamr-737e7f59330532755167a222d4d52bb8cc2d6009.tar.xz
Get multi file parsing working.
Diffstat (limited to 'LuaSL')
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c214
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h37
-rw-r--r--LuaSL/src/LuaSL_lexer.l46
3 files changed, 130 insertions, 167 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
675int nextFile(LuaSL_yyparseExtra *extra) 675static 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
695int main(int argc, char **argv) 718static 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 771int 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(&param, 0, sizeof(param));
797 param.argc = argc;
798 param.argv = argv;
799
800 // Loop through the files.
801 while (nextFile(&param))
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(&param, &(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, &param); 817 while((yv = yylex(param.lval, param.scanner)) != 0)
793 if (LSL_SCRIPT == yv) 818 {
794 break; 819 Parse(pParser, yv, param.lval, &param);
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, &param); 826 Parse (pParser, 0, param.lval, &param);
801 ParseFree(pParser, free); 827 ParseFree(pParser, free);
802 828 doneParsing(&param);
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
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
index 2ef48ee..b87e7d2 100644
--- a/LuaSL/src/LuaSL_LSL_tree.h
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -3,7 +3,6 @@
3#define __LSL_TREE_H__ 3#define __LSL_TREE_H__
4 4
5#define LUASL_DEBUG 5#define LUASL_DEBUG
6//#define LUASL_FILES
7 6
8 7
9#include <stddef.h> // So we can have NULL defined. 8#include <stddef.h> // So we can have NULL defined.
@@ -264,44 +263,30 @@ struct _LSL_Script
264 LSL_Identifier *variables; 263 LSL_Identifier *variables;
265}; 264};
266 265
266// Define the type for flex and lemon.
267#define YYSTYPE LSL_Leaf
268
267typedef struct 269typedef struct
268{ 270{
269 char *ignorableText; 271 void *scanner; // This should be of type yyscan_t, which is typedef to void * anyway, but that does not get defined until LuaSL_lexer.h, which depends on this struct being defined first.
270 int column;
271 int line;
272 int argc; 272 int argc;
273 char **argv; 273 char **argv;
274 char *fileName; 274 char fileName[PATH_MAX];
275 FILE *file; 275 FILE *file;
276} LuaSL_yyparseExtra; 276 LSL_Leaf *ast;
277 277 char *ignorableText;
278 278 LSL_Leaf *lval;
279// define the type for flex and lemon3 279 int column;
280#define YYSTYPE LSL_Leaf 280 int line;
281} LuaSL_yyparseParam;
281 282
282 283
283#ifndef excludeLexer 284#ifndef excludeLexer
284 #include "LuaSL_lexer.h" 285 #include "LuaSL_lexer.h"
285#endif 286#endif
286 287
287typedef struct
288{
289 yyscan_t scanner;
290 LSL_Leaf *ast;
291 LSL_Leaf *lval;
292} LuaSL_yyparseParam;
293
294// the parameter name (of the reentrant 'yyparse' function)
295// data is a pointer to a 'yyparseParam' structure
296//#define YYPARSE_PARAM data
297
298// the argument for the 'yylex' function
299#define YYLEX_PARAM ((LuaSL_yyparseParam*)data)->scanner
300//#define ParseTOKENTYPE YYSTYPE *
301//#define ParseARG_PDECL , LuaSL_yyparseParam *param
302 288
303void burnLeaf(LSL_Leaf *leaf); 289void burnLeaf(LSL_Leaf *leaf);
304int nextFile(LuaSL_yyparseExtra *extra);
305LSL_Leaf *addExpression(LSL_Leaf *exp); 290LSL_Leaf *addExpression(LSL_Leaf *exp);
306LSL_Leaf *addOperation(LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); 291LSL_Leaf *addOperation(LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right);
307LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Leaf *rval); 292LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Leaf *rval);
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l
index 8e5e4ca..15cfae2 100644
--- a/LuaSL/src/LuaSL_lexer.l
+++ b/LuaSL/src/LuaSL_lexer.l
@@ -3,7 +3,7 @@
3#define excludeLexer 3#define excludeLexer
4#include "LuaSL_LSL_tree.h" 4#include "LuaSL_LSL_tree.h"
5 5
6int common(YYSTYPE *lval, char *text, LuaSL_yyparseExtra *extra, boolean checkIgnorable, int type); 6int common(YYSTYPE *lval, char *text, LuaSL_yyparseParam *param, boolean checkIgnorable, int type);
7 7
8%} 8%}
9 9
@@ -12,7 +12,7 @@ int common(YYSTYPE *lval, char *text, LuaSL_yyparseExtra *extra, boolean checkIg
12%option noreject noyymore 12%option noreject noyymore
13%option backup debug perf-report perf-report verbose warn 13%option backup debug perf-report perf-report verbose warn
14%option align full 14%option align full
15%option extra-type="LuaSL_yyparseExtra *" 15%option extra-type="LuaSL_yyparseParam *"
16 16
17HEX [[:xdigit:]] 17HEX [[:xdigit:]]
18DECIMAL [[:digit:]] 18DECIMAL [[:digit:]]
@@ -110,54 +110,49 @@ IDENTIFIER [[:alpha:]](_|[[:alpha:]]|[[:digit:]])*
110 110
111%% 111%%
112 112
113int common(YYSTYPE *lval, char *text, LuaSL_yyparseExtra *extra, boolean checkIgnorable, int type) 113int common(YYSTYPE *lval, char *text, LuaSL_yyparseParam *param, boolean checkIgnorable, int type)
114{ 114{
115 int i; 115 int i;
116 116
117 for (i = 0; text[i] != '\0'; i++) 117 for (i = 0; text[i] != '\0'; i++)
118 if (text[i] == '\n') 118 if (text[i] == '\n')
119 { 119 {
120 extra->column = 0; 120 param->column = 0;
121 extra->line++; 121 param->line++;
122 } 122 }
123 else if (text[i] == '\t') 123 else if (text[i] == '\t')
124 extra->column += 8 - (extra->column % 8); 124 param->column += 8 - (param->column % 8);
125 else 125 else
126 extra->column++; 126 param->column++;
127 127
128 lval->token = tokens[type - lowestToken]; 128 lval->token = tokens[type - lowestToken];
129 lval->line = extra->line; 129 lval->line = param->line;
130 lval->column = extra->column; 130 lval->column = param->column;
131 131
132 if (checkIgnorable) 132 if (checkIgnorable)
133 { 133 {
134 lval->ignorableText = extra->ignorableText; 134 lval->ignorableText = param->ignorableText;
135 extra->ignorableText = NULL; 135 param->ignorableText = NULL;
136 } 136 }
137 else 137 else
138 { 138 {
139 if (extra->ignorableText) 139 if (param->ignorableText)
140 { 140 {
141 int lenI = strlen(extra->ignorableText); 141 int lenI = strlen(param->ignorableText);
142 int lenT = strlen(text); 142 int lenT = strlen(text);
143 143
144 extra->ignorableText = realloc(extra->ignorableText, lenI + lenT + 1); 144 param->ignorableText = realloc(param->ignorableText, lenI + lenT + 1);
145 sprintf(&(extra->ignorableText[lenI]), "%s", text); 145 sprintf(&(param->ignorableText[lenI]), "%s", text);
146 } 146 }
147 else 147 else
148 extra->ignorableText = strdup(text); 148 param->ignorableText = strdup(text);
149 } 149 }
150 150
151 return type; 151 return type;
152} 152}
153 153
154int yywrap(yyscan_t yyscanner) 154int yywrap(yyscan_t yyscanner) // This as actually useless for our needs, as it is called BEFORE the last token is dealt with.
155{ 155{
156#ifdef LUASL_FILES
157 LuaSL_yyparseExtra *extra = yyget_extra(yyscanner);
158#endif
159 int result = 1;
160
161#ifdef FLEX_SCANNER 156#ifdef FLEX_SCANNER
162 #ifndef LL_WINDOWS 157 #ifndef LL_WINDOWS
163 // Get gcc to stop complaining about lack of use of yyunput and input. 158 // Get gcc to stop complaining about lack of use of yyunput and input.
@@ -166,11 +161,6 @@ int yywrap(yyscan_t yyscanner)
166 #endif 161 #endif
167#endif 162#endif
168 163
169#ifdef LUASL_FILES 164 return 1;
170 result = nextFile(extra);
171 if (0 == result)
172 yyset_in(extra->file, yyscanner);
173#endif
174 return result;
175} 165}
176 166