make index operator just read 1 byte
This commit is contained in:
@@ -24,4 +24,10 @@ func main[] : I64
|
|||||||
io.print("Too low!")
|
io.print("Too low!")
|
||||||
else
|
else
|
||||||
io.print("Too high!")
|
io.print("Too high!")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
```
|
||||||
|
cargo install --git https://github.com/antpiasecki/zern
|
||||||
|
zern -m -r hello.zr
|
||||||
```
|
```
|
||||||
@@ -9,38 +9,38 @@ func main[] : I64
|
|||||||
let p: I64 = 0
|
let p: I64 = 0
|
||||||
|
|
||||||
while i < src_len
|
while i < src_len
|
||||||
let op: U8 = str.nth(src, i)
|
let op: U8 = src[i]
|
||||||
|
|
||||||
if op == '>'
|
if op == '>'
|
||||||
p = p + 1
|
p = p + 1
|
||||||
else if op == '<'
|
else if op == '<'
|
||||||
p = p - 1
|
p = p - 1
|
||||||
else if op == '+'
|
else if op == '+'
|
||||||
str.set(memory, p, mem.read8(memory + p)+1)
|
str.set(memory, p, memory[p]+1)
|
||||||
else if op == '-'
|
else if op == '-'
|
||||||
str.set(memory, p, mem.read8(memory + p)-1)
|
str.set(memory, p, memory[p]-1)
|
||||||
else if op == '.'
|
else if op == '.'
|
||||||
c.printf("%c", mem.read8(memory + p))
|
c.printf("%c", memory[p])
|
||||||
else if op == ','
|
else if op == ','
|
||||||
str.set(memory, p, c.getchar())
|
str.set(memory, p, c.getchar())
|
||||||
else if op == '['
|
else if op == '['
|
||||||
if !mem.read8(memory + p)
|
if !memory[p]
|
||||||
i = i + 1
|
i = i + 1
|
||||||
let opened: I64 = 0
|
let opened: I64 = 0
|
||||||
while i < src_len & !(str.nth(src, i) == ']' & !opened)
|
while i < src_len & !(src[i] == ']' & !opened)
|
||||||
if str.nth(src, i) == '['
|
if src[i] == '['
|
||||||
opened = opened + 1
|
opened = opened + 1
|
||||||
else if str.nth(src, i) == ']'
|
else if src[i] == ']'
|
||||||
opened = opened - 1
|
opened = opened - 1
|
||||||
i = i + 1
|
i = i + 1
|
||||||
else if op == ']'
|
else if op == ']'
|
||||||
if mem.read8(memory + p)
|
if memory[p]
|
||||||
i = i - 1
|
i = i - 1
|
||||||
let closed: I64 = 0
|
let closed: I64 = 0
|
||||||
while i >= 0 & !(str.nth(src, i) == '[' & !closed)
|
while i >= 0 & !(src[i] == '[' & !closed)
|
||||||
if str.nth(src, i) == ']'
|
if src[i] == ']'
|
||||||
closed = closed + 1
|
closed = closed + 1
|
||||||
else if str.nth(src, i) == '['
|
else if src[i] == '['
|
||||||
closed = closed - 1
|
closed = closed - 1
|
||||||
i = i - 1
|
i = i - 1
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func main[argc: I64, argv: Ptr] : I64
|
|||||||
let host_start: I64 = 7
|
let host_start: I64 = 7
|
||||||
let i: I64 = host_start
|
let i: I64 = host_start
|
||||||
while i < url_len
|
while i < url_len
|
||||||
if str.nth(url, i) == '/'
|
if url[i] == '/'
|
||||||
break
|
break
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ func main[argc: I64, argv: Ptr] : I64
|
|||||||
let current_size: I64 = header_size + n
|
let current_size: I64 = header_size + n
|
||||||
i = 0
|
i = 0
|
||||||
while i <= current_size - 4
|
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
|
found = true
|
||||||
end_index = i + 4
|
end_index = i + 4
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ func main[] : I64
|
|||||||
let s: I64 = 1
|
let s: I64 = 1
|
||||||
let j: I64 = 0
|
let j: I64 = 0
|
||||||
while j < 13
|
while j < 13
|
||||||
s = s * (str.nth(n, i + j) - '0')
|
s = s * (n[i + j] - '0')
|
||||||
j = j + 1
|
j = j + 1
|
||||||
if s > out
|
if s > out
|
||||||
out = s
|
out = s
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ func main[] : I64
|
|||||||
array.push(arr, math.abs(math.urandom() % 1000))
|
array.push(arr, math.abs(math.urandom() % 1000))
|
||||||
|
|
||||||
for i in 0..array.size(arr)
|
for i in 0..array.size(arr)
|
||||||
io.print_i64(arr[i])
|
io.print_i64(array.nth(arr, i))
|
||||||
io.print("------------")
|
io.print("------------")
|
||||||
|
|
||||||
alg.quicksort(arr)
|
alg.quicksort(arr)
|
||||||
|
|
||||||
for i in 0..array.size(arr)
|
for i in 0..array.size(arr)
|
||||||
io.print_i64(arr[i])
|
io.print_i64(array.nth(arr, i))
|
||||||
|
|
||||||
array.free(arr)
|
array.free(arr)
|
||||||
@@ -5,11 +5,11 @@ func rule110_step[state: Array] : Array
|
|||||||
for i in 0..state_len
|
for i in 0..state_len
|
||||||
let left: Bool = false
|
let left: Bool = false
|
||||||
if i - 1 >= 0
|
if i - 1 >= 0
|
||||||
left = state[i-1]
|
left = array.nth(state, i-1)
|
||||||
let center: Bool = state[i]
|
let center: Bool = array.nth(state, i)
|
||||||
let right: Bool = false
|
let right: Bool = false
|
||||||
if i + 1 < state_len
|
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)))
|
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
|
func print_state[state: Array]: Void
|
||||||
for i in 0..array.size(state)
|
for i in 0..array.size(state)
|
||||||
if state[i]
|
if array.nth(state, i)
|
||||||
c.putchar('#')
|
c.putchar('#')
|
||||||
else
|
else
|
||||||
c.putchar(' ')
|
c.putchar(' ')
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ section .text
|
|||||||
"
|
"
|
||||||
section .text._builtin_read8
|
section .text._builtin_read8
|
||||||
_builtin_read8:
|
_builtin_read8:
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
mov al, byte [rdi]
|
mov al, byte [rdi]
|
||||||
ret
|
ret
|
||||||
|
|
||||||
@@ -503,11 +503,10 @@ _builtin_set64:
|
|||||||
}
|
}
|
||||||
Expr::Index { expr, index } => {
|
Expr::Index { expr, index } => {
|
||||||
self.compile_expr(env, *expr)?;
|
self.compile_expr(env, *expr)?;
|
||||||
emit!(&mut self.output, " push rax");
|
emit!(&mut self.output, " mov rdi, rax");
|
||||||
self.compile_expr(env, *index)?;
|
self.compile_expr(env, *index)?;
|
||||||
emit!(&mut self.output, " pop rbx");
|
emit!(&mut self.output, " add rdi, rax");
|
||||||
emit!(&mut self.output, " mov rbx, [rbx]");
|
emit!(&mut self.output, " call _builtin_read8");
|
||||||
emit!(&mut self.output, " mov rax, [rbx + rax*8]");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
56
src/std.zr
56
src/std.zr
@@ -65,14 +65,11 @@ func io.write_file[path: String, content: String] : Void
|
|||||||
func str.len[s: String] : I64
|
func str.len[s: String] : I64
|
||||||
return c.strlen(s)
|
return c.strlen(s)
|
||||||
|
|
||||||
func str.nth[s: String, n: I64] : U8
|
|
||||||
return mem.read8(s + n)
|
|
||||||
|
|
||||||
func str.copy[s: String] : String
|
func str.copy[s: String] : String
|
||||||
let size: I64 = str.len(s) + 1
|
let size: I64 = str.len(s) + 1
|
||||||
let dup: String = mem.alloc(size)
|
let dup: String = mem.alloc(size)
|
||||||
for i in 0..size+1
|
for i in 0..size+1
|
||||||
str.set(dup, i, str.nth(s, i))
|
str.set(dup, i, s[i])
|
||||||
return dup
|
return dup
|
||||||
|
|
||||||
func str.set[s: String, n: I64, c: U8] : Void
|
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
|
func str.find[s: String, c: U8] : I64
|
||||||
let s_len: I64 = str.len(s)
|
let s_len: I64 = str.len(s)
|
||||||
for i in 0..s_len
|
for i in 0..s_len
|
||||||
if str.nth(s, i) == c
|
if s[i] == c
|
||||||
return i
|
return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
@@ -110,10 +107,10 @@ func str.trim[s: String] : String
|
|||||||
let start: I64 = 0
|
let start: I64 = 0
|
||||||
let end: I64 = str.len(s) - 1
|
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
|
start = start + 1
|
||||||
|
|
||||||
while end >= start & str.is_whitespace(str.nth(s, end))
|
while end >= start & str.is_whitespace(s[end])
|
||||||
end = end - 1
|
end = end - 1
|
||||||
|
|
||||||
return str.substr(s, start, end - start + 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
|
if i <= haystack_len - needle_len
|
||||||
let match: Bool = true
|
let match: Bool = true
|
||||||
for j in 0..needle_len
|
for j in 0..needle_len
|
||||||
if str.nth(haystack, i+j) != str.nth(needle, j)
|
if haystack[i+j] != needle[j]
|
||||||
match = false
|
match = false
|
||||||
break
|
break
|
||||||
if match
|
if match
|
||||||
@@ -155,7 +152,7 @@ func str.reverse[s: String] : String
|
|||||||
let out: String = mem.alloc(len + 1)
|
let out: String = mem.alloc(len + 1)
|
||||||
|
|
||||||
for i in 0..len
|
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)
|
str.set(out, len, 0)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@@ -174,10 +171,10 @@ func str.hex_encode[s: String] : String
|
|||||||
let out: String = mem.alloc(s_len*2+1)
|
let out: String = mem.alloc(s_len*2+1)
|
||||||
|
|
||||||
for i in 0..s_len
|
for i in 0..s_len
|
||||||
let high: U8 = (str.nth(s, i) >> 4) & 15
|
let high: U8 = (s[i] >> 4) & 15
|
||||||
let low: U8 = str.nth(s, i) & 15
|
let low: U8 = s[i] & 15
|
||||||
str.set(out, j, str.nth(hex_chars, high))
|
str.set(out, j, hex_chars[high])
|
||||||
str.set(out, j+1, str.nth(hex_chars, low))
|
str.set(out, j+1, hex_chars[low])
|
||||||
j = j + 2
|
j = j + 2
|
||||||
|
|
||||||
str.set(out, j, 0)
|
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)
|
let out: String = mem.alloc(s_len/2+1)
|
||||||
|
|
||||||
while i < s_len
|
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
|
i = i + 2
|
||||||
j = j + 1
|
j = j + 1
|
||||||
|
|
||||||
@@ -283,7 +280,8 @@ func array.nth[xs: Array, n: I64] : I64
|
|||||||
// this probably should be implemented in the codegen
|
// this probably should be implemented in the codegen
|
||||||
if n < 0 | n >= array.size(xs)
|
if n < 0 | n >= array.size(xs)
|
||||||
dbg.panic("array.nth out of bounds")
|
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
|
func array.set[xs: Array, n: I64, x: I64] : Void
|
||||||
let data: Ptr = mem.read64(xs)
|
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)
|
alg._do_quicksort(arr, i + 1, high)
|
||||||
|
|
||||||
func alg._partition[arr: Array, low: I64, high: I64] : I64
|
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
|
let i: I64 = low - 1
|
||||||
for j in (low)..high
|
for j in (low)..high
|
||||||
if arr[j] <= pivot
|
if array.nth(arr, j) <= pivot
|
||||||
i = i + 1
|
i = i + 1
|
||||||
let temp: I64 = arr[i]
|
let temp: I64 = array.nth(arr ,i)
|
||||||
array.set(arr, i, arr[j])
|
array.set(arr, i, array.nth(arr, j))
|
||||||
array.set(arr, j, temp)
|
array.set(arr, j, temp)
|
||||||
let temp: I64 = arr[i + 1]
|
let temp: I64 = array.nth(arr, i + 1)
|
||||||
array.set(arr, i + 1, arr[high])
|
array.set(arr, i + 1, array.nth(arr, high))
|
||||||
array.set(arr, high, temp)
|
array.set(arr, high, temp)
|
||||||
return i + 1
|
return i + 1
|
||||||
|
|
||||||
@@ -359,11 +357,11 @@ func os.listdir[path: String] : Array
|
|||||||
break
|
break
|
||||||
|
|
||||||
let skip: Bool = false
|
let skip: Bool = false
|
||||||
if mem.read8(entry + 19) == '.'
|
if entry[19] == '.'
|
||||||
if mem.read8(entry + 20) == 0
|
if entry[20] == 0
|
||||||
skip = true
|
skip = true
|
||||||
else if mem.read8(entry + 20) == '.'
|
else if entry[20] == '.'
|
||||||
if mem.read8(entry + 21) == 0
|
if entry[21] == 0
|
||||||
skip = true
|
skip = true
|
||||||
|
|
||||||
if !skip
|
if !skip
|
||||||
@@ -410,10 +408,10 @@ func net.connect[host: String, port: I64] : I64
|
|||||||
mem.write8(sa + 0, 2)
|
mem.write8(sa + 0, 2)
|
||||||
mem.write8(sa + 2, (port >> 8) & 255)
|
mem.write8(sa + 2, (port >> 8) & 255)
|
||||||
mem.write8(sa + 3, port & 255)
|
mem.write8(sa + 3, port & 255)
|
||||||
mem.write8(sa + 4, mem.read8(ip_ptr + 0))
|
mem.write8(sa + 4, ip_ptr[0])
|
||||||
mem.write8(sa + 5, mem.read8(ip_ptr + 1))
|
mem.write8(sa + 5, ip_ptr[1])
|
||||||
mem.write8(sa + 6, mem.read8(ip_ptr + 2))
|
mem.write8(sa + 6, ip_ptr[2])
|
||||||
mem.write8(sa + 7, mem.read8(ip_ptr + 3))
|
mem.write8(sa + 7, ip_ptr[3])
|
||||||
|
|
||||||
if c.connect(s, sa, 16) < 0
|
if c.connect(s, sa, 16) < 0
|
||||||
mem.free(sa)
|
mem.free(sa)
|
||||||
|
|||||||
Reference in New Issue
Block a user