-
Notifications
You must be signed in to change notification settings - Fork 0
/
computer.jl
64 lines (49 loc) · 1.42 KB
/
computer.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function parse_instructions(line)
instr, arg = match(r"^([a-z]+) ([-+][0-9]+)$", line).captures
instr, parse(Int, arg)
end
function create_computer(lines)
instructions = parse_instructions.(lines)
Computer(instructions, 1, 0)
end
mutable struct Computer
instructions::Vector{Tuple{AbstractString, Int64}}
ip::Int64
r1::Int64
end
function standard_instruction(f)
return (computer, value) -> begin
f(computer, value)
computer.ip += 1
end
end
function acc(computer, value)
computer.r1 += value
end
function jmp(computer, value)
computer.ip += value
end
dispatchDict = Dict("acc" => standard_instruction(acc), "jmp" => jmp, "nop" => standard_instruction((_, _) -> nothing))
function dispatch(computer, (instruction, value))
@assert haskey(dispatchDict, instruction)
dispatchDict[instruction](computer, value)
end
function step(computer)
instruction = computer.instructions[computer.ip]
dispatch(computer, instruction)
end
function run(computer; callback=nothing)
cb_done = false
while computer.ip <= length(computer.instructions) && !cb_done
step(computer)
if callback !== nothing
cb_done = callback(computer)
end
end
!cb_done && (computer.ip - length(computer.instructions) > 1 ? false : true)
end
function reset(computer)
computer.r1 = 0
computer.ip = 1
end
export Computer, create_computer, step, run, reset