Aber is a high-level programming language inspired by languages such as Rust, Lisp, Elixir, and Zig. It includes high-level abstractions such as structures, unions and traits, compile-time computation and automatic generalization. But it also allows low-level memory access and platform-dependent code writing.
Contents of the file hello_world.aber:
std::use_prelude;
print("Hello World!", run nodisplay());
Сompiler command:
aberc --script hello_world.aber
Output:
Hello World!
Contents of the file fibonacci.aber:
std::use_prelude;
fn fibonacci(n) {
if n.< 2 {
return n;
}
fibonacci(n.- 1).+ fibonacci(n.- 2)
};
print("result: ", run fibonacci(9));
Сompiler command:
aberc --script fibonacci.aber
Output:
result: 21
Contents of the file structures_unions.aber:
std::use_prelude;
struct @[T] Vec2(x: T, y: T);
impl Vec2 {
fn new(x, y) {
Self::(x, y)
}
};
union Option(None::(), Some::(_));
let vec2(run Vec2::new(10, 15));
println("x: ", vec2.x, ", y: ", vec2.y);
let option(Option::Some::(23));
option.match(
Option::Some::(i): println("Some::(", i, ")")
);
Сompiler command:
aberc --script structures_unions.aber
Output:
x: 10, y: 15
Some(23)
Contents of the file traits.aber:
std::use_prelude;
use std::(traits::derive, fmt::Debug, ops::Operator);
struct @[T] Vec2(x: T, y: T);
derive(@[T] Vec2[T], Debug);
impl Vec2 {
fn new(x, y) {
Self::(x, y)
}
};
impl
@[T]::(T: Operator+[T])
Operator+[Vec2[T]]: Vec2[T]
{
let Output(Vec2[T::Output]);
fn +(self: Self, other: Self) {
Self::new(self.x.+ other.x, self.y.+ other.y)
};
};
println("result: ", debug run { Vec2::new(10, 15).+ Vec2::new(42, 31) });
Сompiler command:
aberc --script traits.aber
Output:
result: Vec2(x: 52, y: 46)
Contents of the file unit_testing.aber:
std::use_prelude;
fn foo(n) { n.+ 1 };
testing fn test() {
assert_eq(foo(1), 1).?;
};
fn main() {
print("result: ", run foo(1));
};
Сompiler command:
aberc --run unit_testing.aber
Output:
result: 2
Сompiler command:
aberc --test unit_testing.aber
Output:
The ''::test failed at ./unit_testing.aber:5:1:
assert_eq(foo(1), 1).?;
left: 2
right: 1