Category:Guish: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 214:
 
<pre> for x 1 2 3 4 { puts @x }</pre>
== Tail recursion optimization (TCO) ==
 
 
 
Since '''2.2.1''' version, guish supports tail recursion optimization too, effectively turning a tail recursive function into a loop:
 
<pre>upto = {
if gt(@1, @2) {
return
}
puts @1
return upto(add(@1, 1), @2)
}
 
upto(1, 7)</pre>
To use TCO, the last phrase of a function must use '''return''' command directly (not inside another block like if-else).
 
== External window id substitution ==
 
Line 727 ⟶ 744:
Symbol ''''...'''' means a variable number of arguments.
 
; '''exists(&lt;eid&gt;)'''
: gets an element id and returns 1 if the element exists, else 0.
; '''read([&lt;file&gt;])'''
: reads and returns a line (expluding newline) from standard input; if an existing [file] is given, reads and returns all its content. Beware that this function blocks the GUI events, and returns nothing when reading from stdin and source is non-blocking.
; '''write(&lt;text&gt;, &lt;file&gt;)'''
: writes text into file and returns the number of characters written. Creates the file if it doesn't exist yet.
; '''append(&lt;text&gt;, &lt;file&gt;)'''
: append text to the end of file and returns the number of characters written. Creates the file if it doesn't exist yet.
; '''call(&lt;name&gt;, ...)'''
: gets a variable name and a variable number of arguments, then calls the function whose name is 'name' with those arguments and returns the result.
; '''env(&lt;var&gt;)'''
: 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.
; '''def([&lt;var&gt;=[&lt;val&gt;], ...)'''
: sets variables, works exactly like assignment with special operator '''=''', but in expressions. This function is somewhat special, it'll return nothing (statement behaviour), neither an empty token.
; '''puts(...)'''
: prints given tokens to stdout. This function is somewhat special, it'll return nothing (statement behaviour), neither an empty token.
; '''push(...)'''
: pushes given tokens to a function arguments (when inside a function). This function is somewhat special, it'll return nothing (statement behaviour), neither an empty token.
; '''pushb(...)'''
: pushes given tokens to a function arguments from the beginning (when inside a function). This function is somewhat special, it'll return nothing (statement behaviour), neither an empty token.
; '''pop()'''
: pops the last token from function arguments (when inside a function). This function is somewhat special, as if there are no arguments to pop, it'll return nothing (statement behaviour), neither an empty token.
; '''popb()'''
: pops the first token from function arguments (when inside a function). This function is somewhat special, as if there are no arguments to pop, 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 747 ⟶ 776:
; '''tail(...)'''
: returns all tokens except the first one. This function is somewhat special, as when there are one or no tokens to get, it'll return nothing (statement behaviour), neither an empty token.
; '''times(&lt;n&gt;, &lt;token&gt;)'''
: returns a &quot;list&quot; made by 'n' times 'token'. This function is somewhat special, as when there are 0 tokens to replicate, it'll return nothing (statement behaviour), neither an empty token.
; '''slice(&lt;si&gt;, &lt;ei&gt;, &lt;token&gt;)'''
: returns the portion of a token starting at index 'si' and ending at 'ei' (inclusive).
; '''get(&lt;i&gt;, &lt;token&gt;)'''
: returns the character of a token at index 'i'.
; '''fetch(&lt;name&gt;)'''
: returns the value of the variable with name 'name', or an empty token if the variable doesn't exist.
; '''in(&lt;substr&gt;, &lt;token&gt;)'''
: returns 1 if substr is found in token, otherwise 0.
; '''join(&lt;sep&gt;, ...)'''
: returns a single token from a variable number of tokens, joining them using 'sep'.
; '''range(&lt;start&gt;, &lt;end&gt;)'''
: returns numbers starting at 'start' and ending at 'end' (inclusive) as multiple tokens.
; '''defined(&lt;name&gt;)'''
: returns 1 if 'name' is a variable, otherwise 0.
; '''isvar(&lt;name&gt;)'''
: returns 1 if 'name' is a (type) variable, otherwise 0.
; '''isfunc(&lt;name&gt;)'''
: returns 1 if 'name' refers to a function, otherwise 0.
; '''isalias(&lt;name&gt;)'''
: returns 1 if 'name' is an alias, otherwise 0.
; '''len(&lt;token&gt;)'''
: returns the length of the token.
; '''split(&lt;token&gt;, &lt;sep&gt;)'''
: splits 'token' using separator 'sep' and returns resulting tokens having their type equal to that of 'token'.
; '''csplit(&lt;token&gt;, &lt;sep&gt;)'''
: splits 'token' using separator 'sep' and returns resulting tokens as normal commands.
; '''seq(&lt;t1&gt;, &lt;t2&gt;, ...)'''
: returns 1 if all arguments are equal (string comparison), 0 otherwise.
; '''count(...)'''
Line 793 ⟶ 822:
; '''mod(...)'''
: perform modulus.
; '''xor(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: perform bitwise XOR.
; '''and(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: perform bitwise AND.
; '''or(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: perform bitwise OR.
; '''lsh(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: perform bitwise left shift.
; '''rsh(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: perform bitwise right shift.
; '''not(&lt;n&gt;)'''
: perform negation
; '''eq(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: equal-to
; '''ne(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: not-equal-to
; '''lt(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: less-than
; '''gt(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: greater-than
; '''le(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: less-equal-than
; '''ge(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: greater-equal-than
; '''abs(&lt;n&gt;)'''
: perform absolute value
; '''neg(&lt;n&gt;)'''
: unary minus.
 
39

edits