use index assignment in existing code

This commit is contained in:
2026-03-11 15:40:05 +01:00
parent 930d7b56cc
commit 88915bbc8a
6 changed files with 67 additions and 74 deletions

View File

@@ -16,13 +16,13 @@ func main[] : i64
else if op == '<' else if op == '<'
p = p - 1 p = p - 1
else if op == '+' else if op == '+'
str.set(memory, p, memory[p] + 1) memory[p] = memory[p] + 1
else if op == '-' else if op == '-'
str.set(memory, p, memory[p] - 1) memory[p] = memory[p] - 1
else if op == '.' else if op == '.'
io.print_char(memory[p]) io.print_char(memory[p])
else if op == ',' else if op == ','
str.set(memory, p, io.read_char()) memory[p] = io.read_char()
else if op == '[' else if op == '['
if !memory[p] if !memory[p]
i = i + 1 i = i + 1

View File

@@ -32,7 +32,7 @@ func main[] : i64
let base_point: ptr = mem.alloc(32) let base_point: ptr = mem.alloc(32)
mem.zero(base_point, 32) mem.zero(base_point, 32)
mem.write8(base_point, 9) base_point[0] = 9
let alice_private: ptr = str.hex_decode("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a") let alice_private: ptr = str.hex_decode("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a")
io.print("A_priv: ") io.print("A_priv: ")

View File

@@ -44,11 +44,11 @@ func part2[lines: array] : void
for y in 1..str.len(array.nth(lines, x))-1 for y in 1..str.len(array.nth(lines, x))-1
if array.nth(lines, x)[y] == 'A' if array.nth(lines, x)[y] == 'A'
let s: str = mem.alloc(5) let s: str = mem.alloc(5)
str.set(s, 0, array.nth(lines, x - 1)[y - 1]) s[0] = array.nth(lines, x - 1)[y - 1]
str.set(s, 1, array.nth(lines, x + 1)[y - 1]) s[1] = array.nth(lines, x + 1)[y - 1]
str.set(s, 2, array.nth(lines, x + 1)[y + 1]) s[2] = array.nth(lines, x + 1)[y + 1]
str.set(s, 3, array.nth(lines, x - 1)[y + 1]) s[3] = array.nth(lines, x - 1)[y + 1]
str.set(s, 4, 0) s[4] = 0
if str.equal(s, "MSSM") || str.equal(s, "SMMS") || str.equal(s, "MMSS") || str.equal(s, "SSMM") if str.equal(s, "MSSM") || str.equal(s, "SMMS") || str.equal(s, "MMSS") || str.equal(s, "SSMM")
out = out + 1 out = out + 1

View File

@@ -8,9 +8,9 @@ func part1[lines: array] : void
for j in 0..str.len(line) for j in 0..str.len(line)
for k in (j+1)..str.len(line) for k in (j+1)..str.len(line)
let s: str = mem.alloc(3) let s: str = mem.alloc(3)
str.set(s, 0, line[j]) s[0] = line[j]
str.set(s, 1, line[k]) s[1] = line[k]
str.set(s, 2, 0) s[2] = 0
let n: i64 = str.parse_i64(s) let n: i64 = str.parse_i64(s)
if n > largest if n > largest
largest = n largest = n

View File

