rename std functions

This commit is contained in:
2025-07-24 14:06:15 +02:00
parent dfec298823
commit 65bdac2fe3
26 changed files with 328 additions and 294 deletions

View File

@@ -1,45 +1,45 @@
func main[] : I64
// https://brainfuck.org/sierpinski.b
let src: String = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
let src_len: I64 = strlen(src)
let src_len: I64 = c.strlen(src)
let i: I64 = 0
let memory: Ptr = calloc(1, 30000)
let memory: Ptr = c.calloc(1, 30000)
let p: I64 = 0
while i < src_len
let op: U8 = String.nth(src, i)
let op: U8 = str.nth(src, i)
if op == '>'
p = p + 1
else if op == '<'
p = p - 1
else if op == '+'
String.set(memory, p, String.nth(memory, p)+1)
str.set(memory, p, str.nth(memory, p)+1)
else if op == '-'
String.set(memory, p, String.nth(memory, p)-1)
str.set(memory, p, str.nth(memory, p)-1)
else if op == '.'
printf("%c", String.nth(memory, p))
c.printf("%c", str.nth(memory, p))
else if op == ','
String.set(memory, p, getchar())
str.set(memory, p, c.getchar())
else if op == '['
if !String.nth(memory, p)
if !str.nth(memory, p)
i = i + 1
let opened: I64 = 0
while i < src_len && !(String.nth(src, i) == ']' && !opened)
if String.nth(src, i) == '['
while i < src_len && !(str.nth(src, i) == ']' && !opened)
if str.nth(src, i) == '['
opened = opened + 1
else if String.nth(src, i) == ']'
else if str.nth(src, i) == ']'
opened = opened - 1
i = i + 1
else if op == ']'
if String.nth(memory, p)
if str.nth(memory, p)
i = i - 1
let closed: I64 = 0
while i >= 0 && !(String.nth(src, i) == '[' && !closed)
if String.nth(src, i) == ']'
while i >= 0 && !(str.nth(src, i) == '[' && !closed)
if str.nth(src, i) == ']'
closed = closed + 1
else if String.nth(src, i) == '['
else if str.nth(src, i) == '['
closed = closed - 1
i = i - 1

View File

@@ -4,4 +4,4 @@ func main[] : I64
for i in 0..1000
if i % 5 == 0 || i % 3 == 0
sum = sum + i
print_i64(sum)
io.print_i64(sum)

View File

@@ -2,6 +2,6 @@ func main[] : I64
let sum: I64 = 0
for i in 0..2000000
if Math.is_prime(i)
if math.is_prime(i)
sum = sum + i
print_i64(sum)
io.print_i64(sum)

View File

@@ -1,5 +1,5 @@
func num_divisors[n: I64] : I64
let end: I64 = Math.isqrt(n)
let end: I64 = math.isqrt(n)
let out: I64 = 0
for i in 1..end+1
@@ -16,6 +16,6 @@ func main[] : I64
while true
n = n + i
if num_divisors(n) > 500
print_i64(n)
io.print_i64(n)
break
i = i + 1

View File

@@ -1,5 +1,5 @@
func main[] : I64
37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226
|> I64.to_string()
|> String.substr(0, 10)
|> print()
|> str.from_i64()
|> str.substr(0, 10)
|> io.print()

View File

@@ -19,4 +19,4 @@ func main[] : I64
if seq > max
max = seq
max_index = i
print_i64(max_index)
io.print_i64(max_index)

View File

@@ -6,4 +6,4 @@ func main[] : I64
for i in 1..r+1
out = out * (n - (r - i)) / i
print_i64(out)
io.print_i64(out)

View File

@@ -10,4 +10,4 @@ func main[] : I64
b = a + b
a = temp
print_i64(sum)
io.print_i64(sum)

View File

@@ -8,4 +8,4 @@ func main[] : I64
else
f = f + 1
print_i64(n)
io.print_i64(n)

View File

@@ -4,10 +4,10 @@ func main[] : I64
for a in 500..1000
for b in 500..1000
if a * b > out
let s: String = I64.to_string(a * b)
let s_rev: String = String.rev(s)
if strcmp(s, s_rev) == 0
let s: String = str.from_i64(a * b)
let s_rev: String = str.reverse(s)
if c.strcmp(s, s_rev) == 0
out = a * b
free(s)
free(s_rev)
print_i64(out)
c.free(s)
c.free(s_rev)
io.print_i64(out)

