From 64ba572fbac74b39c4d74d2a5b24289e803c6acb Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Thu, 2 Feb 2012 03:16:00 +1000 Subject: Numby magic. --- LuaSL/src/LuaSL_LSL_tree.h | 14 ++++++++++++ LuaSL/src/LuaSL_compile.c | 48 ++++++++++++++++++++++++++++++++++++------ LuaSL/src/LuaSL_lemon_yaccer.y | 4 ++-- LuaSL/src/LuaSL_lexer.l | 4 ++-- 4 files changed, 60 insertions(+), 10 deletions(-) (limited to 'LuaSL') diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 94be1a6..64832d2 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -40,6 +40,7 @@ typedef struct _allowedTypes allowedTypes; typedef struct _LSL_Token LSL_Token; typedef struct _LSL_Text LSL_Text; typedef struct _LSL_Leaf LSL_Leaf; +typedef struct _LSL_Numby LSL_Numby; typedef struct _LSL_Parenthesis LSL_Parenthesis; typedef struct _LSL_Identifier LSL_Identifier; typedef struct _LSL_Statement LSL_Statement; @@ -185,6 +186,7 @@ struct _LSL_Leaf float vectorValue[3]; float rotationValue[4]; int integerValue; + LSL_Numby *numbyValue; LSL_Leaf *listValue; const char *stringValue; opType operationValue; @@ -199,6 +201,17 @@ struct _LSL_Leaf } value; }; +struct _LSL_Numby +{ + LSL_Text text; + LSL_Type type; + union + { + float floatValue; + int integerValue; + } value; +}; + struct _LSL_Parenthesis { LSL_Leaf *contents; @@ -403,6 +416,7 @@ LSL_Leaf *addFunctionBody(LuaSL_compiler *compiler, LSL_Leaf *function, LSL_Leaf LSL_Leaf *addFunctionCall(LuaSL_compiler *compiler, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close); LSL_Leaf *addIfElse(LuaSL_compiler *compiler, LSL_Leaf *ifBlock, LSL_Leaf *elseBlock); LSL_Leaf *addList(LSL_Leaf *left, LSL_Leaf *list, LSL_Leaf *right); +LSL_Leaf *addNumby(LSL_Leaf *numby); LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); LSL_Leaf *addParameter(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *newParam); LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval); diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c index de23b78..73b43dd 100644 --- a/LuaSL/src/LuaSL_compile.c +++ b/LuaSL/src/LuaSL_compile.c @@ -710,6 +710,40 @@ LSL_Leaf *addList(LSL_Leaf *left, LSL_Leaf *list, LSL_Leaf *right) return left; } +LSL_Leaf *addNumby(LSL_Leaf *numby) +{ + LSL_Numby *num = calloc(1, sizeof(LSL_Numby)); + + if ((numby) && (num)) + { + num->text.text = numby->value.stringValue; +#if LUASL_DIFF_CHECK + num->text.ignorable = numby->ignorable; + numby->ignorable = NULL; +#endif + switch (numby->toKen->type) + { + case LSL_FLOAT : + { + num->value.floatValue = atof(num->text.text); + numby->basicType = OT_float; + break; + } + case LSL_INTEGER : + { + num->value.integerValue = atoi(num->text.text); + numby->basicType = OT_integer; + break; + } + default: + break; + } + numby->value.numbyValue = num; + num->type = numby->basicType; + } + return numby; +} + LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval) { LSL_Parenthesis *parens = calloc(1, sizeof(LSL_Parenthesis)); @@ -1103,10 +1137,11 @@ static LSL_Leaf *evaluateFloatToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf if (content && result) { - if (LUASL_DEBUG) - printf(" <%g> ", content->value.floatValue); memcpy(result, content, sizeof(LSL_Leaf)); + result->value.floatValue = content->value.numbyValue->value.floatValue; result->basicType = OT_float; + if (LUASL_DEBUG) + printf(" <%g> ", content->value.floatValue); } return result; } @@ -1117,10 +1152,11 @@ static LSL_Leaf *evaluateIntegerToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Lea if (content && result) { - if (LUASL_DEBUG) - printf(" <%d> ", content->value.integerValue); memcpy(result, content, sizeof(LSL_Leaf)); + result->value.integerValue = content->value.numbyValue->value.integerValue; result->basicType = OT_integer; + if (LUASL_DEBUG) + printf(" <%d> ", result->value.integerValue); } return result; } @@ -1681,7 +1717,7 @@ static void outputCrementsToken(FILE *file, outputMode mode, LSL_Leaf *content) static void outputFloatToken(FILE *file, outputMode mode, LSL_Leaf *content) { if (content) - fprintf(file, "%g", content->value.floatValue); + outputText(file, &(content->value.numbyValue->text), !(LSL_NOIGNORE & content->toKen->flags)); } static void outputFunctionToken(FILE *file, outputMode mode, LSL_Leaf *content) @@ -1734,7 +1770,7 @@ static void outputFunctionCallToken(FILE *file, outputMode mode, LSL_Leaf *conte static void outputIntegerToken(FILE *file, outputMode mode, LSL_Leaf *content) { if (content) - fprintf(file, "%d", content->value.integerValue); + outputText(file, &(content->value.numbyValue->text), !(LSL_NOIGNORE & content->toKen->flags)); } static void outputIdentifierToken(FILE *file, outputMode mode, LSL_Leaf *content) diff --git a/LuaSL/src/LuaSL_lemon_yaccer.y b/LuaSL/src/LuaSL_lemon_yaccer.y index 565a431..d2731d8 100644 --- a/LuaSL/src/LuaSL_lemon_yaccer.y +++ b/LuaSL/src/LuaSL_lemon_yaccer.y @@ -196,9 +196,9 @@ expr(A) ::= LSL_INCREMENT_PRE(C) identifier(B). { A = addCrement(compiler, // Values. %nonassoc LSL_FLOAT. -expr(A) ::= LSL_FLOAT(B). { B->basicType = OT_float; A = B; } +expr(A) ::= LSL_FLOAT(B). { A = addNumby(B); } %nonassoc LSL_INTEGER. -expr(A) ::= LSL_INTEGER(B). { B->basicType = OT_integer; A = B; } +expr(A) ::= LSL_INTEGER(B). { A = addNumby(B); } %nonassoc LSL_KEY. expr(A) ::= LSL_KEY(B). { B->basicType = OT_key; A = B; } %nonassoc LSL_LIST. diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index 4329bb2..adee1a7 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l @@ -104,8 +104,8 @@ STRING \"(\\.|[^\\"\n])*\" {IDENTIFIER} %{ yylval->value.stringValue = eina_stringshare_add_length(yytext, yyleng); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_IDENTIFIER); %} /* Types. */ -{INTEGER} %{ yylval->value.integerValue = atoi(yytext); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_INTEGER); %} -{FLOAT} %{ yylval->value.floatValue = atof(yytext); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_FLOAT); %} +{INTEGER} %{ yylval->value.stringValue = eina_stringshare_add_length(yytext, yyleng); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_INTEGER); %} +{FLOAT} %{ yylval->value.stringValue = eina_stringshare_add_length(yytext, yyleng); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_FLOAT); %} {KEY} %{ yylval->value.stringValue = eina_stringshare_add_length(yytext, yyleng); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_KEY); %} {STRING} %{ yylval->value.stringValue = eina_stringshare_add_length(yytext, yyleng); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_STRING); %} -- cgit v1.1