Browse Source

docs: add functions

github-actions
Garrit Franke 3 years ago
parent
commit
1fa86badab
  1. 1
      docs/SUMMARY.md
  2. 42
      docs/concepts/functions.md
  3. 2
      src/parser/node_type.rs

1
docs/SUMMARY.md

@ -6,6 +6,7 @@
- [Common language concepts](./concepts/SUMMARY.md) - [Common language concepts](./concepts/SUMMARY.md)
- [Variables](./concepts/variables.md) - [Variables](./concepts/variables.md)
- [Data Types](./concepts/datatypes.md) - [Data Types](./concepts/datatypes.md)
- [Functions](./concepts/functions.md)
- [Developer Resources](./developers/SUMMARY.md) - [Developer Resources](./developers/SUMMARY.md)
- [Contributing to Sabre](./developers/contributing.md) - [Contributing to Sabre](./developers/contributing.md)
- [Compiler Backends](./developers/backends.md) - [Compiler Backends](./developers/backends.md)

42
docs/concepts/functions.md

@ -0,0 +1,42 @@
# Functions
Functions are pervasive in Sabre code. You’ve already seen one of the most important functions in the language: the `main` function, which is the entry point of many programs. You've also seen the `fn` keyword, which allows you to declare new functions.
Sabre code uses snake_case as the conventional style for function and variable names. In snake case, all letters are lowercase and underscores separate words. Here’s a program that contains an example function definition:
```
fn main() {
println("Hello, world!")
anotherFunction()
}
fn another_function() {
println("Another function.")
}
```
We can call any function we’ve defined by entering its name followed by a set of parentheses. Because `another_function` is defined in the program, it can be called from inside the main function. Note that we defined `another_function` after the `main` function in the source code; we could have defined it before as well. Sabre doesn’t care where you define your functions, only that they’re defined somewhere.
## Function parameters
Functions can also be defined to have parameters, which are special variables that are part of a function’s signature. When a function has parameters, you can provide it with concrete values for those parameters. Technically, the concrete values are called arguments, but in casual conversation, people tend to use the words parameter and argument interchangeably for either the variables in a function’s definition or the concrete values passed in when you call a function.
The following rewritten version of `another_function` shows what parameters look like in Sabre:
```
fn main() {
another_function(5)
}
fn another_function(x: int) {
println(x)
}
```
## Functions contain statements
TODO
## Return types
TODO

2
src/parser/node_type.rs

@ -58,7 +58,7 @@ impl TryFrom<String> for Type {
"int" => Ok(Self::Int), "int" => Ok(Self::Int),
"string" => Ok(Self::Str), "string" => Ok(Self::Str),
"bool" => Ok(Self::Bool), "bool" => Ok(Self::Bool),
_ => Err("Expected Type".into()), _ => Err("Unknown Type".into()),
} }
} }
} }

Loading…
Cancel
Save