-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualMachine.cs
37 lines (32 loc) · 1.07 KB
/
VirtualMachine.cs
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
using System;
using System.Collections.Generic;
namespace func.brainfuck
{
public class VirtualMachine : IVirtualMachine
{
public string Instructions { get; }
public int InstructionPointer { get; set; }
public byte[] Memory { get; }
public int MemoryPointer { get; set; }
private readonly Dictionary<char, Action<IVirtualMachine>> _dictionary =
new Dictionary<char, Action<IVirtualMachine>>();
public VirtualMachine(string program, int memorySize)
{
Instructions = program;
Memory = new byte[memorySize];
}
public void RegisterCommand(char symbol, Action<IVirtualMachine> execute)
{
_dictionary.Add(symbol, execute);
}
public void Run()
{
while (InstructionPointer < Instructions.Length)
{
if (_dictionary.ContainsKey(Instructions[InstructionPointer]))
_dictionary[Instructions[InstructionPointer]](this);
InstructionPointer++;
}
}
}
}