compute for's end only once

This commit is contained in:
2026-06-06 19:36:28 +02:00
parent 22874181cd
commit c41c22e936
4 changed files with 10 additions and 8 deletions

View File

@@ -39,11 +39,11 @@ func part1[lines: Array] : void
func part2[lines: Array] : void func part2[lines: Array] : void
out := 0 out := 0
s := _stackalloc(5) as str
for x in 1..lines->size-1 for x in 1..lines->size-1
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'
s := _stackalloc(5) as str
s[0] = array.nth(lines, x - 1)[y - 1] s[0] = array.nth(lines, x - 1)[y - 1]
s[1] = array.nth(lines, x + 1)[y - 1] s[1] = array.nth(lines, x + 1)[y - 1]
s[2] = array.nth(lines, x + 1)[y + 1] s[2] = array.nth(lines, x + 1)[y + 1]

View File

@@ -1,5 +1,6 @@
func part1[lines: Array] : void func part1[lines: Array] : void
sum := 0 sum := 0
s := _stackalloc(3) as str
for i in 0..lines->size for i in 0..lines->size
line : str = array.nth(lines, i) line : str = array.nth(lines, i)
@@ -7,7 +8,6 @@ func part1[lines: Array] : void
largest := 0 largest := 0
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)
s := _stackalloc(3) as str
s[0] = line[j] s[0] = line[j]
s[1] = line[k] s[1] = line[k]
s[2] = 0 s[2] = 0

View File

@@ -436,12 +436,14 @@ _builtin_environ:
self.compile_expr(env, start)?; self.compile_expr(env, start)?;
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset); emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
self.compile_expr(env, end)?;
let end_offset = env.next_offset;
env.next_offset += 8;
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", end_offset);
emit!(&mut self.output, "{}:", env.loop_begin_label); emit!(&mut self.output, "{}:", env.loop_begin_label);
emit!(&mut self.output, " mov rax, QWORD [rbp-{}]", offset); emit!(&mut self.output, " mov rax, QWORD [rbp-{}]", offset);
emit!(&mut self.output, " push rax"); emit!(&mut self.output, " mov rcx, QWORD [rbp-{}]", end_offset);
self.compile_expr(env, end)?; emit!(&mut self.output, " cmp rax, rcx");
emit!(&mut self.output, " pop rcx");
emit!(&mut self.output, " cmp rcx, rax");
emit!(&mut self.output, " jge {}", env.loop_end_label); emit!(&mut self.output, " jge {}", env.loop_end_label);
self.compile_stmt(env, body)?; self.compile_stmt(env, body)?;
emit!(&mut self.output, "{}:", env.loop_continue_label); emit!(&mut self.output, "{}:", env.loop_continue_label);

View File

@@ -279,12 +279,13 @@ func io.snprintf[..] : i64
i := 3 i := 3
n := 0 n := 0
tmp := _stackalloc(21)
while s[0] while s[0]
if s[0] == '%' if s[0] == '%'
s += 1 s += 1
if s[0] == 'd' if s[0] == 'd'
tmp := _stackalloc(21)
str.from_i64_buf(tmp, _var_arg(i) as i64) str.from_i64_buf(tmp, _var_arg(i) as i64)
tmp_len := str.len(tmp as str) tmp_len := str.len(tmp as str)
remaining := size - n - 1 remaining := size - n - 1
@@ -292,7 +293,6 @@ func io.snprintf[..] : i64
n += tmp_len n += tmp_len
i += 1 i += 1
else if s[0] == 'x' else if s[0] == 'x'
tmp := _stackalloc(17)
str.hex_from_i64_buf(tmp, _var_arg(i) as i64) str.hex_from_i64_buf(tmp, _var_arg(i) as i64)
tmp_len := str.len(tmp as str) tmp_len := str.len(tmp as str)
remaining := size - n - 1 remaining := size - n - 1