View File

@@ -2,5 +2,5 @@ func main[] : I64
let out: I64 = 1
for i in 1..21
out = Math.lcm(out, i)
print_i64(out)
out = math.lcm(out, i)
io.print_i64(out)

View File

@@ -8,4 +8,4 @@ func main[] : I64
square_of_sum = square_of_sum + i
square_of_sum = square_of_sum * square_of_sum
print_i64(square_of_sum - sum_of_squares)
io.print_i64(square_of_sum - sum_of_squares)

View File

@@ -3,9 +3,9 @@ func main[] : I64
let i: I64 = 1
while true
if Math.is_prime(i)
if math.is_prime(i)
found = found + 1
if found == 10001
print_i64(i)
io.print_i64(i)
break
i = i + 1

View File

@@ -2,13 +2,13 @@ func main[] : I64
let n: String = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
let out: I64 = 0
let max: I64 = strlen(n) - 13
let max: I64 = c.strlen(n) - 13
for i in 0..max
let s: I64 = 1
let j: I64 = 0
while j < 13
s = s * (String.nth(n, i + j) - '0')
s = s * (str.nth(n, i + j) - '0')
j = j + 1
if s > out
out = s
print_i64(out)
io.print_i64(out)

View File

@@ -3,5 +3,5 @@ func main[] : I64
for b in 1..1000
let c: I64 = 1000 - b - a
if a * a + b * b == c * c
print_i64(a * b * c)
io.print_i64(a * b * c)
return 0

View File

@@ -3,7 +3,7 @@ func main[] : I64
let b: I64 = 1
while a < 100000
print_i64(a)
io.print_i64(a)
let temp: I64 = b
b = a + b
a = temp

View File

@@ -1,14 +1,14 @@
func main[] : I64
let answer: I64 = Math.abs(Math.urandom()) % 100
let answer: I64 = math.abs(math.urandom()) % 100
while true
print("Guess a number: ")
let guess: I64 = IO.read_line() |> String.trim() |> I64.parse()
io.print("Guess a number: ")
let guess: I64 = io.read_line() |> str.trim() |> str.parse_i64()
if guess == answer
print("You win!")
io.print("You win!")
break
else if guess < answer
print("Too low!")
io.print("Too low!")
else
print("Too high!")
io.print("Too high!")

View File

@@ -1,2 +1,2 @@
func main[] : I64
print("Hello, World!")
io.print("Hello, World!")

View File

@@ -1,5 +1,5 @@
func quicksort[arr: Array] : Void
do_quicksort(arr, 0, Array.size(arr)-1)
do_quicksort(arr, 0, array.size(arr)-1)
func do_quicksort[arr: Array, low: I64, high: I64] : Void
if low < high
@@ -14,23 +14,23 @@ func partition[arr: Array, low: I64, high: I64] : I64
if arr[j] <= pivot
i = i + 1
let temp: I64 = arr[i]
Array.set(arr, i, arr[j])
Array.set(arr, j, temp)
array.set(arr, i, arr[j])
array.set(arr, j, temp)
let temp: I64 = arr[i + 1]
Array.set(arr, i + 1, arr[high])
Array.set(arr, high, temp)
array.set(arr, i + 1, arr[high])
array.set(arr, high, temp)
return i + 1
func main[] : I64
let arr: Array = []
for i in 0..10
Array.push(arr, Math.abs(Math.urandom() % 1000))
array.push(arr, math.abs(math.urandom() % 1000))
for i in 0..Array.size(arr)
print_i64(arr[i])
print("------------")
for i in 0..array.size(arr)
io.print_i64(arr[i])
io.print("------------")
quicksort(arr)
for i in 0..Array.size(arr)
print_i64(arr[i])
for i in 0..array.size(arr)
io.print_i64(arr[i])

View File

@@ -1,6 +1,6 @@
func rule110_step[state: Array] : Array
let new_state: Array = []
let state_len: I64 = Array.size(state)
let state_len: I64 = array.size(state)
for i in 0..state_len
let left: Bool = false
@@ -11,17 +11,17 @@ func rule110_step[state: Array] : Array
if i + 1 < state_len
right = state[i+1]
Array.push(new_state, !((!left && !center && !right) || (left && !center && !right) || (left && center && right)))
array.push(new_state, !((!left && !center && !right) || (left && !center && !right) || (left && center && right)))
return new_state
func to_str[state: Array]: String
let out: String = malloc(Array.size(state))
for i in 0..Array.size(state)
let out: String = malloc(array.size(state))
for i in 0..array.size(state)
if state[i]
String.set(out, i, '#')
str.set(out, i, '#')
else
String.set(out, i, ' ')
str.set(out, i, ' ')
return out
func main[] : I64
@@ -29,10 +29,10 @@ func main[] : I64
let state: Array = []
for i in 0..SIZE
Array.push(state, false)
Array.push(state, true)
array.push(state, false)
array.push(state, true)
print(to_str(state))
io.print(to_str(state))
for i in 0..SIZE
state = rule110_step(state)
print(to_str(state))
io.print(to_str(state))

View File

@@ -1,31 +1,31 @@
func main[] : I64
let s: I64 = socket(2, 1, 0)
let s: I64 = c.socket(2, 1, 0)
if s < 0
panic("socket() failed")
dbg.panic("socket() failed")
let port: I64 = 80
let sa: Ptr = calloc(1, 16)
String.set(sa, 0, 2)
String.set(sa, 1, 0)
String.set(sa, 2, Bit.rshift(port, 8) && 255)
String.set(sa, 3, port && 255)
let sa: Ptr = c.calloc(1, 16)
str.set(sa, 0, 2)
str.set(sa, 1, 0)
str.set(sa, 2, bit.rshift(port, 8) && 255)
str.set(sa, 3, port && 255)
// 23.192.228.80 (example.com)
String.set(sa, 4, 23)
String.set(sa, 5, 192)
String.set(sa, 6, 228)
String.set(sa, 7, 80)
str.set(sa, 4, 23)
str.set(sa, 5, 192)
str.set(sa, 6, 228)
str.set(sa, 7, 80)
if connect(s, sa, 16) < 0
panic("connect() failed")
free(sa)
if c.connect(s, sa, 16) < 0
dbg.panic("connect() failed")
c.free(sa)
let req: String = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
send(s, req, strlen(req), 0)
c.send(s, req, c.strlen(req), 0)
let resp: String = malloc(60000)
let n: I64 = read(s, resp, 60000)
String.set(resp, n, 0)
print(resp)
free(resp)
let resp: String = c.malloc(60000)
let n: I64 = c.read(s, resp, 60000)
str.set(resp, n, 0)
io.print(resp)
c.free(resp)
close(s)
c.close(s)

View File

@@ -1,27 +1,27 @@
func main[] : I64
let s: I64 = socket(2, 1, 0)
let s: I64 = c.socket(2, 1, 0)
if s < 0
panic("socket() failed")
dbg.panic("socket() failed")
let port: I64 = 8080
let sa: Ptr = calloc(1, 16)
String.set(sa, 0, 2)
String.set(sa, 1, 0)
String.set(sa, 2, Bit.rshift(port, 8) && 255)
String.set(sa, 3, port && 255)
let sa: Ptr = c.calloc(1, 16)
str.set(sa, 0, 2)
str.set(sa, 1, 0)
str.set(sa, 2, bit.rshift(port, 8) && 255)
str.set(sa, 3, port && 255)
if bind(s, sa, 16) < 0
panic("bind() failed")
if c.bind(s, sa, 16) < 0
dbg.panic("bind() failed")
if listen(s, 1) < 0
panic("listen() failed")
if c.listen(s, 1) < 0
dbg.panic("listen() failed")
let resp: String = malloc(60000)
let resp: String = c.malloc(60000)
while true
let c: I64 = accept(s, 0, 0)
let c: I64 = c.accept(s, 0, 0)
if c < 0
panic("accept() failed")
dbg.panic("accept() failed")
let n: I64 = read(c, resp, 60000)
send(c, resp, n, 0)
close(c)
let n: I64 = c.read(c, resp, 60000)
c.send(c, resp, n, 0)
c.close(c)