Permalink
Browse files

Extract out class

Extract out class
  • Loading branch information...
michaelgwelch committed Mar 22, 2012
1 parent 43453c2 commit 356b6b6b7ef46b4b32a20183fdc3c699efa9863b
Showing with 51 additions and 33 deletions.
  1. +1 −0 csharp/BrainmessShort/BrainmessShort.csproj
  2. +8 −33 csharp/BrainmessShort/Main.cs
  3. +42 −0 csharp/BrainmessShort/Program.cs
@@ -64,6 +64,7 @@
<Compile Include="MainTest.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="StringExtensionsTests.cs" />
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
@@ -5,13 +5,13 @@ namespace BrainmessShort
{
public class Brainmess
{
private readonly string program;
private int pc = 0;
private readonly Program _program;
private readonly int[] tape = new int[5000];
private int tc = 2500;
public Brainmess(string program)
public Brainmess(string programString)
{
this.program = program;
_program = new Program(programString);
}
public static void Main(string[] args)
@@ -20,37 +20,12 @@ public static void Main(string[] args)
new Brainmess(reader.ReadToEnd()).Run();
reader.Close();
}
char Fetch()
{
var instruction = program[pc];
pc++;
return instruction;
}
void JumpForward()
{
pc = program.FindMatch(pc - 1) + 1;
}
void JumpBackward()
{
pc = program.FindMatch(pc - 1);
}
bool EndOfProgram
{
get
{
return (pc >= program.Length);
}
}
public void Run()
{
while(!EndOfProgram)
while(!_program.EndOfProgram)
{
char instruction = Fetch();
char instruction = _program.Fetch();
switch(instruction)
{
case '>':
@@ -74,13 +49,13 @@ public void Run()
case '[':
if (tape[tc] == 0)
{
JumpForward();
_program.JumpForward();
}
break;
case ']':
if (tape[tc] != 0)
{
JumpBackward();
_program.JumpBackward();
}
break;
}
@@ -0,0 +1,42 @@
using System;
namespace BrainmessShort
{
public class Program
{
private readonly string program;
private int pc;
public Program(string program)
{
this.program = program;
pc = 0;
}
public bool EndOfProgram
{
get
{
return pc >= program.Length;
}
}
public char Fetch()
{
var instruction = program[pc];
pc++;
return instruction;
}
public void JumpForward()
{
pc = program.FindMatch(pc - 1) + 1;
}
public void JumpBackward()
{
pc = program.FindMatch(pc - 1);
}
}
}

0 comments on commit 356b6b6

Please sign in to comment.