Skip to content

Commit 1841230

Browse files
Separate out my script engine for further improvements
Fix some stupid bugs in the test and the actual engine.
1 parent ac5ea26 commit 1841230

6 files changed

Lines changed: 52 additions & 45 deletions

File tree

InterpreteTests/CommandBuilder.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Diagnostics;
1111
using ExtendedSystemObjects;
1212
using Interpreter;
13+
using Interpreter.ScriptEngine;
1314
using Microsoft.VisualStudio.TestTools.UnitTesting;
1415

1516
namespace InterpreteTests
@@ -45,10 +46,12 @@ public void ParseIfElseClausesSingleIfClauseReturnsOneIfElseObj()
4546
// Act
4647
var result = ConditionalExpressions.ParseIfElseClauses(input);
4748

48-
// Assert
49+
// Assert: only one IfElseObj should exist
50+
Assert.IsNotNull(result);
4951
Assert.AreEqual(1, result.Count);
5052

51-
var ifElseObj = result[0];
53+
// Grab the only object
54+
Assert.IsTrue(result.TryGetValue(0, out var ifElseObj));
5255
Assert.AreEqual(0, ifElseObj.Id);
5356
Assert.AreEqual(-1, ifElseObj.ParentId);
5457
Assert.AreEqual(0, ifElseObj.Position);
@@ -57,34 +60,34 @@ public void ParseIfElseClausesSingleIfClauseReturnsOneIfElseObj()
5760
Assert.IsFalse(ifElseObj.Nested);
5861
Assert.AreEqual("if (condition1) {com1; }", ifElseObj.Input);
5962

60-
foreach (var value in result)
61-
{
62-
Trace.WriteLine(value.ToString());
63-
}
64-
65-
66-
var commands = IrtParserCommand.BuildCommand(input);
67-
68-
Trace.WriteLine(commands.ToString());
63+
// Output debug info
64+
Trace.WriteLine(ifElseObj.ToString());
6965

66+
// Validate commands inside the IfElseObj
7067
var expectedResults = new List<(int Key, string Category, string Value)>
7168
{
72-
(0, "IF", "condition1"), (1, "COMMAND", "com1")
69+
(0, "If_Condition", "condition1"),
70+
(1, "If", "com1;")
7371
};
7472

75-
//// Assert
76-
foreach (var expected in expectedResults)
73+
foreach (var (key, cat, val) in ifElseObj.Commands)
7774
{
78-
var (key, category, value) = expected;
79-
//Assert.IsTrue(result.TryGetCategory(key, out var actualCategory));
75+
Trace.WriteLine($"Parsed Command -> Key: {key}, Category: {cat}, Value: {val}");
76+
}
8077

81-
//Assert.AreEqual(category, actualCategory, $"Category mismatch for key {key}");
82-
//Assert.IsTrue(result.TryGetValue(key, out var actualValue));
83-
//TODO error here
84-
//Assert.AreEqual(value, actualValue, $"Value mismatch for key {key}");
78+
foreach (var (key, category, value) in expectedResults)
79+
{
80+
Assert.IsTrue(ifElseObj.Commands.TryGetCategory(key, out var actualCategory),
81+
$"Missing category for key {key}");
82+
Assert.AreEqual(category, actualCategory, $"Category mismatch for key {key}");
83+
84+
Assert.IsTrue(ifElseObj.Commands.TryGetValue(key, out var actualValue),
85+
$"Missing value for key {key}");
86+
Assert.AreEqual(value, actualValue, $"Value mismatch for key {key}");
8587
}
8688
}
8789

90+
8891
/// <summary>
8992
/// Parses if else clauses single if clause returns correct object.
9093
/// </summary>
@@ -94,13 +97,13 @@ public void ParseIfElseClausesSingleIfClauseReturnsCorrectObject()
9497
const string input = "if (condition) { doSomething(); }";
9598
var result = ConditionalExpressions.ParseIfElseClauses(input);
9699

