Skip to content

Commit

Permalink
Add data types for variables / function references
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Jan 22, 2022
1 parent 0678d24 commit 6880cce
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 5 deletions.
12 changes: 9 additions & 3 deletions EOBot/EOBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
<Compile Include="Interpreter\BotToken.cs" />
<Compile Include="Interpreter\BotTokenParser.cs" />
<Compile Include="Interpreter\BotTokenType.cs" />
<Compile Include="Interpreter\Variables\VoidFunction.cs" />
<Compile Include="Interpreter\Variables\Function.cs" />
<Compile Include="Interpreter\Variables\ICallable.cs" />
<Compile Include="Interpreter\Variables\IIdentifiable.cs" />
<Compile Include="Interpreter\Variables\IntVariable.cs" />
<Compile Include="Interpreter\Variables\IVariable.cs" />
<Compile Include="Interpreter\Variables\LabelIdentifier.cs" />
<Compile Include="Interpreter\Variables\StringVariable.cs" />
<Compile Include="NamesList.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -126,9 +134,7 @@
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Interpreter\States\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 4 additions & 2 deletions EOBot/Interpreter/BotInterpreter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using EOBot.Interpreter.Variables;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -7,7 +8,7 @@ namespace EOBot.Interpreter
{
public class BotInterpreter
{
private readonly Dictionary<string, string> _symbolTable;
private readonly Dictionary<string, IIdentifiable> _symbolTable;
private readonly BotTokenParser _parser;

public BotInterpreter(string filePath)
Expand All @@ -18,6 +19,7 @@ public BotInterpreter(string filePath)
public BotInterpreter(StreamReader inputStream)
{
_parser = new BotTokenParser(inputStream);
_symbolTable = new Dictionary<string, IIdentifiable>();
}

public List<BotToken> Parse()
Expand Down
130 changes: 130 additions & 0 deletions EOBot/Interpreter/Variables/Function.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;

namespace EOBot.Interpreter.Variables
{
public class Function<T> : ICallable<T>
{
private readonly Func<T> _referenceFunc;

public string StringValue { get; }

public Function(string functionName, Func<T> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public T Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 0)
throw new ArgumentException("Calling parameterless function with parameters", nameof(parameters));

return _referenceFunc();
}
}

public class FunctionRef<TParam1, T> : ICallable<T>
{
private readonly Func<TParam1, T> _referenceFunc;

public string StringValue { get; }

public FunctionRef(string functionName, Func<TParam1, T> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public T Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 1)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

return _referenceFunc((TParam1)parameters[0]);
}
}

public class FunctionRef<TParam1, TParam2, T> : ICallable<T>
{
private readonly Func<TParam1, TParam2, T> _referenceFunc;

public string StringValue { get; }

public FunctionRef(string functionName, Func<TParam1, TParam2, T> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public T Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 2)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

return _referenceFunc((TParam1)parameters[0], (TParam2)parameters[1]);
}
}

public class FunctionRef<TParam1, TParam2, TParam3, T> : ICallable<T>
{
private readonly Func<TParam1, TParam2, TParam3, T> _referenceFunc;

public string StringValue { get; }

public FunctionRef(string functionName, Func<TParam1, TParam2, TParam3, T> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public T Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 0)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

return _referenceFunc((TParam1)parameters[0], (TParam2)parameters[1], (TParam3)parameters[2]);
}
}

public class FunctionRef<TParam1, TParam2, TParam3, TParam4, T> : ICallable<T>
{
private readonly Func<TParam1, TParam2, TParam3, TParam4, T> _referenceFunc;

public string StringValue { get; }

public FunctionRef(string functionName, Func<TParam1, TParam2, TParam3, TParam4, T> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public T Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 0)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

return _referenceFunc((TParam1)parameters[0], (TParam2)parameters[1], (TParam3)parameters[2], (TParam4)parameters[3]);
}
}

