A Smoll game engine
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

84 rindas
2.1 KiB

  1. namespace Smoll.Ex4.Commands {
  2. interface ICommand{
  3. void Perform(List<IScriptable> stack);
  4. }
  5. class PushNumberCommand : ICommand
  6. {
  7. public double self;
  8. public PushNumberCommand(double value) {
  9. self = value;
  10. }
  11. public void Perform(List<IScriptable> stack)
  12. {
  13. stack.Add(new Number(self));;
  14. }
  15. }
  16. class PushStringCommand : ICommand
  17. {
  18. public string self;
  19. public PushStringCommand(string value) {
  20. self = value;
  21. }
  22. public void Perform(List<IScriptable> stack)
  23. {
  24. stack.Add(new Commands.String(self));;
  25. }
  26. }
  27. class PushAtomCommand : ICommand
  28. {
  29. public string self;
  30. public PushAtomCommand(string value) {
  31. self = value;
  32. }
  33. public void Perform(List<IScriptable> stack)
  34. {
  35. stack.Add(new Commands.Atom(self));;
  36. }
  37. }
  38. class AddNumberCommand : ICommand
  39. {
  40. public void Perform(List<IScriptable> stack)
  41. {
  42. if(! stack.TakeLast(2).All(x => x is Ex4.Commands.Number)) {
  43. throw new Exception("Bad arguments to add as numbers");
  44. }
  45. var ret = stack.TakeLast(2).Cast<Ex4.Commands.Number>().Select(x => x.value).Sum();
  46. stack.RemoveAt(stack.Count - 1);
  47. stack.RemoveAt(stack.Count - 1);
  48. stack.Add(new Number(ret));
  49. }
  50. }
  51. class PrintCommand : ICommand
  52. {
  53. public void Perform(List<IScriptable> stack)
  54. {
  55. var ret = stack.Last();
  56. stack.RemoveAt(stack.Count - 1);
  57. System.Console.Out.Write(ret.ToString());
  58. }
  59. }
  60. class CompositeCommand : ICommand
  61. {
  62. List<ICommand> executable;
  63. public CompositeCommand(List<ICommand> executable) {
  64. this.executable = executable;
  65. }
  66. public void Perform(List<IScriptable> stack)
  67. {
  68. foreach (var item in executable)
  69. {
  70. item.Perform(stack);
  71. }
  72. }
  73. }
  74. }