Compare commits
8 commits
c5a843ea39
...
df639235b0
| Author | SHA1 | Date | |
|---|---|---|---|
| df639235b0 | |||
| 86760689f1 | |||
| 7a5203cc26 | |||
| 3bccc227ba | |||
| 09ac16a42d | |||
| ff11d7e0ed | |||
| 19011e67da | |||
| ca0031431d |
19 changed files with 287 additions and 0 deletions
40
examples/bad.ang
Normal file
40
examples/bad.ang
Normal 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
20
examples/blemish.ang
Normal 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
15
examples/chars.ang
Normal 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
24
examples/codegen.ang
Normal 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
2
examples/emoji.ang
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
write(char(0x12) + char(0x85) + char(0x07))
|
||||||
11
examples/era3.ang
Normal file
11
examples/era3.ang
Normal 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
4
examples/fails.ang
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
import "lib/honning.ang"
|
||||||
|
|
||||||
|
write(_bell+_italic+"Hello "+_underline+"world "+_strike+"micheal"+_reset)
|
||||||
|
|
||||||
8
examples/foo.ang
Normal file
8
examples/foo.ang
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
fn foo(n: int) -> str {
|
||||||
|
if n % 2 == 0 {
|
||||||
|
"foo"
|
||||||
|
} else {
|
||||||
|
"bar"
|
||||||
|
}
|
||||||
|
}
|
||||||
15
examples/imp.ang
Normal file
15
examples/imp.ang
Normal 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
2
examples/inc.ang
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
foo := include "foo.ang"
|
||||||
4
examples/massacre.ang
Normal file
4
examples/massacre.ang
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
a := fn b(n: int) -> int {
|
||||||
|
n + 1
|
||||||
|
}
|
||||||
|
|
||||||
8
examples/network.ang
Normal file
8
examples/network.ang
Normal 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
18
examples/neutral-pH.ang
Normal 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
21
examples/pos.ang
Normal 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
6
examples/sqrt.ang
Executable 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
8
examples/very_bad.ang
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
a := 0
|
||||||
|
while a < 10 {
|
||||||
|
print(".")
|
||||||
|
a = a + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
write("")
|
||||||
25
examples/warp.ang
Normal file
25
examples/warp.ang
Normal 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
4
examples/worst.ang
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
import "worst.ang"
|
||||||
|
|
||||||
|
write("Hello world!")
|
||||||
|
|
||||||
52
tests/forp.ang
Normal file
52
tests/forp.ang
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
|
||||||
|
fn range(from: int, to: int) -> (fn() -> (int, bool)) {
|
||||||
|
i := from - 1
|
||||||
|
|
||||||
|
fn() -> (int, bool) {
|
||||||
|
i = i + 1
|
||||||
|
if i >= to {
|
||||||
|
(-1, false)
|
||||||
|
} else {
|
||||||
|
(i, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in range(0, 10) {
|
||||||
|
println(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn chars(s: str) -> (fn() -> (str, bool)) {
|
||||||
|
i := 0
|
||||||
|
|
||||||
|
fn() -> (str, bool) {
|
||||||
|
if i >= s.length() {
|
||||||
|
("", false)
|
||||||
|
} else {
|
||||||
|
c := s[i]
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
|
(c, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for c in chars("hello") {
|
||||||
|
print(c)
|
||||||
|
print(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
println("")
|
||||||
|
|
||||||
|
fn items(l: [any]) -> (fn() -> (any, bool)) {
|
||||||
|
i := 0
|
||||||
|
|
||||||
|
fn() -> (any, bool) {
|
||||||
|
if i >= l.length() {
|
||||||
|
(nil, false)
|
||||||
|
} else {
|
||||||
|
i = i + 1
|
||||||
|
(l[i - 1], true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue