Skip to content

Commit

Permalink
simplified all compound statements
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjbreisch committed May 22, 2022
1 parent e2c27ba commit ab41588
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
10 changes: 5 additions & 5 deletions Trs80.Level1Basic.VirtualMachine/Interpreter/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ public Void VisitClsStatement(Cls statement)

public Void VisitCompoundStatement(Compound statement)
{
_machine.RunStatementList(statement.Statements[0], this, true);

_machine.RunCompoundStatement(statement.Statements, this);
return null!;
}

Expand All @@ -282,7 +282,7 @@ public void RunProgram(IStatement statement, bool initialize)
if (initialize)
_machine.Initialize();

_machine.RunStatementList(statement, this, false);
_machine.RunStatementList(statement, this);
_trs80.WriteLine();
_trs80.WriteLine("READY");
}
Expand Down Expand Up @@ -375,7 +375,7 @@ public Void VisitIfStatement(If statement)
{
if (!IsTruthy(Evaluate(statement.Condition))) return null!;

_machine.RunThenBranch(statement.ThenBranch, this);
_machine.RunCompoundStatement(statement.ThenBranch, this);

return null!;
}
Expand Down Expand Up @@ -595,7 +595,7 @@ private void ExecuteGosub(IStatement jumpToStatement, IStatement resumeStatement
{
try
{
_machine.RunStatementList(jumpToStatement, this, false);
_machine.RunStatementList(jumpToStatement, this);
}
catch (ReturnFromGosub)
{
Expand Down
4 changes: 2 additions & 2 deletions Trs80.Level1Basic.VirtualMachine/Machine/IMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public interface IMachine
void SaveProgram(string path);
void LoadProgram(string path);
void NewProgram();
void RunStatementList(IStatement statement, IInterpreter interpreter, bool breakOnLineChange);
void RunThenBranch(CompoundStatementList thenBranch, IInterpreter interpreter);
void RunStatementList(IStatement statement, IInterpreter interpreter);
void RunCompoundStatement(CompoundStatementList compound, IInterpreter interpreter);
void SetNextStatement(IStatement statement);
void HaltRun();
IStatement GetStatementByLineNumber(int lineNumber);
Expand Down
12 changes: 5 additions & 7 deletions Trs80.Level1Basic.VirtualMachine/Machine/Machine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,30 @@ public void NewProgram()
Initialize();
}

public void RunStatementList(IStatement statement, IInterpreter interpreter, bool breakOnLineChange)
public void RunStatementList(IStatement statement, IInterpreter interpreter)
{
ExecutionHalted = false;
if (statement == null) return;
int lineNumber = statement.LineNumber;

while (statement != null && !ExecutionHalted)
{
_nextStatement = GetNextStatement(statement);
interpreter.Execute(statement);
statement = _nextStatement;
if (breakOnLineChange && statement?.LineNumber != lineNumber) break;
}
}

public void RunThenBranch(CompoundStatementList thenBranch, IInterpreter interpreter)
public void RunCompoundStatement(CompoundStatementList compound, IInterpreter interpreter)
{
IStatement nextStatement = _nextStatement;

ExecutionHalted = false;
if (thenBranch == null) return;
IStatement statement = thenBranch[0];
if (compound == null) return;
IStatement statement = compound[0];

while (statement != null && !ExecutionHalted)
{
_nextStatement = GetNextStatement(statement);
_nextStatement = statement.Next;
interpreter.Execute(statement);
if (statement is IListStatementDecorator decorated && decorated.BaseType() == typeof(Goto)) return;
statement = _nextStatement;
Expand Down

0 comments on commit ab41588

Please sign in to comment.