|
|
|
NavigationPersonal tools |
Embryo Basics
[edit] Regular Variable DeclarationSyntax:
new varname; /* simple uninitialized variable called 'varname' */
new tag:varname; /* tagged uninitialized variable called 'varname' */
new varname[arraysize]; /* uninitialized array called 'varname' of size '
* arraysize'
*/
new varname = value; /* simple variable called 'varname' set to 'value' */
new tag:varname = value; /* tagged variable called 'varname' set to 'value' */
new varname[arraysize] = "text"; /* array called 'varname' of size 'arraysize'
* set to text "text"
*/
new varname[] = "text"; /* array called 'varname' initialized to text "text",
* size will be automatically set to sizeof("text"),
* that includes null-terminator
*/
new varname[arraysize] = {0, 1}; /* array called 'varname' of size 'arraysize'
* set to numbers {0, 1}
*/
new varname[] = {0, 1}; /* array called 'varname' initialized to numbers
* {0, 1} size will be automatically set to 2.
*/
Variables will be alive in the scope they were defined, be it an Embryo function or Edje program script block. Although Embryo just handle integers -- also called native, one can use tags as type-hinting mechanism. Once you tag your variable, it will just fit into other variables or function parameters of the same tag. There is no "cast" to convert between them: you need to provide a function that implements this conversion. Embryo ships with Float as tag and provide a set of functions to convert and operate with them, see default.inc for pre-defined functions. Arrays, including text strings, are handled as a sequence of integers. You can read or write the indexes, get the size with sizeof(varname). Usage example:
new i = 1, j = 2, k;
k = i + j;
if (i < j)
i = 10 * j;
[edit] Constant DeclarationSyntax: const constname = value; Constants are useful to give meaningful names to some values that might be used elsewhere. [edit] Global Variable DeclarationSyntax: public varname; /* simple uninitialized variable called 'varname' */ public tag:varname; /* tagged uninitialized variable called 'varname' */ The construction public <value>; declares a global variable that can be accessed from any function. Arrays, local variables and function parameters cannot be public. Unlike regular variables, globals requires special access by means of get_int(varname) or set_int(varname, value). Failing to do so will provide you no compile errors, but runtime values will be totally off. Globals will always be set to 0. Although setting it to some value with public name = value; will not emit any compile error, it will not have any effect. Report a bug? Usage example:
public i, j, k;
public my_func() {
new my_i, my_k;
my_i = get_int(i);
my_k = 1 + my_i + get_int(j);
set_int(k, my_k);
if (my_i < get_int(j)) {
my_i = 10 * get_int(j);
set_int(i, my_i);
}
}
The construction public name(args...) {body...} defines a function. [edit] Function DeclarationSyntax:
public funcname(param1, param2) {code;} /* define fixed number of arguments */
public funcname(param1, ...) {code;} /* define variable number of arguments */
funcname(param1, param2); /* call function with 2 parameters */
Functions are used to avoid code duplication. When they're called, they will create an execution frame and any regular variable defined will stay restricted to it, when the function returns they will automatically freed. Usage example:
public global_var = 1;
public f1(p1, p2) {
new i;
i = p1 + p2 + get_int(global_var);
return i;
}
public f2(p1, p2, ...) {
new i, j;
i = f1(p1, p2);
for (j = 2; j < numargs(); j++)
i += getarg(j);
return i;
}
|