Browse Source

Add variables section in docs

github-actions
Garrit Franke 3 years ago
parent
commit
34b32d1a34
  1. 2
      docs/SUMMARY.md
  2. 5
      docs/concepts/SUMMARY.md
  3. 22
      docs/concepts/variables.md
  4. 10
      examples/playground.sb
  5. 14
      src/parser/tests.rs

2
docs/SUMMARY.md

@ -3,3 +3,5 @@
- [Introduction](./introduction/SUMMARY.md)
- [Installation](./introduction/installation.md)
- [Hello World!](./introduction/hello-world.md)
- [Common language concepts](./concepts/SUMMARY.md)
- [Variables](./concepts/variables.md)

5
docs/concepts/SUMMARY.md

@ -0,0 +1,5 @@
# Common language concepts
This chapter covers concepts that appear in almost every programming language and how they work in Rust. Many programming languages have much in common at their core.
Specifically, you’ll learn about variables, basic types, functions, comments, and control flow. These foundations will be in every Sabre program, and learning them early will give you a strong core to start from.

22
docs/concepts/variables.md

@ -0,0 +1,22 @@
# Variables
If you are familiar with some other programming language, the way Sabre handles variables won't surprise you.
To declare a variable, the `let` keyword is used.
```
// variables.sb
fn main() {
let x = 10
let y = 5
return x + y
}
```
Run this code using the sabre CLI:
```
$ sabre build variables.sb -o variables.js
$ node variables.js
15
```

10
examples/playground.sb

@ -1,8 +1,4 @@
fn main() {
let x = true && false
let y = false && true || true
let z = x && true
while true && x {
return x
}
}
let x = 10
return x + 10
}

14
src/parser/tests.rs

@ -89,6 +89,20 @@ fn test_parse_variable_declaration() {
assert!(tree.is_ok())
}
#[test]
fn test_parse_variable_declaration_added() {
let raw = "
fn main() {
let x = 10
let y = 5
return x + y
}
";
let tokens = tokenize(raw);
let tree = parse(tokens, Some(raw.to_string()));
assert!(tree.is_ok())
}
#[test]
fn test_parse_function_with_args() {
let raw = "

Loading…
Cancel
Save