Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Create label lists before execution and also track length of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
worksofliam committed Sep 9, 2019
1 parent 5f92501 commit 60b5f2c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
20 changes: 19 additions & 1 deletion NetRPG/Runtime/Procedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Procedure
private Dictionary<string, bool> _Parameters; //Name, passByValue
private Dictionary<string, DataSet> _DataSets;

private Dictionary<string, int> Labels;

private bool _HasEntrypoint;

public Procedure(string Name, Types ReturnType = Types.Void)
Expand All @@ -24,6 +26,8 @@ public Procedure(string Name, Types ReturnType = Types.Void)
_Parameters = new Dictionary<string, bool>();
_DataSets = new Dictionary<string, DataSet>();
_HasEntrypoint = false;

Labels = null;
}

public void AddDataSet(DataSet var)
Expand All @@ -39,7 +43,6 @@ public void AddInstruction(Instructions Instruction, string Value = "")
_HasEntrypoint = true;
}

//TODO: Get variables
public Instruction[] GetInstructions() => _Instructions.ToArray();
public string[] GetDataSetList() => _DataSets.Keys.ToArray();
public DataSet GetDataSet(string Name)
Expand All @@ -60,5 +63,20 @@ public DataSet GetDataSet(string Name)

public string GetName() => _Name;
public bool HasEntrypoint => _HasEntrypoint;

public void CalculateLabels() {
Labels = new Dictionary<string, int>();

for(int i = 0; i < _Instructions.Count(); i++)
if (_Instructions[i]._Instruction == Instructions.LABEL)
Labels.Add(_Instructions[i]._Value, i);
}

public int GetLabel(string Label) {
if (!Labels.ContainsKey(Label))
Error.ThrowRuntimeError("GetLabel", "Label '" + Label + " does not exist.");

return Labels[Label];
}
}
}
28 changes: 13 additions & 15 deletions NetRPG/Runtime/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void AddModule(Module module)

_Procedures.Add(proc.GetName(), proc);
if (proc.HasEntrypoint) _EntryProcedure = proc.GetName();

proc.CalculateLabels();
}

//Handle adding references to internal functions
Expand Down Expand Up @@ -142,30 +144,31 @@ private object Execute(string Name, object[] Parms = null)

Dictionary<string, int> Labels = new Dictionary<string, int>();
Dictionary<string, DataValue> LocalVariables = new Dictionary<string, DataValue>();
Instruction[] instructions = _Procedures[Name].GetInstructions();
Procedure currentProcedure = _Procedures[Name];
Instruction[] instructions = currentProcedure.GetInstructions();

string ModuleName = _Procedures[Name]._ParentModule;
string ModuleName = currentProcedure._ParentModule;

CallStack.Add(Name);

//Initialise local variables
foreach (string local in _Procedures[Name].GetDataSetList())
foreach (string local in currentProcedure.GetDataSetList())
{
set = _Procedures[Name].GetDataSet(local).ToDataValue();
set = currentProcedure.GetDataSet(local).ToDataValue();
LocalVariables.Add(set.GetName(), set);
LocalVariables[set.GetName()].DoInitialValue();
}

if (Parms != null)
{
string[] Parameters = _Procedures[Name].GetParameterNames();
string[] Parameters = currentProcedure.GetParameterNames();
for (int x = 0; x < Parameters.Length; x++)
{
if (x < Parms.Length)
{
if (Parms[x] is DataValue)
{
if (_Procedures[Name].ParameterIsValue(Parameters[x]))
if (currentProcedure.ParameterIsValue(Parameters[x]))
{
LocalVariables[Parameters[x]].SetEntire((Parms[x] as DataValue).GetEntire());
}
Expand All @@ -184,11 +187,6 @@ private object Execute(string Name, object[] Parms = null)
}
}

//TODO: Do this only once and not everytime a procedure is called.
for(int i = 0; i < instructions.Count(); i++)
if (instructions[i]._Instruction == Instructions.LABEL)
Labels.Add(instructions[i]._Value, i);

for (int ip = 0; ip < instructions.Count(); ip++)
{
switch (instructions[ip]._Instruction)
Expand All @@ -212,21 +210,21 @@ private object Execute(string Name, object[] Parms = null)
break;

case Instructions.BR:
ip = Labels[instructions[ip]._Value];
ip = currentProcedure.GetLabel(instructions[ip]._Value);
break;

case Instructions.BRFALSE:
Values[0] = Stack[Stack.Count - 1];
Stack.RemoveRange(Stack.Count - 1, 1);
if ((bool) Operate(Instructions.EQUAL, Values[0], false))
ip = Labels[instructions[ip]._Value];
ip = currentProcedure.GetLabel(instructions[ip]._Value);
break;

case Instructions.BRTRUE:
Values[0] = Stack[Stack.Count - 1];
Stack.RemoveRange(Stack.Count - 1, 1);
if ((bool)Operate(Instructions.EQUAL, Values[0], true))
ip = Labels[instructions[ip]._Value];
ip = currentProcedure.GetLabel(instructions[ip]._Value);
break;

case Instructions.CALL:
Expand Down Expand Up @@ -384,7 +382,7 @@ private object Execute(string Name, object[] Parms = null)

case Instructions.RETURN:
CallStack.RemoveAt(CallStack.Count-1);
if (_Procedures[Name]._ReturnType == Types.Void)
if (currentProcedure._ReturnType == Types.Void)
return null;
else
{
Expand Down
9 changes: 8 additions & 1 deletion NetRPG/Testing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public static void RunTests(string testsStarting = "")
Reader reader;
VM vm;

DateTime start, end;
TimeSpan diff;

int run = 0, passed = 0, failed = 0;
Exception lastError = null;

Expand All @@ -160,6 +163,8 @@ public static void RunTests(string testsStarting = "")
vm = new VM(true);
run++;

start = DateTime.Now;

foreach (string file in files.Split(',')) {
Console.Write("Testing " + file.PadRight(35) + " ... ");
SourcePath = Path.Combine(Environment.CurrentDirectory, "RPGCode", file);
Expand Down Expand Up @@ -193,11 +198,13 @@ public static void RunTests(string testsStarting = "")
result = null;
}
}
end = DateTime.Now;
diff = (end - start);

if (result != null && result == TestCases[files])
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("successful.");
Console.WriteLine("successful (" + diff.ToString(@"mm\:ss\:f\:ff") + ").");
Console.ForegroundColor = originalColor;

passed++;
Expand Down

0 comments on commit 60b5f2c

Please sign in to comment.