|
|
namespace Smoll.Ex4.Commands {
|
|
interface ICommand{
|
|
void Perform(List<IScriptable> stack);
|
|
}
|
|
|
|
class PushNumberCommand : ICommand
|
|
{
|
|
public double self;
|
|
public PushNumberCommand(double value) {
|
|
self = value;
|
|
}
|
|
|
|
public void Perform(List<IScriptable> stack)
|
|
{
|
|
stack.Add(new Number(self));;
|
|
}
|
|
}
|
|
|
|
class PushStringCommand : ICommand
|
|
{
|
|
public string self;
|
|
public PushStringCommand(string value) {
|
|
self = value;
|
|
}
|
|
|
|
public void Perform(List<IScriptable> stack)
|
|
{
|
|
stack.Add(new Commands.String(self));;
|
|
}
|
|
}
|
|
|
|
class PushAtomCommand : ICommand
|
|
{
|
|
public string self;
|
|
public PushAtomCommand(string value) {
|
|
self = value;
|
|
}
|
|
|
|
public void Perform(List<IScriptable> stack)
|
|
{
|
|
stack.Add(new Commands.Atom(self));;
|
|
}
|
|
}
|
|
|
|
class AddNumberCommand : ICommand
|
|
{
|
|
public void Perform(List<IScriptable> stack)
|
|
{
|
|
if(! stack.TakeLast(2).All(x => x is Ex4.Commands.Number)) {
|
|
throw new Exception("Bad arguments to add as numbers");
|
|
}
|
|
var ret = stack.TakeLast(2).Cast<Ex4.Commands.Number>().Select(x => x.value).Sum();
|
|
stack.RemoveAt(stack.Count - 1);
|
|
stack.RemoveAt(stack.Count - 1);
|
|
stack.Add(new Number(ret));
|
|
}
|
|
}
|
|
|
|
class PrintCommand : ICommand
|
|
{
|
|
public void Perform(List<IScriptable> stack)
|
|
{
|
|
var ret = stack.Last();
|
|
stack.RemoveAt(stack.Count - 1);
|
|
System.Console.Out.Write(ret.ToString());
|
|
}
|
|
}
|
|
|
|
class CompositeCommand : ICommand
|
|
{
|
|
List<ICommand> executable;
|
|
public CompositeCommand(List<ICommand> executable) {
|
|
this.executable = executable;
|
|
}
|
|
|
|
public void Perform(List<IScriptable> stack)
|
|
{
|
|
foreach (var item in executable)
|
|
{
|
|
item.Perform(stack);
|
|
}
|
|
}
|
|
}
|
|
}
|