Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Extract out program related methods in prep for extract class
- Loading branch information
Showing
with
18 additions
and
3 deletions.
-
+18
−3
csharp/BrainmessShort/Main.cs
|
@@ -20,12 +20,27 @@ public static void Main(string[] args) |
|
|
new Brainmess(reader.ReadToEnd()).Run(); |
|
|
reader.Close(); |
|
|
} |
|
|
|
|
|
char Fetch() |
|
|
{ |
|
|
return program[pc]; |
|
|
} |
|
|
|
|
|
int JumpForward() |
|
|
{ |
|
|
return program.FindMatch(pc - 1) + 1; |
|
|
} |
|
|
|
|
|
int JumpBackward() |
|
|
{ |
|
|
return program.FindMatch(pc - 1); |
|
|
} |
|
|
|
|
|
public void Run() |
|
|
{ |
|
|
while(pc < program.Length) |
|
|
{ |
|
|
char instruction = program[pc]; |
|
|
char instruction = Fetch (); |
|
|
pc++; |
|
|
switch(instruction) |
|
|
{ |
|
@@ -50,13 +65,13 @@ public void Run() |
|
|
case '[': |
|
|
if (tape[tc] == 0) |
|
|
{ |
|
|
pc = program.FindMatch(pc - 1) + 1; |
|
|
pc = JumpForward (); |
|
|
} |
|
|
break; |
|
|
case ']': |
|
|
if (tape[tc] != 0) |
|
|
{ |
|
|
pc = program.FindMatch(pc - 1); |
|
|
pc = JumpBackward (); |
|
|
} |
|
|
break; |
|
|
} |
|
|