namespace Smoll.Ex4.Commands { interface ICommand{ void Perform(List stack); } class PushNumberCommand : ICommand { public double self; public PushNumberCommand(double value) { self = value; } public void Perform(List stack) { stack.Add(new Number(self));; } } class PushStringCommand : ICommand { public string self; public PushStringCommand(string value) { self = value; } public void Perform(List stack) { stack.Add(new Commands.String(self));; } } class PushAtomCommand : ICommand { public string self; public PushAtomCommand(string value) { self = value; } public void Perform(List stack) { stack.Add(new Commands.Atom(self));; } } class AddNumberCommand : ICommand { public void Perform(List 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().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 stack) { var ret = stack.Last(); stack.RemoveAt(stack.Count - 1); System.Console.Out.Write(ret.ToString()); } } class CompositeCommand : ICommand { List executable; public CompositeCommand(List executable) { this.executable = executable; } public void Perform(List stack) { foreach (var item in executable) { item.Perform(stack); } } } }