-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimization05.cs
More file actions
166 lines (146 loc) · 5.66 KB
/
Copy pathOptimization05.cs
File metadata and controls
166 lines (146 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.IO;
namespace BfInterpreter
{
class Optimization05 : IBfInterpreter
{
private enum InstructionType
{
None = 0,
Add, Subtract, ShiftLeft, ShiftRight, Print, Read, BeginLoop, EndLoop
}
private class Instruction
{
public InstructionType Type { get; private set; }
public int Parameter { get; set; }
public Instruction(InstructionType type) : this(type, 0)
{ }
public Instruction(InstructionType type, int parameter)
{
Type = type;
Parameter = parameter;
}
}
private List<char> _legalCharacters = new List<char>() { '+', '-', '>', '<', '.', ',', '[', ']' };
public void Run(FileStream source)
{
var strippedSource = Strip(source);
var instructions = BuildInstructions(strippedSource);
Execute(instructions);
}
private void Execute(Instruction[] instructions)
{
var instructionPointer = 0;
var pointer = 0;
var memory = new byte[1024 * 1024];
while (instructionPointer < instructions.Length)
{
var instruction = instructions[instructionPointer];
switch(instruction.Type)
{
case InstructionType.Add:
memory[pointer] += (byte)instruction.Parameter;
break;
case InstructionType.Subtract:
memory[pointer] -= (byte)instruction.Parameter;
break;
case InstructionType.ShiftRight:
pointer += instruction.Parameter;
break;
case InstructionType.ShiftLeft:
pointer -= instruction.Parameter;
break;
case InstructionType.Print:
Console.Write((char)memory[pointer]);
break;
case InstructionType.Read:
var newChar = Console.Read();
memory[pointer] = (byte)newChar;
break;
case InstructionType.BeginLoop:
if (memory[pointer] == 0)
{
instructionPointer = instruction.Parameter;
}
break;
case InstructionType.EndLoop:
if (memory[pointer] != 0)
{
instructionPointer = instruction.Parameter;
}
break;
}
instructionPointer++;
}
}
private Instruction[] BuildInstructions(Stream source)
{
var instructions = new List<Instruction>();
var reader = new CharReader(source);
var jumpTable = new Stack<int>();
while(reader.HasCharacters())
{
var c = reader.GetChar();
switch(c)
{
case '+':
instructions.Add(new Instruction(InstructionType.Add, CountSeries(reader)));
break;
case '-':
instructions.Add(new Instruction(InstructionType.Subtract, CountSeries(reader)));
break;
case '>':
instructions.Add(new Instruction(InstructionType.ShiftRight, CountSeries(reader)));
break;
case '<':
instructions.Add(new Instruction(InstructionType.ShiftLeft, CountSeries(reader)));
break;
case '.':
instructions.Add(new Instruction(InstructionType.Print));
break;
case ',':
instructions.Add(new Instruction(InstructionType.Read));
break;
case '[':
instructions.Add(new Instruction(InstructionType.BeginLoop));
jumpTable.Push(instructions.Count - 1);
break;
case ']':
var beginPosition = jumpTable.Pop();
var beginInstruction = instructions[beginPosition];
instructions.Add(new Instruction(InstructionType.EndLoop, beginPosition));
beginInstruction.Parameter = instructions.Count - 1;
break;
}
reader.Forward();
}
return instructions.ToArray();
}
private int CountSeries(CharReader cr)
{
var count = 0;
var currentChar = cr.GetChar();
while (cr.HasCharacters() && cr.GetChar() == currentChar)
{
count++;
cr.Forward();
}
cr.Back();
return count;
}
private Stream Strip(Stream source)
{
var newStream = new MemoryStream();
var data = source.ReadByte();
while(data != -1)
{
var readChar = (char)data;
if (_legalCharacters.Contains(readChar)) { newStream.WriteByte((byte)readChar); }
data = source.ReadByte();
}
newStream.Seek(0, SeekOrigin.Begin);
return newStream;
}
}
}