hare言語について。
hello_world.ha
use fmt; export fn main() void = { fmt::println("Hello world!")!; }; function_and_parameter.ha
use bufio; use fmt; use os; use strings; export fn main() void = { const user = askname(); greet(user); }; // Ask the user to provide their name. fn askname() str = { fmt::println("Hello! Please enter your name:")!; const name = bufio::read_line(os::stdin)! as []u8; return strings::fromutf8(name)!; }; // Greets a user by name. fn greet (user: str) void = { fmt::printfln("Hello, {}!", user)!; }; use fmt; use io; use os; use strings; export fn main() void = { // Example A const source = os::open("main.ha")!; const source = io::drain(source)!; const source = strings::fromutf8(source)!; const source = strings::split(source, "\n"); first3(source); // Example B let i: int= 1337, j: int = 42; fmt::printfln("{} + {} = {}", i, j, i + j)!; j = i; fmt::printfln("{} + {} = {}", i, j, i + j)!; }; fn first3(lines: []str) void = { fmt::println("The first three lines of main.ha are:")!; fmt::println(lines[0])!; fmt::println(lines[1])!; fmt::println(lines[2])!; }; export fn main() void = { // Numeric types can be declared explicitly; let a: int = 10, // Signe integer b: uint = 10, // Unsigned integer c: u8 = 10, // Unsigned 8-bit integer d: f32 = 13.37; // 32-bit floating point number // Or inferrd from a suffix: let a = 10i, // Signed integer b = 10u, // Unsigned integer c = 10u8, d = 13.37f32; // Some other types: let a: str = "hi", b: (int, int) = (42, 24), c: struct { x: int, y: int, } = struct { x: int = 10, y: int = 20, }, d: [4]int = [1, 2, 3, 4], e: []int = [1, 2, 3, 4]; }; use fmt; type coords = struct { x: int, y: int }; export fn main() void = { let player1 = struct { x: int = 10, y: int = 20, }; let player2 = coords { y = 10, x = 20, }; let player3: (int, int) = (42, 24); fmt::printfln("Player 1: ({}, {})", player1.x, player1.y)!; fmt::printfln("Player 2: ({}, {})", player2.x, player2.y)!; fmt::printfln("Player 3: ({}, {})", player3.0, player3.1)!; }; use fmt; export fn main() void = { let x: [_]int = [1, 3, 3, 7]; assert(len(x) == 4); //len() built=in assert(x[3] == 7); // 0-indexed x[3] = 8; // assignment assert(x[3] == 8); let y: [1024]int = [1, 3, 3, 7, 42...]; // Fill remainder with 42 printvals(y[..4]); printvals(y[2..100]); printvals(y[2..100]); }; fn printvals(in: []int) void = { fmt::printfln("input: {} integers", len(in))!; for(let i = 0z; i < len(in); i += 1) { fmt::printfln("inp[{}]: {}", i, in[i])!; in[i]+=1; }; fmt::println()!; }; use fmt; export fn main() void = { let x: [_]int = [1, 3, 3, 7]; assert(len(x) == 4); //len() built=in assert(x[3] == 7); // 0-indexed x[3] = 8; // assignment assert(x[3] == 8); let y: [1024]int = [1, 3, 3, 7, 42...]; // Fill remainder with 42 printvals(y[..4]); printvals(y[2..100]); printvals(y[2..100]); let x = [1, 2, 3]; // x[4]; // Abort! let x: [_]int = [1 ,2 ,3]; let y: *[*]int = &x; y[4]; // Undefined behavior! }; fn printvals(in: []int) void = { fmt::printfln("input: {} integers", len(in))!; for(let i = 0z; i < len(in); i += 1) { fmt::printfln("inp[{}]: {}", i, in[i])!; in[i]+=1; }; fmt::println()!; }; use crypto::sha256; use encoding::hex; use hash; use io; use os; use fmt; export fn main() void = { // Pointer basics let i = 10; fmt::println(i)!; increment(&i); fmt::println(i)!; // Applied usage const hash = sha256::sha256(); const file = os::open("main.ha")!; io::copy(&hash, file)!; let sum: [sha256::SZ]u8 = [0...]; hex::encode(os::stdout, sum)!; fmt::println()!; }; fn increment(ptr: *int) void = { *ptr = *ptr + 1; }; An introduction to the Hare programming language
...