Discusses how variables and operations work.
Variables
MIFL variables behave almost exactly like those in Flash's
ActionScript because they are "un-typed".
They are very different to variables in Java, C, and C++,
which are "typed".
In MIFL, a variale it a storage slot for data that can
store any type of data. MIFL handles several
data types. The important ones are:
-
Number - This is a floating point number.
For those of you familiar with C, it's a "double".
(Example: 123.435)
-
Boolean - This can either
be "True" or "False".
-
Character - A single character, such as
'a' or '!'. Characters are identified by the
single-quotes (') surrounding it.
-
Null - This can only be one
value, Null. It represents an empty value.
-
Undefined - This can only be one
value, Undefined. Undefined implies that the
value is not only empty, but it wasn't even initialized.
-
String - A series of characters.
Strings are identified by the quotes (") surrounding them.
(Example: "Hello world!")
-
List - A list if an array of variables.
Lists are identified by the brackets ([ ]) that surround it.
(Example: [1, 3, "hi there", true] is a list with the 1st
element contain the number 1. 2nd is 3. 3rd is the
string "hi there", and 4th is the boolean, true.
-
Object - This is reference to an object.
I'll explain this in detail when I get into objects.
-
Function - A reference to a function.
In C or C++ this is equivalent to a pointer to a function.
-
Method - A reference to a method within an object.
In C or C++ this is equivalent to a pointer to a method.
-
String table entry - I'll discuss this later.
-
Resource - I'll discuss this later.
Unlike C, C++, and Java, a variable can change data types at
run-time. So, even if "x" were initialized to 54.45, it
could be changed to "Hi there" later one.
Using variables in functions
Creating a variable in a function is easy. In your
HelloWord() code, just add the following line:
var x = 54.3534;
Trace (x);
|
If you then Run this the "Debug trace" will
also display "54.3534".
(Actually, it probably won't do this unless you first
run "Compile" under "Misc." again.)
In C or C++ you would have typed in "double x = 54.3534" or
"int x = 53" or "char x[] = "hello there!"" because variables
are "typed". You don't need to do this in MIFL. It's
just "var x = 54.3534", "var x = 53", or "var x = "hello there!"".
As with C, C++, Java, and ActionScript, you can also create
several variables at once using "var x,y,z;" or
"var x=43, y="hi there", z=true;".
Note: All names (including variables)
are case insensative in MIFL. That means that
varible "x" is equivalent to "X", and functon "HelloWorld"
is equivalent to "helloWORLD". C, C++, and Java work
just the opposite, being case sensative, and differentiating
between HelloWorld() and helloWORLD().
Operators
Once you have variables, you'll want to do operations on them,
such as addition and assignment. The operators in MIFL are
almost identical to C, C++, Java, and ActionScript. They are (in
order of precedence):
. |
Object property or method access, like "object.property". |
[ ] |
Creating a list or indexing into a list or string. |
( ) |
Parenthesis or parameters for a function call. |
|
++ -- |
Increment or decrement a variable. If ++ (or --) is to the right of
the variable the variable's value is put on the stack and then incremented/decremented.
If ++ (or --) is to the left, the variable is first incremented or
decremented, and then the variable's value is put on the stack.
|
- |
Negation, such as in "-(x+y)" |
~ |
Bitwise not. |
! |
Logical not. |
|
* / % |
Multiply, divide, and modulo. |
|
+ - |
Addition and subtraction. |
|
<< >> |
Bitwise shift left or right. |
|
< <= > >= |
Less than, less than or equal, greater than, greater than or equal |
|
== != |
Equality or not equality |
=== !== |
Strict equality or strict not equality. These do not appear
in C, C++, or Java. They test equality but are strict about it.
Because of automatic conversions between numbers and strings,
43 == "43" but 43 !== "43", although 43 === 43. Strict equality does not
do automatic conversions.
|
|
& |
Bitwise AND |
|
^ |
Bitwise XOR |
|
| |
Bitwise OR |
|
&& |
Logical AND |
|
|| |
Logical OR |
|
? : |
Contional. Example: "(1 < 2) ? 5 : 3" will return "5".
|
|
= += -= *= /= %= <<= >>= &= ^= |= |
Various forms of assignment |
|
, |
Comma. Separates parameters, list elements, and (sometimes) lines of code. |
You can combine variables and operators to make your
HelloWorld() function do some calculations. Try tying
in and then running:
var x = 18;
var y = 6;
var z = x * y + 89;
z *= 2;
Trace (z);
|
Debugging
MIFL includes a debugger that allows you to step through the
code and see the flow of control along what variables
are at each step of the execution.
To test this:
-
Type in the code (as per above).
-
Select "Compile" underneath
the "Misc." menu.
-
Select "Test compiled code" underneath
the "Misc." menu.
-
In the "Test compiled code" page, check
the "Debug" checkbox.
-
Type in "HelloWorld()" and
press "Run this".
-
The "Debug" page will be displayed with "HelloWorld()" being
highlighted. Press "Step in" to step in.
The Debug page will not display the code from the HelloWorld()
function.
-
To see the values of the variables, x, y, and z, type
in "x", "y", and "z" (each on their own line)
into the "Watch variables" list. At first each will
be "undefined" since they haven't been set.
-
Press "Step" to step to the next line of code.
If, for example, you stepped over the "x = 18;" line then
you'll see the watched variable, x, change to "18".
-
Keep on pressing "Step" until the code finishes
executing. You can watch how the variables change with each
step.
If your function called another function (other than Trace()),
you could press "Step in" to debug that function.
(It's not worth stepping into Trace() because it just ends up
calling a built-in function, which can't be debugged.)
You can also press "Run" to run the code without
stepping, or "Exit" to stop running the code.
Passing parameters to functions
Just like other languages, you can pass parameters to functions.
To do this:
-
Select "HelloWorld" under
the "Functions" menu.
-
Press the "Parameters" tab in the "Modify
function" page.
-
Type in a parameter name, "Input" and
a description for the meaning of the parameter.
If you want more than one parameter, just
press "Add more parameter slots" to add another
slot. When you fill that one in, you can press "Add more
parameter slots" again to add more.
-
Type in a short desciption for the Return value of
HelloWorld(), such as "Does complicated math on Input and then
returns that."
-
Switch to the "Code" tab.
-
Type in the following code:
var x = 18 + Input;
var y = 6;
var z = x * y + 89;
z *= 2;
Trace (z);
return z;
|
-
Compile the project.
-
Select the "Test compiled code" menu item.
-
Type in "HelloWorld (53)" into the edit field,
and then press "Run this".
If you have debuggin turned on you'll be able to watch the
value of "Input", in addition to that of the variables,
"x", "y", and "z".
If you look at the debug log, you'll see that the Trace(z)
outputs "1030" and then the return value is printed out
as "Returns: 1030".
In addition to the parameters you add to the function, MIFL
functions (and methods) support two "hidden" parameters:
-
Arguments - This is a list of all the parameters
passed into the function. In the case of the HelloWorld()
example it would be [53]. If more parameters were passed in
the list would have more elements.
-
This - When you run HelloWorld(), This will be
set to Undefined. However, if HelloWorld() were
a method in an object, This would contain a reference to the
object. C++, Java, and ActionScript have a similar construct.
Global variables
Like other languages, MIFL supports "global variables" which
are variables that can be accessed by any function or method, and
which persist from one function call to the next.
To create a global variable:
-
Select the "Add new variable" menu item
under the "Variables (global)" menu item.
-
In the "Modify global variable" page that appears, type
in a "Name" for the variable, such as "MyGlobal".
-
Under "Initialized to", type in the value you'd
like the global to being with, such as 54.456. If you leave
this blank the global will initially be set to Undefined.
You can also type in a short and long description, just like
with the function.
-
Re-Compile the project and return
to Test compiled code.
-
Type "MyGlobal" into the watch list to see
it's value, which should be 54.456.
You can also see the value of MyGlobal by clicking
on the "Global variables..." link on the right
side of the test window. This will display the list of all
global variables. Hovering the mouse over the "MyGlobal" entry
will show it's value in a tooltip.
-
You can change the value of MyGlobal by writing some code
into HelloWorld(), and then compiling and running it, or
by typing "MyGlobal = 12345" in the test
edit window and pressing "Run this".
Warning: A global variable (or object property)
can initialize itself (with "initialized to") to use the contents of another global
variable. This has two potential problems:
-
Undefined - If the global variable that's being
referenced has not yet been initialized due to the
semi-arbitrary initialization errors of globals, then the
global variable will be undefined. This problem
won't happen when initializing an object's property to a global
variable because globals are always initialized first.
-
Fracturing - If a global variable or property is
initialized by referencing a global variable which is in turn
initialized by a string or list, then the new global variable
or property will contain the correct string/list but the
string/list will be different pieces of data.
Hence, if
global variable A is initialized to "Hello", and global variable
B is initialized to A, then global variable
B will also be initialized to "Hello".
However, If A.StringAppend(" there") is called, A will
be "Hello there", but B will still be "Hello".
Share with your friends: |