LastWave Package Kernel 2.0 | Author | Description | Examples |
The kernel package is the core of LastWave. It mainly includes
- the parser and the structure of the command language
- basic commands
- definition of the basic types such as strings (&str), numbers (&num,&float,&int), list of values (&listv, i.e., list of object of arbitrary type) etc...
- basic graphic management
For examples about LastWave Graphic features you should look at the
examples in the numerical packages and read the LastWave
documentation
Basics | Strings | |||
The command line | Basic manipulation | |||
The [...] syntax | Extraction (Get) | |||
Extraction (Set) | ||||
Wild cards | ||||
Listv (list of values) | Arrays | |||
Basic manipulation | Basic manipulation | |||
Extraction (Set/Get) | Indirect access using the syntax | |||
Extraction (Set) | ||||
Wild cards | ||||
The foreach loop | Commands | |||
Defining new commands | ||||
Optional arguments | ||||
Type of arguments | ||||
Variable number of arguments | ||||
Anonymous commands |
Basics: The command line
Syntaxically a command line is just a list of words, the first
word being the command name and the other words the arguments. Each word is
separated using either the space character, the new line character or the carriage
return character. Moreover no word separation occurs within {...} (block
separation) , (...) (expression grouping), '...'
(used for strings), "..." (same as '...') ,
`...` (see manual) and [...]
Thus for instance the command setv can be used to set a variable.
The first argument is the name of the variable and the second one the value
it should be set to, e.g.,
a> setv
x 2
= 2
a> setv s"hello"
= "hello"
The print command lets you print a variable
a> print x
x= 2
a> print s
s="hello"
Actually, you can directly type in an expression instead of a command line LastWave
returns its evaluation, e.g.,
a> x
= 2
a> x+10
= 12
a> s
= "hello"
a> s+" "+s
="hello hello"
Moerover the = syntax can be used instead of the setv command, e.g.,
a> x=3
= 3
a> x*=2
= 6
a> x
= 6
a> s="hello"
= "hello"
a> s*=2
= "hellohello"
Some commands have actions associated to them. Their behavior depends on the
action, e.g., (conversion string to/from ascii codes)
a> str
2ascii "hello"
= {104 101 108 108 111}
a> str ascii2 {104 101 108 108 111}
= "hello"
Basics: The [...] syntax
If the argument a command must be evaluated and if this argument is
surrounded by brackets, whatever is between brackets is considered as a command
line which is evaluated. The resut of this evaluation is the value of the argument.
Thus
a> l
= [str 2ascii "hello"]
= {104 101 108 108 111}
a> h = [str ascii2 l]*2
= "hellohello"
a> h
= "hellohello"
Strings: Basic manipulation
a> x
= "this is"
= 'this is'
a> type(x)
= '&string'
a> x.length
= 7
a> x+' a concatenation'
= 'this is a concatenation'
a> x*3
= 'this isthis isthis is'
a> (x*2 == x+x)
= 1
a> x
= 'this is'
a> x+=' what'
= 'this is what'
a> x
= 'this is what'
Strings: Extraction (Get)
a> s='abcdef'
= 'abcdef'
a> s[1,3,5]
= 'bdf'
a> s[1:2:5]
= 'bdf'
a> s[5:-1:0]
= 'fedcba'
a> s[1:2:]
= 'bdf'
a> s[:-1:]
= 'fedcba'
a> s[1:3,0,0,1:2:]
= 'bcdaabdf'
You can use periodic border effects (there are many more!)
a> s[*bper,-1:10]
= 'fabcdefabcde'
Between the [] you can put signals or listvs made of integers. Thus, for instance,
if you want to randomly choose 10 characters from that string (allowing repetitions)
you can do
a> s[int(Urand(10)*s.length)]
= 'cefcabfdef'
Strings: Extraction (Set)
a> s = 'hello'
= 'hello'
a> s[1,2] = 'ELL'
** Error : String lengths do not match
--> s[1,2] = 'ELL'
a> s[1,2] = 'EL'
= 'hELlo'
a> s = 'hello'
= 'hello'
Using a listv on the right handside lets you set block of characters
a> s[3:4] = {'p'}
= 'help'
a> s[0,3:4] = {'la b' 'la donna'}
= 'la bella donna'
To erase the first word, you could simply do
a> s[0:2] = {''}
= 'bella donna'
Strings: Wild cards
a> l = 'hello hello baby'
= 'hello hello baby'
Looking for b or e characters
a> str match l '[be]'
= {1 7 12 14}
Replacing them by the X character (the := is necessary
whenever the size of the two handsides do not match)
a> l[[str match l '[be]']] :=
'X'
= 'hXllo hXllo XaXy'
a> l = 'hello hello baby'
= 'hello hello baby'
Looking for a b character that follows an a (the |...|
syntax specify what the returned ranges should correspond to)
a> l[[str match l 'a|b|']] :=
'B'
= 'hello hello baBy'
There are many other wild cards... you should read the documentation.
Listvs: Basic manipulation
a> {'a listv' 'made' 'of' 4 'elements'}
= {'a listv' 'made' 'of' 4 'elements'}
a> type({1 're'})
= '&listv'
a> {}
= {}
a> l = {'like' {'this' 'listv'} <1,2>}
= {'like' {length=2} <1,2>}
a> type(l[0])
= '&string'
a> type(l[1])
= '&listv'
a> type(l[2])
= '&signali'
a> x={'a' 1}
= {'a' 1}
a> x+='b'+2
= {'a' 1 'b' 2}
a> x+='c'+3
= {'a' 1 'b' 2 'c' 3}
a> x*=2
= {'a' 1 'b' 2 'c' 3 'a' 1 'b' 2 'c' 3}
a> x={'a' 1}
= {'a' 1}
a> x+x
= {'a' 1 'a' 1}
a> x+{x}
= {'a' 1 {'a' 1}}
a> l = {'a' 1}
= {'a' 1}
a> l.length
= 2
a> l.length=7
= 7
a> l
= {'a' 1 0 0 0 0 0}
Listvs: Extraction (set/get)
a> l = {'a' 1}
= {'a' 1}
a> l[0]
= 'a'
a> l[0] = 12
= 12
a> l
= {12 1}
a>
l = {'a' 1 {1 2}}
= {'a' 1 {1 2}}
a> {e f g}=l
= {'a' 1 {1 2}}
a> e
= 'a'
a> f
= 1
a> g
= {1 2}
Arrays: Basic manipulation
LastWave lets you manipulate arrays of values indexed by strings.
a> tab.1='one'
= 'one'
a> tab.2='two'
= 'two'
a> tab.3='three'
= 'three'
a> tab.total=6
= 6
a> tab.subarray.list={1 2 3}
= {1 2 3}
To print in a "nice" way arrays or listv you can use the nice
command
a> nice tab
-> 1 : 'one'
-> 2 : 'two'
-> 3 : 'three'
-> subarray :
....... -> list :
.............. -> 0 : 1
..............-> 1 : 2
..............-> 2 : 3
-> total : 6
Arrays: Indirect access using the syntax
The syntax lets you make string substitution before the expression
is executed. You can use it to perform indirect access on arrays:
a> tab.1='one'
= 'one'
a> tab.2='two'
= 'two'
a> tab.3='three'
= 'three'
a> index=3
= 3
a> tab.index
'three'
a> tab.index=4
=4
a> tab.index
4
The foreach loop
The foreach command lets you loop on various objects such as
listv, ranges or signals. Here are some examples
a> l = {}
= {}
a> foreach i 1:10 {l+=i}
a> l
= {1 2 3 4 5 6 7 8 9 10}
or
a>
l = {}
= {}
a> foreach r Grand(10) {
--> l+=r
--> }
a> l
= {0.067469 -0.0630144 0.356936 1.45415 -0.320958 0.804815 -0.739786 -2.00512
-1.39016 -0.69139}
or
a>
p = 1
= 1
a> s=0
= 0
a> foreach e l {
--> p*=e
--> s+=e
--> }
a> p
= 0.000812681
a> s
= -2.52705
Commands: Defining new commands
setproc
my {n} {
return n+1
}
a> my 10
= 11
You can specify the usage and a one line help (that
will be used by the interactivel help system)
setproc my {n} "{{{<n>} {Returns <n>+1}}}" {
return n+1
}
Commands: Optional arguments
setproc
my {i {n 1}} {
return i+n
}
a> my 10
= 11
a> my 10 2
= 12
a> my 'ab' 'cd'
= 'abcd'
Commands: Type of arguments
setproc
my {{&num i} {&num n 1}} {
return i+n
}
a> my 'ab' 'cd'
** Error : Bad type '&num' for argument ''ab''
The &word type allows not
to evaluate the argument. This can be used to specify actions associated
to commands, e.g.,
setproc
my {{&word action} i} {
if (action=='str') {
if ([val type i]=='&string') {return i+'1'}
errorf 'Bad type %s for second argument' [val type i]
}
if (action=='num') {
if ([val type i]=='&num') {return i+1}
errorf 'Bad type %s for second argument' [val type i]
}
errorf 'unknown action %s' action
}
Commands: Variable number of arguments
The .l argument corresponds to a listv made of all the remaining
arguments
setproc
my {arg1 .l} {
printf 'arg1=%V, l=%V\n' arg1 l
}
a> my 1 2
arg1=1, l={2}
a> my 1 2 'e'
arg1=1, l={2 'e'}
a> my 1
arg1=1, l={}
Commands: Anonymous commands
Regular command
a> setproc my{} {return 1}
= %my
a> type(%my)
= '&proc'
the string '\texttt{&proc}'.
Anonymous command
a> c = %{n m}`return n+m`
= %<0x23740c40>
a> apply args c 1 10
= 11
a> apply listv c {1 10}
= 11
a> listv sort {1 2 7 26 3 5 42 7 1} %{e1 e2}`return e1%2-e2%2`
= {1 7 1 3 5 7 42 26 2}
setproc generate {{&int n}} {
return %{x}`return x*\n`
}
a> c = [generate 3]
= %<0x2380d0a8>
a> apply args c 9
= 27
a> apply args c 'e'
= 'eee'