Fixed keyboard input & instruction pointer display

This commit is contained in:
RogueAI42 2017-06-19 17:30:55 +10:00
parent b453978afc
commit 40b4c29b96
2 changed files with 61 additions and 52 deletions

View file

@ -22,6 +22,7 @@ namespace ShiftOS.WinForms.Applications
{
private TextBox box;
private KeysConverter kc;
public KeyPressEventHandler handler;
public TextBoxStream(TextBox mybox)
{
kc = new KeysConverter();
@ -55,7 +56,7 @@ namespace ShiftOS.WinForms.Applications
{
object lck = new object();
int ptr = offset;
KeyPressEventHandler handler = (o, a) =>
handler = (o, a) =>
{
lock (lck)
{
@ -98,6 +99,7 @@ namespace ShiftOS.WinForms.Applications
private static string[] DefaultMem;
private BFInterpreter Interpreter;
private Thread InterpreterThread;
private TextBoxStream consolestr;
private void DoLayout()
{
memlist.Left = 0;
@ -121,7 +123,8 @@ namespace ShiftOS.WinForms.Applications
for (ushort i = 0; i < 30000; i++)
DefaultMem[i] = "0";
memlist.Items.AddRange(DefaultMem);
Interpreter = new BFInterpreter(new TextBoxStream(consoleout), this);
consolestr = new TextBoxStream(consoleout);
Interpreter = new BFInterpreter(consolestr, this);
}
public void IPtrMoved(int newval)
@ -212,6 +215,7 @@ namespace ShiftOS.WinForms.Applications
InterpreterThread.Abort();
}
catch { }
consoleout.KeyPress -= consolestr.handler;
}
private void reset_Click(object sender, EventArgs e)

View file

@ -62,60 +62,65 @@ namespace ShiftOS.Engine
Reset();
lst = listener;
}
public void Execute(string program)
public void Execute(string program, int offset = 0)
{
int c = 0;
lock (lck)
while (c < program.Length)
switch (program[c++])
while (c < program.Length)
{
case '<':
ptr--;
if (lst != null)
lst.PointerMoved(ptr);
break;
case '>':
ptr++;
if (lst != null)
lst.PointerMoved(ptr);
break;
case '+':
mem[ptr]++;
if (lst != null)
lst.MemChanged(ptr, mem[ptr]);
break;
case '-':
mem[ptr]--;
if (lst != null)
lst.MemChanged(ptr, mem[ptr]);
break;
case '.':
str.WriteByte(mem[ptr]);
break;
case ',':
mem[ptr] = (byte)str.ReadByte();
if (lst != null)
lst.MemChanged(ptr, mem[ptr]);
break;
case '[':
int b;
int oldc = c;
for (b = 1; b != 0 && c < program.Length; c++)
{
if (program[c] == '[')
b++;
else if (program[c] == ']')
b--;
}
if (b == 0)
{
string block = program.Substring(oldc, c - oldc - 1);
while (mem[ptr] != 0)
Execute(block);
}
break;
case ']':
throw new Exception("Unbalanced brackets");
switch (program[c++])
{
case '<':
ptr--;
if (lst != null)
lst.PointerMoved(ptr);
break;
case '>':
ptr++;
if (lst != null)
lst.PointerMoved(ptr);
break;
case '+':
mem[ptr]++;
if (lst != null)
lst.MemChanged(ptr, mem[ptr]);
break;
case '-':
mem[ptr]--;
if (lst != null)
lst.MemChanged(ptr, mem[ptr]);
break;
case '.':
str.WriteByte(mem[ptr]);
break;
case ',':
mem[ptr] = (byte)str.ReadByte();
if (lst != null)
lst.MemChanged(ptr, mem[ptr]);
break;
case '[':
int b;
int oldc = c;
for (b = 1; b != 0 && c < program.Length; c++)
{
if (program[c] == '[')
b++;
else if (program[c] == ']')
b--;
}
if (b == 0)
{
string block = program.Substring(oldc, c - oldc - 1);
while (mem[ptr] != 0)
Execute(block, offset + oldc);
}
break;
case ']':
throw new Exception("Unbalanced brackets");
}
if (lst != null)
lst.IPtrMoved(offset + c);
}
}
public void Execute()