-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data types for variables / function references
- Loading branch information
1 parent
0678d24
commit 6880cce
Showing
10 changed files
with
345 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace EOBot.Interpreter.Variables | ||
{ | ||
public interface IIdentifiable | ||
{ | ||
string StringValue { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |