Skip to content

Commit

Permalink
Extract out tape related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgwelch committed Mar 23, 2012
1 parent 7ad3fd6 commit 746f4e3
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions csharp/BrainmessShort/Main.cs
Expand Up @@ -20,6 +20,38 @@ public static void Main(string[] args)
new Brainmess(reader.ReadToEnd()).Run();
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()
{
Expand All @@ -29,31 +61,31 @@ public void Run()
switch(instruction)
{
case '>':
tc++;
MoveForward();
break;
case '<':
tc--;
MoveBackward();
break;
case '+':
tape[tc]++;
Increment();
break;
case '-':
tape[tc]--;
Decrement();
break;
case '.':
Console.Write((char)tape[tc]);
Console.Write((char)Current);
break;
case ',':
tape[tc] = Console.Read();
Current = Console.Read();
break;
case '[':
if (tape[tc] == 0)
if (Current == 0)
{
_program.JumpForward();
}
break;
case ']':
if (tape[tc] != 0)
if (Current != 0)
{
_program.JumpBackward();
}
Expand Down

0 comments on commit 746f4e3

Please sign in to comment.