Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] 68 unit functions #77

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions SudoScript.Core.Test/PluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ internal sealed class PluginTests
[Test]
public void CreateEmptyRuleTest()
{
IRule rule = Plugins.CreateRule("TestRule1");
IRule rule = new SymbolTable().GetRules("TestRule1");
Assert.IsInstanceOf<TestRule1>(rule);
}

[Test]
public void CreateRuleWithCellAndDigitTest()
{
IRule rule = Plugins.CreateRule("TestRule2", new CellReference(1, 2), 3);
IRule rule = new SymbolTable().GetRules("TestRule2", new CellReference(1, 2), 3);
Assert.IsInstanceOf<TestRule2>(rule);
TestRule2 rule2 = (TestRule2)rule;
Assert.That(rule2.Cell.X, Is.EqualTo(1));
Expand All @@ -93,7 +93,7 @@ public void CreateRuleWithCellAndDigitTest()
[Test]
public void CreateRuleWithDigitAndCellTest()
{
IRule rule = Plugins.CreateRule("TestRule2", 3, new CellReference(1, 2));
IRule rule = new SymbolTable().GetRules("TestRule2", 3, new CellReference(1, 2));
Assert.IsInstanceOf<TestRule2>(rule);
TestRule2 rule2 = (TestRule2)rule;
Assert.That(rule2.Cell.X, Is.EqualTo(1));
Expand All @@ -104,20 +104,20 @@ public void CreateRuleWithDigitAndCellTest()
[Test]
public void InvalidOperatorsCreateRule()
{
Assert.Throws<Exception>(() => Plugins.CreateRule("TestRule2", 1, 2));
Assert.Throws<Exception>(() => new SymbolTable().GetRules("TestRule2", 1, 2));
}

[Test]
public void CreateEmptyUnitTest()
{
Unit rule = Plugins.CreateUnit("TestUnit1", new SymbolTable()).First();
Unit rule = new SymbolTable().GetUnits("TestUnit1").First();
Assert.IsInstanceOf<TestUnit1>(rule);
}

[Test]
public void CreateUnitWithCellAndDigitTest()
{
Unit unit = Plugins.CreateUnit("TestUnit2", new SymbolTable(), new CellReference(1, 2), 3).First();
Unit unit = new SymbolTable().GetUnits("TestUnit2", new CellReference(1, 2), 3).First();
Assert.IsInstanceOf<TestUnit2>(unit);
TestUnit2 unit2 = (TestUnit2)unit;
Assert.That(unit2.Cell.X, Is.EqualTo(1));
Expand All @@ -128,7 +128,7 @@ public void CreateUnitWithCellAndDigitTest()
[Test]
public void CreateUnitWithDigitAndCellTest()
{
Unit unit = Plugins.CreateUnit("TestUnit2", new SymbolTable(), 3, new CellReference(1, 2)).First();
Unit unit = new SymbolTable().GetUnits("TestUnit2", 3, new CellReference(1, 2)).First();
Assert.IsInstanceOf<TestUnit2>(unit);
TestUnit2 unit2 = (TestUnit2)unit;
Assert.That(unit2.Cell.X, Is.EqualTo(1));
Expand All @@ -139,6 +139,6 @@ public void CreateUnitWithDigitAndCellTest()
[Test]
public void InvalidOperatorsCreateUnit()
{
Assert.Throws<Exception>(() => Plugins.CreateRule("TestUnit2", 1, 2));
Assert.Throws<Exception>(() => new SymbolTable().GetUnits("TestUnit2", 1, 2));
}
}
56 changes: 44 additions & 12 deletions SudoScript.Core/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public static class Generator
{
static Generator()
{
Plugins.AddUnitFunction("Union", Union);
// Plugins.AddUnitFunction("Union", Union);
}

private static IEnumerable<Unit> Union(SymbolTable table, object[] args)
private static IEnumerable<Unit> Union(object[] args)
{
//Creates a new instance of unit class.
Unit unit = new Unit();
Expand All @@ -24,7 +24,7 @@ private static IEnumerable<Unit> Union(SymbolTable table, object[] args)
}
else
{
throw new Exception("Union only accepts Cells as arguments.");
throw new ArgumentException();
}
}
yield return unit;
Expand All @@ -48,9 +48,10 @@ public static Board GenerateBoard(ProgramNode node, SymbolTable symbolTable)

// Takes UnitNode, SymbolTable as inputs and a new function from the Plugin class.
private static void AddUnitFunction(UnitNode node, SymbolTable symbolTable)
{
{
SymbolTable.ArgType[] args = ArgTypeFromParameterNodes(node.Parameters);
//register the new Unit function.
Plugins.AddUnitFunction(node.NameToken!.Match, (symbolTable, args) =>
symbolTable.AddUnitFunction(node.NameToken!.Match, (args) =>
{
//Create a new SymbolTable
SymbolTable table = new SymbolTable(symbolTable);
Expand All @@ -73,11 +74,33 @@ private static void AddUnitFunction(UnitNode node, SymbolTable symbolTable)
}
else
{
throw new Exception($"Invalid parameter type.");
throw new ArgumentException();
}
}
return GetUnitFromStatements(node, symbolTable);
});
return GetUnitFromStatements(node, table);
}, args);
}

private static SymbolTable.ArgType[] ArgTypeFromParameterNodes(IReadOnlyList<ParameterNode> parameters)
{
SymbolTable.ArgType[] args = new SymbolTable.ArgType[parameters.Count];
for (int i = 0; i < parameters.Count; i++)
{
if (parameters[i] is ParameterCellNode)
{
args[i] = SymbolTable.ArgType.Cell;
}
else if (parameters[i] is ParameterIdentifierNode)
{
args[i] = SymbolTable.ArgType.Digit;
}
else
{
throw new ArgumentOutOfRangeException();
}
}

return args;
}

private static IEnumerable<Unit> GetUnitFromStatements(UnitNode node, SymbolTable symbolTable)
Expand Down Expand Up @@ -136,7 +159,7 @@ private static IEnumerable<IRule> GetRules(RulesNode node, SymbolTable symbolTab

foreach(List<object> arguments in argumentCombinations)
{
yield return Plugins.CreateRule(child.Name.Match, arguments.ToArray());
yield return symbolTable.GetRules(child.Name.Match, arguments.ToArray());
}
}
}
Expand Down Expand Up @@ -222,8 +245,17 @@ private static IEnumerable<Unit> GetCellsAndUnitsFromFunction(FunctionCallNode n
List<List<object>> argumentCombinations = ArgumentToArgumentCombinations(node.Arguments, symbolTable);
foreach (List<object> arguments in argumentCombinations)
{
//Use Plugin to create a unit from the funtion call and arguments.
IEnumerable<Unit> units = Plugins.CreateUnit(node.Name.Match, symbolTable, arguments.ToArray());
//Use SymbolTable to create a unit from the funtion call and arguments.
IEnumerable<Unit> units;
if (node.Name.Match == "Union") // TODO: Properly support array arguments.
{
units = Union(arguments.ToArray());
}
else
{
units = symbolTable.GetUnits(node.Name.Match, arguments.ToArray());
}

foreach (Unit unit in units)
{
//Add all cells in the unit to the symbolTable.
Expand Down Expand Up @@ -352,7 +384,7 @@ private static List<int> CalculateUnaryNode(UnaryNode node, SymbolTable symbolTa

private static List<int> IdentifierRetriever(IdentifierNode node, SymbolTable symbolTable)
{
throw new NotImplementedException();
return new List<int>() { symbolTable.GetDigit(node.NameToken.Match) };
}

//Method takes in two lists of T2 and a function for two arguments to return T1 value.
Expand Down
41 changes: 33 additions & 8 deletions SudoScript.Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private static List<ArgumentNode> ParseArguments(TokenStream stream)
}

// If more arguments follow, parse them recursively.
if (stream.Peek(false, out Token? identifier) && (identifier.Type == TokenType.Identifier || identifier.Type == TokenType.LeftParenthesis))
if (stream.Peek(false, out Token? identifier) && (identifier.Type == TokenType.Identifier || identifier.Type == TokenType.LeftParenthesis || identifier.Type == TokenType.Number))
{
arguments.AddRange(ParseArguments(stream));
}
Expand Down Expand Up @@ -299,12 +299,37 @@ private static ArgumentNode ParseElement(TokenStream stream)

private static ExpressionNode ParseExpression(TokenStream stream)
{
if(stream.Expect(TokenType.Number, out Token? value)) //
{ // Temporary Expression Solution
return new ValueNode(value); //
} //
stream.Peek(true, out Token? token);

throw new NotImplementedException();
// Parses Element depending on the type of token.
switch (token?.Type) {
case TokenType.Identifier:
if (stream.Expect(TokenType.Identifier, out Token? identifierToken))
{
return new IdentifierNode(identifierToken);
}

break;
// Minus, plus and number are all parsed as valuenodes
case TokenType.Minus:
case TokenType.Plus:
case TokenType.Number:
if(stream.Expect(TokenType.Number, out Token? valueToken))
{
return new ValueNode(valueToken);
}

throw new NullReferenceException();
// Left- and rightbrackets indicate start of a range.
case TokenType.LeftBracket:
case TokenType.RightBracket:
return ParseRange(stream);
// Leftparenthesis indicates start of expression wrapped in parentheses.
case TokenType.LeftParenthesis:
return ParseExpression(stream);
}

throw new Exception("Argument not identified");
}

private static ExpressionNode ParseExpression(IEnumerable<Token> tokens)
Expand Down Expand Up @@ -509,7 +534,7 @@ private static CellNode ParseCell(TokenStream stream)
// Parses LeftParenthesis.
if(stream.Expect(TokenType.LeftParenthesis, out Token? startToken))
{
if (stream.Peek(true, out Token? identifier) && identifier.Type != TokenType.Number)
if (stream.Peek(true, out Token? identifier) && identifier.Type != TokenType.Number && identifier.Type != TokenType.Identifier)
{
throw new Exception("Expected identifier");
}
Expand All @@ -523,7 +548,7 @@ private static CellNode ParseCell(TokenStream stream)
throw new Exception(", expected");
}

if (stream.Peek(true, out identifier) && identifier.Type != TokenType.Number)
if (stream.Peek(true, out identifier) && identifier.Type != TokenType.Number && identifier.Type != TokenType.Identifier)
{
throw new Exception("Expected identifier");
}
Expand Down
143 changes: 0 additions & 143 deletions SudoScript.Core/Plugins.cs

This file was deleted.

Loading