Category:Guish: Difference between revisions
no edit summary
No edit summary |
No edit summary |
||
Line 132:
A button which is a factory of buttons.
In addition, being a the last argument a code block, it's possible to pass arguments to it (like functions), by putting them inside brackets '''[]''' (note that this works just for signals):
<pre> guish -zc '|b|<generator =>c{|b|<"clone: @{1}"}
Here the evaluation of the code block is done when the click signal is triggered.
Line 143:
All variables (functions too, as blocks can be assigned to variables) have a specific scope:
<pre> a = 1; {b = 2}(); puts "@{a}:@{b}"</pre>
Here the variable 'a' is defined in the global scope, while
Line 150:
When accessing reading/defining a variable in a local scope, if the variable is already defined in the enclosing scope (or recursively in any enclosing scope of the enclosing scope till the global scope), then that variable is picked to be read/modified:
<pre> a = 1; {a = 5; puts@a}(); puts@a</pre>
Here 'a' is defined in the global scope, then modified from block scope and printed from there, and its value doesn't change.
Line 162:
Anything embedded inside '''{}''' is treated as a code block and no variable substitution is done at definition time.
<pre> v = myvar; a = {puts "here is @{v}"}; a()</pre>
Line 174:
<pre> fn = {return add(@1, @2)}
puts join(" ", "my func res:", fn(2, 4)) </pre>
In addition, when defining a new function, it's possible to "overwrite" the builtin functions in the current scope.
Line 599 ⟶ 597:
Every phrase is reduced to an empty phrase while evaluating
<pre> a = 234 {i1=|i|<'input1'+}(); {i2=|i|<'input2'+}() |b|<btn+</pre>
This example is composed by 2 phrases, specifically: "a = 234 {i1=|i|<'input1'+}()" and "{i2=|i|<'input2'+}() |b|<btn+".
Here the code block in each phrase is executed before each assignment.
== Hex substitution ==
Line 621 ⟶ 619:
If a ''''*'''' is given, then all widgets wids are substituted.
<pre> |b||b||b|+;
== Variable substitution ==
Line 632 ⟶ 630:
Each block has it's own scope, and variable resolution works by searching from the last scope to the first. Ex:
<pre> a = 1; puts(@a) ;{a=345; b=6534}(); puts@a; puts"b:@{b}"</pre>
In the last example, a is set to 1 and printed, then it's changed to 345 from another scope, in which another variable (b) is set. After code block,
For example:
Line 741 ⟶ 739:
; '''env(var)'''
: returns the value of the environment variable 'var'
; '''rev(...)'''
: returns a reversed list of tokens. This function is somewhat special, as when there are no tokens to get, it'll return nothing (statement behaviour), neither an empty token.
; '''take(si, ei, ...)'''
: returns tokens starting at 'si' and ending at 'ei' (inclusive). This function is somewhat special, as when there are no tokens to get, it'll return nothing (statement behaviour), neither an empty token.
Line 753:
; '''get(i, token)'''
: returns the character of a token at index 'i'.
; '''fetch(name)'''
: returns the value of the variable with name 'name', or an empty token if the variable doesn't exist.
; '''in(substr, token)'''
: returns 1 if substr is found in token, otherwise 0.
Line 778 ⟶ 780:
: returns the number of given tokens.
; '''any(...)'''
: returns 1 if at least one token is not empty or equal to 0, 0 otherwise. This function is special, as the evaluation will be interrupted as soon as there is at least 1 true argument.
; '''all(...)'''
: returns 1 if all tokens are not empty or equal to 0, 0 otherwise. This function is special, as the evaluation will be interrupted as soon as there is at least 1 false argument.
; '''add(...)'''
: perform addition.
|