You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+62-11Lines changed: 62 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,11 @@ let b = 'world';
38
38
let c = "100";
39
39
```
40
40
41
+
Chaining declarations is also valid:
42
+
```
43
+
let a = "hello", b = 'world', c = "100";
44
+
```
45
+
41
46
Strings may not have mismatching quotes.
42
47
As such, the following is **invalid** syntax:
43
48
```
@@ -162,7 +167,7 @@ let a = 1;
162
167
# [ 0, 1, 2]
163
168
let x = [ 0, :a, 2]
164
169
165
-
# Consequently, this is also possible
170
+
# This is also possible, although not very useful
166
171
# [ 3 ]
167
172
let y = [ :3 ]
168
173
```
@@ -189,18 +194,37 @@ print ( x[a] );
189
194
print ( x['a'] );
190
195
```
191
196
192
-
If you try to access data that is beyond the length of a list, it is a runtime error as opposed to returning`none`
197
+
If you try to access a key that isn't in the list, it returns`none`
193
198
194
-
Note that this makes the following code **Invalid**:
199
+
For example:
195
200
```
196
-
let a = 10;
197
-
let x = [ 1, a: 2, 3 ];
201
+
let x = [ 1, 2, 3 ];
198
202
199
-
if ( x[a] == none ) {
200
-
# because the 10th index of the list does not exist this will error on runtime
203
+
if ( x[10] == none ) {
204
+
// 10 is way outside the list! So this executes
205
+
print("Lists much be shorter than 10!");
201
206
}
202
207
```
203
208
209
+
By extention, setting a key to `none` deletes it:
210
+
```
211
+
let x = [ 1, 2, 3 ];
212
+
213
+
# 2
214
+
print ( x[1] );
215
+
216
+
x[1] = none:
217
+
218
+
# 3
219
+
print ( x[1] );
220
+
```
221
+
222
+
However, this does mean that having `none` as keys is ignored:
223
+
```
224
+
# Prints 4, but because print returns none, x is set to [4]
225
+
let x = [ 4, print(4) ];
226
+
```
227
+
204
228
Lists can be appended to:
205
229
```
206
230
let x = [ 1, 2, 3 ];
@@ -229,6 +253,14 @@ x += [[ 4, 5, 6 ]];
229
253
# x is now [ 1, 2, 3, [ 4, 5, 6 ] ]
230
254
```
231
255
256
+
Lists can be multiplied:
257
+
```
258
+
let x = [ 1 ] * 3;
259
+
x += [ 2, 3 ] * 2;
260
+
261
+
# x is now [ 1, 1, 1, 2, 3, 2, 3 ]
262
+
```
263
+
232
264
### Files and Binary
233
265
234
266
File IO is a core feature of Sack, and as such it has it's own syntax.
@@ -406,12 +438,12 @@ functi add_numbers ( a, b, c ) {
406
438
407
439
}
408
440
409
-
// Is this add_numbers with 2 or 3 args? It's both!
441
+
# Is this add_numbers with 2 or 3 args? It's both!
410
442
let adder = add_numbers;
411
443
412
-
// Calls the 2 argument version
444
+
# Calls the 2 argument version
413
445
adder ( 47, 17 );
414
-
// Calls the 3 argument version
446
+
# Calls the 3 argument version
415
447
adder ( 21, 5, 1940 );
416
448
```
417
449
@@ -509,7 +541,7 @@ if a > 1 {
509
541
}
510
542
```
511
543
512
-
Else if is valid because it's a combination of the else and the if operator, it is not sytatically unique.
544
+
Else if is valid because it's a combination of the else and the if operator, it is not syntactically unique.
513
545
514
546
### Print
515
547
Print is a language defined output function. It takes in a single variable and returns the output to the terminal appending a newline
@@ -599,6 +631,13 @@ loop ( while a < 100 ) {
599
631
}
600
632
```
601
633
634
+
There is a quicker way to write a infinite loop:
635
+
```
636
+
loop {
637
+
print("This will print forever!");
638
+
}
639
+
```
640
+
602
641
The keywords `break` and `continue` will exit the loop, or continue to the next iteration respectively.
603
642
604
643
```
@@ -654,6 +693,11 @@ let x = 3;
654
693
# prints `Number`
655
694
print ( type ( x ) );
656
695
696
+
x = 5.25;
697
+
698
+
# prints `Decimal`
699
+
print ( type ( x ) );
700
+
657
701
x = string ( x );
658
702
659
703
# prints `String`
@@ -691,6 +735,13 @@ Args will also return a list of arguments passed to the program if called outsid
691
735
print ( args() );
692
736
```
693
737
If the function that is called has no arguments, `args()` will return `none`, however if args is called in the global scope it's length will always be greater than 0.
738
+
The `rand()` function can be used to get a random number between 0 and 1 (0 included), for example:
739
+
```
740
+
let e = rand();
741
+
# Nobody knows exactly what e is a now
742
+
# But it's known that type(e) == "Decimal"
743
+
# And that 0 <= e && e < 1
744
+
``
694
745
695
746
### Importing functions
696
747
By using the `import` keyword you can use functions from other sack programs.
0 commit comments