| 1 |
Math::Script NSIS plugin. |
| 2 |
|
| 3 |
C-like style scripting (operators at least). |
| 4 |
Tip1: plugin watches the case of the letters. |
| 5 |
Tip2: plugin makes almost no error checks. So YOU should check your script |
| 6 |
twice before run :) |
| 7 |
|
| 8 |
New HOW TO USE: run the MathTest.Exe, and try yourself. After spending |
| 9 |
some minutes your should be able to write your script by yourself. |
| 10 |
To include it to your NSIS script just insert that: |
| 11 |
Math::Script "YourScript1" |
| 12 |
Math::Script "YourScript2" |
| 13 |
Math::Script "YourScriptFinal" |
| 14 |
|
| 15 |
How to use it? Simple: |
| 16 |
Strcpy $0 "Brainsucker" |
| 17 |
Math::Script "a = 'Math'; B = 'Script'; r0 += ' wants to use ' + a + '::' + b +'!'" |
| 18 |
DetailPrint "$0" |
| 19 |
That string will fill r0 with some stuff. |
| 20 |
|
| 21 |
Here are some other samples: |
| 22 |
10! (factorial, r0 will contain '10! = 362880'): |
| 23 |
r0 = '10! = ' + (1*2*3*4*5*6*7*8*9) |
| 24 |
the same: |
| 25 |
a = b = 1; #{++a <= 10, b = b*a}; r0 = (a-1) + '! = ' + b |
| 26 |
Some floating point: |
| 27 |
Strcpy $R0 "1e1" |
| 28 |
Math::Script "pi = 3.14159; R1 = 2*pi*R0; r0 = 'Length of circle with radius ' + R0 + ' is equal to ' + R1 + '.'" |
| 29 |
Detailprint "$0" |
| 30 |
|
| 31 |
Ok. Variables. |
| 32 |
NSIS: r0-r9 -> $0-$9. R0-R9 -> $R0-$R9. |
| 33 |
Also CL ($CMDLINE), ID ($INSTDIR), OD ($OUTDIR), LG ($LANG), ED ($EXEDIR). |
| 34 |
User definable: name starting from character, up to 28 letters long. |
| 35 |
|
| 36 |
Stacks. Two stacks are supported: NSIS stack and plugin's own stack. I see no |
| 37 |
reasons for using plugin stack, but if you will, remember - the plugin stores |
| 38 |
variables used at function to that stack before function execution, and restores |
| 39 |
after execution. Even less I recommend you to use NSIS stack. You should use it |
| 40 |
only for input/output. |
| 41 |
How to use? It's variable styled. Plugins stack is associated with S variable, |
| 42 |
and NSIS stack associated with NS variable. To push to stack just do "S=0" or |
| 43 |
"NS=0", to pop from stack "a=S" or "b=NS". Combined operations supported too: |
| 44 |
"S += 1.5" will increment value at the top of stack by 1.5. |
| 45 |
|
| 46 |
Supported types: int (in fact that is __int64), float (double in fact), |
| 47 |
string. |
| 48 |
Int: just numbers, may include sign. |
| 49 |
Float: -123.456, 123.456e-78, 123e-45 |
| 50 |
String: something in quotes ("", '', ``). |
| 51 |
|
| 52 |
There is also an array type. It is actually a reference type, so if b is array |
| 53 |
and you will perform "a=b", the a and b will reference a single array. |
| 54 |
To create a copy of array, use ca func: dest = ca(source). Btw - you couldn't |
| 55 |
control dimensions of arrays - they are autosized. |
| 56 |
To declare array: |
| 57 |
a = {}; |
| 58 |
To declare array and initialize some items with values: |
| 59 |
{"Hello!", "Use", "mixed types", 1.01e23, "like that" ,1234}; |
| 60 |
To access array: |
| 61 |
a[index] = "Cool"; |
| 62 |
|
| 63 |
Also [] operation could be used to strings. str[x] gives you a single char with |
| 64 |
index x (zero-based) as new string. str[-x] - the same, but x counts from the |
| 65 |
string end (so the last char is -1). str[x,y] gives you characters in range x-y |
| 66 |
(inclusive), both x and y could be <0 - in this case they counted from the end |
| 67 |
of the string. |
| 68 |
|
| 69 |
The function could be useful - is conversion of arrays to strings and back. |
| 70 |
Example: |
| 71 |
a = a("Hello"); str = s(a); |
| 72 |
After running such script array a will contain 6 integers (chars and last zero |
| 73 |
- end of string), and str will contain your string back. |
| 74 |
|
| 75 |
Operators (some binary, some unary): |
| 76 |
>>= <<= -= += /= *= |= &= ^= %= -- ++ >> << && || <= =< >= => != == |
| 77 |
= + - * / % < > & | ^ ~ ! |
| 78 |
Only some are applicable to float (logic & arithmetic) and string (+ and logic) |
| 79 |
of course. |
| 80 |
Additional case: reference/de-reference operators (& and *). & will |
| 81 |
give you the reference to argument which should be a variable (NSIS, user, array |
| 82 |
item, stack), and * will convert it back to original variable. For example |
| 83 |
(a=&b; *a=10) will set b to 10. Expression (*&a) is equal to simple (a). |
| 84 |
|
| 85 |
Script is set of expressions (mathematical in general) delimited with ';'. |
| 86 |
Processing is mathematically right (2+2*2 will give 6), operations are performed |
| 87 |
in a C like order (precedence). |
| 88 |
|
| 89 |
Flow control: |
| 90 |
if-then-else like: #[if-expression, then-expr, else-expr] |
| 91 |
example: |
| 92 |
#[a==0, b=1; c=2, b *= (--c); c/=10] |
| 93 |
C eq: |
| 94 |
if (a==0) { b=1; c=2;} else { b*=(c++);c-=10; } |
| 95 |
while (expr) do; like #{expr, do} |
| 96 |
example: |
| 97 |
#{(c<1.1e25)&&(b < 10), b++; c*=1.23} |
| 98 |
C eq: |
| 99 |
while ((c<1.1e25)&&(b<10)) { b++; c*=1.23; } |
| 100 |
|
| 101 |
WATCH OUT! Comma (,) separates if-expr, then-expr, and else-expr from each |
| 102 |
other. All sub-expressions separated by (;) are the part of one expression, |
| 103 |
and the result of the last one of these sub-exprs gives you the result of |
| 104 |
expression. |
| 105 |
|
| 106 |
All the stuff (like variables and functions) will be saved between calls. |
| 107 |
|
| 108 |
Functions: |
| 109 |
type conversions: |
| 110 |
l(string) returns the length of string or array argument |
| 111 |
s(source) converts source to string type |
| 112 |
i(source) converts source to int type |
| 113 |
f(source) converts source to float type |
| 114 |
c(source) if source is string, returns int value of first |
| 115 |
char, if source is int, returns string which consists |
| 116 |
of a single char (source) (+0 terminator). |
| 117 |
a(source) converts source to array (only string supported) |
| 118 |
ff(float, format) converts float to string, with format |
| 119 |
options. |
| 120 |
options = precision + flags. |
| 121 |
Precision shows how many digits after decimal point |
| 122 |
will be shown. Flags: |
| 123 |
16 (or 0x10) - No Exponential View |
| 124 |
(number will be shown as 123.123) |
| 125 |
32 (or 0x20) - Only exponential view |
| 126 |
(number will be shown as 123.12e123) |
| 127 |
64 (or 0x40) - use 'E' character instead of 'e' |
| 128 |
By default the plugin decides itself how to show your |
| 129 |
number. |
| 130 |
|
| 131 |
math (description of all these functions is available at MSDN, use the |
| 132 |
second given name for search): |
| 133 |
sin(x), sin Sine of argument |
| 134 |
cos(x), cos Cosine of argument |
| 135 |
cel(x), ceil Ceil of argument (no fract. part) |
| 136 |
csh(x), cosh Hyperbolic Cosine of Argument |
| 137 |
exp(x), exp Exponential |
| 138 |
abs(x), abs Absolute value (warning: float) |
| 139 |
flr(x), floor Floor of argument (no fract. part) |
| 140 |
asn(x), asin ArcSine of argument |
| 141 |
acs(x), acos ArcCosine of argument |
| 142 |
atn(x), atan ArcTangent of argument |
| 143 |
ln(x), log Exponential Logarithm |
| 144 |
log(x), log10 Decimal logarithm |
| 145 |
snh(x), sinh Hyperbolic Sine of Argument |
| 146 |
sqt(x), sqrt Square root of argument |
| 147 |
tan(x), tan Tangent of argument |
| 148 |
tnh(x), tanh Hyperbolic tangent of argument |
| 149 |
|
| 150 |
functions taking two arguments |
| 151 |
at2(x, y) atan2 Arctangent of the value (y/x) |
| 152 |
pow(x, y) pow power, x^y |
| 153 |
fmd(x, y) fmod floating point remainder |
| 154 |
fex(x, o) frexp Gets the mantissa (result = r) |
| 155 |
and exponent (o) of floating-point |
| 156 |
number (x): x = r*(2^o) |
| 157 |
mdf(x, o) modf Splits a floating-point value into |
| 158 |
fractional and integer parts. |
| 159 |
|
| 160 |
User-defined functions. |
| 161 |
It's very simple. Example: |
| 162 |
test(a,b) (a+b); |
| 163 |
After that test(1,2) will give you 3. |
| 164 |
test2(a,b) (a=a+b; b *= a); |
| 165 |
The result of function is always the result of last expression. |
| 166 |
As said before it better not to use stack (S) in between function calls. |
| 167 |
It will be better to develop variable-safe functions, i.e. functions which will |
| 168 |
not corrupt variables. For this you should either push/pop them to stack, or |
| 169 |
declare as additional arguments, which will never be used. Example: |
| 170 |
test3(a,b,c) (c=10; #{--c > 0, a=sqrt(a*b)}; a) |
| 171 |
No matter how many arguments will be passed to function, the values of all three |
| 172 |
vars (a,b,c) will be saved. |
| 173 |
Such variable-safe functions could be recursive: |
| 174 |
Math::Script 'rec(a) (#[a > 0, rec(a-1), 0]+a);' |
| 175 |
Math::Script 'R1 = rec(10)' |
| 176 |
will set R1 to right result 55. |
| 177 |
Sometimes functions will need to return more than one value, in this case you |
| 178 |
could declare argument as referent (b at example): |
| 179 |
test4(a, &b) (*b = a*a; a*a*a) |
| 180 |
In this case test4 will return a^3, and if we will call it like that test4(a,c), |
| 181 |
it will place a^2 to c. BUT! Note: you should use de-referencer (*) with variable, |
| 182 |
at example *b. CAUTION: never use the same variable as function internal reference |
| 183 |
variable and external argument variable (for example test4(a,b)). It will surely |
| 184 |
fail. Also: if you declared argument as reference - you should never supply |
| 185 |
a constant expression to it. It could be either array item (array[1]), NSIS |
| 186 |
register R0, any of the user variables (beside the variable with the same name:), |
| 187 |
but never the constant. |
| 188 |
|
| 189 |
Another may-be-useful possibility is to redeclare the function (the usual |
| 190 |
declaration at the time when function already defined will simply call that |
| 191 |
function). For such task you could use "#name", like "func()(1); #func()(2);". |
| 192 |
But beware, function declaration occurs at time of parsing, so it's not possible |
| 193 |
to perform flow controlled declaration. |
| 194 |
SUCH IS NOT POSSIBLE: "#[a<0, #func()(1), #func()(2)]" |
| 195 |
IT WILL SIMPLY DEFINE #func as (2), as the latest variant. |
| 196 |
|
| 197 |
(c) Nik Medved (brainsucker) |