Skip to content
Permalink
Browse files

Extract jump methods

  • Loading branch information...
michaelgwelch committed Mar 14, 2012
1 parent 05348d6 commit 4b15b4caf3eeeaef4da1eba9fcfd78071a8ed51a
Showing with 44 additions and 24 deletions.
  1. +0 −3 csharp/BrainmessShort/BrainmessShort.csproj
  2. +44 −21 csharp/BrainmessShort/Main.cs
@@ -80,8 +80,5 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
@@ -17,7 +17,6 @@ public static void Run(string program)
int pc = 0;
int[] tape = new int[5000];
int tc = 2500;
int nestLevel;
while(pc < program.Length)
{
char instruction = program[pc];
@@ -45,33 +44,57 @@ public static void Run(string program)
case '[':
if (tape[tc] == 0)
{
nestLevel = 1;
while(nestLevel > 0)
{
instruction = program[pc];
if (instruction == '[') nestLevel++;
else if (instruction == ']') nestLevel--;
pc++;
}
pc = JumpForward(program, pc);
}
break;
break;
case ']':
if (tape[tc] != 0)
{
pc -= 2;
nestLevel = 1;
while(nestLevel > 0)
{
instruction = program[pc];
if (instruction == '[') nestLevel--;
else if (instruction == ']') nestLevel++;
pc--;
}
pc++;
pc = JumpBackward(program, pc);
}
break;
}
}
}
}

private static int JumpForward(string program, int pc)
{
int nestLevel = 1;
while (nestLevel > 0)
{
char instruction = program[pc];
if (instruction == '[')
{
nestLevel++;
}
else if (instruction == ']')
{
nestLevel--;
}
pc++;
}
return pc;
}

private static int JumpBackward(string program, int pc)
{
pc -= 2;
int nestLevel = 1;
while (nestLevel > 0)
{
char instruction = program[pc];
if (instruction == '[')
{
nestLevel--;
}
else if (instruction == ']')
{
nestLevel++;
}
pc--;
}
pc++;
return pc;
}
}
}

0 comments on commit 4b15b4c

Please sign in to comment.
You can’t perform that action at this time.