From 428e27426d9e2503b475c4e608ee780bbb9e72e6 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sun, 8 Jan 2012 00:20:28 +1000 Subject: Parse an actual file. --- LuaSL/src/LuaSL_LSL_tree.c | 153 ++++++++++++++++++++++++++++++++------------- LuaSL/src/LuaSL_LSL_tree.h | 16 ++++- LuaSL/src/LuaSL_lexer.l | 9 +-- LuaSL/test.lsl | 2 + 4 files changed, 130 insertions(+), 50 deletions(-) create mode 100644 LuaSL/test.lsl (limited to 'LuaSL') 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) return 0; } -static LSL_AST *newTree(const char *expr) -{ - LuaSL_yyparseParam param; - YY_BUFFER_STATE state; - -#ifdef LUASL_DEBUG - yydebug= 5; -#endif - - param.ast = NULL; - if (yylex_init(&(param.scanner))) - return NULL; - -#ifdef LUASL_DEBUG - yyset_debug(1, param.scanner); -#endif - - state = yy_scan_string(expr, param.scanner); - if (yyparse(¶m)) - return NULL; - - yy_delete_buffer(state, param.scanner); - yylex_destroy(param.scanner); - - return param.ast; -} - -int main(void) +int main(int argc, char **argv) { + char *programName = argv[0]; int i; // Figure out what numbers yacc gave to our tokens. @@ -362,8 +336,12 @@ int main(void) tokens = calloc(i + 1, sizeof(LSL_Token *)); if (tokens) { - const char test[] = " 4 + 2 * 10 + 3 * ( 5 + 1 )"; + char buffer[4096]; LSL_AST *ast; + LuaSL_yyparseParam param; + int count; + FILE *file; + boolean badArgs = FALSE; // Sort the token table. for (i = 0; LSL_Tokens[i].token != NULL; i++) @@ -373,26 +351,113 @@ int main(void) tokens[j] = &(LSL_Tokens[i]); } - // Run the parser on a test. - if ((ast = newTree(test))) + buffer[0] = '\0'; + + // get the arguments passed in + while (--argc > 0 && *++argv != '\0') { - LSL_Value left, right; + if (*argv[0] == '-') + { + // point to the characters after the '-' sign + char *s = argv[0] + 1; + + switch (*s) + { + case 'f': // file + { + if (--argc > 0 && *++argv != '\0') + { + strncpy(buffer, *argv, 4095); + buffer[4095] = '\0';; + } + else + badArgs = TRUE; + break; + } + default: + badArgs = TRUE; + } + } + else + badArgs = TRUE; + } - left.content.integerValue = 0; - left.type = LSL_INTEGER; - right.content.integerValue = 0; - right.type = LSL_INTEGER; - evaluateAST(ast, &left, &right); + if (badArgs) + { + printf("Usage: %s [-f filename]\n", programName); + printf(" -f: Script file to run.\n"); + printf("Or pass filenames in stdin.\n"); + return 1; + } + + if ('\0' == buffer[0]) + { + count = read(STDIN_FILENO, buffer, sizeof(buffer)); + if (0 > count) + { + printf("Error in stdin!\n"); + return 1; + } + else if (0 == count) + { + printf("No bytes in stdin!\n"); + return 1; + } + else + { + buffer[count] = '\0'; + printf("Filename %s in stdin.\n", buffer); + } + } + else + printf("Filename %s in argument.\n", buffer); + + file = fopen(buffer, "r"); + if (NULL == file) + { + printf("Error opening file %s.\n", buffer); + return 1; + } + +#ifdef LUASL_DEBUG + yydebug= 5; +#endif + + param.ast = NULL; + if (yylex_init(&(param.scanner))) + return 1; #ifdef LUASL_DEBUG - printf("\n"); + yyset_debug(1, param.scanner); #endif - printf("Result of '%s' is %d %d\n", test, left.content.integerValue, right.content.integerValue); - outputAST(ast); - printf("\n"); - convertAST2Lua(ast); - printf("\n"); - burnAST(ast); + yyset_in(file, param.scanner); + + if (!yyparse(¶m)) + { + yylex_destroy(param.scanner); + ast = param.ast; + + if (ast) + { + LSL_Value left, right; + + left.content.integerValue = 0; + left.type = LSL_INTEGER; + right.content.integerValue = 0; + right.type = LSL_INTEGER; + evaluateAST(ast, &left, &right); + +#ifdef LUASL_DEBUG + printf("\n"); +#endif + printf("Result of -\n"); + outputAST(ast); + printf("\n"); + printf("is %d %d. And converted to Lua it is -\n", left.content.integerValue, right.content.integerValue); + convertAST2Lua(ast); + printf("\n"); + burnAST(ast); + } } } else diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 750d3f7..e01273b 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -3,14 +3,17 @@ #define __EXPRESSION_H__ //#define LUASL_USE_ENUM -#define LUASL_DEBUG +//#define LUASL_DEBUG #ifndef LUASL_USE_ENUM #include "LuaSL_yaccer.tab.h" #endif #include // So we can have NULL defined. - +#include +#include +#include + #define YYERRCODE 256 #define YYDEBUG 1 extern int yydebug; @@ -18,6 +21,15 @@ extern int yydebug; // http://w-hat.com/stackdepth is a useful discussion about some aspects of the LL parser. +#ifndef FALSE +// NEVER change this +typedef enum +{ + FALSE = 0, + TRUE = 1 +} boolean; +#endif + typedef enum { LSL_NONE = 0, diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index 3eb59fb..d52ea4b 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l @@ -15,12 +15,11 @@ %option reentrant never-interactive batch %option bison-bridge yylineno 8bit -%option noreject noyymore noyywrap +%option noreject noyymore %option backup debug perf-report perf-report verbose warn %option align full SPACE [ \r\n\t]* -ENDSPACE {SPACE}$ LETTER [A-Za-z] DECIMAL [0-9] @@ -34,7 +33,6 @@ FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}? %% /* Basic tokens */ -{ENDSPACE} %{ /* Skip blanks at end of lines. */ %} {SPACE} %{ ECHO; /* Skip blanks. */ %} {NAME} %{ ECHO; /* yylval->nameValue=strdup(yytext); return LSL_NAME; */ %} {INTEGER} %{ ECHO; yylval->integerValue = atoi(yytext); return LSL_INTEGER; %} @@ -64,12 +62,14 @@ FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}? "(" { ECHO; return LSL_PARENTHESIS_OPEN; } ")" { ECHO; return LSL_PARENTHESIS_CLOSE; } +<> { yyterminate(); } + /* Everything else */ . %{ ECHO; printf(" unexpected character.\n"); %} %% -int XXyywrap() +int yywrap(yyscan_t yyscanner) { #ifdef FLEX_SCANNER #ifndef LL_WINDOWS @@ -78,6 +78,7 @@ int XXyywrap() (void) input; #endif #endif +// TODO - If we are getting files from stdin, or multiple -f arguments, we should loop through them asd return 0. Return 1 when there are no more files. return(1); } diff --git a/LuaSL/test.lsl b/LuaSL/test.lsl new file mode 100644 index 0000000..f2491f1 --- /dev/null +++ b/LuaSL/test.lsl @@ -0,0 +1,2 @@ +4 + 2 * 10 + 3 * (5 + 1) + -- cgit v1.1