Skip to main content
FW version: Stable

Stringification

Stringification is a mechanism in YOS offering expansion of:

  • an expression
  • ASCII character code
  • a string stream input
  • a character stream input
  • a non-blocking character stream input

into a string, within a shell token.

Stringification directive is delimited with $ character and ends with token end or, with ~ character. If the shell finds $ in the token, it tries to interpret the consequent characters as a stringification directive.

Stringification is evaluated inside each token independently (including the command name token). The maximum length of the token after the expansion is usually limited to 32 characters.

All YOS commands accept arguments as token strings. Thus, with 'stringification', the stringified result of an arbitrary expression can be passed as an argument to an arbitrary command. If the stringification fails, the content of the string token remains unchanged.

Stringification expansion can be nested and composite. e.g. for $name$i, the system will first stringify the content of i. If the content of i is, e.g. 1, the second expansion will look for the variable named name1 and try to stringify its contents. In this way, stringification offers a powerful mechanism for indirect variable/command/function operations and referencing.

Special stringification command characters

CharacterDescription
numberASCII numeric-to-character conversion If the first character after the stringify operator $ is a number" the integer to ASCII conversion takes place and single character" is generated".
%Blocking stream character input. A stream input is redirected to the stringify and characters are converted to their ASCII codes. If no character is pedning in the stream input FIFO the command is blocked until a character arrives.
*Non-blocking stream character input. A stream input is redirected to the stringify and characters are converted to their ASCII codes. If no character is pedning in the stream input FIFO the resulted string is 0
?Stream string input. A stream input is redirected to the stringify and characters are directly stacked to the stringification token. The command is blocked until enter arrives or until the stringification token is full.
note

All characters present after the code block" which contains stream input (*, ?, %) will be sent" to the input of stringification."

Example

echo
echo Example - an array indexing

var a.3 int8
var i int8
set a 10,20,30
set i 0
echo a.$i is $a.$i
set i 1
echo a.$i is $a.$i
set i 2
echo a.$i is $a.$i


echo
echo Example - an array indexing with indirect variable name

var a0.3 a1.3 a2.3 int8
var i j int8
set a0 a1 a2 10,20,30
set a1 a1+a0
set a2 a2+a1
set i j 0

{
{
echo $a$i~.$j \
set j j+1
if j<3 branch s
}
echo
set j 0
set i i+1
if i<3 branch s
}


echo
echo Example - indirect command call

var cmdname.10 string
set cmdname "echo"
$cmdname ahoj!

echo
echo Example - indirect function call

.func1 { echo I am func1 }
.func2 { echo I am func2 }
var funcname.10 string
var i int8
set i 1
call func$i
set i 2
call func$i

echo
echo Example - indirect expression evaluation
var expression.10 string
var i int8
set expression "1+1"
set i $expression
echo $expression is $i
#or nested evaluation
echo $expression is $($expression~)

echo
echo Example - ASCII to character

echo few characters: $48~$49~$50~
set i 65
echo ascii $i is '$$i~~'

echo
echo Bonus example - non-blocking stream character input
echo hint: press arrows, enter to quit
{
set i $*
if i echo you pressed $i
echo \13 $j
set j j+1
if i!=13 branch s
}

Result:

Example - an array indexing
a.0 is 10
a.1 is 20
a.2 is 30

Example - an array indexing with indirect variable name
10 20 30
20 40 60
30 60 90

Example - indirect command call
ahoj!

Example - indirect function call
I am func1
I am func2

Example - indirect expression evaluation
1+1 is 2
1+1 is 2

Example - ASCII to character
few characters: 012
ascii 65 is 'A'

Bonus example - non-blocking stream character input
hint: press arrows, enter to quit
you pressed 10
71 you pressed 72
125 you pressed 80
you pressed 13
16