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|&lt;generator =&gt;c{|b|&lt;&quot;clone: @{1}&quot;}([&quot;myname&quot;)]'</pre>
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 &quot;@{a}:@{b}&quot;</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.
 
IfTo &quot;execute&quot; a block is the first argument in a phrase (and while evaluating a phrase), thensimply it's executed, and parameters can be specified insideuse parens '''()'''. If arguments are given, andthey can be referenced inside block by using '''@n''', where 'n' is a number which represents a specific argument by position. To refer to all arguments given to the block as a &quot;list&quot; instead, it's possible to use '''@*''' operator (when in string interpolation, argument separator is a space).
 
<pre> v = myvar; a = {puts &quot;here is @{v}&quot;}; a()</pre>
Line 174:
<pre> fn = {return add(@1, @2)}
puts join(&quot; &quot;, &quot;my func res:&quot;, fn(2, 4)) </pre>
To call a function, simply use the variable name with parens (possibly with additional function arguments).
 
In addition, when defining a new function, it's possible to &quot;overwrite&quot; the builtin functions in the current scope.
 
Line 599 ⟶ 597:
 
 
Every phrase is reduced to an empty phrase while evaluating; if the first argument of each eval/substitution phase is of type code, then it's evaluated. Ex.:
 
<pre> a = 234 {i1=|i|&lt;'input1'+}(); {i2=|i|&lt;'input2'+}() |b|&lt;btn+</pre>
This example is composed by 2 phrases, specifically: &quot;a = 234 {i1=|i|&lt;'input1'+}()&quot; and &quot;{i2=|i|&lt;'input2'+}() |b|&lt;btn+&quot;.
 
Here the code block in each phrase is executed before each assignment.
Here first the value 234 is assigned to variable 'a', and the phrase is reduced by 3 tokens; then the first argument becomes the code block, which is reduced and executed.
 
== Hex substitution ==
Line 621 ⟶ 619:
If a ''''*'''' is given, then all widgets wids are substituted.
 
<pre> |b||b||b|+; putsdump *</pre>
== 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&quot;b:@{b}&quot;</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, ajust only'a' is updated, and 'b' doesn't exist anymore.
 
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.
39

edits