Browse Source

Add bubblesort example

github-actions
Garrit Franke 3 years ago
parent
commit
4842d92b74
  1. 3
      TODO
  2. 25
      examples/bubblesort.sb
  3. 46
      src/parser/tests.rs

3
TODO

@ -1,8 +1,7 @@
# Bugs
- Uninitialized variables don't compile
# Features
- Array access syntax
- For loop
- Type system
- Nested expressions like (1 + (2 * 2))

25
examples/bubblesort.sb

@ -0,0 +1,25 @@
// Doesn't compile yet
fn main() {
let arr = [2, 5, 3, 1, 4]
let n = len(arr)
let c = 0
while c < n {
let d = 0
while d < n - c - 1 {
let current = arr[d]
let next = arr[d+1]
if current > next {
let swap = arr[d]
arr[d] = arr[d+1]
arr[d+1] = swap
}
d = d + 1
}
c = c + 1
}
}

46
src/parser/tests.rs

@ -484,3 +484,49 @@ fn test_array_access_standalone() {
let tree = parse(tokens, Some(raw.to_string()));
assert!(tree.is_ok())
}
#[test]
fn test_array_access_assignment() {
let raw = "
fn main() {
let arr = [1, 2, 3]
let x = arr[0]
return x
}
";
let tokens = tokenize(raw);
let tree = parse(tokens, Some(raw.to_string()));
assert!(tree.is_ok())
}
#[test]
#[ignore]
fn test_array_access_in_if() {
let raw = "
fn main() {
if arr[d] > arr[d+1] {
let swap = arr[d]
arr[d] = arr[d+1]
arr[d+1] = swap
}
}
";
let tokens = tokenize(raw);
let tree = parse(tokens, Some(raw.to_string()));
assert!(tree.is_ok())
}
#[test]
#[ignore]
fn test_uninitialized_variables() {
let raw = "
fn main() {
let x
let y
}
";
let tokens = tokenize(raw);
let tree = parse(tokens, Some(raw.to_string()));
assert!(tree.is_ok())
}

Loading…
Cancel
Save