sample-code: update for new WIP syntax (it's more persnickety)

This commit is contained in:
John 2025-10-19 19:27:09 -04:00
parent 6b24980fc7
commit b54826cdd5
7 changed files with 46 additions and 42 deletions

View File

@ -8,9 +8,5 @@ fn main() {
/// Implements the classic recursive definition of fib()
fn fib(a: i64) -> i64 {
if a > 1 {
fib(a - 1) + fib(a - 2)
} else {
1
}
if a > 1 { fib(a - 1) + fib(a - 2) } else { a }
}

View File

@ -26,7 +26,7 @@ fn ascii() {
}
print(n_digit(row), n_digit(col), ' ')
}
print(" │")
print(" │");
for col in 0..16 {
print(ascii_picture(row << 4 | col))
}

View File

@ -1,7 +1,7 @@
//! Implements format string evaluation in weak Conlang
/// Formats a string
#[rustfmt::skip]
fn f(__fmt: &str) -> &str {
let __out = "";
let __expr = "";
@ -10,13 +10,13 @@ fn f(__fmt: &str) -> &str {
for __c in chars(__fmt) {
match __c {
'{' => {
__depth += 1
__depth += 1;
if __depth <= 1 {
continue
}
},
'}' => {
__depth -= 1
__depth -= 1;
if __depth <= 0 {
__out = fmt(__out, __label, eval(__expr));
(__expr, __label) = ("", "");
@ -24,11 +24,11 @@ fn f(__fmt: &str) -> &str {
}
},
':' => if __depth == 1 && __label.len() == 0 {
__label = __expr + __c
__label = __expr + __c;
continue
},
'=' => if __depth == 1 && __label.len() == 0 {
__label = __expr + __c
__label = __expr + __c;
continue
},
_ => {}

View File

@ -30,15 +30,17 @@ Computes the frequency of ascii characters in a block of text, and prints it buc
Press Ctrl+D to quit.";
fn main() {
println(msg)
println(msg);
let lines = "";
loop {
let line = get_line();
if line == "" { break() }
if line == "" {
break ();
}
lines += line;
}
let freq = frequency(lines)
let plot = plot_freq(freq)
let freq = frequency(lines);
let plot = plot_freq(freq);
println(plot)
}

View File

@ -6,24 +6,28 @@ const EPSILON: f64 = 8.8541878188 / 1000000000000.0;
/// Calcuates the absolute value of a number
fn f64_abs(n: f64) -> f64 {
let n = n as f64
if n < (0.0) { -n } else { n }
let n = n as f64;
if n < (0.0) {
-n
} else {
n
}
}
/// Square root approximation using Newton's method
fn sqrt(n: f64) -> f64 {
let n = n as f64
let n = n as f64;
if n < 0.0 {
return 0.0 / 0.0 // TODO: NaN constant
return 0.0 / 0.0; // TODO: NaN constant
}
if n == 0.0 {
return 0.0
return 0.0;
}
let z = n
let z = n;
loop {
let adj = (z * z - n) / (2.0 * z)
z -= adj
let adj = (z * z - n) / (2.0 * z);
z -= adj;
if adj.f64_abs() < EPSILON {
break z;
}
@ -37,7 +41,9 @@ fn pythag(a: f64, b: f64) -> f64 {
/// Quadratic formula: (-b ± (b² - 4ac)) / 2a
fn quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
let a = a as f64; let b = b as f64; let c = c as f64;
let a = a as f64;
let b = b as f64;
let c = c as f64;
(
(-b + sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
(-b - sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
@ -48,9 +54,9 @@ fn main() {
for i in 0..10 {
println("sqrt(", i, ") ≅ ", sqrt(i as f64))
}
println("\nPythagorean Theorem")
println("Hypotenuse of ⊿(5, 12): ", pythag(5.0, 12.0))
println("\nPythagorean Theorem");
println("Hypotenuse of ⊿(5, 12): ", pythag(5.0, 12.0));
println("\nQuadratic formula")
println("Roots of 10x² + 4x - 1: ", quadratic(10.0, 40, -1.0))
println("\nQuadratic formula");
println("Roots of 10x² + 4x - 1: ", quadratic(10.0, 40, -1.0));
}

View File

@ -1,15 +1,15 @@
//! Disjoint Set Forest implementation of the Union Find algorithm
// enum TreeNode {
// Empty,
// Filled { parent: usize, rank: usize }
// }
enum TreeNode {
Empty,
Filled { parent: usize, rank: usize }
}
// TODO: Namespace based on type of first argument
fn makeset(f, x) {
match (*f)[x] {
() => (*f)[x] = Filled { parent: x, rank: 0 },
() => {(*f)[x] = Filled { parent: x, rank: 0 }},
_ => {}
}
}
@ -52,11 +52,11 @@ fn show(f) {
}
fn test(f) {
"Union Find on Disjoint Set Forest".println()
"Union Find on Disjoint Set Forest".println();
for i in 0..10 { makeset(f, i) }
for i in 10..20 { makeset(f, i) }
show(f)
println()
show(f);
println();
for i in 1..10 { union(f, i*2-1, i*2) }
for i in 5..10 { union(f, i*2+1, i*2) }
@ -64,6 +64,6 @@ fn test(f) {
}
fn main() {
let f = [();20]
let f = [();20];
f.test()
}

View File

@ -4,7 +4,7 @@ use super::preamble::*;
struct UnitLike;
struct TupleLike(super::num::i32, super::test::char)
struct TupleLike(super::num::i32, super::test::char);
struct StructLike {
pub member1: UnitLike,