-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrainfuckBasicCommands.cs
48 lines (44 loc) · 1.61 KB
/
BrainfuckBasicCommands.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
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
namespace func.brainfuck
{
public class BrainfuckBasicCommands
{
public static void RegisterTo (IVirtualMachine vm, Func<int> read, Action<char> write)
{
unchecked
{
vm.RegisterCommand('+', b => { b.Memory[b.MemoryPointer]++; });
vm.RegisterCommand('-', b => { b.Memory[b.MemoryPointer]--; });
}
vm.RegisterCommand('.', b => { write((char) b.Memory[b.MemoryPointer]); });
vm.RegisterCommand(',', b => { b.Memory[b.MemoryPointer] = (byte) read(); });
vm.RegisterCommand('>', b =>
{
if (b.MemoryPointer == b.Memory.Length - 1)
b.MemoryPointer = 0;
else
b.MemoryPointer++;
});
vm.RegisterCommand('<', b =>
{
if (b.MemoryPointer == 0)
b.MemoryPointer = b.Memory.Length - 1;
else
b.MemoryPointer--;
});
var addedSymbols = new List<char>();
for (var i = 0; i < vm.Instructions.Length; i++)
{
if (!char.IsLetterOrDigit(vm.Instructions[i]) || addedSymbols.Contains(vm.Instructions[i]))
continue;
addedSymbols.Add(vm.Instructions[i]);
var i1 = i;
vm.RegisterCommand(vm.Instructions[i1], b =>
{
b.Memory[b.MemoryPointer] = (byte) vm.Instructions[i1];
});
}
}
}
}