Permalink
Browse files

Extract out tape class in BrainmessShort

  • Loading branch information...
michaelgwelch committed Mar 23, 2012
1 parent 746f4e3 commit c75cc6a8cefaca42c66570f7bf49f6e16846dea6
Showing with 55 additions and 41 deletions.
  1. +1 −0 csharp/BrainmessShort/BrainmessShort.csproj
  2. +11 −41 csharp/BrainmessShort/Main.cs
  3. +43 −0 csharp/BrainmessShort/Tape.cs
@@ -66,6 +66,7 @@
<Compile Include="StringExtensionsTests.cs" />
<Compile Include="Program.cs" />
<Compile Include="ProgramTests.cs" />
<Compile Include="Tape.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
@@ -6,12 +6,12 @@ namespace BrainmessShort
public class Brainmess
{
private readonly Program _program;
private readonly int[] tape = new int[5000];
private int tc = 2500;
private readonly Tape _tape;
public Brainmess(string programString)
{
_program = new Program(programString);
_tape = new Tape();
}
public static void Main(string[] args)
@@ -21,37 +21,7 @@ public static void Main(string[] args)
reader.Close();
}
int MoveForward()
{
return tc++;
}
int MoveBackward()
{
return tc--;
}
int Increment()
{
return tape[tc]++;
}
int Decrement()
{
return tape[tc]--;
}
int Current
{
get
{
return tape[tc];
}
set
{
tape[tc] = value;
}
}
public void Run()
{
@@ -61,31 +31,31 @@ public void Run()
switch(instruction)
{
case '>':
MoveForward();
_tape.MoveForward();
break;
case '<':
MoveBackward();
_tape.MoveBackward();
break;
case '+':
Increment();
_tape.Increment();
break;
case '-':
Decrement();
_tape.Decrement();
break;
case '.':
Console.Write((char)Current);
Console.Write((char)_tape.Current);
break;
case ',':
Current = Console.Read();
_tape.Current = Console.Read();
break;
case '[':
if (Current == 0)
if (_tape.Current == 0)
{
_program.JumpForward();
}
break;
case ']':
if (Current != 0)
if (_tape.Current != 0)
{
_program.JumpBackward();
}
@@ -0,0 +1,43 @@
using System;
namespace BrainmessShort
{
public class Tape
{
private readonly int[] tape = new int[5000];
private int tc = 2500;
public int MoveForward()
{
return tc++;
}
public int MoveBackward()
{
return tc--;
}
public int Increment()
{
return tape[tc]++;
}
public int Decrement()
{
return tape[tc]--;
}
public int Current
{
get
{
return tape[tc];
}
set
{
tape[tc] = value;
}
}
}
}

0 comments on commit c75cc6a

Please sign in to comment.