@@ -85,9 +85,9 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
if keylen > 0 if keylen > 0
for i in 0..keylen for i in 0..keylen
mem.write8(block + i, key[i]) block[i] = key[i]
for i in (keylen)..128 for i in (keylen)..128
mem.write8(block + i, 0) block[i] = 0
c = 128 c = 128
else else
c = 0 c = 0
@@ -99,7 +99,7 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
t1 = t1 + 1 t1 = t1 + 1
crypto.blake2b._compress(h, block, t0, t1, 0, iv, sigma) crypto.blake2b._compress(h, block, t0, t1, 0, iv, sigma)
c = 0 c = 0
mem.write8(block + c, input[i]) block[c] = input[i]
c = c + 1 c = c + 1
t0 = t0 + c t0 = t0 + c
@@ -108,11 +108,11 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
if c < 128 if c < 128
for i in (c)..128 for i in (c)..128
mem.write8(block + i, 0) block[i] = 0
crypto.blake2b._compress(h, block, t0, t1, 1, iv, sigma) crypto.blake2b._compress(h, block, t0, t1, 1, iv, sigma)
for i in 0..outlen for i in 0..outlen
mem.write8(out + i, ((mem.read64(h + (i >> 3) * 8) >> (8 * (i & 7))) & 0xff)) out[i] = (mem.read64(h + (i >> 3) * 8) >> (8 * (i & 7))) & 0xff
mem.zero_and_free(h, 8 * 8) mem.zero_and_free(h, 8 * 8)
mem.zero_and_free(block, 128) mem.zero_and_free(block, 128)
@@ -210,10 +210,9 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
crypto.xchacha20._hchacha20(key, nonce, subkey) crypto.xchacha20._hchacha20(key, nonce, subkey)
let nonce12: ptr = mem.alloc(12) let nonce12: ptr = mem.alloc(12)
for i in 0..12 mem.zero(nonce12, 12)
mem.write8(nonce12 + i, 0)
for i in 0..8 for i in 0..8
mem.write8(nonce12 + 4 + i, nonce[16 + i]) nonce12[i + 4] = nonce[16 + i]
let blocknum = 0 let blocknum = 0
let remaining: i64 = len let remaining: i64 = len
@@ -225,7 +224,7 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
if remaining < 64 if remaining < 64
take = remaining take = remaining
for i in 0..take for i in 0..take
mem.write8(out + (len - remaining) + i, block[i]) out[len - remaining + i] = block[i]
remaining = remaining - take remaining = remaining - take
blocknum = blocknum + 1 blocknum = blocknum + 1
mem.zero_and_free(block, 64) mem.zero_and_free(block, 64)
@@ -237,13 +236,13 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
func crypto.xchacha20.xor_no_auth[key: ptr, nonce: ptr, input: ptr, len: i64] : ptr func crypto.xchacha20.xor_no_auth[key: ptr, nonce: ptr, input: ptr, len: i64] : ptr
if len <= 0 if len <= 0
let out: ptr = mem.alloc(1) let out: ptr = mem.alloc(1)
mem.write8(out, 0) out[0] = 0
return out return out
let out: ptr = mem.alloc(len) let out: ptr = mem.alloc(len)
let ks: ptr = mem.alloc(len) let ks: ptr = mem.alloc(len)
crypto.xchacha20._stream(key, nonce, ks, len) crypto.xchacha20._stream(key, nonce, ks, len)
for i in 0..len for i in 0..len
mem.write8(out + i, input[i] ^ ks[i]) out[i] = input[i] ^ ks[i]
mem.zero_and_free(ks, len) mem.zero_and_free(ks, len)
return out return out
@@ -328,8 +327,8 @@ func crypto.x25519.pack[out: ptr, input: ptr] : void
for i in 0..16 for i in 0..16
let v: i64 = mem.read64(t + i * 8) let v: i64 = mem.read64(t + i * 8)
mem.write8(out + i * 2, v & 0xff) out[i * 2] = v & 0xff
mem.write8(out + i * 2 + 1, (v >> 8) & 0xff) out[i * 2 + 1] = (v >> 8) & 0xff
mem.zero_and_free(t, 16 * 8) mem.zero_and_free(t, 16 * 8)
mem.zero_and_free(m, 16 * 8) mem.zero_and_free(m, 16 * 8)
@@ -353,9 +352,9 @@ func crypto.x25519.scalarmult[scalar: ptr, point: ptr] : ptr
// copy and clamp scalar // copy and clamp scalar
for i in 0..32 for i in 0..32
mem.write8(clamped + i, scalar[i]) clamped[i] = scalar[i]
mem.write8(clamped, clamped[0] & 0xf8) clamped[0] = clamped[0] & 0xf8
mem.write8(clamped + 31, (clamped[31] & 0x7f) | 0x40) clamped[31] = (clamped[31] & 0x7f) | 0x40
// load point // load point
crypto.x25519.unpack(x, point) crypto.x25519.unpack(x, point)

View File

