make index operator just read 1 byte

This commit is contained in:
2025-11-13 20:17:12 +01:00
parent c1bd84464c
commit 852c463532
9 changed files with 59 additions and 56 deletions

View File

@@ -24,4 +24,10 @@ func main[] : I64
io.print("Too low!")
else
io.print("Too high!")
```
## Quick Start
```
cargo install --git https://github.com/antpiasecki/zern
zern -m -r hello.zr
```

View File

@@ -9,38 +9,38 @@ func main[] : I64
let p: I64 = 0
while i < src_len
let op: U8 = str.nth(src, i)
let op: U8 = src[i]
if op == '>'
p = p + 1
else if op == '<'
p = p - 1
else if op == '+'
str.set(memory, p, mem.read8(memory + p)+1)
str.set(memory, p, memory[p]+1)
else if op == '-'
str.set(memory, p, mem.read8(memory + p)-1)
str.set(memory, p, memory[p]-1)
else if op == '.'
c.printf("%c", mem.read8(memory + p))
c.printf("%c", memory[p])
else if op == ','
str.set(memory, p, c.getchar())
else if op == '['
if !mem.read8(memory + p)
if !memory[p]
i = i + 1
let opened: I64 = 0
while i < src_len & !(str.nth(src, i) == ']' & !opened)
if str.nth(src, i) == '['
while i < src_len & !(src[i] == ']' & !opened)
if src[i] == '['
opened = opened + 1
else if str.nth(src, i) == ']'
else if src[i] == ']'
opened = opened - 1
i = i + 1
else if op == ']'
if mem.read8(memory + p)
if memory[p]
i = i - 1
let closed: I64 = 0
while i >= 0 & !(str.nth(src, i) == '[' & !closed)
if str.nth(src, i) == ']'
while i >= 0 & !(src[i] == '[' & !closed)
if src[i] == ']'
closed = closed + 1
else if str.nth(src, i) == '['
else if src[i] == '['
closed = closed - 1
i = i - 1

View File

@@ -14,7 +14,7 @@ func main[argc: I64, argv: Ptr] : I64
let host_start: I64 = 7
let i: I64 = host_start
while i < url_len
if str.nth(url, i) == '/'
if url[i] == '/'
break
i = i + 1
@@ -44,7 +44,7 @@ func main[argc: I64, argv: Ptr] : I64
let current_size: I64 = header_size + n
i = 0
while i <= current_size - 4
if str.nth(header_buf, i) == 13 & str.nth(header_buf, i+1) == 10 & str.nth(header_buf, i+2) == 13 & str.nth(header_buf, i+3) == 10
if header_buf[i] == 13 & header_buf[i+1] == 10 & header_buf[i+2] == 13 & header_buf[i+3] == 10
found = true
end_index = i + 4
break

View File

@@ -7,7 +7,7 @@ func main[] : I64
let s: I64 = 1
let j: I64 = 0
while j < 13
s = s * (str.nth(n, i + j) - '0')
s = s * (n[i + j] - '0')
j = j + 1
if s > out
out = s

View File

@@ -4,12 +4,12 @@ func main[] : I64
array.push(arr, math.abs(math.urandom() % 1000))
for i in 0..array.size(arr)
io.print_i64(arr[i])
io.print_i64(array.nth(arr, i))
io.print("------------")
alg.quicksort(arr)
for i in 0..array.size(arr)
io.print_i64(arr[i])
io.print_i64(array.nth(arr, i))
array.free(arr)

View File

@@ -5,11 +5,11 @@ func rule110_step[state: Array] : Array
for i in 0..state_len
let left: Bool = false
if i - 1 >= 0
left = state[i-1]
let center: Bool = state[i]
left = array.nth(state, i-1)
let center: Bool = array.nth(state, i)
let right: Bool = false
if i + 1 < state_len
right = state[i+1]
right = array.nth(state, i+1)
array.push(new_state, !((!left & !center & !right) | (left & !center & !right) | (left & center & right)))
@@ -17,7 +17,7 @@ func rule110_step[state: Array] : Array
func print_state[state: Array]: Void
for i in 0..array.size(state)
if state[i]
if array.nth(state, i)
c.putchar('#')
else
c.putchar(' ')

View File

@@ -117,7 +117,7 @@ section .text
"
section .text._builtin_read8
_builtin_read8:
xor rax, rax
xor rax, rax
mov al, byte [rdi]
ret
@@ -503,11 +503,10 @@ _builtin_set64:
}
Expr::Index { expr, index } => {
self.compile_expr(env, *expr)?;
emit!(&mut self.output, " push rax");
emit!(&mut self.output, " mov rdi, rax");
self.compile_expr(env, *index)?;
emit!(&mut self.output, " pop rbx");
emit!(&mut self.output, " mov rbx, [rbx]");
emit!(&mut self.output, " mov rax, [rbx + rax*8]");
emit!(&mut self.output, " add rdi, rax");
emit!(&mut self.output, " call _builtin_read8");
}
}
Ok(())

View File

@@ -65,14 +65,11 @@ func io.write_file[path: String, content: String] : Void
func str.len[s: String] : I64
return c.strlen(s)
func str.nth[s: String, n: I64] : U8
return mem.read8(s + n)
func str.copy[s: String] : String
let size: I64 = str.len(s) + 1
let dup: String = mem.alloc(size)
for i in 0..size+1
str.set(dup, i, str.nth(s, i))
str.set(dup, i, s[i])
return dup
func str.set[s: String, n: I64, c: U8] : Void
@@ -93,7 +90,7 @@ func str.concat[a: String, b: String] : String
func str.find[s: String, c: U8] : I64
let s_len: I64 = str.len(s)
for i in 0..s_len
if str.nth(s, i) == c
if s[i] == c
return i
return -1
@@ -110,10 +107,10 @@ func str.trim[s: String] : String
let start: I64 = 0
let end: I64 = str.len(s) - 1
while start <= end & str.is_whitespace(str.nth(s, start))
while start <= end & str.is_whitespace(s[start])
start = start + 1
while end >= start & str.is_whitespace(str.nth(s, end))
while end >= start & str.is_whitespace(s[end])
end = end - 1
return str.substr(s, start, end - start + 1)
@@ -137,7 +134,7 @@ func str.split[haystack: String, needle: String]: Array
if i <= haystack_len - needle_len
let match: Bool = true
for j in 0..needle_len
if str.nth(haystack, i+j) != str.nth(needle, j)
if haystack[i+j] != needle[j]
match = false
break
if match
@@ -155,7 +152,7 @@ func str.reverse[s: String] : String
let out: String = mem.alloc(len + 1)
for i in 0..len
str.set(out, i, str.nth(s, len - i - 1))
str.set(out, i, s[len-i-1])
str.set(out, len, 0)
return out
@@ -174,10 +171,10 @@ func str.hex_encode[s: String] : String
let out: String = mem.alloc(s_len*2+1)
for i in 0..s_len
let high: U8 = (str.nth(s, i) >> 4) & 15
let low: U8 = str.nth(s, i) & 15
str.set(out, j, str.nth(hex_chars, high))
str.set(out, j+1, str.nth(hex_chars, low))
let high: U8 = (s[i] >> 4) & 15
let low: U8 = s[i] & 15
str.set(out, j, hex_chars[high])
str.set(out, j+1, hex_chars[low])
j = j + 2
str.set(out, j, 0)
@@ -197,7 +194,7 @@ func str.hex_decode[s: String] : String
let out: String = mem.alloc(s_len/2+1)
while i < s_len
str.set(out, j, str._from_hex_digit(str.nth(s, i)) * 16 + str._from_hex_digit(str.nth(s, i+1)))
str.set(out, j, str._from_hex_digit(s[i]) * 16 + str._from_hex_digit(s[i+1]))
i = i + 2
j = j + 1
@@ -283,7 +280,8 @@ func array.nth[xs: Array, n: I64] : I64
// this probably should be implemented in the codegen
if n < 0 | n >= array.size(xs)
dbg.panic("array.nth out of bounds")
return xs[n]
let data: Ptr = mem.read64(xs)
return mem.read64(data + n * 8)
func array.set[xs: Array, n: I64, x: I64] : Void
let data: Ptr = mem.read64(xs)
@@ -325,16 +323,16 @@ func alg._do_quicksort[arr: Array, low: I64, high: I64] : Void
alg._do_quicksort(arr, i + 1, high)
func alg._partition[arr: Array, low: I64, high: I64] : I64
let pivot: I64 = arr[high]
let pivot: I64 = array.nth(arr, high)
let i: I64 = low - 1
for j in (low)..high
if arr[j] <= pivot
if array.nth(arr, j) <= pivot
i = i + 1
let temp: I64 = arr[i]
array.set(arr, i, arr[j])
let temp: I64 = array.nth(arr ,i)
array.set(arr, i, array.nth(arr, j))
array.set(arr, j, temp)
let temp: I64 = arr[i + 1]
array.set(arr, i + 1, arr[high])
let temp: I64 = array.nth(arr, i + 1)
array.set(arr, i + 1, array.nth(arr, high))
array.set(arr, high, temp)
return i + 1
@@ -359,11 +357,11 @@ func os.listdir[path: String] : Array
break
let skip: Bool = false
if mem.read8(entry + 19) == '.'
if mem.read8(entry + 20) == 0
if entry[19] == '.'
if entry[20] == 0
skip = true
else if mem.read8(entry + 20) == '.'
if mem.read8(entry + 21) == 0
else if entry[20] == '.'
if entry[21] == 0
skip = true
if !skip
@@ -410,10 +408,10 @@ func net.connect[host: String, port: I64] : I64
mem.write8(sa + 0, 2)
mem.write8(sa + 2, (port >> 8) & 255)
mem.write8(sa + 3, port & 255)
mem.write8(sa + 4, mem.read8(ip_ptr + 0))
mem.write8(sa + 5, mem.read8(ip_ptr + 1))
mem.write8(sa + 6, mem.read8(ip_ptr + 2))
mem.write8(sa + 7, mem.read8(ip_ptr + 3))
mem.write8(sa + 4, ip_ptr[0])
mem.write8(sa + 5, ip_ptr[1])
mem.write8(sa + 6, ip_ptr[2])
mem.write8(sa + 7, ip_ptr[3])
if c.connect(s, sa, 16) < 0
mem.free(sa)

View File

@@ -29,6 +29,6 @@ func main[] : I64
let files: Array = os.listdir("examples/")
for i in 0..array.size(files)
run_test(files[i])
run_test(array.nth(files, i))
array.free(files)