more examples

This commit is contained in:
Neemek 2026-07-15 21:50:23 +02:00
parent c5a843ea39
commit 9003149114
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
19 changed files with 287 additions and 0 deletions

40
examples/bad.ang Normal file
View file

@ -0,0 +1,40 @@
import "lib/math.ang"
primes := [2]
func is_prime(x: number) boolean {
i := 0
while i < primes.length() && primes.at(i)*primes.at(i) < x {
if mod(x, primes.at(i)) == 0 {
return false
}
i = i + 1
}
return true
}
n := 1
max := 100000
while n < max {
n = n + 2
if is_prime(n) {
primes.append(n)
# Update counter
print(char(0x0D))
print(str(n))
print("/")
print(str(max))
print(char(0x09))
print(str(roundd(n/max*100, 2)))
print("%")
print(char(0x09))
print(str(primes.length()))
print(" primes")
}
}
write(str(primes))

20
examples/blemish.ang Normal file
View file

@ -0,0 +1,20 @@
fn fib(n: int) -> int {
if n < 2 {
n
} else {
fib(n-1) + fib(n-2)
}
}
println(fib(2))
fn other(n: int) -> int {
if n <= 0 {
n
} else {
println(n)
(n - 1)
}
}
println(other(2))

15
examples/chars.ang Normal file
View file

@ -0,0 +1,15 @@
MAX_WIDTH := 16
print(" ")
w := 1
n := 0x21
while n < 0xA0 {
print(char(n))
n = n + 1
w = w + 1
if w >= MAX_WIDTH {
write("")
w = 0
}
}

24
examples/codegen.ang Normal file
View file

@ -0,0 +1,24 @@
passphrase := "Hello world!".split("")
start := [0, 0, 0]
modulus := 10
base := byte("!")
i := 0
n := 0
while n < passphrase.length() {
b := byte(passphrase.at(n))
v = start.at(i) + b - base
while v >= modulus {
v = v - modulus
}
start.put(i, v)
if i >= 3 {
i = 0
}
n = n + 1
}

2
examples/emoji.ang Normal file
View file

@ -0,0 +1,2 @@
write(char(0x12) + char(0x85) + char(0x07))

11
examples/era3.ang Normal file
View file

@ -0,0 +1,11 @@
fn counter() -> (fn() -> int) {
i := 0
fn() -> int { i = i + 1 }
}
next := counter()
println(next())
println(next())
println(next())

4
examples/fails.ang Normal file
View file

@ -0,0 +1,4 @@
import "lib/honning.ang"
write(_bell+_italic+"Hello "+_underline+"world "+_strike+"micheal"+_reset)

8
examples/foo.ang Normal file
View file

@ -0,0 +1,8 @@
fn foo(n: int) -> str {
if n % 2 == 0 {
"foo"
} else {
"bar"
}
}

15
examples/imp.ang Normal file
View file

@ -0,0 +1,15 @@
func is_cool(x: number|string) boolean {
if x == "cool" {
return true
} else if x == 69 {
return true
}
return nil
}
write(str(is_cool("not cool")))
write(str(is_cool("cool")))
write(str(is_cool(0)))
write(str(is_cool(69)))

2
examples/inc.ang Normal file
View file

@ -0,0 +1,2 @@
foo := include "foo.ang"

4
examples/massacre.ang Normal file
View file

@ -0,0 +1,4 @@
a := fn b(n: int) -> int {
n + 1
}

8
examples/network.ang Normal file
View file

@ -0,0 +1,8 @@
func get(url: string) string {
res := request("GET", url, "")
return res.text
}
write(get("https://www.neemek.com/hello.txt"))

18
examples/neutral-pH.ang Normal file
View file

@ -0,0 +1,18 @@
import "../lib/math.ang"
# Inputs
c := 1.0*pow(10.0, -2.0)
println(c)
pH := -log(c, 10.0)
println(pH)
if pH == 7.0 {
println("pH-en er nøytral (=7)")
} else if pH < 7.0 {
println("pH-en er sur (<7)")
} else {
println("pH-en er basisk (>7)")
}

21
examples/pos.ang Normal file
View file

@ -0,0 +1,21 @@
type Vec2 = (float, float)
fn add(a: Vec2, b: Vec2) -> Vec2 {
(a[0] + b[0], a[1] + b[1])
}
fn sub(a: Vec2, b: Vec2) -> Vec2 {
(a[0] - b[0], a[1] - b[1])
}
fn dot(a: Vec2, b: Vec2) -> float {
a[0]*b[0] + a[1]*b[1]
}
u := (0.0, 1.0)
v := (2.0, 3.0)
println(add(u, v))
println(sub(u, v))
println(dot(u, v))

6
examples/sqrt.ang Executable file
View file

@ -0,0 +1,6 @@
#!/Users/neemek/Code/anglais/cli/cli run
import "lib/math.ang"
x := sqrt(9485739448)
write(str(x))

8
examples/very_bad.ang Normal file
View file

@ -0,0 +1,8 @@
a := 0
while a < 10 {
print(".")
a = a + 1
}
write("")

25
examples/warp.ang Normal file
View file

@ -0,0 +1,25 @@
# Aliases
type Receiver = fn() -> any
type Sender = fn(any)
fn new_channel() -> (Sender, Receiver) {
queue := []
fn send(n: any) {
queue.push(n) # mutate in-place
}
fn recv() -> any | nil {
queue.pop()
}
(send, recv)
}
(send, recv) := new_channel()
(
send:,
recv:,
)

4
examples/worst.ang Normal file
View file

@ -0,0 +1,4 @@
import "worst.ang"
write("Hello world!")