From 8b6c0f0960aaf4b886d0004105df886e3d3c22e7 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Fri, 6 Jan 2012 14:06:47 +1000 Subject: Use the LSL_ enums for parser tokens. Some clean up and debugging. OK, so the parsers prefer to make them defines instead of enums. sigh --- LuaSL/build.sh | 5 +- LuaSL/src/LuaSL_LSL_tree.c | 195 +++++++++++++++++++++++---------------------- LuaSL/src/LuaSL_LSL_tree.h | 50 ++++++------ LuaSL/src/LuaSL_lexer.l | 23 +++--- LuaSL/src/LuaSL_yaccer.y | 22 ++--- 5 files changed, 147 insertions(+), 148 deletions(-) (limited to 'LuaSL') diff --git a/LuaSL/build.sh b/LuaSL/build.sh index bf82075..9e5e2b7 100755 --- a/LuaSL/build.sh +++ b/LuaSL/build.sh @@ -51,7 +51,7 @@ names="LuaSL_main LuaSL_compile LuaSL_utilities" EDJE_FLAGS="-id images -fd fonts" -rm -f ../LuaSL ../LuaSL_parser *.o *.edj LuaSL_lexer.h LuaSL_lexer.c LuaSL_yaccer.h LuaSL_yaccer.tab.c +rm -f ../LuaSL ../LuaSL_parser ../*.o *.output ../*.edj LuaSL_lexer.h LuaSL_lexer.c LuaSL_yaccer.h LuaSL_yaccer.tab.c command="edje_cc $EDJE_FLAGS LuaSL.edc ../LuaSL.edj" echo $command $command @@ -77,7 +77,8 @@ command="flex --outfile=LuaSL_lexer.c --header-file=LuaSL_lexer.h LuaSL_lexer.l" echo $command $command -command="btyacc -d -b LuaSL_yaccer -S btyacc-c.ske LuaSL_yaccer.y" +# Should add -t as well for debugging, but it causes errors. +command="btyacc -d -v -b LuaSL_yaccer -S btyacc-c.ske LuaSL_yaccer.y" echo $command $command diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 9137692..552de04 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c @@ -106,88 +106,88 @@ int evaluateExpression(LSL_Expression *exp, int old) { switch(exp->type) { - case LSL_COMMENT : - case LSL_TYPE : - case LSL_NAME : - case LSL_IDENTIFIER : - break; - case LSL_FLOAT : return (int) exp->value.floatValue; +// case LSL_COMMENT : +// case LSL_TYPE : +// case LSL_NAME : +// case LSL_IDENTIFIER : +// break; +// case LSL_FLOAT : return (int) exp->value.floatValue; case LSL_INTEGER : return exp->value.integerValue; - case LSL_STRING : - case LSL_KEY : - case LSL_VECTOR : - case LSL_ROTATION : - case LSL_LIST : - case LSL_LABEL : - break; +// case LSL_STRING : +// case LSL_KEY : +// case LSL_VECTOR : +// case LSL_ROTATION : +// case LSL_LIST : +// case LSL_LABEL : +// break; case LSL_EXPRESSION : { switch (exp->expression) { - case LSL_COMMA : - case LSL_INCREMENT_PRE : - case LSL_INCREMENT_POST : - case LSL_DECREMENT_PRE : - case LSL_DECREMENT_POST : - case LSL_DOT : - case LSL_ASSIGNMENT_PLAIN : - case LSL_ASSIGNMENT_DIVIDE : - case LSL_ASSIGNMENT_MODULO : - case LSL_ASSIGNMENT_MULTIPLY : - case LSL_ASSIGNMENT_SUBTRACT : - case LSL_ASSIGNMENT_ADD : - case LSL_ASSIGNMENT_CONCATENATE : +// case LSL_COMMA : +// case LSL_INCREMENT_PRE : +// case LSL_INCREMENT_POST : +// case LSL_DECREMENT_PRE : +// case LSL_DECREMENT_POST : +// case LSL_DOT : +// case LSL_ASSIGNMENT_PLAIN : +// case LSL_ASSIGNMENT_DIVIDE : +// case LSL_ASSIGNMENT_MODULO : +// case LSL_ASSIGNMENT_MULTIPLY : +// case LSL_ASSIGNMENT_SUBTRACT : +// case LSL_ASSIGNMENT_ADD : +// case LSL_ASSIGNMENT_CONCATENATE : case LSL_PARENTHESIS_OPEN : case LSL_PARENTHESIS_CLOSE : - case LSL_BRACKET_OPEN : - case LSL_BRACKET_CLOSE : - case LSL_ANGLE_OPEN : - case LSL_ANGLE_CLOSE : - case LSL_TYPECAST : - case LSL_BIT_NOT : - case LSL_BOOL_NOT : - case LSL_NEGATION : +// case LSL_BRACKET_OPEN : +// case LSL_BRACKET_CLOSE : +// case LSL_ANGLE_OPEN : +// case LSL_ANGLE_CLOSE : +// case LSL_TYPECAST : +// case LSL_BIT_NOT : +// case LSL_BOOL_NOT : +// case LSL_NEGATION : break; - case LSL_DIVIDE : return evaluateExpression(exp->left, old) / evaluateExpression(exp->right, old); - case LSL_MODULO : return evaluateExpression(exp->left, old) % evaluateExpression(exp->right, old); +// case LSL_DIVIDE : return evaluateExpression(exp->left, old) / evaluateExpression(exp->right, old); +// case LSL_MODULO : return evaluateExpression(exp->left, old) % evaluateExpression(exp->right, old); case LSL_MULTIPLY : return evaluateExpression(exp->left, old) * evaluateExpression(exp->right, old); - case LSL_DOT_PRODUCT : break; - case LSL_CROSS_PRODUCT : break; - case LSL_SUBTRACT : return evaluateExpression(exp->left, old) - evaluateExpression(exp->right, old); +// case LSL_DOT_PRODUCT : break; +// case LSL_CROSS_PRODUCT : break; +// case LSL_SUBTRACT : return evaluateExpression(exp->left, old) - evaluateExpression(exp->right, old); case LSL_ADD : return evaluateExpression(exp->left, old) + evaluateExpression(exp->right, old); - case LSL_CONCATENATE : break; - case LSL_LEFT_SHIFT : return evaluateExpression(exp->left, old) << evaluateExpression(exp->right, old); - case LSL_RIGHT_SHIFT : return evaluateExpression(exp->left, old) >> evaluateExpression(exp->right, old); - case LSL_LESS_THAN : return evaluateExpression(exp->left, old) < evaluateExpression(exp->right, old); - case LSL_GREATER_THAN : return evaluateExpression(exp->left, old) > evaluateExpression(exp->right, old); - case LSL_LESS_EQUAL : return evaluateExpression(exp->left, old) <= evaluateExpression(exp->right, old); - case LSL_GREATER_EQUAL : return evaluateExpression(exp->left, old) >= evaluateExpression(exp->right, old); - case LSL_EQUAL : return evaluateExpression(exp->left, old) == evaluateExpression(exp->right, old); - case LSL_NOT_EQUAL : return evaluateExpression(exp->left, old) != evaluateExpression(exp->right, old); - case LSL_BIT_AND : return evaluateExpression(exp->left, old) & evaluateExpression(exp->right, old); - case LSL_BIT_XOR : return evaluateExpression(exp->left, old) ^ evaluateExpression(exp->right, old); - case LSL_BIT_OR : return evaluateExpression(exp->left, old) | evaluateExpression(exp->right, old); - case LSL_BOOL_OR : return evaluateExpression(exp->left, old) || evaluateExpression(exp->right, old); - case LSL_BOOL_AND : return evaluateExpression(exp->left, old) && evaluateExpression(exp->right, old); +// case LSL_CONCATENATE : break; +// case LSL_LEFT_SHIFT : return evaluateExpression(exp->left, old) << evaluateExpression(exp->right, old); +// case LSL_RIGHT_SHIFT : return evaluateExpression(exp->left, old) >> evaluateExpression(exp->right, old); +// case LSL_LESS_THAN : return evaluateExpression(exp->left, old) < evaluateExpression(exp->right, old); +// case LSL_GREATER_THAN : return evaluateExpression(exp->left, old) > evaluateExpression(exp->right, old); +// case LSL_LESS_EQUAL : return evaluateExpression(exp->left, old) <= evaluateExpression(exp->right, old); +// case LSL_GREATER_EQUAL : return evaluateExpression(exp->left, old) >= evaluateExpression(exp->right, old); +// case LSL_EQUAL : return evaluateExpression(exp->left, old) == evaluateExpression(exp->right, old); +// case LSL_NOT_EQUAL : return evaluateExpression(exp->left, old) != evaluateExpression(exp->right, old); +// case LSL_BIT_AND : return evaluateExpression(exp->left, old) & evaluateExpression(exp->right, old); +// case LSL_BIT_XOR : return evaluateExpression(exp->left, old) ^ evaluateExpression(exp->right, old); +// case LSL_BIT_OR : return evaluateExpression(exp->left, old) | evaluateExpression(exp->right, old); +// case LSL_BOOL_OR : return evaluateExpression(exp->left, old) || evaluateExpression(exp->right, old); +// case LSL_BOOL_AND : return evaluateExpression(exp->left, old) && evaluateExpression(exp->right, old); } break; } - case LSL_DO : - case LSL_FOR : - case LSL_IF : - case LSL_ELSE : - case LSL_ELSEIF : - case LSL_JUMP : - case LSL_STATE_CHANGE : - case LSL_WHILE : - case LSL_RETURN : - case LSL_STATEMENT : - case LSL_BLOCK : - case LSL_PARAMETER : - case LSL_FUNCTION : - case LSL_STATE : - case LSL_SCRIPT : - break; +// case LSL_DO : +// case LSL_FOR : +// case LSL_IF : +// case LSL_ELSE : +// case LSL_ELSEIF : +// case LSL_JUMP : +// case LSL_STATE_CHANGE : +// case LSL_WHILE : +// case LSL_RETURN : +// case LSL_STATEMENT : +// case LSL_BLOCK : +// case LSL_PARAMETER : +// case LSL_FUNCTION : +// case LSL_STATE : +// case LSL_SCRIPT : +// break; } return old; @@ -198,43 +198,45 @@ void outputExpression(LSL_Expression *exp) { switch(exp->type) { - case LSL_COMMENT : return; - case LSL_TYPE : return; - case LSL_NAME : return; - case LSL_IDENTIFIER : return; - case LSL_FLOAT : printf("%f", exp->value.floatValue); break; +// case LSL_COMMENT : return; +// case LSL_TYPE : return; +// case LSL_NAME : return; +// case LSL_IDENTIFIER : return; +// case LSL_FLOAT : printf("%f", exp->value.floatValue); break; case LSL_INTEGER : printf("%d", exp->value.integerValue); break; - case LSL_STRING : return; - case LSL_KEY : return; - case LSL_VECTOR : return; - case LSL_ROTATION : return; - case LSL_LIST : return; - case LSL_LABEL : return; +// case LSL_STRING : return; +// case LSL_KEY : return; +// case LSL_VECTOR : return; +// case LSL_ROTATION : return; +// case LSL_LIST : return; +// case LSL_LABEL : return; case LSL_EXPRESSION : outputExpression(exp->left); - printf(" %s ", LSL_Tokens[exp->expression].token); +// printf(" %s ", LSL_Tokens[exp->expression].token); +printf(" # "); outputExpression(exp->right); break; - case LSL_DO : return; - case LSL_FOR : return; - case LSL_IF : return; - case LSL_ELSE : return; - case LSL_ELSEIF : return; - case LSL_JUMP : return; - case LSL_STATE_CHANGE : return; - case LSL_WHILE : return; - case LSL_RETURN : return; - case LSL_STATEMENT : return; - case LSL_BLOCK : return; - case LSL_PARAMETER : return; - case LSL_FUNCTION : return; - case LSL_STATE : return; - case LSL_SCRIPT : return; +// case LSL_DO : return; +// case LSL_FOR : return; +// case LSL_IF : return; +// case LSL_ELSE : return; +// case LSL_ELSEIF : return; +// case LSL_JUMP : return; +// case LSL_STATE_CHANGE : return; +// case LSL_WHILE : return; +// case LSL_RETURN : return; +// case LSL_STATEMENT : return; +// case LSL_BLOCK : return; +// case LSL_PARAMETER : return; +// case LSL_FUNCTION : return; +// case LSL_STATE : return; +// case LSL_SCRIPT : return; } } void convertExpression2Lua(LSL_Expression *exp) { +/* switch(exp->type) { case LSL_COMMENT : return; @@ -266,6 +268,7 @@ void convertExpression2Lua(LSL_Expression *exp) case LSL_STATE : return; case LSL_SCRIPT : return; } +*/ } int yyerror(const char *msg) diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index a5d090e..7973509 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -8,6 +8,8 @@ #define YY_NO_UNISTD_H 1 #endif // YY_NO_UNISTD_H +#include "LuaSL_yaccer.tab.h" + // http://w-hat.com/stackdepth is a useful discussion about some aspects of the LL parser. typedef enum @@ -20,6 +22,7 @@ typedef enum LSL_CREATION = 16 } LSL_Flags; +/* typedef enum // In order of precedence, high to low. // Left to right, unless oterwise stated. // According to http://wiki.secondlife.com/wiki/Category:LSL_Operators @@ -69,6 +72,9 @@ typedef enum // In order of precedence, high to low. LSL_BOOL_OR, LSL_BOOL_AND } LSL_Operation; +*/ + +typedef int LSL_Operation; typedef struct { @@ -131,6 +137,7 @@ LSL_Operator LSL_Tokens[] = }; #endif +/* typedef enum { LSL_COMMENT, @@ -162,6 +169,11 @@ typedef enum LSL_STATE, LSL_SCRIPT } LSL_Type; +*/ + +typedef int LSL_Type; +#define LSL_EXPRESSION 1 + #ifdef LSL_Keywords_define char *LSL_Keywords[] = @@ -285,29 +297,6 @@ typedef union LSL_Leaf LSL_Function *functionValue; LSL_State *stateValue; LSL_Script *scriptValue; -} LSL_Leaf; - -typedef struct LSL_AST -{ - struct LSL_AST *left; - struct LSL_AST *right; - int line; - int character; - LSL_Type type; - LSL_Leaf content; -} LSL_AST; - - -/** - * @brief The structure used by flex and bison - */ -//typedef union tagTypeParser -//{ -// SExpression *expression; -// int value; -// int ival; -// float fval; -// char *sval; // class LLScriptType *type; // class LLScriptConstant *constant; // class LLScriptIdentifier *identifier; @@ -322,7 +311,18 @@ typedef struct LSL_AST // class LLScriptState *state; // class LLScritpGlobalStorage *global_store; // class LLScriptScript *script; -//}STypeParser; +} LSL_Leaf; + +typedef struct LSL_AST +{ + struct LSL_AST *left; + struct LSL_AST *right; + int line; + int character; + LSL_Type type; + LSL_Leaf content; +} LSL_AST; + // define the type for flex and bison #define YYSTYPE LSL_Leaf @@ -359,8 +359,6 @@ void convertExpression2Lua(LSL_Expression *exp); int yyerror(const char *msg); int yyparse(void *param); -#include "LuaSL_yaccer.tab.h" - #endif // __EXPRESSION_H__ diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index d204916..478e62c 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l @@ -8,24 +8,21 @@ %option reentrant noyywrap never-interactive nounistd %option bison-bridge -LPAREN "(" -RPAREN ")" -PLUS "+" -MULTIPLY "*" - +SPACE [ \r\n\t]* NUMBER [0-9]+ -WS [ \r\n\t]* +NAME [a-zA-Z][a-zA-Z0-9]* %% -{WS} { /* Skip blanks. */ } -{NUMBER} { sscanf(yytext,"%d",&yylval->integerValue); return TOKEN_NUMBER; } +{SPACE} { /* Skip blanks. */ } +{NUMBER} { yylval->integerValue = atoi(yytext); return LSL_INTEGER; } +{NAME} { /* yylval->nameValue=strdup(yytext); return LSL_NAME; */ } -{MULTIPLY} { return TOKEN_MULTIPLY; } -{PLUS} { return TOKEN_PLUS; } -{LPAREN} { return TOKEN_LPAREN; } -{RPAREN} { return TOKEN_RPAREN; } -. { } +"*" { return LSL_MULTIPLY; } +"+" { return LSL_ADD; } +"(" { return LSL_PARENTHESIS_OPEN; } +")" { return LSL_PARENTHESIS_CLOSE; } +. { /* This should return an "unknown character" error */ } %% diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y index 820f7da..897a83e 100644 --- a/LuaSL/src/LuaSL_yaccer.y +++ b/LuaSL/src/LuaSL_yaccer.y @@ -6,15 +6,15 @@ %define api.pure -%left '+' TOKEN_PLUS -%left '*' TOKEN_MULTIPLY +%left '+' LSL_ADD +%left '*' LSL_MULTIPLY -%token TOKEN_LPAREN -%token TOKEN_RPAREN -%token TOKEN_PLUS -%token TOKEN_MULTIPLY +%token LSL_PARENTHESIS_OPEN +%token LSL_PARENTHESIS_CLOSE +%token LSL_ADD +%token LSL_MULTIPLY -%token TOKEN_NUMBER +%token LSL_INTEGER %type expr @@ -25,10 +25,10 @@ input: ; expr: - expr TOKEN_PLUS expr { $$ = addOperation( LSL_ADD, $1, $3 ); } - | expr TOKEN_MULTIPLY expr { $$ = addOperation( LSL_MULTIPLY, $1, $3 ); } - | TOKEN_LPAREN expr TOKEN_RPAREN { $$ = $2; } - | TOKEN_NUMBER { $$ = addInteger($1); } + expr LSL_ADD expr { $$ = addOperation( LSL_ADD, $1, $3 ); } + | expr LSL_MULTIPLY expr { $$ = addOperation( LSL_MULTIPLY, $1, $3 ); } + | LSL_PARENTHESIS_OPEN expr LSL_PARENTHESIS_CLOSE { $$ = $2; } + | LSL_INTEGER { $$ = addInteger($1); } ; %% -- cgit v1.1