public class FunctionRef<TParam1, TParam2, TParam3, TParam4, TParam5, T> : ICallable<T>
{
private readonly Func<TParam1, TParam2, TParam3, TParam4, TParam5, T> _referenceFunc;

public string StringValue { get; }

public FunctionRef(string functionName, Func<TParam1, TParam2, TParam3, TParam4, TParam5, T> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public T Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 0)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

return _referenceFunc((TParam1)parameters[0], (TParam2)parameters[1], (TParam3)parameters[2], (TParam4)parameters[3], (TParam5)parameters[4]);
}
}
}
12 changes: 12 additions & 0 deletions EOBot/Interpreter/Variables/ICallable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace EOBot.Interpreter.Variables
{
public interface ICallable :IIdentifiable
{
void Call(params IIdentifiable[] parameters);
}

public interface ICallable<T> : IIdentifiable
{
T Call(params IIdentifiable[] parameters);
}
}
7 changes: 7 additions & 0 deletions EOBot/Interpreter/Variables/IIdentifiable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EOBot.Interpreter.Variables
{
public interface IIdentifiable
{
string StringValue { get; }
}
}
9 changes: 9 additions & 0 deletions EOBot/Interpreter/Variables/IVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EOBot.Interpreter.Variables
{
public interface IVariable<T> : IIdentifiable
{
T Value { get; }

IVariable<T> WithNewValue(T value);
}
}
18 changes: 18 additions & 0 deletions EOBot/Interpreter/Variables/IntVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace EOBot.Interpreter.Variables
{
public class IntVariable : IVariable<int>
{
public int Value { get; }

public IntVariable(int value) => Value = value;

public string StringValue => Value.ToString();

public IVariable<int> WithNewValue(int value) => new IntVariable(value);

public static explicit operator int(IntVariable input)
{
return input.Value;
}
}
}
11 changes: 11 additions & 0 deletions EOBot/Interpreter/Variables/LabelIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace EOBot.Interpreter.Variables
{
public class LabelIdentifier : IIdentifiable
{
private readonly string _value;

public LabelIdentifier(string value) => _value = value;

public string StringValue => _value;
}
}
15 changes: 15 additions & 0 deletions EOBot/Interpreter/Variables/StringVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace EOBot.Interpreter.Variables
{
public class StringVariable : IVariable<string>
{
public string Value { get; }

public StringVariable(string value) => Value = value;

public string StringValue => Value;

public IVariable<string> WithNewValue(string value) => new StringVariable(value);

public static explicit operator string(StringVariable variable) => variable.Value;
}
}
130 changes: 130 additions & 0 deletions EOBot/Interpreter/Variables/VoidFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;

namespace EOBot.Interpreter.Variables
{
public class VoidFunction : ICallable
{
private readonly Action _referenceFunc;

public string StringValue { get; }

public VoidFunction(string functionName, Action referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public void Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 0)
throw new ArgumentException("Calling parameterless function with parameters", nameof(parameters));

_referenceFunc();
}
}

public class VoidFunctionRef<TParam1> : ICallable
{
private readonly Action<TParam1> _referenceFunc;

public string StringValue { get; }

public VoidFunctionRef(string functionName, Action<TParam1> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public void Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 1)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

_referenceFunc((TParam1)parameters[0]);
}
}

public class VoidFunctionRef<TParam1, TParam2> : ICallable
{
private readonly Action<TParam1, TParam2> _referenceFunc;

public string StringValue { get; }

public VoidFunctionRef(string functionName, Action<TParam1, TParam2> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public void Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 2)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

_referenceFunc((TParam1)parameters[0], (TParam2)parameters[1]);
}
}

public class VoidFunctionRef<TParam1, TParam2, TParam3> : ICallable
{
private readonly Action<TParam1, TParam2, TParam3> _referenceFunc;

public string StringValue { get; }

public VoidFunctionRef(string functionName, Action<TParam1, TParam2, TParam3> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public void Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 0)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

_referenceFunc((TParam1)parameters[0], (TParam2)parameters[1], (TParam3)parameters[2]);
}
}

public class VoidFunctionRef<TParam1, TParam2, TParam3, TParam4> : ICallable
{
private readonly Action<TParam1, TParam2, TParam3, TParam4> _referenceFunc;

public string StringValue { get; }

public VoidFunctionRef(string functionName, Action<TParam1, TParam2, TParam3, TParam4> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public void Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 0)
throw new ArgumentException("Calling function with wrong number of parameters", nameof(parameters));

_referenceFunc((TParam1)parameters[0], (TParam2)parameters[1], (TParam3)parameters[2], (TParam4)parameters[3]);
}
}

public class VoidFunctionRef<TParam1, TParam2, TParam3, TParam4, TParam5> : ICallable
{
private readonly Action<TParam1, TParam2, TParam3, TParam4, TParam5> _referenceFunc;

public string StringValue { get; }

public VoidFunctionRef(string functionName, Action<TParam1, TParam2, TParam3, TParam4, TParam5> referenceFunc)
{
StringValue = functionName;
_referenceFunc = referenceFunc;
}

public void Call(params IIdentifiable[] parameters)
{
if (parameters.Length != 0)
throw new ArgumentException("Calling VoidVoidFunction with wrong number of parameters", nameof(parameters));

_referenceFunc((TParam1)parameters[0], (TParam2)parameters[1], (TParam3)parameters[2], (TParam4)parameters[3], (TParam5)parameters[4]);
}
}
}

0 comments on commit 6880cce

Please sign in to comment.