Compare commits
2 Commits
535364931e
...
c41c22e936
| Author | SHA1 | Date | |
|---|---|---|---|
|
c41c22e936
|
|||
|
22874181cd
|
@@ -12,38 +12,38 @@ func main[] : i64
|
|||||||
op := src[i]
|
op := src[i]
|
||||||
|
|
||||||
if op == '>'
|
if op == '>'
|
||||||
p = p + 1
|
p += 1
|
||||||
else if op == '<'
|
else if op == '<'
|
||||||
p = p - 1
|
p -= 1
|
||||||
else if op == '+'
|
else if op == '+'
|
||||||
memory[p] = memory[p] + 1
|
memory[p] += 1
|
||||||
else if op == '-'
|
else if op == '-'
|
||||||
memory[p] = memory[p] - 1
|
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 == ','
|
||||||
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 += 1
|
||||||
opened := 0
|
opened := 0
|
||||||
while i < src_len && !(src[i] == ']' && !opened)
|
while i < src_len && !(src[i] == ']' && !opened)
|
||||||
if src[i] == '['
|
if src[i] == '['
|
||||||
opened = opened + 1
|
opened += 1
|
||||||
else if src[i] == ']'
|
else if src[i] == ']'
|
||||||
opened = opened - 1
|
opened -= 1
|
||||||
i = i + 1
|
i += 1
|
||||||
else if op == ']'
|
else if op == ']'
|
||||||
if memory[p]
|
if memory[p]
|
||||||
i = i - 1
|
i -= 1
|
||||||
closed := 0
|
closed := 0
|
||||||
while i >= 0 && !(src[i] == '[' && !closed)
|
while i >= 0 && !(src[i] == '[' && !closed)
|
||||||
if src[i] == ']'
|
if src[i] == ']'
|
||||||
closed = closed + 1
|
closed += 1
|
||||||
else if src[i] == '['
|
else if src[i] == '['
|
||||||
closed = closed - 1
|
closed -= 1
|
||||||
i = i - 1
|
i -= 1
|
||||||
|
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
mem.free(memory)
|
mem.free(memory)
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func chip8_disassemble[c: CHIP8, ins_count: i64] : void
|
|||||||
|
|
||||||
high := c->memory[c->pc] as i64
|
high := c->memory[c->pc] as i64
|
||||||
low := c->memory[c->pc + 1] as i64
|
low := c->memory[c->pc + 1] as i64
|
||||||
c->pc = c->pc + 2
|
c->pc += 2
|
||||||
|
|
||||||
ins := (high << 8) | low
|
ins := (high << 8) | low
|
||||||
nnn := ins & 0x0fff
|
nnn := ins & 0x0fff
|
||||||
@@ -156,7 +156,7 @@ func chip8_disassemble[c: CHIP8, ins_count: i64] : void
|
|||||||
func chip8_step[c: CHIP8] : void
|
func chip8_step[c: CHIP8] : void
|
||||||
high := c->memory[c->pc] as i64
|
high := c->memory[c->pc] as i64
|
||||||
low := c->memory[c->pc + 1] as i64
|
low := c->memory[c->pc + 1] as i64
|
||||||
c->pc = c->pc + 2
|
c->pc += 2
|
||||||
|
|
||||||
ins := (high << 8) | low
|
ins := (high << 8) | low
|
||||||
nnn := ins & 0x0fff
|
nnn := ins & 0x0fff
|
||||||
@@ -175,22 +175,22 @@ func chip8_step[c: CHIP8] : void
|
|||||||
else if op == 0x1
|
else if op == 0x1
|
||||||
c->pc = nnn
|
c->pc = nnn
|
||||||
else if op == 0x2
|
else if op == 0x2
|
||||||
c->sp = c->sp + 1
|
c->sp += 1
|
||||||
array.set(c->stack, c->sp, c->pc)
|
array.set(c->stack, c->sp, c->pc)
|
||||||
c->pc = nnn
|
c->pc = nnn
|
||||||
else if op == 0x3
|
else if op == 0x3
|
||||||
if c->reg[x] == kk
|
if c->reg[x] == kk
|
||||||
c->pc = c->pc + 2
|
c->pc += 2
|
||||||
else if op == 0x4
|
else if op == 0x4
|
||||||
if c->reg[x] != kk
|
if c->reg[x] != kk
|
||||||
c->pc = c->pc + 2
|
c->pc += 2
|
||||||
else if op == 0x5
|
else if op == 0x5
|
||||||
if c->reg[x] == c->reg[y]
|
if c->reg[x] == c->reg[y]
|
||||||
c->pc = c->pc + 2
|
c->pc += 2
|
||||||
else if op == 0x6
|
else if op == 0x6
|
||||||
c->reg[x] = kk
|
c->reg[x] = kk
|
||||||
else if op == 0x7
|
else if op == 0x7
|
||||||
c->reg[x] = c->reg[x] + kk
|
c->reg[x] += kk
|
||||||
else if op == 0x8
|
else if op == 0x8
|
||||||
if n == 0x0
|
if n == 0x0
|
||||||
c->reg[x] = c->reg[y]
|
c->reg[x] = c->reg[y]
|
||||||
@@ -218,7 +218,7 @@ func chip8_step[c: CHIP8] : void
|
|||||||
c->reg[x] = c->reg[x] << 1
|
c->reg[x] = c->reg[x] << 1
|
||||||
else if op == 0x9
|
else if op == 0x9
|
||||||
if c->reg[x] != c->reg[y]
|
if c->reg[x] != c->reg[y]
|
||||||
c->pc = c->pc + 2
|
c->pc += 2
|
||||||
else if op == 0xa
|
else if op == 0xa
|
||||||
c->I = nnn
|
c->I = nnn
|
||||||
else if op == 0xb
|
else if op == 0xb
|
||||||
@@ -240,10 +240,10 @@ func chip8_step[c: CHIP8] : void
|
|||||||
else if op == 0xe
|
else if op == 0xe
|
||||||
if kk == 0x9e
|
if kk == 0x9e
|
||||||
if IsKeyDown(array.nth(c->keyboard_map, c->reg[x] as i64))
|
if IsKeyDown(array.nth(c->keyboard_map, c->reg[x] as i64))
|
||||||
c->pc = c->pc + 2
|
c->pc += 2
|
||||||
else if kk == 0xa1
|
else if kk == 0xa1
|
||||||
if !IsKeyDown(array.nth(c->keyboard_map, c->reg[x] as i64))
|
if !IsKeyDown(array.nth(c->keyboard_map, c->reg[x] as i64))
|
||||||
c->pc = c->pc + 2
|
c->pc += 2
|
||||||
else if op == 0xf
|
else if op == 0xf
|
||||||
if kk == 0x07
|
if kk == 0x07
|
||||||
c->reg[x] = c->delay_timer
|
c->reg[x] = c->delay_timer
|
||||||
@@ -259,7 +259,7 @@ func chip8_step[c: CHIP8] : void
|
|||||||
else if kk == 0x18
|
else if kk == 0x18
|
||||||
c->sound_timer = c->reg[x]
|
c->sound_timer = c->reg[x]
|
||||||
else if kk == 0x1E
|
else if kk == 0x1E
|
||||||
c->I = c->I + c->reg[x]
|
c->I += c->reg[x]
|
||||||
else if kk == 0x29
|
else if kk == 0x29
|
||||||
c->I = c->reg[x] as i64 * 5
|
c->I = c->reg[x] as i64 * 5
|
||||||
else if kk == 0x33
|
else if kk == 0x33
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func main[argc: i64, argv: ptr] : i64
|
|||||||
while i < url_len
|
while i < url_len
|
||||||
if url[i] == '/'
|
if url[i] == '/'
|
||||||
break
|
break
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
host := str.substr(url, host_start, i - host_start)
|
host := str.substr(url, host_start, i - host_start)
|
||||||
path := "/"
|
path := "/"
|
||||||
@@ -59,7 +59,7 @@ func main[argc: i64, argv: ptr] : i64
|
|||||||
found = true
|
found = true
|
||||||
end_index = i + 4
|
end_index = i + 4
|
||||||
break
|
break
|
||||||
i = i + 1
|
i += 1
|
||||||
header_size = current_size
|
header_size = current_size
|
||||||
|
|
||||||
if end_index < header_size
|
if end_index < header_size
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ func main[] : i64
|
|||||||
while a < 100000
|
while a < 100000
|
||||||
io.println_i64(a)
|
io.println_i64(a)
|
||||||
temp := b
|
temp := b
|
||||||
b = a + b
|
b += a
|
||||||
a = temp
|
a = temp
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ func part1[l1: Array, l2: Array] : void
|
|||||||
out := 0
|
out := 0
|
||||||
|
|
||||||
for i in 0..l1->size
|
for i in 0..l1->size
|
||||||
out = out + math.abs(array.nth(l1, i) - array.nth(l2, i))
|
out += math.abs(array.nth(l1, i) - array.nth(l2, i))
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ func part2[l1: Array, l2: Array] : void
|
|||||||
out := 0
|
out := 0
|
||||||
|
|
||||||
for i in 0..l1->size
|
for i in 0..l1->size
|
||||||
out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
out += array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func part1[data: Array] : void
|
|||||||
|
|
||||||
for i in 0..data->size
|
for i in 0..data->size
|
||||||
if check(array.nth(data, i))
|
if check(array.nth(data, i))
|
||||||
out = out + 1
|
out += 1
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
@@ -27,14 +27,14 @@ func part2[data: Array] : void
|
|||||||
|
|
||||||
for i in 0..data->size
|
for i in 0..data->size
|
||||||
if check(array.nth(data, i))
|
if check(array.nth(data, i))
|
||||||
out = out + 1
|
out += 1
|
||||||
else
|
else
|
||||||
arr : Array = array.nth(data, i)
|
arr : Array = array.nth(data, i)
|
||||||
for j in 0..arr->size
|
for j in 0..arr->size
|
||||||
sliced := array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1)))
|
sliced := array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1)))
|
||||||
|
|
||||||
if check(sliced)
|
if check(sliced)
|
||||||
out = out + 1
|
out += 1
|
||||||
break
|
break
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|||||||
@@ -19,31 +19,31 @@ func part1[lines: Array] : void
|
|||||||
for x in 0..lines->size
|
for x in 0..lines->size
|
||||||
for y in 0..str.len(array.nth(lines, x))
|
for y in 0..str.len(array.nth(lines, x))
|
||||||
if check(lines, x, y, 0, 1)
|
if check(lines, x, y, 0, 1)
|
||||||
out = out + 1
|
out += 1
|
||||||
if check(lines, x, y, 0, -1)
|
if check(lines, x, y, 0, -1)
|
||||||
out = out + 1
|
out += 1
|
||||||
if check(lines, x, y, 1, 0)
|
if check(lines, x, y, 1, 0)
|
||||||
out = out + 1
|
out += 1
|
||||||
if check(lines, x, y, -1, 0)
|
if check(lines, x, y, -1, 0)
|
||||||
out = out + 1
|
out += 1
|
||||||
if check(lines, x, y, 1, 1)
|
if check(lines, x, y, 1, 1)
|
||||||
out = out + 1
|
out += 1
|
||||||
if check(lines, x, y, -1, -1)
|
if check(lines, x, y, -1, -1)
|
||||||
out = out + 1
|
out += 1
|
||||||
if check(lines, x, y, 1, -1)
|
if check(lines, x, y, 1, -1)
|
||||||
out = out + 1
|
out += 1
|
||||||
if check(lines, x, y, -1, 1)
|
if check(lines, x, y, -1, 1)
|
||||||
out = out + 1
|
out += 1
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
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]
|
||||||
@@ -51,7 +51,7 @@ func part2[lines: Array] : void
|
|||||||
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 += 1
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func part1[updates: Array, rules_left: Array, rules_right: Array] : void
|
|||||||
for i in 0..updates->size
|
for i in 0..updates->size
|
||||||
update : Array = array.nth(updates, i)
|
update : Array = array.nth(updates, i)
|
||||||
if check(update, rules_left, rules_right)
|
if check(update, rules_left, rules_right)
|
||||||
out = out + array.nth(update, update->size / 2)
|
out += array.nth(update, update->size / 2)
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ func part2[updates: Array, rules_left: Array, rules_right: Array] : void
|
|||||||
update : Array = array.nth(updates, i)
|
update : Array = array.nth(updates, i)
|
||||||
if !check(update, rules_left, rules_right)
|
if !check(update, rules_left, rules_right)
|
||||||
sort_by_rules(update, rules_left, rules_right)
|
sort_by_rules(update, rules_left, rules_right)
|
||||||
out = out + array.nth(update, update->size / 2)
|
out += array.nth(update, update->size / 2)
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func solve[ops: Array, e: Array] : i64
|
|||||||
op : str = array.nth(ops, array.nth(indices, i))
|
op : str = array.nth(ops, array.nth(indices, i))
|
||||||
|
|
||||||
if str.equal(op, "add")
|
if str.equal(op, "add")
|
||||||
res = res + array.nth(e_1, i + 1)
|
res += array.nth(e_1, i + 1)
|
||||||
else if str.equal(op, "mul")
|
else if str.equal(op, "mul")
|
||||||
res = res * array.nth(e_1, i + 1)
|
res = res * array.nth(e_1, i + 1)
|
||||||
else if str.equal(op, "concat")
|
else if str.equal(op, "concat")
|
||||||
@@ -33,7 +33,7 @@ func solve[ops: Array, e: Array] : i64
|
|||||||
done = false
|
done = false
|
||||||
break
|
break
|
||||||
array.set(indices, i, 0)
|
array.set(indices, i, 0)
|
||||||
i = i - 1
|
i -= 1
|
||||||
|
|
||||||
if done
|
if done
|
||||||
return 0
|
return 0
|
||||||
@@ -42,7 +42,7 @@ func part1[equations: Array] : void
|
|||||||
out := 0
|
out := 0
|
||||||
|
|
||||||
for i in 0..equations->size
|
for i in 0..equations->size
|
||||||
out = out + solve(["add", "mul"], array.nth(equations, i))
|
out += solve(["add", "mul"], array.nth(equations, i))
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ func part2[equations: Array] : void
|
|||||||
out := 0
|
out := 0
|
||||||
|
|
||||||
for i in 0..equations->size
|
for i in 0..equations->size
|
||||||
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
|
out += solve(["add", "mul", "concat"], array.nth(equations, i))
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
|
|||||||
@@ -8,16 +8,16 @@ func part1[lines: Array] : void
|
|||||||
n := str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
n := str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||||
|
|
||||||
if dir == 'L'
|
if dir == 'L'
|
||||||
dial = dial - n
|
dial -= n
|
||||||
while dial < 0
|
while dial < 0
|
||||||
dial = 100 + dial
|
dial += 100
|
||||||
else
|
else
|
||||||
dial = dial + n
|
dial += n
|
||||||
while dial >= 100
|
while dial >= 100
|
||||||
dial = dial - 100
|
dial -= 100
|
||||||
|
|
||||||
if dial == 0
|
if dial == 0
|
||||||
password = password + 1
|
password += 1
|
||||||
|
|
||||||
io.println_i64(password)
|
io.println_i64(password)
|
||||||
|
|
||||||
@@ -32,17 +32,17 @@ func part2[lines: Array] : void
|
|||||||
|
|
||||||
if dir == 'L'
|
if dir == 'L'
|
||||||
for i in 0..n
|
for i in 0..n
|
||||||
dial = dial - 1
|
dial -= 1
|
||||||
if dial == 0
|
if dial == 0
|
||||||
password = password + 1
|
password += 1
|
||||||
if dial == -1
|
if dial == -1
|
||||||
dial = 99
|
dial = 99
|
||||||
else
|
else
|
||||||
for i in 0..n
|
for i in 0..n
|
||||||
dial = dial + 1
|
dial += 1
|
||||||
if dial == 100
|
if dial == 100
|
||||||
dial = 0
|
dial = 0
|
||||||
password = password + 1
|
password += 1
|
||||||
|
|
||||||
io.println_i64(password)
|
io.println_i64(password)
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func part1[ranges: Array] : void
|
|||||||
|
|
||||||
for n in (start)..end+1
|
for n in (start)..end+1
|
||||||
if part1_is_invalid_id(str.from_i64(n))
|
if part1_is_invalid_id(str.from_i64(n))
|
||||||
sum = sum + n
|
sum += n
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ func part2[ranges: Array] : void
|
|||||||
|
|
||||||
for n in (start)..end+1
|
for n in (start)..end+1
|
||||||
if part2_is_invalid_id(str.from_i64(n))
|
if part2_is_invalid_id(str.from_i64(n))
|
||||||
sum = sum + n
|
sum += n
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -15,7 +15,7 @@ func part1[lines: Array] : void
|
|||||||
if n > largest
|
if n > largest
|
||||||
largest = n
|
largest = n
|
||||||
|
|
||||||
sum = sum + largest
|
sum += largest
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|
||||||
// had to cheat this one
|
// had to cheat this one
|
||||||
@@ -41,7 +41,7 @@ func part2[lines: Array] : void
|
|||||||
for i in 0..lines->size
|
for i in 0..lines->size
|
||||||
line : str = array.nth(lines, i)
|
line : str = array.nth(lines, i)
|
||||||
|
|
||||||
sum = sum + part2_rec(line, 0, 12)
|
sum += part2_rec(line, 0, 12)
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ func main[] : i64
|
|||||||
|
|
||||||
for i in 0..266000
|
for i in 0..266000
|
||||||
if i % 2 != 0
|
if i % 2 != 0
|
||||||
sum = sum + i * i
|
sum += i * i
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ func main[] : i64
|
|||||||
|
|
||||||
for i in 0..1000
|
for i in 0..1000
|
||||||
if i % 5 == 0 || i % 3 == 0
|
if i % 5 == 0 || i % 3 == 0
|
||||||
sum = sum + i
|
sum += i
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ func main[] : i64
|
|||||||
|
|
||||||
while a < 4000000
|
while a < 4000000
|
||||||
if a % 2 == 0
|
if a % 2 == 0
|
||||||
sum = sum + a
|
sum += a
|
||||||
temp := b
|
temp := b
|
||||||
b = a + b
|
b += a
|
||||||
a = temp
|
a = temp
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -6,6 +6,6 @@ func main[] : i64
|
|||||||
if n % f == 0
|
if n % f == 0
|
||||||
n = n / f
|
n = n / f
|
||||||
else
|
else
|
||||||
f = f + 1
|
f += 1
|
||||||
|
|
||||||
io.println_i64(n)
|
io.println_i64(n)
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
func main[] : i64
|
func main[] : i64
|
||||||
sum_of_squares := 0
|
sum_of_squares := 0
|
||||||
for i in 1..101
|
for i in 1..101
|
||||||
sum_of_squares = sum_of_squares + i * i
|
sum_of_squares += i * i
|
||||||
|
|
||||||
square_of_sum := 0
|
square_of_sum := 0
|
||||||
for i in 1..101
|
for i in 1..101
|
||||||
square_of_sum = square_of_sum + i
|
square_of_sum += i
|
||||||
square_of_sum = square_of_sum * square_of_sum
|
square_of_sum = square_of_sum * square_of_sum
|
||||||
|
|
||||||
io.println_i64(square_of_sum - sum_of_squares)
|
io.println_i64(square_of_sum - sum_of_squares)
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ func main[] : i64
|
|||||||
i := 1
|
i := 1
|
||||||
while true
|
while true
|
||||||
if math.is_prime(i)
|
if math.is_prime(i)
|
||||||
found = found + 1
|
found += 1
|
||||||
if found == 10001
|
if found == 10001
|
||||||
io.println_i64(i)
|
io.println_i64(i)
|
||||||
break
|
break
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ func main[] : i64
|
|||||||
j := 0
|
j := 0
|
||||||
while j < 13
|
while j < 13
|
||||||
s = s * (n[i + j] - '0')
|
s = s * (n[i + j] - '0')
|
||||||
j = j + 1
|
j += 1
|
||||||
if s > out
|
if s > out
|
||||||
out = s
|
out = s
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ func main[] : i64
|
|||||||
|
|
||||||
for i in 0..2000000
|
for i in 0..2000000
|
||||||
if math.is_prime(i)
|
if math.is_prime(i)
|
||||||
sum = sum + i
|
sum += i
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -4,18 +4,18 @@ func num_divisors[n: i64] : i64
|
|||||||
out := 0
|
out := 0
|
||||||
for i in 1..end+1
|
for i in 1..end+1
|
||||||
if n % i == 0
|
if n % i == 0
|
||||||
out = out + 2
|
out += 2
|
||||||
|
|
||||||
if end * end == n
|
if end * end == n
|
||||||
out = out - 1
|
out -= 1
|
||||||
return out
|
return out
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
n := 0
|
n := 0
|
||||||
i := 1
|
i := 1
|
||||||
while true
|
while true
|
||||||
n = n + i
|
n += i
|
||||||
if num_divisors(n) > 500
|
if num_divisors(n) > 500
|
||||||
io.println_i64(n)
|
io.println_i64(n)
|
||||||
break
|
break
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ func collatz_seq[n: i64]: i64
|
|||||||
i := 1
|
i := 1
|
||||||
while n != 1
|
while n != 1
|
||||||
n = collatz_num(n)
|
n = collatz_num(n)
|
||||||
i = i + 1
|
i += 1
|
||||||
return i
|
return i
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
|
|||||||
@@ -14,6 +14,6 @@ func main[] : i64
|
|||||||
|
|
||||||
sum := 0
|
sum := 0
|
||||||
for i in 0..n->size
|
for i in 0..n->size
|
||||||
sum = sum + array.nth(n, i)
|
sum += array.nth(n, i)
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -6,21 +6,21 @@ func main[] : i64
|
|||||||
sum := 0
|
sum := 0
|
||||||
|
|
||||||
for i in 1..10
|
for i in 1..10
|
||||||
sum = sum + array.nth(s1, i)
|
sum += array.nth(s1, i)
|
||||||
for i in 0..10
|
for i in 0..10
|
||||||
sum = sum + array.nth(s2, i)
|
sum += array.nth(s2, i)
|
||||||
for i in 20..100
|
for i in 20..100
|
||||||
sum = sum + array.nth(s3, i / 10) + array.nth(s1, i % 10)
|
sum += array.nth(s3, i / 10) + array.nth(s1, i % 10)
|
||||||
|
|
||||||
for i in 1..10
|
for i in 1..10
|
||||||
sum = sum + array.nth(s1, i) + 7
|
sum += array.nth(s1, i) + 7
|
||||||
for j in 1..10
|
for j in 1..10
|
||||||
sum = sum + array.nth(s1, i) + 7 + 3 + array.nth(s1, j)
|
sum += array.nth(s1, i) + 7 + 3 + array.nth(s1, j)
|
||||||
for j in 0..10
|
for j in 0..10
|
||||||
sum = sum + array.nth(s1, i) + 7 + 3 + array.nth(s2, j)
|
sum += array.nth(s1, i) + 7 + 3 + array.nth(s2, j)
|
||||||
for j in 20..100
|
for j in 20..100
|
||||||
sum = sum + array.nth(s1, i) + 7 + 3 + array.nth(s3, j / 10) + array.nth(s1, j % 10)
|
sum += array.nth(s1, i) + 7 + 3 + array.nth(s3, j / 10) + array.nth(s1, j % 10)
|
||||||
|
|
||||||
sum = sum + array.nth(s1, 1) + 8
|
sum += array.nth(s1, 1) + 8
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func main[] : i64
|
|||||||
for year in 1901..2001
|
for year in 1901..2001
|
||||||
for mon in 1..13
|
for mon in 1..13
|
||||||
if wday == 5
|
if wday == 5
|
||||||
sun = sun + 1
|
sun += 1
|
||||||
wday = (wday + days(year, mon)) % 7
|
wday = (wday + days(year, mon)) % 7
|
||||||
|
|
||||||
io.println_i64(sun)
|
io.println_i64(sun)
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ func main[] : i64
|
|||||||
|
|
||||||
sum := 0
|
sum := 0
|
||||||
for i in 0..n->size
|
for i in 0..n->size
|
||||||
sum = sum + array.nth(n, i)
|
sum += array.nth(n, i)
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ func main[] : i64
|
|||||||
for i in 2..10000
|
for i in 2..10000
|
||||||
d := divisors_sum(i)
|
d := divisors_sum(i)
|
||||||
if i < d && i == divisors_sum(d)
|
if i < d && i == divisors_sum(d)
|
||||||
sum = sum + i + d
|
sum += i + d
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -26,13 +26,13 @@ func main[] : i64
|
|||||||
|
|
||||||
while !WindowShouldClose()
|
while !WindowShouldClose()
|
||||||
if IsKeyDown(KEY_W) & 255
|
if IsKeyDown(KEY_W) & 255
|
||||||
y = y - 10
|
y -= 10
|
||||||
if IsKeyDown(KEY_S) & 255
|
if IsKeyDown(KEY_S) & 255
|
||||||
y = y + 10
|
y += 10
|
||||||
if IsKeyDown(KEY_A) & 255
|
if IsKeyDown(KEY_A) & 255
|
||||||
x = x - 10
|
x -= 10
|
||||||
if IsKeyDown(KEY_D) & 255
|
if IsKeyDown(KEY_D) & 255
|
||||||
x = x + 10
|
x += 10
|
||||||
|
|
||||||
BeginDrawing()
|
BeginDrawing()
|
||||||
ClearBackground(WHITE)
|
ClearBackground(WHITE)
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -355,6 +355,35 @@ impl Parser {
|
|||||||
op,
|
op,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
|
} else if self.match_token(&[TokenType::PlusEqual, TokenType::MinusEqual]) {
|
||||||
|
let op = self.previous().clone();
|
||||||
|
let right = self.expression()?;
|
||||||
|
let binary_token = Token {
|
||||||
|
token_type: match op.token_type {
|
||||||
|
TokenType::PlusEqual => TokenType::Plus,
|
||||||
|
TokenType::MinusEqual => TokenType::Minus,
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
|
lexeme: match op.token_type {
|
||||||
|
TokenType::PlusEqual => String::from("+"),
|
||||||
|
TokenType::MinusEqual => String::from("-"),
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
|
loc: op.loc.clone(),
|
||||||
|
};
|
||||||
|
Ok(Stmt::Assign {
|
||||||
|
left: expr.clone(),
|
||||||
|
op: Token {
|
||||||
|
token_type: TokenType::Equal,
|
||||||
|
lexeme: String::from("="),
|
||||||
|
loc: op.loc,
|
||||||
|
},
|
||||||
|
value: Expr::new(ExprKind::Binary {
|
||||||
|
left: Box::new(expr),
|
||||||
|
op: binary_token,
|
||||||
|
right: Box::new(right),
|
||||||
|
}),
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
Ok(Stmt::Expression(expr))
|
Ok(Stmt::Expression(expr))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,11 +138,11 @@ func net.encode_dns_name[domain: str] : Blob
|
|||||||
if i == domain_len || domain[i] == '.'
|
if i == domain_len || domain[i] == '.'
|
||||||
part_len := i - part_start
|
part_len := i - part_start
|
||||||
buf->data[out_pos] = part_len & 0xff
|
buf->data[out_pos] = part_len & 0xff
|
||||||
out_pos = out_pos + 1
|
out_pos += 1
|
||||||
mem.copy(domain as ptr + part_start, buf->data + out_pos, part_len)
|
mem.copy(domain as ptr + part_start, buf->data + out_pos, part_len)
|
||||||
out_pos = out_pos + part_len
|
out_pos += part_len
|
||||||
part_start = i + 1
|
part_start = i + 1
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
buf->data[out_pos] = 0
|
buf->data[out_pos] = 0
|
||||||
return buf
|
return buf
|
||||||
@@ -188,11 +188,11 @@ func net.resolve[domain: str] : i64, bool
|
|||||||
pos := 12 // skip header (12 bytes)
|
pos := 12 // skip header (12 bytes)
|
||||||
|
|
||||||
while pkt->data[pos] != 0
|
while pkt->data[pos] != 0
|
||||||
pos = pos + pkt->data[pos] + 1 // skip question
|
pos += pkt->data[pos] + 1 // skip question
|
||||||
|
|
||||||
pos = pos + 5 // skip null byte, type(2), class(2)
|
pos += 5 // skip null byte, type(2), class(2)
|
||||||
|
|
||||||
pos = pos + 12 // skip name(2), type(2), class(2), ttl(4), rdlength(2)
|
pos += 12 // skip name(2), type(2), class(2), ttl(4), rdlength(2)
|
||||||
|
|
||||||
out := net.pack_addr(pkt->data[pos] as i64, pkt->data[pos+1] as i64, pkt->data[pos+2] as i64, pkt->data[pos+3] as i64)
|
out := net.pack_addr(pkt->data[pos] as i64, pkt->data[pos+1] as i64, pkt->data[pos+2] as i64, pkt->data[pos+3] as i64)
|
||||||
net.UDPPacket.free(pkt)
|
net.UDPPacket.free(pkt)
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ func mem.free[x: any] : void
|
|||||||
if next as ptr && next->free
|
if next as ptr && next->free
|
||||||
expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
|
expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
|
||||||
if expected_next as ptr == next as ptr
|
if expected_next as ptr == next as ptr
|
||||||
blk->size = blk->size + MEM_BLOCK_SIZE + next->size
|
blk->size += MEM_BLOCK_SIZE + next->size
|
||||||
blk->next = next->next
|
blk->next = next->next
|
||||||
if next->next as ptr
|
if next->next as ptr
|
||||||
next->next->prev = blk
|
next->next->prev = blk
|
||||||
@@ -101,7 +101,7 @@ func mem.free[x: any] : void
|
|||||||
if prev as ptr && prev->free
|
if prev as ptr && prev->free
|
||||||
expected_blk := (prev as ptr + MEM_BLOCK_SIZE + prev->size) as mem.Block
|
expected_blk := (prev as ptr + MEM_BLOCK_SIZE + prev->size) as mem.Block
|
||||||
if expected_blk as ptr == blk as ptr
|
if expected_blk as ptr == blk as ptr
|
||||||
prev->size = prev->size + MEM_BLOCK_SIZE + blk->size
|
prev->size += MEM_BLOCK_SIZE + blk->size
|
||||||
prev->next = blk->next
|
prev->next = blk->next
|
||||||
if blk->next as ptr
|
if blk->next as ptr
|
||||||
blk->next->prev = prev
|
blk->next->prev = prev
|
||||||
@@ -168,28 +168,28 @@ func mem.zero[x: ptr, size: i64] : void
|
|||||||
i := 0
|
i := 0
|
||||||
while i + 8 <= size
|
while i + 8 <= size
|
||||||
mem.write64(x + i, 0)
|
mem.write64(x + i, 0)
|
||||||
i = i + 8
|
i += 8
|
||||||
while i < size
|
while i < size
|
||||||
x[i] = 0 as u8
|
x[i] = 0 as u8
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
func mem.copy[src: ptr, dst: ptr, n: i64] : void
|
func mem.copy[src: ptr, dst: ptr, n: i64] : void
|
||||||
if dst > src && dst < src + n
|
if dst > src && dst < src + n
|
||||||
i := n
|
i := n
|
||||||
while i - 8 >= 0
|
while i - 8 >= 0
|
||||||
i = i - 8
|
i -= 8
|
||||||
mem.write64(dst + i, mem.read64(src + i))
|
mem.write64(dst + i, mem.read64(src + i))
|
||||||
while i > 0
|
while i > 0
|
||||||
i = i - 1
|
i -= 1
|
||||||
dst[i] = src[i]
|
dst[i] = src[i]
|
||||||
else
|
else
|
||||||
i := 0
|
i := 0
|
||||||
while i + 8 <= n
|
while i + 8 <= n
|
||||||
mem.write64(dst + i, mem.read64(src + i))
|
mem.write64(dst + i, mem.read64(src + i))
|
||||||
i = i + 8
|
i += 8
|
||||||
while i < n
|
while i < n
|
||||||
dst[i] = src[i]
|
dst[i] = src[i]
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
func mem.read8[x: ptr] : u8
|
func mem.read8[x: ptr] : u8
|
||||||
return x[0]
|
return x[0]
|
||||||
@@ -246,20 +246,20 @@ func io.printf[..] : void
|
|||||||
|
|
||||||
while s[0]
|
while s[0]
|
||||||
if s[0] == '%'
|
if s[0] == '%'
|
||||||
s = s + 1
|
s += 1
|
||||||
|
|
||||||
if s[0] == 'd'
|
if s[0] == 'd'
|
||||||
io.print_i64(_var_arg(i) as i64)
|
io.print_i64(_var_arg(i) as i64)
|
||||||
i = i + 1
|
i += 1
|
||||||
else if s[0] == 'x'
|
else if s[0] == 'x'
|
||||||
io.print_i64_hex(_var_arg(i) as i64)
|
io.print_i64_hex(_var_arg(i) as i64)
|
||||||
i = i + 1
|
i += 1
|
||||||
else if s[0] == 's'
|
else if s[0] == 's'
|
||||||
io.print(_var_arg(i) as str)
|
io.print(_var_arg(i) as str)
|
||||||
i = i + 1
|
i += 1
|
||||||
else if s[0] == 'c'
|
else if s[0] == 'c'
|
||||||
io.print_char(_var_arg(i) as u8)
|
io.print_char(_var_arg(i) as u8)
|
||||||
i = i + 1
|
i += 1
|
||||||
else if s[0] == '%'
|
else if s[0] == '%'
|
||||||
io.print_char('%')
|
io.print_char('%')
|
||||||
else if s[0] == 0
|
else if s[0] == 0
|
||||||
@@ -268,7 +268,7 @@ func io.printf[..] : void
|
|||||||
panic("io.printf: unrecognized format")
|
panic("io.printf: unrecognized format")
|
||||||
else
|
else
|
||||||
io.print_char(s[0])
|
io.print_char(s[0])
|
||||||
s = s + 1
|
s += 1
|
||||||
|
|
||||||
func io.snprintf[..] : i64
|
func io.snprintf[..] : i64
|
||||||
buf := _var_arg(0) as ptr
|
buf := _var_arg(0) as ptr
|
||||||
@@ -279,42 +279,42 @@ 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 = 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
|
||||||
mem.copy(tmp, buf + n, math.min(tmp_len, remaining))
|
mem.copy(tmp, buf + n, math.min(tmp_len, remaining))
|
||||||
n = n + tmp_len
|
n += tmp_len
|
||||||
i = 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
|
||||||
mem.copy(tmp, buf + n, math.min(tmp_len, remaining))
|
mem.copy(tmp, buf + n, math.min(tmp_len, remaining))
|
||||||
n = n + tmp_len
|
n += tmp_len
|
||||||
i = i + 1
|
i += 1
|
||||||
else if s[0] == 's'
|
else if s[0] == 's'
|
||||||
tmp_str := _var_arg(i) as str
|
tmp_str := _var_arg(i) as str
|
||||||
tmp_len := str.len(tmp_str)
|
tmp_len := str.len(tmp_str)
|
||||||
remaining := size - n - 1
|
remaining := size - n - 1
|
||||||
mem.copy(tmp_str as ptr, buf + n, math.min(tmp_len, remaining))
|
mem.copy(tmp_str as ptr, buf + n, math.min(tmp_len, remaining))
|
||||||
n = n + tmp_len
|
n += tmp_len
|
||||||
i = i + 1
|
i += 1
|
||||||
else if s[0] == 'c'
|
else if s[0] == 'c'
|
||||||
if n < size - 1
|
if n < size - 1
|
||||||
buf[n] = _var_arg(i) as u8
|
buf[n] = _var_arg(i) as u8
|
||||||
n = n + 1
|
n += 1
|
||||||
i = i + 1
|
i += 1
|
||||||
else if s[0] == '%'
|
else if s[0] == '%'
|
||||||
if n < size - 1
|
if n < size - 1
|
||||||
buf[n] = '%'
|
buf[n] = '%'
|
||||||
n = n + 1
|
n += 1
|
||||||
else if s[0] == 0
|
else if s[0] == 0
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
@@ -322,8 +322,8 @@ func io.snprintf[..] : i64
|
|||||||
else
|
else
|
||||||
if n < size - 1
|
if n < size - 1
|
||||||
buf[n] = s[0]
|
buf[n] = s[0]
|
||||||
n = n + 1
|
n += 1
|
||||||
s = s + 1
|
s += 1
|
||||||
|
|
||||||
if n < size
|
if n < size
|
||||||
buf[n] = 0
|
buf[n] = 0
|
||||||
@@ -449,7 +449,7 @@ func io.write_binary_file[path: str, content: ptr, size: i64] : bool
|
|||||||
func str.len[s: str] : i64
|
func str.len[s: str] : i64
|
||||||
i := 0
|
i := 0
|
||||||
while s[i]
|
while s[i]
|
||||||
i = i + 1
|
i += 1
|
||||||
return i
|
return i
|
||||||
|
|
||||||
func str.make_copy[s: str] : str
|
func str.make_copy[s: str] : str
|
||||||
@@ -463,7 +463,7 @@ func str.equal[a: str, b: str] : bool
|
|||||||
while a[i] != 0 && b[i] != 0
|
while a[i] != 0 && b[i] != 0
|
||||||
if a[i] != b[i]
|
if a[i] != b[i]
|
||||||
return false
|
return false
|
||||||
i = i + 1
|
i += 1
|
||||||
return a[i] == b[i]
|
return a[i] == b[i]
|
||||||
|
|
||||||
func str.is_whitespace[x: u8] : bool
|
func str.is_whitespace[x: u8] : bool
|
||||||
@@ -543,10 +543,10 @@ func str.trim[s: str] : str
|
|||||||
end := len - 1
|
end := len - 1
|
||||||
|
|
||||||
while start <= end && str.is_whitespace(s[start])
|
while start <= end && str.is_whitespace(s[start])
|
||||||
start = start + 1
|
start += 1
|
||||||
|
|
||||||
while end >= start && str.is_whitespace(s[end])
|
while end >= start && str.is_whitespace(s[end])
|
||||||
end = end - 1
|
end -= 1
|
||||||
|
|
||||||
return str.substr(s, start, end - start + 1)
|
return str.substr(s, start, end - start + 1)
|
||||||
|
|
||||||
@@ -575,9 +575,9 @@ func str.split[haystack: str, needle: str]: Array
|
|||||||
if match
|
if match
|
||||||
array.push(result, str.substr(haystack, start, i - start))
|
array.push(result, str.substr(haystack, start, i - start))
|
||||||
start = i + needle_len
|
start = i + needle_len
|
||||||
i = i + needle_len
|
i += needle_len
|
||||||
continue
|
continue
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
array.push(result, str.substr(haystack, start, haystack_len - start))
|
array.push(result, str.substr(haystack, start, haystack_len - start))
|
||||||
return result
|
return result
|
||||||
@@ -609,16 +609,16 @@ func str.from_i64_buf[buf: ptr, n: i64] : void
|
|||||||
tmp := _stackalloc(21)
|
tmp := _stackalloc(21)
|
||||||
end := 20
|
end := 20
|
||||||
tmp[end] = 0
|
tmp[end] = 0
|
||||||
end = end - 1
|
end -= 1
|
||||||
for i in 0..19
|
for i in 0..19
|
||||||
if n == 0
|
if n == 0
|
||||||
break
|
break
|
||||||
tmp[end] = '0' + (n % 10)
|
tmp[end] = '0' + (n % 10)
|
||||||
n = n / 10
|
n = n / 10
|
||||||
end = end - 1
|
end -= 1
|
||||||
if neg
|
if neg
|
||||||
tmp[end] = '-'
|
tmp[end] = '-'
|
||||||
end = end - 1
|
end -= 1
|
||||||
len := 19 - end
|
len := 19 - end
|
||||||
for i in 0..len
|
for i in 0..len
|
||||||
buf[i] = tmp[end + 1 + i]
|
buf[i] = tmp[end + 1 + i]
|
||||||
@@ -646,7 +646,7 @@ func str.hex_from_i64_buf[buf: ptr, n: i64] : void
|
|||||||
break
|
break
|
||||||
tmp[len] = hex_chars[n & 15]
|
tmp[len] = hex_chars[n & 15]
|
||||||
n = (n >> 4) & mask
|
n = (n >> 4) & mask
|
||||||
len = len + 1
|
len += 1
|
||||||
|
|
||||||
for j in 0..len
|
for j in 0..len
|
||||||
buf[j] = tmp[len - 1 - j]
|
buf[j] = tmp[len - 1 - j]
|
||||||
@@ -666,7 +666,7 @@ func str.parse_i64[s: str] : i64
|
|||||||
sign := 1
|
sign := 1
|
||||||
if i < len && s[i] == '-'
|
if i < len && s[i] == '-'
|
||||||
sign = -1
|
sign = -1
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
num := 0
|
num := 0
|
||||||
while i < len
|
while i < len
|
||||||
@@ -674,7 +674,7 @@ func str.parse_i64[s: str] : i64
|
|||||||
if d < '0' || d > '9'
|
if d < '0' || d > '9'
|
||||||
break
|
break
|
||||||
num = num * 10 + (d - '0')
|
num = num * 10 + (d - '0')
|
||||||
i = i + 1
|
i += 1
|
||||||
return num * sign
|
return num * sign
|
||||||
|
|
||||||
func str.hex_encode[s: str, s_len: i64] : str
|
func str.hex_encode[s: str, s_len: i64] : str
|
||||||
@@ -731,13 +731,13 @@ func str.Builder._grow[b: str.Builder, needed: i64] : void
|
|||||||
func str.Builder.append_char[b: str.Builder, c: u8] : void
|
func str.Builder.append_char[b: str.Builder, c: u8] : void
|
||||||
str.Builder._grow(b, 1)
|
str.Builder._grow(b, 1)
|
||||||
b->data[b->size] = c
|
b->data[b->size] = c
|
||||||
b->size = b->size + 1
|
b->size += 1
|
||||||
|
|
||||||
func str.Builder.append[b: str.Builder, s: str] : void
|
func str.Builder.append[b: str.Builder, s: str] : void
|
||||||
len := str.len(s)
|
len := str.len(s)
|
||||||
str.Builder._grow(b, len)
|
str.Builder._grow(b, len)
|
||||||
mem.copy(s as ptr, b->data + b->size, len)
|
mem.copy(s as ptr, b->data + b->size, len)
|
||||||
b->size = b->size + len
|
b->size += len
|
||||||
|
|
||||||
func str.Builder.build[b: str.Builder] : str
|
func str.Builder.build[b: str.Builder] : str
|
||||||
s := mem.alloc(b->size + 1) as str
|
s := mem.alloc(b->size + 1) as str
|
||||||
@@ -820,7 +820,7 @@ func math.is_prime[n: i64]: bool
|
|||||||
while i * i <= n
|
while i * i <= n
|
||||||
if n % i == 0 || n % (i + 2) == 0
|
if n % i == 0 || n % (i + 2) == 0
|
||||||
return false
|
return false
|
||||||
i = i + 6
|
i += 6
|
||||||
return true
|
return true
|
||||||
|
|
||||||
struct Array
|
struct Array
|
||||||
@@ -856,7 +856,7 @@ func array.push[xs: Array, x: any] : void
|
|||||||
xs->capacity = new_capacity
|
xs->capacity = new_capacity
|
||||||
|
|
||||||
mem.write64(xs->data + xs->size * 8, x)
|
mem.write64(xs->data + xs->size * 8, x)
|
||||||
xs->size = xs->size + 1
|
xs->size += 1
|
||||||
|
|
||||||
func array.free[xs: Array] : void
|
func array.free[xs: Array] : void
|
||||||
if xs->data != 0
|
if xs->data != 0
|
||||||
@@ -986,7 +986,7 @@ func alg._partition[arr: Array, low: i64, high: i64] : i64
|
|||||||
i := low - 1
|
i := low - 1
|
||||||
for j in (low)..high
|
for j in (low)..high
|
||||||
if array.nth(arr, j) as i64 <= pivot
|
if array.nth(arr, j) as i64 <= pivot
|
||||||
i = i + 1
|
i += 1
|
||||||
temp : i64 = array.nth(arr ,i)
|
temp : i64 = array.nth(arr ,i)
|
||||||
array.set(arr, i, array.nth(arr, j))
|
array.set(arr, i, array.nth(arr, j))
|
||||||
array.set(arr, j, temp)
|
array.set(arr, j, temp)
|
||||||
@@ -999,7 +999,7 @@ func alg.count[arr: Array, item: any] : i64
|
|||||||
count := 0
|
count := 0
|
||||||
for i in 0..arr->size
|
for i in 0..arr->size
|
||||||
if array.nth(arr, i) == item
|
if array.nth(arr, i) == item
|
||||||
count = count + 1
|
count += 1
|
||||||
return count
|
return count
|
||||||
|
|
||||||
func alg.map[arr: Array, fn: fnptr] : Array
|
func alg.map[arr: Array, fn: fnptr] : Array
|
||||||
@@ -1044,7 +1044,7 @@ func os.urandom_buf[buf: ptr, n: i64] : void
|
|||||||
ret := _builtin_syscall(SYS_getrandom, buf + pos, n - pos, 0)
|
ret := _builtin_syscall(SYS_getrandom, buf + pos, n - pos, 0)
|
||||||
if ret <= 0
|
if ret <= 0
|
||||||
panic("os.urandom_buf: syscall failed")
|
panic("os.urandom_buf: syscall failed")
|
||||||
pos = pos + ret
|
pos += ret
|
||||||
|
|
||||||
func os.urandom[n: i64]: ptr
|
func os.urandom[n: i64]: ptr
|
||||||
buf := mem.alloc(n)
|
buf := mem.alloc(n)
|
||||||
@@ -1119,7 +1119,7 @@ func os.list_directory[path: str] : Array, bool
|
|||||||
skip = true
|
skip = true
|
||||||
if !skip
|
if !skip
|
||||||
array.push(files, str.make_copy(name as str))
|
array.push(files, str.make_copy(name as str))
|
||||||
pos = pos + len
|
pos += len
|
||||||
|
|
||||||
_builtin_syscall(SYS_close, fd)
|
_builtin_syscall(SYS_close, fd)
|
||||||
return files, true
|
return files, true
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ pub enum TokenType {
|
|||||||
RightBracket,
|
RightBracket,
|
||||||
Comma,
|
Comma,
|
||||||
Plus,
|
Plus,
|
||||||
|
PlusEqual,
|
||||||
Minus,
|
Minus,
|
||||||
|
MinusEqual,
|
||||||
Star,
|
Star,
|
||||||
Slash,
|
Slash,
|
||||||
Mod,
|
Mod,
|
||||||
@@ -154,7 +156,13 @@ impl Tokenizer {
|
|||||||
')' => self.add_token(TokenType::RightParen)?,
|
')' => self.add_token(TokenType::RightParen)?,
|
||||||
'[' => self.add_token(TokenType::LeftBracket)?,
|
'[' => self.add_token(TokenType::LeftBracket)?,
|
||||||
']' => self.add_token(TokenType::RightBracket)?,
|
']' => self.add_token(TokenType::RightBracket)?,
|
||||||
'+' => self.add_token(TokenType::Plus)?,
|
'+' => {
|
||||||
|
if self.match_char('=') {
|
||||||
|
self.add_token(TokenType::PlusEqual)?
|
||||||
|
} else {
|
||||||
|
self.add_token(TokenType::Plus)?
|
||||||
|
}
|
||||||
|
}
|
||||||
'*' => self.add_token(TokenType::Star)?,
|
'*' => self.add_token(TokenType::Star)?,
|
||||||
',' => self.add_token(TokenType::Comma)?,
|
',' => self.add_token(TokenType::Comma)?,
|
||||||
'%' => self.add_token(TokenType::Mod)?,
|
'%' => self.add_token(TokenType::Mod)?,
|
||||||
@@ -162,7 +170,9 @@ impl Tokenizer {
|
|||||||
':' => self.add_token(TokenType::Colon)?,
|
':' => self.add_token(TokenType::Colon)?,
|
||||||
'~' => self.add_token(TokenType::Tilde)?,
|
'~' => self.add_token(TokenType::Tilde)?,
|
||||||
'-' => {
|
'-' => {
|
||||||
if self.match_char('>') {
|
if self.match_char('=') {
|
||||||
|
self.add_token(TokenType::MinusEqual)?
|
||||||
|
} else if self.match_char('>') {
|
||||||
self.add_token(TokenType::Arrow)?
|
self.add_token(TokenType::Arrow)?
|
||||||
} else {
|
} else {
|
||||||
self.add_token(TokenType::Minus)?
|
self.add_token(TokenType::Minus)?
|
||||||
|
|||||||
Reference in New Issue
Block a user