Created smnp.lang (markdown)
157
smnp.lang.md
Normal file
157
smnp.lang.md
Normal file
@@ -0,0 +1,157 @@
|
||||
The `smnp.lang` module provides essential functions and methods that haven't been implemented directly in SMNP interpreter. The `smnp.lang` is kind of privileged because of fact, that it is implicitly loaded in _each_ SMNP script, even without any `import` statement.
|
||||
|
||||
# Constructors
|
||||
Constructors technically are nothing but functions. They just "emulate" real constructors from other general purpose programming languages that support object-oriented programming paradigm.
|
||||
## `Int(value: <int, float>)`
|
||||
Creates new `int` from other `int` or `float`.
|
||||
The construction is typically used in other stdlib modules to round `float` types down.
|
||||
|
||||
### Arguments
|
||||
* value of `int` or `float` type
|
||||
|
||||
### Output
|
||||
* newly created `int`
|
||||
|
||||
### Example
|
||||
```
|
||||
x = 3.14;
|
||||
y = Int(x);
|
||||
|
||||
println(typeOf(y)); # int
|
||||
println(y); # 3
|
||||
```
|
||||
|
||||
## `Note(pitch: string, octave: int, duration: int, dot: bool)`
|
||||
Creates new `note` with given parameters.
|
||||
|
||||
### Arguments
|
||||
* `pitch` - the pitch of note. Available values: `C, Cb, C#, D, Db, D#, E, ...`
|
||||
* `octave` - the octave of note
|
||||
* `duration` - the denominator of fraction that determines the duration of note: `1 = whole, 2 = half, 4 = quarter, ...`
|
||||
* `dot` - an optional dot which extends the note duration by half of its value
|
||||
|
||||
### Output
|
||||
* note of given parameters
|
||||
|
||||
### Example
|
||||
```
|
||||
x = Note("Eb", 3, 16, true);
|
||||
println(x == @Eb3:16d); # true
|
||||
```
|
||||
|
||||
## `Note(pitch: string, octave: int, durationNumerator: int, durationDenominator: int)`
|
||||
Creates new `note` with given parameters.
|
||||
|
||||
### Arguments
|
||||
* `pitch` - the pitch of note. Available values: `C, Cb, C#, D, Db, D#, E, ...`
|
||||
* `octave` - the octave of note
|
||||
* `durationNumerator` - together with `durationDenominator` determines the total duration of note:
|
||||
```
|
||||
When:
|
||||
durationNumerator == durationDenominator -> whole note
|
||||
durationNumerator == 0.5 * durationDenominator -> half note
|
||||
durationNumerator == 0.25 * durationDenominator -> quarter note
|
||||
...
|
||||
```
|
||||
* `durationDenominator` - as above
|
||||
|
||||
### Output
|
||||
* note of given parameters
|
||||
|
||||
### Example
|
||||
```
|
||||
x = Note("Eb", 3, 3, 32);
|
||||
println(x == @Eb3:16d); # true
|
||||
```
|
||||
|
||||
# Functions
|
||||
## `typeOf(object)`
|
||||
Returns the type name of passed object.
|
||||
|
||||
### Arguments
|
||||
* object - object of any type
|
||||
|
||||
### Output
|
||||
* type name of passed object as string
|
||||
|
||||
### Example
|
||||
```
|
||||
println(typeOf(14)); # integer
|
||||
println(typeOf(@A#)); # note
|
||||
println(typeOf([1, 2, 3])); # list
|
||||
println(typeOf([@c, @d, 4])); # list
|
||||
println(typeOf({ c -> @c, d -> @d })); # map
|
||||
```
|
||||
|
||||
# Methods
|
||||
## `<list>.get(index: int)`
|
||||
Returns list element of given index.
|
||||
|
||||
### Arguments
|
||||
* index - index of desired element. Note, that counting starts with 0.
|
||||
|
||||
### Output
|
||||
* element of list with given index. Throws an error if index is out of bounds.
|
||||
|
||||
### Example
|
||||
```
|
||||
myList = [1, 2, 3, 4];
|
||||
lastElement = myList.get(3);
|
||||
println(lastElement); # 4
|
||||
```
|
||||
|
||||
## `<map>.get(key: <int, note, string, bool>)`
|
||||
Returns map element associated with given key.
|
||||
|
||||
### Arguments
|
||||
* key - key of value that is about to be returned
|
||||
|
||||
### Output
|
||||
* element associated with given key. Throws an error if key doesn't exist.
|
||||
|
||||
### Example
|
||||
```
|
||||
myMap = {
|
||||
true -> false,
|
||||
@Eb3:2d -> 14,
|
||||
hello -> "world"
|
||||
};
|
||||
|
||||
element = myMap.get("hello");
|
||||
println(element); # world
|
||||
```
|
||||
|
||||
## `<string>.charAt(index: int)`
|
||||
Returns string's character of given index.
|
||||
|
||||
### Arguments
|
||||
* index - index of desired character. Counting starts with 0.
|
||||
|
||||
### Output
|
||||
*character of given index. Throws an error if index is out of bounds.
|
||||
|
||||
### Example
|
||||
```
|
||||
x = "hello";
|
||||
println(x.charAt(1)); # e
|
||||
```
|
||||
|
||||
## `<any>.toString()`
|
||||
Returns a string representation of any available object.
|
||||
|
||||
### Arguments
|
||||
_none_
|
||||
|
||||
### Output
|
||||
* string representation of object
|
||||
|
||||
### Example
|
||||
```
|
||||
println(@Eb3:8d.toString()); # D#3:(3/16)
|
||||
|
||||
println(typeOf(14.toString())); # string
|
||||
println(typeOf("hello, world".toString())); # string
|
||||
println(typeOf(true.toString())); # string
|
||||
println(typeOf(@Gb3:16d.toString())); # string
|
||||
println(typeOf({ first -> @c, second -> [1, 2, 3] }.toString())); # string
|
||||
```
|
||||
Reference in New Issue
Block a user