aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c153
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h16
-rw-r--r--LuaSL/src/LuaSL_lexer.l9
-rw-r--r--LuaSL/test.lsl2
4 files changed, 130 insertions, 50 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
325static LSL_AST *newTree(const char *expr) 325int 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(&param))
344 return NULL;
345
346 yy_delete_buffer(state, param.scanner);
347 yylex_destroy(param.scanner);
348
349 return param.ast;
350}
351
352int 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(&param))
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
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 @@
3#define __EXPRESSION_H__ 3#define __EXPRESSION_H__
4 4
5//#define LUASL_USE_ENUM 5//#define LUASL_USE_ENUM
6#define LUASL_DEBUG 6//#define LUASL_DEBUG
7 7
8#ifndef LUASL_USE_ENUM 8#ifndef LUASL_USE_ENUM
9#include "LuaSL_yaccer.tab.h" 9#include "LuaSL_yaccer.tab.h"
10#endif 10#endif
11 11
12#include <stddef.h> // So we can have NULL defined. 12#include <stddef.h> // So we can have NULL defined.
13 13#include <sys/types.h>
14#include <sys/stat.h>
15#include <fcntl.h>
16
14#define YYERRCODE 256 17#define YYERRCODE 256
15#define YYDEBUG 1 18#define YYDEBUG 1
16extern int yydebug; 19extern int yydebug;
@@ -18,6 +21,15 @@ extern int yydebug;
18 21
19// http://w-hat.com/stackdepth is a useful discussion about some aspects of the LL parser. 22// http://w-hat.com/stackdepth is a useful discussion about some aspects of the LL parser.
20 23
24#ifndef FALSE
25// NEVER change this
26typedef enum
27{
28 FALSE = 0,
29 TRUE = 1
30} boolean;
31#endif
32
21typedef enum 33typedef enum
22{ 34{
23 LSL_NONE = 0, 35 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 @@
15 15
16%option reentrant never-interactive batch 16%option reentrant never-interactive batch
17%option bison-bridge yylineno 8bit 17%option bison-bridge yylineno 8bit
18%option noreject noyymore noyywrap 18%option noreject noyymore
19%option backup debug perf-report perf-report verbose warn 19%option backup debug perf-report perf-report verbose warn
20%option align full 20%option align full
21 21
22SPACE [ \r\n\t]* 22SPACE [ \r\n\t]*
23ENDSPACE {SPACE}$
24 23
25LETTER [A-Za-z] 24LETTER [A-Za-z]
26DECIMAL [0-9] 25DECIMAL [0-9]
@@ -34,7 +33,6 @@ FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}?
34%% 33%%
35 34
36 /* Basic tokens */ 35 /* Basic tokens */
37{ENDSPACE} %{ /* Skip blanks at end of lines. */ %}
38{SPACE} %{ ECHO; /* Skip blanks. */ %} 36{SPACE} %{ ECHO; /* Skip blanks. */ %}
39{NAME} %{ ECHO; /* yylval->nameValue=strdup(yytext); return LSL_NAME; */ %} 37{NAME} %{ ECHO; /* yylval->nameValue=strdup(yytext); return LSL_NAME; */ %}
40{INTEGER} %{ ECHO; yylval->integerValue = atoi(yytext); return LSL_INTEGER; %} 38{INTEGER} %{ ECHO; yylval->integerValue = atoi(yytext); return LSL_INTEGER; %}
@@ -64,12 +62,14 @@ FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}?
64"(" { ECHO; return LSL_PARENTHESIS_OPEN; } 62"(" { ECHO; return LSL_PARENTHESIS_OPEN; }
65")" { ECHO; return LSL_PARENTHESIS_CLOSE; } 63")" { ECHO; return LSL_PARENTHESIS_CLOSE; }
66 64
65<<EOF>> { yyterminate(); }
66
67 /* Everything else */ 67 /* Everything else */
68. %{ ECHO; printf(" unexpected character.\n"); %} 68. %{ ECHO; printf(" unexpected character.\n"); %}
69 69
70%% 70%%
71 71
72int XXyywrap() 72int yywrap(yyscan_t yyscanner)
73{ 73{
74#ifdef FLEX_SCANNER 74#ifdef FLEX_SCANNER
75 #ifndef LL_WINDOWS 75 #ifndef LL_WINDOWS
@@ -78,6 +78,7 @@ int XXyywrap()
78 (void) input; 78 (void) input;
79 #endif 79 #endif
80#endif 80#endif
81// 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.
81 return(1); 82 return(1);
82} 83}
83 84
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 @@
14 + 2 * 10 + 3 * (5 + 1)
2