From 21ae463bfa5ff2398734605fb04fb8d585ee879f Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Tue, 10 Jan 2012 02:47:49 +1000 Subject: Move more things into the LSL_Leaf structure, and some clean up related to that. --- LuaSL/src/LuaSL_LSL_tree.c | 140 ++++++++++++++++++++------------------- LuaSL/src/LuaSL_LSL_tree.h | 43 ++++++------ LuaSL/src/LuaSL_lexer.l | 158 +++++++++++++++++++++++---------------------- LuaSL/src/LuaSL_yaccer.y | 5 ++ 4 files changed, 173 insertions(+), 173 deletions(-) diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index ff7da8b..016dbc9 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c @@ -3,10 +3,10 @@ #include #include -static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); -static void evaluateNoToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); -static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); -static void evaluateStatementToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right); +static void evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); +static void evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); +static void evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); +static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); static void outputIntegerToken(LSL_Leaf *content); static void outputOperationToken(LSL_Leaf *content); static void outputStatementToken(LSL_Leaf *content); @@ -14,8 +14,8 @@ static void outputSpaceToken(LSL_Leaf *content); LSL_Token LSL_Tokens[] = { -// {LSL_COMMENT, "/*", LSL_NONE, NULL, NULL, NULL}, -// {LSL_COMMENT_LINE, "//", LSL_NONE, NULL, NULL, NULL}, + {LSL_COMMENT, "/*", LSL_NONE, NULL, NULL, NULL}, + {LSL_COMMENT_LINE, "//", LSL_NONE, NULL, NULL, NULL}, {LSL_SPACE, " ", LSL_NONE, outputSpaceToken, NULL, NULL}, // Operators, in order of precedence, low to high @@ -95,7 +95,7 @@ LSL_Token LSL_Tokens[] = // Then the rest of the syntax tokens. -// {LSL_IDENTIFIER, "identifier", LSL_NONE, NULL, NULL, NULL}, + {LSL_IDENTIFIER, "identifier", LSL_NONE, NULL, NULL, NULL}, {LSL_LABEL, "@", LSL_NONE, NULL, NULL, NULL}, @@ -117,7 +117,7 @@ LSL_Token LSL_Tokens[] = // {LSL_STATE, "state", LSL_NONE, NULL, NULL, NULL}, // {LSL_SCRIPT, "script", LSL_NONE, NULL, NULL, NULL}, -// {LSL_UNKNOWN, "unknown", LSL_NONE, NULL, NULL, NULL}, + {LSL_UNKNOWN, "unknown", LSL_NONE, NULL, NULL, NULL}, // A sentinal. @@ -136,9 +136,7 @@ static LSL_AST *newAST(LSL_Type type, LSL_AST *left, LSL_AST *right) ast->left = left; ast->right = right; - ast->line = -1; - ast->character = -1; - ast->token = tokens[type - lowestToken]; + ast->content.token = tokens[type - lowestToken]; return ast; } @@ -224,8 +222,7 @@ LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr) if (stat == NULL) return NULL; - stat->type = type; - stat->expression = expr; + stat->expressions = expr; return stat; } @@ -240,21 +237,21 @@ LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root) return checkAndAddIgnorable(ast); } -static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right) +static void evaluateAST(LSL_AST *ast, LSL_Leaf *left, LSL_Leaf *right) { if (ast) { - LSL_Value lresult; - LSL_Value rresult; + LSL_Leaf lresult; + LSL_Leaf rresult; - memcpy(&lresult, left, sizeof(LSL_Value)); - memcpy(&rresult, right, sizeof(LSL_Value)); + memcpy(&lresult, left, sizeof(LSL_Leaf)); + memcpy(&rresult, right, sizeof(LSL_Leaf)); - if (LSL_RIGHT2LEFT & ast->token->flags) + if (LSL_RIGHT2LEFT & ast->content.token->flags) { - memcpy(&rresult, left, sizeof(LSL_Value)); + memcpy(&rresult, left, sizeof(LSL_Leaf)); evaluateAST(ast->right, &rresult, right); - if (!(LSL_UNARY & ast->token->flags)) + if (!(LSL_UNARY & ast->content.token->flags)) { evaluateAST(ast->left, &lresult, right); } @@ -262,50 +259,51 @@ static void evaluateAST(LSL_AST *ast, LSL_Value *left, LSL_Value *right) else // Assume left to right. { evaluateAST(ast->left, &lresult, right); - if (!(LSL_UNARY & ast->token->flags)) + if (!(LSL_UNARY & ast->content.token->flags)) { - memcpy(&rresult, left, sizeof(LSL_Value)); + memcpy(&rresult, left, sizeof(LSL_Leaf)); evaluateAST(ast->right, &rresult, right); } } - if (ast->token->evaluate) + if (ast->content.token->evaluate) { - ast->token->evaluate(&(ast->content), &lresult, &rresult); - memcpy(left, &lresult, sizeof(LSL_Value)); + ast->content.token->evaluate(&(ast->content), &lresult, &rresult); + memcpy(left, &lresult, sizeof(LSL_Leaf)); } else { #ifdef LUASL_DEBUG - printf(" eval <%s %d %d %d %d> ", ast->token->token, left->content.value.integerValue, right->content.value.integerValue, lresult.content.value.integerValue, rresult.content.value.integerValue); + printf(" eval <%s %d %d %d %d> ", ast->content.token->token, left->value.integerValue, right->value.integerValue, lresult.value.integerValue, rresult.value.integerValue); #endif - memcpy(left, &rresult, sizeof(LSL_Value)); + memcpy(left, &rresult, sizeof(LSL_Leaf)); } } } -static void evaluateIntegerToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) +static void evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { if (content) { #ifdef LUASL_DEBUG printf(" <%d> ", content->value.integerValue); #endif - left->content.value.integerValue = content->value.integerValue; + left->value.integerValue = content->value.integerValue; left->type = LSL_INTEGER; } } -static void evaluateNoToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) +static void evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { + // Do nothing, that's the point. } -static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) +static void evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { if ((content) && (content->value.operationValue)) { #ifdef LUASL_DEBUG - printf(" [%s] ", tokens[content->value.operationValue - lowestToken]->token); + printf(" [%s] ", content->token->token); #endif switch (content->value.operationValue) @@ -332,41 +330,41 @@ static void evaluateOperationToken(LSL_Leaf *content, LSL_Value *left, LSL_Value // case LSL_TYPECAST_OPEN : // case LSL_TYPECAST_CLOSE : break; - case LSL_BIT_NOT : left->content.value.integerValue = ~ right->content.value.integerValue; break; - case LSL_BOOL_NOT : left->content.value.integerValue = ! right->content.value.integerValue; break; - case LSL_NEGATION : left->content.value.integerValue = 0 - right->content.value.integerValue; break; - case LSL_DIVIDE : left->content.value.integerValue = left->content.value.integerValue / right->content.value.integerValue; break; - case LSL_MODULO : left->content.value.integerValue = left->content.value.integerValue % right->content.value.integerValue; break; - case LSL_MULTIPLY : left->content.value.integerValue = left->content.value.integerValue * right->content.value.integerValue; break; + case LSL_BIT_NOT : left->value.integerValue = ~ right->value.integerValue; break; + case LSL_BOOL_NOT : left->value.integerValue = ! right->value.integerValue; break; + case LSL_NEGATION : left->value.integerValue = 0 - right->value.integerValue; break; + case LSL_DIVIDE : left->value.integerValue = left->value.integerValue / right->value.integerValue; break; + case LSL_MODULO : left->value.integerValue = left->value.integerValue % right->value.integerValue; break; + case LSL_MULTIPLY : left->value.integerValue = left->value.integerValue * right->value.integerValue; break; // case LSL_DOT_PRODUCT : break; // case LSL_CROSS_PRODUCT : break; - case LSL_SUBTRACT : left->content.value.integerValue = left->content.value.integerValue - right->content.value.integerValue; break; - case LSL_ADD : left->content.value.integerValue = left->content.value.integerValue + right->content.value.integerValue; break; + case LSL_SUBTRACT : left->value.integerValue = left->value.integerValue - right->value.integerValue; break; + case LSL_ADD : left->value.integerValue = left->value.integerValue + right->value.integerValue; break; // case LSL_CONCATENATE : break; - case LSL_LEFT_SHIFT : left->content.value.integerValue = left->content.value.integerValue << right->content.value.integerValue; break; - case LSL_RIGHT_SHIFT : left->content.value.integerValue = left->content.value.integerValue >> right->content.value.integerValue; break; - case LSL_LESS_THAN : left->content.value.integerValue = left->content.value.integerValue < right->content.value.integerValue; break; - case LSL_GREATER_THAN : left->content.value.integerValue = left->content.value.integerValue > right->content.value.integerValue; break; - case LSL_LESS_EQUAL : left->content.value.integerValue = left->content.value.integerValue <= right->content.value.integerValue; break; - case LSL_GREATER_EQUAL : left->content.value.integerValue = left->content.value.integerValue >= right->content.value.integerValue; break; - case LSL_EQUAL : left->content.value.integerValue = left->content.value.integerValue == right->content.value.integerValue; break; - case LSL_NOT_EQUAL : left->content.value.integerValue = left->content.value.integerValue != right->content.value.integerValue; break; - case LSL_BIT_AND : left->content.value.integerValue = left->content.value.integerValue & right->content.value.integerValue; break; - case LSL_BIT_XOR : left->content.value.integerValue = left->content.value.integerValue ^ right->content.value.integerValue; break; - case LSL_BIT_OR : left->content.value.integerValue = left->content.value.integerValue | right->content.value.integerValue; break; - case LSL_BOOL_OR : left->content.value.integerValue = left->content.value.integerValue || right->content.value.integerValue; break; - case LSL_BOOL_AND : left->content.value.integerValue = left->content.value.integerValue && right->content.value.integerValue; break; + case LSL_LEFT_SHIFT : left->value.integerValue = left->value.integerValue << right->value.integerValue; break; + case LSL_RIGHT_SHIFT : left->value.integerValue = left->value.integerValue >> right->value.integerValue; break; + case LSL_LESS_THAN : left->value.integerValue = left->value.integerValue < right->value.integerValue; break; + case LSL_GREATER_THAN : left->value.integerValue = left->value.integerValue > right->value.integerValue; break; + case LSL_LESS_EQUAL : left->value.integerValue = left->value.integerValue <= right->value.integerValue; break; + case LSL_GREATER_EQUAL : left->value.integerValue = left->value.integerValue >= right->value.integerValue; break; + case LSL_EQUAL : left->value.integerValue = left->value.integerValue == right->value.integerValue; break; + case LSL_NOT_EQUAL : left->value.integerValue = left->value.integerValue != right->value.integerValue; break; + case LSL_BIT_AND : left->value.integerValue = left->value.integerValue & right->value.integerValue; break; + case LSL_BIT_XOR : left->value.integerValue = left->value.integerValue ^ right->value.integerValue; break; + case LSL_BIT_OR : left->value.integerValue = left->value.integerValue | right->value.integerValue; break; + case LSL_BOOL_OR : left->value.integerValue = left->value.integerValue || right->value.integerValue; break; + case LSL_BOOL_AND : left->value.integerValue = left->value.integerValue && right->value.integerValue; break; } #ifdef LUASL_DEBUG - printf(" (=%d) ", left->content.value.integerValue); + printf(" (=%d) ", left->value.integerValue); #endif } } -static void evaluateStatementToken(LSL_Leaf *content, LSL_Value *left, LSL_Value *right) +static void evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right) { if (content) - evaluateAST(content->value.statementValue->expression, left, right); + evaluateAST(content->value.statementValue->expressions, left, right); } static void outputAST(LSL_AST *ast) @@ -374,10 +372,10 @@ static void outputAST(LSL_AST *ast) if (ast) { outputAST(ast->left); - if (ast->token->output) - ast->token->output(&(ast->content)); + if (ast->content.token->output) + ast->content.token->output(&(ast->content)); else - printf("%s", ast->token->token); + printf("%s", ast->content.token->token); outputAST(ast->right); } } @@ -391,13 +389,13 @@ static void outputIntegerToken(LSL_Leaf *content) static void outputOperationToken(LSL_Leaf *content) { if (content) - printf("%s", tokens[content->value.operationValue - lowestToken]->token); + printf("%s", content->token->token); } static void outputStatementToken(LSL_Leaf *content) { if (content) - outputAST(content->value.statementValue->expression); + outputAST(content->value.statementValue->expressions); printf(";"); } @@ -412,12 +410,12 @@ static void convertAST2Lua(LSL_AST *ast) if (ast) { convertAST2Lua(ast->left); - if (ast->token->convert) - ast->token->convert(&(ast->content)); - else if (ast->token->output) - ast->token->output(&(ast->content)); + if (ast->content.token->convert) + ast->content.token->convert(&(ast->content)); + else if (ast->content.token->output) + ast->content.token->output(&(ast->content)); else - printf("%s", ast->token->token); + printf("%s", ast->content.token->token); convertAST2Lua(ast->right); } } @@ -539,11 +537,11 @@ int main(int argc, char **argv) if (ast) { - LSL_Value left, right; + LSL_Leaf left, right; - left.content.value.integerValue = 0; + left.value.integerValue = 0; left.type = LSL_INTEGER; - right.content.value.integerValue = 0; + right.value.integerValue = 0; right.type = LSL_INTEGER; evaluateAST(ast, &left, &right); @@ -553,7 +551,7 @@ int main(int argc, char **argv) printf("Result of -\n"); outputAST(ast); printf("\n"); - printf("is %d %d. And converted to Lua it is -\n", left.content.value.integerValue, right.content.value.integerValue); + printf("is %d %d. And converted to Lua it is -\n", left.value.integerValue, right.value.integerValue); convertAST2Lua(ast); printf("\n"); burnAST(ast); diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index fe56c29..5d39b3b 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -20,8 +20,8 @@ extern int yydebug; // http://w-hat.com/stackdepth is a useful discussion about some aspects of the LL parser. +typedef struct _LSL_Token LSL_Token; typedef struct _LSL_Leaf LSL_Leaf; -typedef struct _LSL_Value LSL_Value; typedef struct _LSL_Identifier LSL_Identifier; typedef struct _LSL_Statement LSL_Statement; typedef struct _LSL_Block LSL_Block; @@ -30,11 +30,14 @@ typedef struct _LSL_State LSL_State; typedef struct _LSL_Script LSL_Script; typedef struct _LSL_AST LSL_AST; +extern LSL_Token **tokens; +extern int lowestToken; + typedef int LSL_Type; typedef void (*convertToken2Lua) (LSL_Leaf *content); typedef void (*outputToken) (LSL_Leaf *content); -typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Value *left, LSL_Value *right); +typedef void (*evaluateToken) (LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); #ifndef FALSE typedef enum @@ -55,7 +58,7 @@ typedef enum LSL_CREATION = 32 } LSL_Flags; -typedef struct +struct _LSL_Token { LSL_Type type; char *token; @@ -63,7 +66,7 @@ typedef struct outputToken output; convertToken2Lua convert; evaluateToken evaluate; -} LSL_Token; +}; struct _LSL_Leaf { @@ -72,13 +75,13 @@ struct _LSL_Leaf char *commentValue; char *spaceValue; - LSL_Type operationValue; + LSL_Type operationValue; LSL_AST *expressionValue; float floatValue; - int integerValue; + int integerValue; char *keyValue; - LSL_Leaf *listValue; + LSL_Leaf *listValue; char *stringValue; float vectorValue[3]; float rotationValue[4]; @@ -97,34 +100,29 @@ struct _LSL_Leaf LSL_Statement *whileValue; LSL_Statement *statementValue; - LSL_Block *blockValue; + LSL_Block *blockValue; LSL_Identifier *parameterValue; LSL_Function *functionValue; - LSL_State *stateValue; - LSL_Script *scriptValue; + LSL_State *stateValue; + LSL_Script *scriptValue; char *unknownValue; } value; - char *ignorableText; - int line, column; -}; - -struct _LSL_Value -{ - LSL_Leaf content; + char *ignorableText; + LSL_Token *token; LSL_Type type; + int line, column; }; struct _LSL_Identifier // For variables and function parameters. { char *name; - LSL_Value value; + LSL_Leaf value; }; struct _LSL_Statement { - LSL_AST *expression; - LSL_Type type; + LSL_AST *expressions; /// For things like a for statement, might hold three expressions. }; struct _LSL_Block @@ -137,7 +135,7 @@ struct _LSL_Function char *name; LSL_Block block; LSL_Identifier *parameters; - LSL_Type type; + LSL_Type type; // Return type. }; struct _LSL_State @@ -158,10 +156,7 @@ struct _LSL_AST { LSL_AST *left; LSL_AST *right; - LSL_Token *token; LSL_Leaf content; - int line; - int character; }; diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index 08a68ef..30e0b6b 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l @@ -5,8 +5,7 @@ #include void comment(yyscan_t yyscanner); -void common(YYSTYPE *lval, char *text, boolean checkIgnorable); -void ignorable(char *text); +int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type); %} @@ -22,90 +21,90 @@ EXPONANT [eE][+-]?{INTEGER} FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}? CHAR '(\\.|[^\\'\n])+' STRING \"(\\.|[^\\"\n])*\" -NAME [[:alpha:]](_|[[:alpha:]]|[[:digit:]])* +IDENTIFIER [[:alpha:]](_|[[:alpha:]]|[[:digit:]])* %% /* The order here is important, in mysterious ways. The more specific the lower in case of ambiguities like "floats contain integers". I think, not tested that well yet. */ /* Ignorables. */ -[[:space:]]+ %{ common(yylval, yytext, FALSE); ignorable(yytext); %} +[[:space:]]+ %{ common(yylval, yytext, FALSE, LSL_SPACE); %} /* Yes I know this will have problems with huge comments, just being simple to get it to work for now. */ -"/*"([^"*/"]*)"*/" %{ common(yylval, yytext, FALSE); ignorable(yytext); %} -"//"[^\n]* %{ common(yylval, yytext, FALSE); ignorable(yytext); %} +"/*"([^"*/"]*)"*/" %{ common(yylval, yytext, FALSE, LSL_COMMENT); %} +"//"[^\n]* %{ common(yylval, yytext, FALSE, LSL_COMMENT_LINE); %} /* Operations. */ -"&&" { common(yylval, yytext, TRUE); return LSL_BOOL_AND; } -"||" { common(yylval, yytext, TRUE); return LSL_BOOL_OR; } -"|" { common(yylval, yytext, TRUE); return LSL_BIT_OR; } -"^" { common(yylval, yytext, TRUE); return LSL_BIT_XOR; } -"&" { common(yylval, yytext, TRUE); return LSL_BIT_AND; } -"!=" { common(yylval, yytext, TRUE); return LSL_NOT_EQUAL; } -"==" { common(yylval, yytext, TRUE); return LSL_EQUAL; } -">=" { common(yylval, yytext, TRUE); return LSL_GREATER_EQUAL; } -"<=" { common(yylval, yytext, TRUE); return LSL_LESS_EQUAL; } -">" { common(yylval, yytext, TRUE); return LSL_GREATER_THAN; } -"<" { common(yylval, yytext, TRUE); return LSL_LESS_THAN; } -">>" { common(yylval, yytext, TRUE); return LSL_RIGHT_SHIFT; } -"<<" { common(yylval, yytext, TRUE); return LSL_LEFT_SHIFT; } -"+" { common(yylval, yytext, TRUE); return LSL_ADD; } -"-" { common(yylval, yytext, TRUE); return LSL_SUBTRACT; } -"*" { common(yylval, yytext, TRUE); return LSL_MULTIPLY; } -"%" { common(yylval, yytext, TRUE); return LSL_MODULO; } -"/" { common(yylval, yytext, TRUE); return LSL_DIVIDE; } -"!" { common(yylval, yytext, TRUE); return LSL_BOOL_NOT; } -"~" { common(yylval, yytext, TRUE); return LSL_BIT_NOT; } -"[" { common(yylval, yytext, TRUE); return LSL_BRACKET_OPEN; } -"]" { common(yylval, yytext, TRUE); return LSL_BRACKET_CLOSE; } -"(" { common(yylval, yytext, TRUE); return LSL_PARENTHESIS_OPEN; } -")" { common(yylval, yytext, TRUE); return LSL_PARENTHESIS_CLOSE; } -"+=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_ADD; } -"-=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_SUBTRACT; } -"*=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_MULTIPLY; } -"%=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_MODULO; } -"/=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_DIVIDE; } -"=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_PLAIN; } -"." { common(yylval, yytext, TRUE); return LSL_DOT; } -"--" { common(yylval, yytext, TRUE); return LSL_DECREMENT_PRE; } -"++" { common(yylval, yytext, TRUE); return LSL_INCREMENT_PRE; } -"," { common(yylval, yytext, TRUE); return LSL_COMMA; } +"&&" { return common(yylval, yytext, TRUE, LSL_BOOL_AND); } +"||" { return common(yylval, yytext, TRUE, LSL_BOOL_OR); } +"|" { return common(yylval, yytext, TRUE, LSL_BIT_OR); } +"^" { return common(yylval, yytext, TRUE, LSL_BIT_XOR); } +"&" { return common(yylval, yytext, TRUE, LSL_BIT_AND); } +"!=" { return common(yylval, yytext, TRUE, LSL_NOT_EQUAL); } +"==" { return common(yylval, yytext, TRUE, LSL_EQUAL); } +">=" { return common(yylval, yytext, TRUE, LSL_GREATER_EQUAL); } +"<=" { return common(yylval, yytext, TRUE, LSL_LESS_EQUAL); } +">" { return common(yylval, yytext, TRUE, LSL_GREATER_THAN); } +"<" { return common(yylval, yytext, TRUE, LSL_LESS_THAN); } +">>" { return common(yylval, yytext, TRUE, LSL_RIGHT_SHIFT); } +"<<" { return common(yylval, yytext, TRUE, LSL_LEFT_SHIFT); } +"+" { return common(yylval, yytext, TRUE, LSL_ADD); } +"-" { return common(yylval, yytext, TRUE, LSL_SUBTRACT); } +"*" { return common(yylval, yytext, TRUE, LSL_MULTIPLY); } +"%" { return common(yylval, yytext, TRUE, LSL_MODULO); } +"/" { return common(yylval, yytext, TRUE, LSL_DIVIDE); } +"!" { return common(yylval, yytext, TRUE, LSL_BOOL_NOT); } +"~" { return common(yylval, yytext, TRUE, LSL_BIT_NOT); } +"[" { return common(yylval, yytext, TRUE, LSL_BRACKET_OPEN); } +"]" { return common(yylval, yytext, TRUE, LSL_BRACKET_CLOSE); } +"(" { return common(yylval, yytext, TRUE, LSL_PARENTHESIS_OPEN); } +")" { return common(yylval, yytext, TRUE, LSL_PARENTHESIS_CLOSE); } +"+=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_ADD); } +"-=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_SUBTRACT); } +"*=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_MULTIPLY); } +"%=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_MODULO); } +"/=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_DIVIDE); } +"=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_PLAIN); } +"." { return common(yylval, yytext, TRUE, LSL_DOT); } +"--" { return common(yylval, yytext, TRUE, LSL_DECREMENT_PRE); } +"++" { return common(yylval, yytext, TRUE, LSL_INCREMENT_PRE); } +"," { return common(yylval, yytext, TRUE, LSL_COMMA); } /* Types. */ -{INTEGER} %{ common(yylval, yytext, TRUE); yylval->value.integerValue = atoi(yytext); return LSL_INTEGER; %} -{FLOAT} %{ common(yylval, yytext, TRUE); yylval->value.floatValue = atof(yytext); return LSL_FLOAT; %} +{INTEGER} %{ yylval->value.integerValue = atoi(yytext); return common(yylval, yytext, TRUE, LSL_INTEGER); %} +{FLOAT} %{ yylval->value.floatValue = atof(yytext); return common(yylval, yytext, TRUE, LSL_FLOAT); %} /* Type keywords. */ -"float" %{ common(yylval, yytext, TRUE); return LSL_TYPE_FLOAT; %} -"integer" %{ common(yylval, yytext, TRUE); return LSL_TYPE_INTEGER; %} -"key" %{ common(yylval, yytext, TRUE); return LSL_TYPE_KEY; %} -"list" %{ common(yylval, yytext, TRUE); return LSL_TYPE_LIST; %} -"quaternion" %{ common(yylval, yytext, TRUE); return LSL_TYPE_ROTATION; %} -"rotation" %{ common(yylval, yytext, TRUE); return LSL_TYPE_ROTATION; %} -"string" %{ common(yylval, yytext, TRUE); return LSL_TYPE_STRING; %} -"vector" %{ common(yylval, yytext, TRUE); return LSL_TYPE_VECTOR; %} +"float" %{ return common(yylval, yytext, TRUE, LSL_TYPE_FLOAT); %} +"integer" %{ return common(yylval, yytext, TRUE, LSL_TYPE_INTEGER); %} +"key" %{ return common(yylval, yytext, TRUE, LSL_TYPE_KEY); %} +"list" %{ return common(yylval, yytext, TRUE, LSL_TYPE_LIST); %} +"quaternion" %{ return common(yylval, yytext, TRUE, LSL_TYPE_ROTATION); %} +"rotation" %{ return common(yylval, yytext, TRUE, LSL_TYPE_ROTATION); %} +"string" %{ return common(yylval, yytext, TRUE, LSL_TYPE_STRING); %} +"vector" %{ return common(yylval, yytext, TRUE, LSL_TYPE_VECTOR); %} /* Statement keywords. */ -"do" %{ common(yylval, yytext, TRUE); return LSL_DO; %} -"for" %{ common(yylval, yytext, TRUE); return LSL_FOR; %} -"else" %{ common(yylval, yytext, TRUE); return LSL_ELSE; %} -"if" %{ common(yylval, yytext, TRUE); return LSL_IF; %} -"jump" %{ common(yylval, yytext, TRUE); return LSL_JUMP; %} -"return" %{ common(yylval, yytext, TRUE); return LSL_RETURN; %} -"state" %{ common(yylval, yytext, TRUE); return LSL_STATE_CHANGE; %} -"while" %{ common(yylval, yytext, TRUE); return LSL_WHILE; %} +"do" %{ return common(yylval, yytext, TRUE, LSL_DO); %} +"for" %{ return common(yylval, yytext, TRUE, LSL_FOR); %} +"else" %{ return common(yylval, yytext, TRUE, LSL_ELSE); %} +"if" %{ return common(yylval, yytext, TRUE, LSL_IF); %} +"jump" %{ return common(yylval, yytext, TRUE, LSL_JUMP); %} +"return" %{ return common(yylval, yytext, TRUE, LSL_RETURN); %} +"state" %{ return common(yylval, yytext, TRUE, LSL_STATE_CHANGE); %} +"while" %{ return common(yylval, yytext, TRUE, LSL_WHILE); %} -{NAME} %{ common(yylval, yytext, TRUE); /* yylval->value.nameValue = strdup(yytext); return LSL_NAME; */ %} +{IDENTIFIER} %{ /* yylval->value.nameValue = strdup(yytext); */ common(yylval, yytext, TRUE, LSL_IDENTIFIER); %} /* Other symbols. */ -"@" %{ common(yylval, yytext, TRUE); return LSL_LABEL; %} -"{" %{ common(yylval, yytext, TRUE); return LSL_BLOCK_OPEN; %} -"}" %{ common(yylval, yytext, TRUE); return LSL_BLOCK_CLOSE; %} -";" %{ common(yylval, yytext, TRUE); return LSL_STATEMENT; %} +"@" %{ return common(yylval, yytext, TRUE, LSL_LABEL); %} +"{" %{ return common(yylval, yytext, TRUE, LSL_BLOCK_OPEN); %} +"}" %{ return common(yylval, yytext, TRUE, LSL_BLOCK_CLOSE); %} +";" %{ return common(yylval, yytext, TRUE, LSL_STATEMENT); %} <> { yyterminate(); } /* Everything else */ -. %{ common(yylval, yytext, TRUE); printf(" unexpected character.\n"); %} +. %{ printf(" unexpected character.\n"); yylval->value.unknownValue = strdup(yytext); common(yylval, yytext, TRUE, LSL_UNKNOWN); %} %% @@ -126,7 +125,7 @@ static char *ignorableText = NULL; static int column = 0; static int line = 0; -void common(YYSTYPE *lval, char *text, boolean checkIgnorable) +int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type) { int i; @@ -141,6 +140,8 @@ void common(YYSTYPE *lval, char *text, boolean checkIgnorable) else column++; + lval->type = type; + lval->token = tokens[type - lowestToken]; lval->line = line; lval->column = column; @@ -149,24 +150,25 @@ void common(YYSTYPE *lval, char *text, boolean checkIgnorable) lval->ignorableText = ignorableText; ignorableText = NULL; } + else + { + if (ignorableText) + { + int lenI = strlen(ignorableText); + int lenT = strlen(text); + + ignorableText = realloc(ignorableText, lenI + lenT + 1); + sprintf(&ignorableText[lenI], "%s", text); + } + else + ignorableText = strdup(text); + } #ifdef LUASL_DEBUG printf ("%04d, %04d [%s]\n", line, column, text); #endif -} - -void ignorable(char *text) -{ - if (ignorableText) - { - int lenI = strlen(ignorableText); - int lenT = strlen(text); - ignorableText = realloc(ignorableText, lenI + lenT + 1); - sprintf(&ignorableText[lenI], "%s", text); - } - else - ignorableText = strdup(text); + return type; } int yyerror(const char *msg) diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y index ed69dd3..2176065 100644 --- a/LuaSL/src/LuaSL_yaccer.y +++ b/LuaSL/src/LuaSL_yaccer.y @@ -11,6 +11,11 @@ %token LSL_SPACE /* Never actually emitted, but we need it in the token table. */ +%token LSL_COMMENT /* Never actually emitted, but we need it in the token table. */ +%token LSL_COMMENT_LINE /* Never actually emitted, but we need it in the token table. */ +%token LSL_UNKNOWN /* Never actually emitted, but we need it in the token table. */ + +%token LSL_IDENTIFIER %type expr %left LSL_BOOL_AND -- cgit v1.1