Exemplo n.º 1
0
 public static void Main(string[] args)
 {
     var compiler = new InstructionParser() {
         setColorAction = arg => Console.WriteLine("Setting Color to {0}", arg),
         setThicknessAction = arg => Console.WriteLine("Setting Thickness to {0}", arg),
         setStrokeAction = arg => { if (arg!=0) { Console.WriteLine("Stroke ON"); } else { Console.WriteLine("Stroke OFF"); } },
         moveAction = arg => Console.WriteLine("Moving {0} Units", arg),
         turnAction = arg => Console.WriteLine("Turning {0} Degrees", arg),
     };
     var program = compiler.Parse(@"
         X = (10+32)
         repeat 2 times
             set color to blue
             set thickness to 10
             start stroke
             move forward X
             turn left
             move backward 5
             stop stroke
         end
         if 0
             move forward 100
         else
             move backward 100
         end
     ");
     var interpreter = new Interpreter(program);
     while(interpreter.ExecuteNextInstruction()) {}
 }
Exemplo n.º 2
0
 public override int Compute(Interpreter interp)
 {
     switch(mOp) {
         case BinaryOperation.Add: return mLeft.Compute(interp) + mRight.Compute(interp);
         case BinaryOperation.Subtract: return mLeft.Compute(interp) - mRight.Compute(interp);
         case BinaryOperation.Multiply: return mLeft.Compute(interp) * mRight.Compute(interp);
         case BinaryOperation.Divide:
             int r = mRight.Compute(interp);
             if (r == 0) {
                 Console.WriteLine("Divide By Zero");
                 return 0;
             } else {
                 return mLeft.Compute(interp) / r;
             }
         case BinaryOperation.And: return (mLeft.Compute(interp) != 0) && (mRight.Compute(interp) != 0) ? 1 : 0;
         case BinaryOperation.Or: return (mLeft.Compute(interp) != 0) || (mRight.Compute(interp) != 0) ? 1 : 0;
         case BinaryOperation.Equals: return mLeft.Compute(interp) == mRight.Compute(interp) ? 1 : 0;
         case BinaryOperation.GreaterThan: return mLeft.Compute(interp) > mRight.Compute(interp) ? 1 : 0;
         case BinaryOperation.LessThan: return mLeft.Compute(interp) < mRight.Compute(interp) ? 1 : 0;
         case BinaryOperation.GreaterThanOrEqualTo: return mLeft.Compute(interp) >= mRight.Compute(interp) ? 1 : 0;
         case BinaryOperation.LessThanOrEqualTo: return mLeft.Compute(interp) <= mRight.Compute(interp) ? 1 : 0;
         default: return 0;
     }
 }
Exemplo n.º 3
0
 public override int Compute(Interpreter interp)
 {
     int result;
     if (!interp.GetVariable(name, out result)) {
         Console.WriteLine("Undefined Variable Expression");
     }
     return result;
 }
Exemplo n.º 4
0
 public override int Compute(Interpreter interp)
 {
     switch(mOp) {
         case UnaryOperation.Negate: return -mExpr.Compute(interp);
         case UnaryOperation.Complement: return mExpr.Compute(interp) != 0 ? 0 : 1;
         default: return 0;
     }
 }
Exemplo n.º 5
0
 public override int Compute(Interpreter interp)
 {
     return 0;
 }
Exemplo n.º 6
0
 public abstract int Compute(Interpreter interp);
Exemplo n.º 7
0
 public abstract void Execute(Interpreter interp);
Exemplo n.º 8
0
 public override void Execute(Interpreter interp)
 {
     interp.Goto(condition.Compute(interp) == 0 ? indexFalse : indexTrue);
 }
Exemplo n.º 9
0
 public override void Execute(Interpreter interp)
 {
     mAction(mExpr.Compute(interp));
 }
Exemplo n.º 10
0
 public override void Execute(Interpreter interp)
 {
     interp.ClearVariable(name);
 }
Exemplo n.º 11
0
 public override void Execute(Interpreter interp)
 {
     interp.SetVariable(mName, mExpr.Compute(interp));
 }
Exemplo n.º 12
0
 public override void Execute(Interpreter interp)
 {
     interp.Goto(index);
 }