Advertisement
yakovmonarh

Интерпретатор (interpreter)

Sep 10th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7.  
  8. class Program
  9. {
  10.    static void Main(string[] args)
  11.    {
  12.     Context context = new Context();
  13.     int x = 5,
  14.     y = 8,
  15.     z = 2;
  16.    
  17.     context.SetVariable("x", 5);
  18.     context.SetVariable("y", 8);
  19.     context.SetVariable("z", 2);
  20.    
  21.     IExpression expression = new SubstractExpression(
  22.         new AddExpression(
  23.             new NumberExpression("x"), new NumberExpression("y")
  24.         ),
  25.         new NumberExpression("z")
  26.     );
  27.     int result = expression.Interpret(context);
  28.     Console.WriteLine("результат {0}", result);
  29.    
  30.     Console.ReadLine();
  31.    }
  32. }
  33.  
  34. class Context
  35. {
  36.     Dictionary<string, int> variables;
  37.    
  38.     public Context()
  39.     {
  40.         variables = new Dictionary<string, int>();
  41.     }
  42.    
  43.     public int GetVariables(string name)
  44.     {
  45.         return variables[name];
  46.     }
  47.    
  48.     public void SetVariable(string name, int value)
  49.     {
  50.         if(variables.ContainsKey(name) == true)
  51.             variables[name] = value;
  52.         else variables.Add(name, value);
  53.     }
  54. }
  55.  
  56. interface IExpression
  57. {
  58.     int Interpret(Context context);
  59. }
  60.  
  61. class NumberExpression : IExpression
  62. {
  63.     string name;
  64.    
  65.     public NumberExpression(string variableName)
  66.     {
  67.         name = variableName;
  68.     }
  69.    
  70.     public int Interpret(Context context)
  71.     {
  72.         return context.GetVariables(name);
  73.     }
  74. }
  75.  
  76. class AddExpression : IExpression
  77. {
  78.     IExpression leftExpression;
  79.     IExpression rightExpression;
  80.    
  81.     public AddExpression(IExpression left, IExpression right)
  82.     {
  83.         leftExpression = left;
  84.         rightExpression = right;
  85.     }
  86.    
  87.     public int Interpret(Context context)
  88.     {
  89.         return leftExpression.Interpret(context) + rightExpression.Interpret(context);
  90.     }
  91. }
  92.  
  93. class SubstractExpression : IExpression
  94. {
  95.     IExpression leftExpression;
  96.     IExpression rightExpression;
  97.    
  98.     public SubstractExpression(IExpression left, IExpression right)
  99.     {
  100.         leftExpression = left;
  101.         rightExpression = right;
  102.     }
  103.    
  104.     public int Interpret(Context context)
  105.     {
  106.         return leftExpression.Interpret(context) - rightExpression.Interpret(context);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement