Skip to content

Commit 9bfc56d

Browse files
authored
Merge pull request #13 from RandomSoup/jango/functis
Update functions; First-class and overloading
2 parents ca88e48 + 18f450c commit 9bfc56d

File tree

1 file changed

+61
-10
lines changed

1 file changed

+61
-10
lines changed

readme.md

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Sack is a dynamically typed scripting language for beginner programmers that foc
2222
- List (Iterable Scope)
2323
- File (File object)
2424
- Byte (8 bit unsigned int)
25+
- Function
2526
- Sack enforces a style guide to improve readability. Compilers should by default warn for violations of the [style guide](style-guide.md).
2627

2728
## Syntax
@@ -136,7 +137,7 @@ let b = 0b11111111;
136137
```
137138

138139
### 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.
140141

141142
A list is defined using square brackets like so:
142143
```
@@ -333,33 +334,75 @@ As an example, here's a function that adds two numbers together:
333334
```
334335
functi add_numbers ( a, b ) {
335336
336-
let c = a + b;
337-
return c;
337+
let ret = a + b;
338+
return ret;
338339
339340
}
340341
```
341342

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:
343344
```
344-
let add_numbers = functi( a, b ) {
345+
functi( a, b ) {
345346
346-
let c = a + b;
347-
return c;
347+
let ret = a + b;
348+
return ret;
348349
349350
}
350351
```
351352

352-
So is this:
353+
Functions can be assigned to variables however:
353354
```
354355
functi add_numbers ( a, b ) {
355356
356-
let c = a + b;
357-
return c;
357+
let ret = a + b;
358+
return ret;
358359
359360
}
360361
let add_numbers_copy = add_numbers;
361362
```
362363

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+
363406
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:
364407
```
365408
functi add_numbers ( a, b ) {
@@ -583,6 +626,9 @@ x = string ( x );
583626
584627
# prints `String`
585628
print ( type ( x ) );
629+
630+
# prints `Functi`
631+
print ( type ( print ) );
586632
```
587633

588634
You can get the length of a list or string with `len()`:
@@ -644,6 +690,11 @@ The following rules govern scope in sack.
644690
3. Calling a function makes a new scope with nothing but the global scope above it.
645691
4. Functions can only access arguments or global vars that are defined before them.
646692

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+
647698
Following these rules, the following should print four:
648699
```
649700
let e = 3;

0 commit comments

Comments
 (0)