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
+61-10Lines changed: 61 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,7 @@ Sack is a dynamically typed scripting language for beginner programmers that foc
22
22
- List (Iterable Scope)
23
23
- File (File object)
24
24
- Byte (8 bit unsigned int)
25
+
- Function
25
26
- Sack enforces a style guide to improve readability. Compilers should by default warn for violations of the [style guide](style-guide.md).
26
27
27
28
## Syntax
@@ -136,7 +137,7 @@ let b = 0b11111111;
136
137
```
137
138
138
139
### Lists
139
-
Lists are self contained iterable scope blocks. See the scope section for rules on scope.
140
+
Lists are self contained iterable scope blocks.
140
141
141
142
A list is defined using square brackets like so:
142
143
```
@@ -333,33 +334,75 @@ As an example, here's a function that adds two numbers together:
333
334
```
334
335
functi add_numbers ( a, b ) {
335
336
336
-
let c = a + b;
337
-
return c;
337
+
let ret = a + b;
338
+
return ret;
338
339
339
340
}
340
341
```
341
342
342
-
Functions can not be retroactively assigned to variables. As such, the following is **invalid** syntax:
343
+
Functions aren't allowed to be anonymous. As such, the following is **invalid** syntax:
343
344
```
344
-
let add_numbers = functi( a, b ) {
345
+
functi( a, b ) {
345
346
346
-
let c = a + b;
347
-
return c;
347
+
let ret = a + b;
348
+
return ret;
348
349
349
350
}
350
351
```
351
352
352
-
So is this:
353
+
Functions can be assigned to variables however:
353
354
```
354
355
functi add_numbers ( a, b ) {
355
356
356
-
let c = a + b;
357
-
return c;
357
+
let ret = a + b;
358
+
return ret;
358
359
359
360
}
360
361
let add_numbers_copy = add_numbers;
361
362
```
362
363
364
+
Functions can also be overloaded, this is done based on the number of arguments:
365
+
```
366
+
functi add_numbers ( a, b ) {
367
+
368
+
let ret = a + b;
369
+
return ret;
370
+
371
+
}
372
+
373
+
functi add_numbers ( a, b, c ) {
374
+
375
+
let ret = a + b + c;
376
+
return ret;
377
+
378
+
}
379
+
```
380
+
381
+
The overloaded function to use is chosen when it is called, this may seem obvious but consider the following (which is valid):
382
+
```
383
+
functi add_numbers ( a, b ) {
384
+
385
+
let ret = a + b;
386
+
return ret;
387
+
388
+
}
389
+
390
+
functi add_numbers ( a, b, c ) {
391
+
392
+
let ret = a + b + c;
393
+
return ret;
394
+
395
+
}
396
+
397
+
// Is this add_numbers with 2 or 3 args? It's both!
398
+
let adder = add_numbers;
399
+
400
+
// Calls the 2 argument version
401
+
adder ( 47, 17 );
402
+
// Calls the 3 argument version
403
+
adder ( 21, 5, 1940 );
404
+
```
405
+
363
406
Functions can return a singular variable using the `return` keyword. Do note that any code after a return is unreachable and will be ignored by the implementation. As such, the following will result in unreachable code:
364
407
```
365
408
functi add_numbers ( a, b ) {
@@ -583,6 +626,9 @@ x = string ( x );
583
626
584
627
# prints `String`
585
628
print ( type ( x ) );
629
+
630
+
# prints `Functi`
631
+
print ( type ( print ) );
586
632
```
587
633
588
634
You can get the length of a list or string with `len()`:
@@ -644,6 +690,11 @@ The following rules govern scope in sack.
644
690
3. Calling a function makes a new scope with nothing but the global scope above it.
645
691
4. Functions can only access arguments or global vars that are defined before them.
646
692
693
+
There are also some rules for defining functions and variables:
694
+
1. Functions and variables can not be defined with the name of a builtin function.
695
+
2. Functions can not be defined if an existing variable has that name, or if a function with the same number of args exists.
696
+
3. Variables can not be defined if a function with the same name exists.
697
+
647
698
Following these rules, the following should print four:
0 commit comments