mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 14:57:18 +00:00
Enhacnementsand toolchain
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
// fib.vr — recursive fibonacci, small enough to benchmark with `vr bench`.
|
||||
|
||||
fn fib(n) {
|
||||
if n < 2 {
|
||||
return n
|
||||
}
|
||||
return fib(n - 1) + fib(n - 2)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println(fib(20))
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// hello.vr — a tour of the VuurRaaf language.
|
||||
// Run with: vr run examples/hello.vr
|
||||
|
||||
fn greet(name) {
|
||||
println("hello, " + name + "!")
|
||||
}
|
||||
|
||||
// recursive fibonacci
|
||||
fn fib(n) {
|
||||
if n < 2 {
|
||||
return n
|
||||
}
|
||||
return fib(n - 1) + fib(n - 2)
|
||||
}
|
||||
|
||||
// returns n! via a loop
|
||||
fn fact(n) {
|
||||
let result = 1
|
||||
let i = 1
|
||||
while i <= n {
|
||||
result = result * i
|
||||
i = i + 1
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fn main() {
|
||||
greet("vuurraaf")
|
||||
|
||||
let x = 6 * 7
|
||||
println("6 * 7 = ")
|
||||
println(x)
|
||||
|
||||
let name = "world"
|
||||
let msg = "hello " + name
|
||||
println(msg)
|
||||
println(msg == "hello world")
|
||||
|
||||
// booleans and short-circuiting
|
||||
let big = x > 40 and x < 50
|
||||
let small = x < 10 or x > 100
|
||||
println("x > 40 and x < 50: ")
|
||||
println(big)
|
||||
println("x < 10 or x > 100: ")
|
||||
println(small)
|
||||
println("not big: ")
|
||||
println(not big)
|
||||
|
||||
// if / else
|
||||
if x > 40 {
|
||||
println("x is big")
|
||||
} else {
|
||||
println("x is small")
|
||||
}
|
||||
|
||||
// while loop
|
||||
let i = 0
|
||||
while i < 3 {
|
||||
println("counting " + i)
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
println("fib(10) = ")
|
||||
println(fib(10))
|
||||
|
||||
println("fact(5) = ")
|
||||
println(fact(5))
|
||||
|
||||
// asserts are checked at runtime by the vm
|
||||
assert x == 42
|
||||
assert fact(5) == 120
|
||||
assert fib(10) == 55
|
||||
println("all asserts passed")
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// lib.vr — a small library compiled to its own object file.
|
||||
// vr compile examples/lib.vr -o /tmp/lib.vobj
|
||||
|
||||
fn double(n) {
|
||||
return n * 2
|
||||
}
|
||||
|
||||
fn square(n) {
|
||||
return n * n
|
||||
}
|
||||
|
||||
fn describe(n) {
|
||||
if n > 100 {
|
||||
return "big"
|
||||
}
|
||||
return "small"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
; math.vasm — raw bytecode, hand-written in VuurRaaf assembly.
|
||||
; Assemble + link + run with:
|
||||
; vr assemble examples/math.vasm
|
||||
; vr link math.vobj -o math.vbin
|
||||
; vr run math.vbin
|
||||
|
||||
.global main
|
||||
|
||||
main:
|
||||
push_int 6
|
||||
push_int 7
|
||||
mul
|
||||
println
|
||||
push_str "computed 6 * 7 = 42"
|
||||
println
|
||||
push_int 40
|
||||
push_int 2
|
||||
call add 2 ; calls the exported `add` symbol below
|
||||
println
|
||||
halt
|
||||
|
||||
; a callable function: takes two args, returns their sum
|
||||
.global add
|
||||
add:
|
||||
enter 0 ; no extra locals (args were copied in by `call`)
|
||||
load 0
|
||||
load 1
|
||||
add
|
||||
retv
|
||||
@@ -0,0 +1,41 @@
|
||||
// tests.vr — functions named test_* are picked up by `vr test`.
|
||||
// vr test examples/tests.vr
|
||||
|
||||
fn add(a, b) {
|
||||
return a + b
|
||||
}
|
||||
|
||||
fn test_add() {
|
||||
assert add(2, 3) == 5
|
||||
assert add(-1, 1) == 0
|
||||
}
|
||||
|
||||
fn test_strings() {
|
||||
let a = "foo"
|
||||
let b = "bar"
|
||||
assert a + b == "foobar"
|
||||
assert a != b
|
||||
assert a == "foo"
|
||||
}
|
||||
|
||||
fn test_loops() {
|
||||
let total = 0
|
||||
let i = 1
|
||||
while i <= 10 {
|
||||
total = total + i
|
||||
i = i + 1
|
||||
}
|
||||
assert total == 55
|
||||
}
|
||||
|
||||
fn test_booleans() {
|
||||
let x = 7
|
||||
assert x > 3 and x < 10
|
||||
assert x == 7 or x == 8
|
||||
assert not (x == 0)
|
||||
}
|
||||
|
||||
fn test_failing() {
|
||||
// this one is meant to fail — shows up in `vr test` output
|
||||
assert 1 == 2
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// use_lib.vr — calls functions defined in lib.vr; the two object files are
|
||||
// linked together by the linker:
|
||||
// vr compile examples/lib.vr -o /tmp/lib.vobj
|
||||
// vr compile examples/use_lib.vr -o /tmp/use_lib.vobj
|
||||
// vr link /tmp/lib.vobj /tmp/use_lib.vobj -o /tmp/use_lib.vbin
|
||||
// vr run /tmp/use_lib.vbin
|
||||
|
||||
fn main() {
|
||||
let d = double(21)
|
||||
println("double(21) = ")
|
||||
println(d)
|
||||
|
||||
let s = square(9)
|
||||
println("square(9) = ")
|
||||
println(s)
|
||||
|
||||
println("describe(500) = ")
|
||||
println(describe(500))
|
||||
|
||||
assert double(21) == 42
|
||||
assert square(9) == 81
|
||||
println("cross-file calls ok")
|
||||
}
|
||||
Reference in New Issue
Block a user