From 34b32d1a3419e5522569f6e365ead0f193c336eb Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Wed, 9 Dec 2020 20:18:45 +0100 Subject: [PATCH] Add variables section in docs --- docs/SUMMARY.md | 2 ++ docs/concepts/SUMMARY.md | 5 +++++ docs/concepts/variables.md | 22 ++++++++++++++++++++++ examples/playground.sb | 10 +++------- src/parser/tests.rs | 14 ++++++++++++++ 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 docs/concepts/SUMMARY.md create mode 100644 docs/concepts/variables.md diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 0854e81..04c9d13 100644 --- a/docs/SUMMARY.md +++ b/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) diff --git a/docs/concepts/SUMMARY.md b/docs/concepts/SUMMARY.md new file mode 100644 index 0000000..7724e4f --- /dev/null +++ b/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. diff --git a/docs/concepts/variables.md b/docs/concepts/variables.md new file mode 100644 index 0000000..a977e9e --- /dev/null +++ b/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 +``` diff --git a/examples/playground.sb b/examples/playground.sb index 9c10151..d272666 100644 --- a/examples/playground.sb +++ b/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 - } -} \ No newline at end of file + let x = 10 + return x + 10 +} diff --git a/src/parser/tests.rs b/src/parser/tests.rs index 633a16d..d3e2107 100644 --- a/src/parser/tests.rs +++ b/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 = "