@@ -21,7 +21,7 @@ func mem.zero_and_free[x: ptr, size: i64] : void
func mem.zero[x: ptr, size: i64] : void func mem.zero[x: ptr, size: i64] : void
for i in 0..size for i in 0..size
mem.write8(x + i, 0) x[i] = 0
func mem.read8[x: ptr] : u8 func mem.read8[x: ptr] : u8
return x[0] return x[0]
@@ -35,14 +35,11 @@ func mem.read32[x: ptr] : i64
func mem.read64[x: ptr] : i64 func mem.read64[x: ptr] : i64
return _builtin_read64(x) return _builtin_read64(x)
func mem.write8[x: ptr, d: u8] : void
_builtin_set8(x, d)
func mem.write32[x: ptr, d: i64] : void func mem.write32[x: ptr, d: i64] : void
mem.write8(x, d & 0xff) x[0] = d & 0xff
mem.write8(x + 1, (d >> 8) & 0xff) x[1] = (d >> 8) & 0xff
mem.write8(x + 2, (d >> 16) & 0xff) x[2] = (d >> 16) & 0xff
mem.write8(x + 3, (d >> 24) & 0xff) x[3] = (d >> 24) & 0xff
func mem.write64[x: ptr, d: i64] : void func mem.write64[x: ptr, d: i64] : void
_builtin_set64(x, d) _builtin_set64(x, d)
@@ -81,7 +78,7 @@ func io.read_line[]: str
let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE) let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
if n < 0 if n < 0
return "" return ""
str.set(buffer, n, 0) buffer[n] = 0
return buffer return buffer
func io.read_file[path: str]: str func io.read_file[path: str]: str
@@ -94,7 +91,7 @@ func io.read_file[path: str]: str
let buffer: str = mem.alloc(size + 1) let buffer: str = mem.alloc(size + 1)
let n: i64 = _builtin_syscall(SYS_read, fd, buffer, size) let n: i64 = _builtin_syscall(SYS_read, fd, buffer, size)
str.set(buffer, n, 0) buffer[n] = 0
_builtin_syscall(SYS_close, fd) _builtin_syscall(SYS_close, fd)
return buffer return buffer
@@ -116,12 +113,9 @@ func str.copy[s: str] : str
let size: i64 = str.len(s) + 1 let size: i64 = str.len(s) + 1
let dup: str = mem.alloc(size) let dup: str = mem.alloc(size)
for i in 0..size for i in 0..size
str.set(dup, i, s[i]) dup[i] = s[i]
return dup return dup
func str.set[s: str, n: i64, c: u8] : void
mem.write8(s + n, c)
func str.equal[a: str, b: str] : bool func str.equal[a: str, b: str] : bool
let i = 0 let i = 0
while a[i] != 0 && b[i] != 0 while a[i] != 0 && b[i] != 0
@@ -156,10 +150,10 @@ func str.concat[a: str, b: str] : str
let b_len: i64 = str.len(b) let b_len: i64 = str.len(b)
let out: str = mem.alloc(a_len + b_len + 1) let out: str = mem.alloc(a_len + b_len + 1)
for i in 0..a_len for i in 0..a_len
str.set(out, i, a[i]) out[i] = a[i]
for i in 0..b_len for i in 0..b_len
str.set(out, a_len + i, b[i]) out[a_len + i] = b[i]
str.set(out, a_len + b_len, 0) out[a_len + b_len] = 0
return out return out
func str.find[haystack: str, needle: str] : i64 func str.find[haystack: str, needle: str] : i64
@@ -185,8 +179,8 @@ func str.substr[s: str, start: i64, length: i64] : str
let out: str = mem.alloc(length + 1) let out: str = mem.alloc(length + 1)
for i in 0..length for i in 0..length
str.set(out, i, s[start + i]) out[i] = s[start + i]
str.set(out, length, 0) out[length] = 0
return out return out
func str.trim[s: str] : str func str.trim[s: str] : str
@@ -242,8 +236,8 @@ func str.reverse[s: str] : str
let out: str = mem.alloc(len + 1) let out: str = mem.alloc(len + 1)
for i in 0..len for i in 0..len
str.set(out, i, s[len - i - 1]) out[i] = s[len - i - 1]
str.set(out, len, 0) out[len] = 0
return out return out
// not sure this covers all wacky edge cases // not sure this covers all wacky edge cases
@@ -258,21 +252,21 @@ func str.from_i64[n: i64] : str
let i = 0 let i = 0
while n > 0 while n > 0
let d: u8 = n % 10 let d: u8 = n % 10
str.set(buf, i, '0' + d) buf[i] = '0' + d
n = n / 10 n = n / 10
i = i + 1 i = i + 1
if neg if neg
str.set(buf, i, '-') buf[i] = '-'
i = i + 1 i = i + 1
str.set(buf, i, 0) buf[i] = 0
let s: str = str.reverse(buf) let s: str = str.reverse(buf)
mem.free(buf) mem.free(buf)
return s return s
func str.from_char[c: u8] : str func str.from_char[c: u8] : str
let s: str = mem.alloc(2) let s: str = mem.alloc(2)
str.set(s, 0, c) s[0] = c
str.set(s, 1, 0) s[1] = 0
return s return s
func str.parse_i64[s: str] : i64 func str.parse_i64[s: str] : i64
@@ -301,11 +295,11 @@ func str.hex_encode[s: str, s_len: i64] : str
for i in 0..s_len for i in 0..s_len
let high: u8 = (s[i] >> 4) & 15 let high: u8 = (s[i] >> 4) & 15
let low: u8 = s[i] & 15 let low: u8 = s[i] & 15
str.set(out, j, hex_chars[high]) out[j] = hex_chars[high]
str.set(out, j + 1, hex_chars[low]) out[j + 1] = hex_chars[low]
j = j + 2 j = j + 2
str.set(out, j, 0) out[j] = 0
return out return out
func str._hex_digit_to_int[d: u8] : i64 func str._hex_digit_to_int[d: u8] : i64
@@ -322,11 +316,11 @@ func str.hex_decode[s: str] : str
let out: str = mem.alloc(s_len / 2 + 1) let out: str = mem.alloc(s_len / 2 + 1)
while i < s_len while i < s_len
str.set(out, j, str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1])) out[j] = str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1])
i = i + 2 i = i + 2
j = j + 1 j = j + 1
str.set(out, j, 0) out[j] = 0
return out return out
func math.gcd[a: i64, b: i64] : i64 func math.gcd[a: i64, b: i64] : i64
@@ -606,14 +600,14 @@ func net.listen[packed_host: i64, port: i64] : i64
let sa: ptr = mem.alloc(16) let sa: ptr = mem.alloc(16)
mem.zero(sa, 16) mem.zero(sa, 16)
mem.write8(sa + 0, 2) sa[0] = 2
mem.write8(sa + 1, 0) sa[1] = 0
mem.write8(sa + 2, (port >> 8) & 255) sa[2] = (port >> 8) & 255
mem.write8(sa + 3, port & 255) sa[3] = port & 255
mem.write8(sa + 4, (packed_host >> 24) & 255) sa[4] = (packed_host >> 24) & 255
mem.write8(sa + 5, (packed_host >> 16) & 255) sa[5] = (packed_host >> 16) & 255
mem.write8(sa + 6, (packed_host >> 8) & 255) sa[6] = (packed_host >> 8) & 255
mem.write8(sa + 7, packed_host & 255) sa[7] = packed_host & 255
if _builtin_syscall(SYS_bind, s, sa, 16) < 0 if _builtin_syscall(SYS_bind, s, sa, 16) < 0
_builtin_syscall(SYS_close, s) _builtin_syscall(SYS_close, s)
@@ -640,13 +634,13 @@ func net.connect[host: str, port: i64] : i64
let sa: ptr = mem.alloc(16) let sa: ptr = mem.alloc(16)
mem.zero(sa, 16) mem.zero(sa, 16)
mem.write8(sa + 0, 2) sa[0] = 2
mem.write8(sa + 2, (port >> 8) & 255) sa[2] = (port >> 8) & 255
mem.write8(sa + 3, port & 255) sa[3] = port & 255
mem.write8(sa + 4, ip_ptr[0]) sa[4] = ip_ptr[0]
mem.write8(sa + 5, ip_ptr[1]) sa[5] = ip_ptr[1]
mem.write8(sa + 6, ip_ptr[2]) sa[6] = ip_ptr[2]
mem.write8(sa + 7, ip_ptr[3]) sa[7] = ip_ptr[3]
if _builtin_syscall(SYS_connect, s, sa, 16) < 0 if _builtin_syscall(SYS_connect, s, sa, 16) < 0
mem.free(sa) mem.free(sa)