97-
//Assert.AreEqual(3, result.Count, "There should be one IfElseObj in the result.");
98-
//var obj = result[0];
99-
//Assert.IsFalse(obj.Else, "The 'Else' flag should be false for an 'if' clause.");
100-
//Assert.AreEqual(-1, obj.ParentId, "The ParentId should be -1 for a top-level 'if' clause.");
101-
//Assert.AreEqual(0, obj.Layer, "The Layer should be 0 for a top-level 'if' clause.");
102-
//Assert.AreEqual(0, obj.Position, "The Position should be 0 for a top-level 'if' clause.");
103-
//Assert.AreEqual("if (condition) { doSomething(); }", obj.Input, "The Input string should match.");
100+
Assert.AreEqual(1, result.Count, "There should be one IfElseObj in the result.");
101+
var obj = result[0];
102+
Assert.IsFalse(obj.Else, "The 'Else' flag should be false for an 'if' clause.");
103+
Assert.AreEqual(-1, obj.ParentId, "The ParentId should be -1 for a top-level 'if' clause.");
104+
Assert.AreEqual(0, obj.Layer, "The Layer should be 0 for a top-level 'if' clause.");
105+
Assert.AreEqual(0, obj.Position, "The Position should be 0 for a top-level 'if' clause.");
106+
Assert.AreEqual("if (condition) { doSomething(); }", obj.Input, "The Input string should match.");
104107
}
105108

106109
/// <summary>

Interpreter/ConditionalExpressions.cs renamed to Interpreter/ScriptEngine/ConditionalExpressions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
3-
* PROJECT: Interpreter
4-
* FILE: Interpreter/ConditionalExpressions.cs
3+
* PROJECT: Interpreter.ScriptEngine
4+
* FILE: Interpreter.ScriptEngine/ConditionalExpressions.cs
55
* PURPOSE: Handle all logical Operations of the parser
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*
@@ -28,7 +28,7 @@
2828
using ExtendedSystemObjects;
2929
using Interpreter.Resources;
3030

31-
namespace Interpreter
31+
namespace Interpreter.ScriptEngine
3232
{
3333
/// <summary>
3434
/// Create Category Dictionary and handle nested structures
@@ -71,7 +71,8 @@ private static void ProcessInput(string input, bool isElse, int parentId, int la
7171
if (!containsIf)
7272
{
7373
//position indicates current block pretty useless for now
74-
obj.Commands.Add(category, key, position.ToString()); // Add the block if it doesn't contain 'if'
74+
//obj.Commands.Add(category, key, position.ToString()); // Add the block if it doesn't contain 'if'
75+
obj.Commands.Add(category, key, value);
7576
continue;
7677
}
7778

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
3-
* PROJECT: Interpreter
4-
* FILE: IfElseClause.cs
3+
* PROJECT: Interpreter.ScriptEngine
4+
* FILE: Interpreter.ScriptEngine/IfElseClause.cs
55
* PURPOSE: Your file purpose here
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

9-
namespace Interpreter
9+
namespace Interpreter.ScriptEngine
1010
{
1111
public sealed class IfElseClause
1212
{
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
3-
* PROJECT: Interpreter
4-
* FILE: IfElseObj.cs
3+
* PROJECT: Interpreter.ScriptEngine
4+
* FILE: Interpreter.ScriptEngine/IfElseObj.cs
55
* PURPOSE: Your file purpose here
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
using ExtendedSystemObjects;
1010

11-
namespace Interpreter
11+
namespace Interpreter.ScriptEngine
1212
{
1313
internal sealed class IfElseObj
1414
{

Interpreter/IrtParserCommand.cs renamed to Interpreter/ScriptEngine/IrtParserCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
3-
* PROJECT: Interpreter
4-
* FILE: IrtParserCommand.cs
3+
* PROJECT: Interpreter.ScriptEngine
4+
* FILE: Interpreter.ScriptEngine/IrtParserCommand.cs
55
* PURPOSE: Your file purpose here
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
@@ -10,7 +10,7 @@
1010
using ExtendedSystemObjects;
1111
using Interpreter.Resources;
1212

13-
namespace Interpreter
13+
namespace Interpreter.ScriptEngine
1414
{
1515
internal static class IrtParserCommand
1616
{

Interpreter/IrtParserIfElse.cs renamed to Interpreter/ScriptEngine/IrtParserIfElse.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
3-
* PROJECT: Interpreter
4-
* FILE: IrtParserIfElse.cs
5-
* PURPOSE: Your file purpose here
3+
* PROJECT: Interpreter.ScriptEngine
4+
* FILE: Interpreter.ScriptEngine/IrtParserIfElse.cs
5+
* PURPOSE: Generate a Category for each command, here we parse if else for each nesting we create
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
using System;
1010
using System.Collections.Generic;
1111
using System.Linq;
1212

13-
namespace Interpreter
13+
namespace Interpreter.ScriptEngine
1414
{
1515
/// <summary>
16-
/// Basic if else Parser
16+
/// Basic if else Parser
1717
/// </summary>
1818
internal static class IrtParserIfElse
1919
{
20+
/// <summary>
21+
/// The identifier counter
22+
/// </summary>
2023
private static int _idCounter;
2124

2225
/// <summary>

0 commit comments

Comments
 (0)