Category:Guish: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 132: Line 132:
A button which is a factory of buttons.
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):
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>
<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.
Here the evaluation of the code block is done when the click signal is triggered.


Line 143: Line 143:
All variables (functions too, as blocks can be assigned to variables) have a specific scope:
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>
<pre> a = 1; {b = 2}(); puts &quot;@{a}:@{b}&quot;</pre>
Here the variable 'a' is defined in the global scope, while
Here the variable 'a' is defined in the global scope, while


Line 150: 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:
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>
<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.
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: Line 162:
Anything embedded inside '''{}''' is treated as a code block and no variable substitution is done at definition time.
Anything embedded inside '''{}''' is treated as a code block and no variable substitution is done at definition time.


If a block is the first argument in a phrase (and while evaluating a phrase), then it's executed, and parameters can be specified inside parens '''()''', and 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).
To &quot;execute&quot; a block, simply use parens '''()'''. If arguments are given, they 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>
<pre> v = myvar; a = {puts &quot;here is @{v}&quot;}; a()</pre>
Line 174: Line 174:
<pre> fn = {return add(@1, @2)}
<pre> fn = {return add(@1, @2)}
puts join(&quot; &quot;, &quot;my func res:&quot;, fn(2, 4)) </pre>
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.
In addition, when defining a new function, it's possible to &quot;overwrite&quot; the builtin functions in the current scope.


Line 599: Line 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.
Every phrase is reduced to an empty phrase while evaluating:


<pre> a = 234 {i1=|i|&lt;'input1'+}; {i2=|i|&lt;'input2'+} |b|&lt;btn+</pre>
<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;.
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 ==
== Hex substitution ==
Line 621: Line 619:
If a ''''*'''' is given, then all widgets wids are substituted.
If a ''''*'''' is given, then all widgets wids are substituted.


<pre> |b||b||b|+; puts *</pre>
<pre> |b||b||b|+; dump *</pre>
== Variable substitution ==
== Variable substitution ==


Line 632: Line 630:
Each block has it's own scope, and variable resolution works by searching from the last scope to the first. Ex:
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>
<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, a only is updated, and b doesn't exist anymore.
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, just 'a' is updated, and 'b' doesn't exist anymore.


For example:
For example:
Line 741: Line 739:
; '''env(var)'''
; '''env(var)'''
: returns the value of the environment variable '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, ...)'''
; '''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.
: 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: Line 753:
; '''get(i, token)'''
; '''get(i, token)'''
: returns the character of a token at index 'i'.
: 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)'''
; '''in(substr, token)'''
: returns 1 if substr is found in token, otherwise 0.
: returns 1 if substr is found in token, otherwise 0.
Line 778: Line 780:
: returns the number of given tokens.
: returns the number of given tokens.
; '''any(...)'''
; '''any(...)'''
: returns 1 if at least one token is not empty or equal to 0, 0 otherwise.
: 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(...)'''
; '''all(...)'''
: returns 1 if all tokens are not empty or equal to 0, 0 otherwise.
: 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(...)'''
; '''add(...)'''
: perform addition.
: perform addition.