Advertisement
KoMeDiAnT

Brainfuck Basic Commands

Mar 11th, 2017
2,502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace func.brainfuck
  6. {
  7.     public class Constant
  8.     {
  9.         static readonly char[] constant = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890".ToCharArray();
  10.  
  11.         public static void Constants(IVirtualMachine vm)
  12.         {
  13.             foreach (var chars in constant)
  14.             {
  15.                 vm.RegisterCommand(chars, machine => machine.Memory[machine.MemoryPointer] = (byte)chars);
  16.             }
  17.         }
  18.     }
  19.  
  20.     public class BrainfuckBasicCommands
  21.     {
  22.         public static void RightOrLeftByteShift(IVirtualMachine vm)
  23.         {
  24.             vm.RegisterCommand('<', machine =>
  25.             {
  26.                 machine.MemoryPointer = Calc(machine.MemoryPointer, -1, machine.Memory.Length);
  27.             });
  28.  
  29.             vm.RegisterCommand('>', machine =>
  30.             {
  31.                 machine.MemoryPointer = Calc(machine.MemoryPointer, 1, machine.Memory.Length);
  32.             });
  33.         }
  34.  
  35.         public static void IncOrDecByte(IVirtualMachine vm)
  36.         {
  37.             vm.RegisterCommand('+', machine =>
  38.             {
  39.                 var bytes = machine.Memory[machine.MemoryPointer];
  40.                 var length = machine.Memory.Length;
  41.  
  42.                 machine.Memory[machine.MemoryPointer] = bytes == 255
  43.                     ? machine.Memory[machine.MemoryPointer] = 0
  44.                     : (byte)Calc(bytes, 1, length);
  45.             });
  46.  
  47.             vm.RegisterCommand('-', machine =>
  48.             {
  49.                 var bytes = machine.Memory[machine.MemoryPointer];
  50.                 var length = machine.Memory.Length;
  51.  
  52.                 machine.Memory[machine.MemoryPointer] = bytes == 0
  53.                     ? machine.Memory[machine.MemoryPointer] = 255
  54.                     : (byte)Calc(bytes, -1, length);
  55.             });
  56.         }
  57.  
  58.         public static int Calc(int a, int b, int modulus)
  59.         {
  60.             return (a + modulus + b % modulus) % modulus;
  61.         }
  62.  
  63.         public static void RegisterTo(IVirtualMachine vm, Func<int> read, Action<char> write)
  64.         {
  65.             Constant.Constants(vm);
  66.  
  67.             RightOrLeftByteShift(vm);
  68.  
  69.             IncOrDecByte(vm);
  70.  
  71.             vm.RegisterCommand('.', machine => write((char)machine.Memory[machine.MemoryPointer]));
  72.  
  73.             vm.RegisterCommand(',', machine => machine.Memory[machine.MemoryPointer] = (byte) read());
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement