Browse Source

Add documentation for datatypes

github-actions
Garrit Franke 12 months ago
parent
commit
0a17c78194
  1. 2
      README.md
  2. 1
      docs/SUMMARY.md
  3. 61
      docs/concepts/datatypes.md
  4. 2
      docs/concepts/variables.md
  5. 2
      docs/introduction/hello-world.md
  6. 2
      examples/fib.sb
  7. 2
      examples/greeter.sb
  8. 4
      examples/hello_world.sb
  9. 4
      examples/leapyear.sb
  10. 2
      examples/playground.sb

2
README.md

@ -19,7 +19,7 @@ The Sabre compiler emits JavaScript, until the language has matured sufficiently
fn main() {
let num = 10
print(fib(num))
println(fib(num))
}
fn fib(n) {

1
docs/SUMMARY.md

@ -5,3 +5,4 @@
- [Hello World!](./introduction/hello-world.md)
- [Common language concepts](./concepts/SUMMARY.md)
- [Variables](./concepts/variables.md)
- [Data Types](./concepts/datatypes.md)

61
docs/concepts/datatypes.md

@ -0,0 +1,61 @@
# Datatypes
> **Note:** Due to the fact that Sabre currently emits javascript, static types are not needed, as of yet. They will however be introduced soon, once a statically typed backend is used.
## The Integer type
The `integer` datatype represents a number. The JavaScript backend interprets any integer as a `Number` type.
```
fn main() {
let sum = 1 + 2
println("1 + 2 is ", sum)
}
```
```
$ sabre build main.sb -o main.js
$ node main.js
1 + 2 is 3
```
## The String type
A string is a sequence of characters.
```
fn main() {
let name = "Jon"
println("Hello " + name)
}
```
```
$ sabre build main.sb -o main.js
$ node main.js
Hello Jon
```
## The Array type
Arrays represent a sequence of values. They can hold any number of values of a specific type.
> **NOTE:** Currently, there is no type-checking involved when creating arrays. There will be, once a type system is in place, so don't get too attached to mixing and matching element types. ;)
```
fn main() {
let fruits = ["Banana", "Apple", "Pineapple"]
for fruit in fruits {
println(fruit)
}
}
```
```
$ sabre build main.sb -o main.js
$ node main.js
Banana
Apple
Pineapple
```

2
docs/concepts/variables.md

@ -9,7 +9,7 @@ To declare a variable, the `let` keyword is used.
fn main() {
let x = 10
let y = 5
print(x + y)
println(x + y)
}
```

2
docs/introduction/hello-world.md

@ -21,7 +21,7 @@ Now open the main.sb file you just created and enter the following code:
```
fn main() {
print("Hello, world!")
println("Hello, world!")
}
```

2
examples/fib.sb

@ -1,6 +1,6 @@
fn main() {
let num = 10
print(fib(num))
println(fib(num))
}
fn fib(n) {

2
examples/greeter.sb

@ -1,5 +1,5 @@
fn main() {
print(greet("World"))
println(greet("World"))
}
fn greet(name) {

4
examples/hello_world.sb

@ -1,3 +1,3 @@
fn main(n) {
print("Hello World")
fn main() {
println("Hello World")
}

4
examples/leapyear.sb

@ -10,8 +10,8 @@ fn main() {
let ly = divisibleBy4 && divisibleBy100
if ly || divisibleBy400 {
print("Leap year")
println("Leap year")
} else {
print("Not a leap year")
println("Not a leap year")
}
}

2
examples/playground.sb

@ -1,5 +1,5 @@
fn main() {
let x = 10
x = 5
print("Hello World!")
println("Hello World!")
}

Loading…
Cancel
Save