From 4ca572a29f794c160a1daa96a020eec8d3659a88 Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 5 Jul 2007 23:55:31 +0000 Subject: [PATCH] Added min and max script pseudo-functions git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@497 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/script/context.cpp | 11 +++++++++++ src/script/parser.cpp | 13 ++++++++++++- src/script/script.hpp | 2 ++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/script/context.cpp b/src/script/context.cpp index 680fe7b7..7234ca30 100644 --- a/src/script/context.cpp +++ b/src/script/context.cpp @@ -275,6 +275,15 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) { } \ break +// operator on doubles or ints, defined as a function +#define OPERATOR_FUN_DI(OP) \ + if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \ + a = to_script(OP((double)*a, (double)*b)); \ + } else { \ + a = to_script(OP((int)*a, (int)*b)); \ + } \ + break + // operator on strings or doubles or ints, when in doubt, uses strings #define OPERATOR_SDI(OP) \ if (at == SCRIPT_INT && bt == SCRIPT_INT) { \ @@ -349,6 +358,8 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& case I_GT: OPERATOR_DI(>); case I_LE: OPERATOR_DI(<=); case I_GE: OPERATOR_DI(>=); + case I_MIN: OPERATOR_FUN_DI(min); + case I_MAX: OPERATOR_FUN_DI(max); case I_OR_ELSE: if (at == SCRIPT_ERROR) a = b; break; diff --git a/src/script/parser.cpp b/src/script/parser.cpp index fc692adc..1d2882a7 100644 --- a/src/script/parser.cpp +++ b/src/script/parser.cpp @@ -505,9 +505,20 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) { parseOper(input, script, PREC_ALL); // b expectToken(input, _(")")); script.addInstruction(I_TERNARY, I_RGB); + } else if (token == _("min") || token == _("max")) { + // min(x,y,z,...) + unsigned int op = token == _("min") ? I_MIN : I_MAX; + expectToken(input, _("(")); + parseOper(input, script, PREC_ALL); // first + while(input.peek() == _(",")) { + expectToken(input, _(",")); + parseOper(input, script, PREC_ALL); // second, third, etc. + script.addInstruction(I_BINARY, op); + } + expectToken(input, _(")")); } else { // variable - unsigned int var = string_to_variable(token.value); + Variable var = string_to_variable(token.value); script.addInstruction(I_GET_VAR, var); } } else if (token == TOK_INT) { diff --git a/src/script/script.hpp b/src/script/script.hpp index 07ac54c7..17273e67 100644 --- a/src/script/script.hpp +++ b/src/script/script.hpp @@ -70,6 +70,8 @@ enum BinaryInstructionType , I_GT ///< operator > , I_LE ///< operator <= , I_GE ///< operator >= +, I_MIN ///< minimum +, I_MAX ///< maximum // Error handling , I_OR_ELSE ///< if a != error then a else b };