Beispiel #1
0
 public void visit(WhileCommand that)
 {
     Console.Write("while (");
     that.Expression.visit(this);
     Console.WriteLine(")");
     that.Command.visit(this);
 }
Beispiel #2
0
    // Use this for initialization
    void Start()
    {
        Button btn  = leftButton.GetComponent <Button>();
        Button btn1 = rightButton.GetComponent <Button>();
        Button btn2 = upButton.GetComponent <Button>();
        Button btn3 = downButton.GetComponent <Button>();
        Button btn4 = whileButton.GetComponent <Button>();
        Button btn5 = methodButton.GetComponent <Button>();

        // methodContainer = GameObject.Find("methodContainer");

        commandList     = new List <string>();
        whileList       = new List <string>();
        methodList      = new List <string>();
        jaggedWhileList = new List <List <string> >();

        //whileActive = false;
        whileInstance  = GetComponent <WhileCommand>();
        methodInstance = GetComponent <MethodCommand>();

        btn.onClick.AddListener(TaskOnClick);
        btn1.onClick.AddListener(TaskOnClick);
        btn2.onClick.AddListener(TaskOnClick);
        btn3.onClick.AddListener(TaskOnClick);
        btn4.onClick.AddListener(TaskOnClick);
        btn5.onClick.AddListener(TaskOnClick);
    }
        public void ExecuteWhileCommand()
        {
            IExpression     incrementX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new ConstantExpression(1), new VariableExpression("a"));
            IExpression     decrementY = new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("b"), new ConstantExpression(1));
            ICommand        setX       = new SetVariableCommand("a", incrementX);
            ICommand        setY       = new SetVariableCommand("b", decrementY);
            List <ICommand> commands   = new List <ICommand>();

            commands.Add(setX);
            commands.Add(setY);
            ICommand    command = new CompositeCommand(commands);
            IExpression yexpr   = new VariableExpression("b");

            WhileCommand whilecmd = new WhileCommand(yexpr, command);

            Context context = new Context();

            context.SetValue("a", 0);
            context.SetValue("b", 5);

            whilecmd.Execute(context);

            Assert.AreEqual(0, context.GetValue("b"));
            Assert.AreEqual(5, context.GetValue("a"));
        }
        public Void VisitWhileCommand(WhileCommand ast, Void arg)
        {
            System.Console.WriteLine("Visiting WhileCommand");
            TypeDenoter expressionType = ast.Expression.Visit(this, null);

            CheckAndReportError(expressionType.Equals(StandardEnvironment.BooleanType), "expression type must be boolean", ast);
            return(ast.Command.Visit(this, null));
        }
        public Void VisitWhileCommand(WhileCommand ast, Void arg)
        {
            var expressionType = ast.Expression.Visit(this);

            CheckAndReportError(expressionType == StandardEnvironment.BooleanType,
                                "Boolean expression expected here", ast.Expression);
            ast.Command.Visit(this);
            return(null);
        }
Beispiel #6
0
        public void visit(WhileCommand that)
        {
            that.Expression.visit(this);
            that.Command.visit(this);

            if (that.Expression.Type.Kind != TypeKind.Boolean)
            {
                throw new CheckerError(that.Position, "Boolean expression expected in 'while' statement");
            }
        }
        public Void VisitWhileCommand(WhileCommand ast, Void arg)
        {
            TypeDenoter expressionType = ast.Expression.Visit(this, null);

            if (expressionType is BoolTypeDenoter == false)
            {
                ReportError("\"%\" is not a boolean type", ast);
            }
            ast.Command.Visit(this, null);
            return(null);
        }
Beispiel #8
0
        public Void VisitWhileCommand(WhileCommand ast, Frame frame)
        {
            var jumpAddr = _emitter.Emit(OpCode.JUMP, Register.CB);
            var loopAddr = _emitter.NextInstrAddr;

            ast.Command.Visit(this, frame);
            _emitter.Patch(jumpAddr);
            ast.Expression.Visit(this, frame);
            _emitter.Emit(OpCode.JUMPIF, Machine.TrueValue, Register.CB, loopAddr);
            return(null);
        }
Beispiel #9
0
    public static void Main()
    {
        MachineComposite dublin = ExampleMachine.Dublin();
        Term             sp     = new Constant((Machine)dublin.Find("StarPress:1401"));
        Term             ub     = new Constant((Machine)dublin.Find("UnloadBuffer:1501"));
        WhileCommand     wc     = new WhileCommand(
            new HasMaterial(sp),
            new CarryCommand(sp, ub));

        wc.Execute();
    }
Beispiel #10
0
        public void ExecuteWhileCommandWithNullCondition()
        {
            Context      context = new Context();
            IExpression  cond    = new ConstantExpression(null);
            ICommand     cmd     = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(42)));
            WhileCommand wcmd    = new WhileCommand(cond, cmd);

            wcmd.Execute(context);

            Assert.IsNull(context.GetValue("a"));
        }
Beispiel #11
0
        public void CreateWhileCommand()
        {
            IExpression  cond = new ConstantExpression(1);
            ICommand     cmd  = new ExpressionCommand(new ConstantExpression(2));
            WhileCommand wcmd = new WhileCommand(cond, cmd);

            Assert.IsNotNull(wcmd.Condition);
            Assert.AreSame(cond, wcmd.Condition);
            Assert.IsNotNull(wcmd.Command);
            Assert.AreSame(cmd, wcmd.Command);
        }
Beispiel #12
0
        public Void VisitWhileCommand(WhileCommand ast, Void arg)
        {
            TypeDenoter wHile = ast.Expression.Visit(this, null);

            if (!(wHile is BoolTypeDenoter))
            {
                ReportError("\"%\" is not a procedure identifier", ast);
            }

            ast.Command.Visit(this, null);
            return(null);
        }
Beispiel #13
0
        private ICommand ParseWhileCommand()
        {
            IExpression condition = this.ParseExpression();

            this.ParseEndOfLine();
            ICommand command = this.ParseCommandList("end", "enddo");

            WhileCommand whileCommand = new WhileCommand(condition, command);

            this.lexer.NextToken();
            return(whileCommand);
        }
Beispiel #14
0
        public void ParseSimpleWhileCommand()
        {
            Parser parser = new Parser("while (a) \r\n b = 2;");
            var    result = parser.ParseCommand();

            Assert.IsNotNull(result);
            Assert.That(result is WhileCommand);

            WhileCommand command = (WhileCommand)result;

            Assert.IsNotNull(command.Condition);
            Assert.IsNotNull(command.Command);
        }
Beispiel #15
0
        public void ExecuteWhileCommandWithTrueCondition()
        {
            Context      context = new Context();
            IExpression  cond    = new NameExpression("a");
            ICommand     cmd     = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(false)));
            WhileCommand wcmd    = new WhileCommand(cond, cmd);

            context.SetValue("a", true);

            wcmd.Execute(context);

            Assert.AreEqual(false, context.GetValue("a"));
        }
Beispiel #16
0
        public void ParseSimpleWhileCommand()
        {
            Parser parser = new Parser("while (a) \r\n b = 2;");
            var    result = parser.ParseCommand();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(WhileCommand));

            WhileCommand command = (WhileCommand)result;

            Assert.IsNotNull(command.Condition);
            Assert.IsNotNull(command.Command);
        }
Beispiel #17
0
        public void ParseSimpleWhile()
        {
            ICommand command = ParseCommand("while (x<10) x=x+1;");

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(WhileCommand));

            WhileCommand whilecmd = (WhileCommand)command;

            Assert.IsNotNull(whilecmd.Condition);
            Assert.IsNotNull(whilecmd.Command);
            Assert.IsInstanceOfType(whilecmd.Command, typeof(SetCommand));
        }
    //public List<List<string>> jaggedCommandList = new List<List<string>>();

    // Use this for initialization
    void Start()
    {
        //confirm = GameObject.Find("confirm").GetComponent<Button>();
        //whilePanel = GameObject.Find("WhileField");

        whileCommand  = GetComponent <WhileCommand>();
        btnInstance   = GetComponent <ButtonClick>();
        methodCommand = GetComponent <MethodCommand>();

        whilePanel = whileCommand.whilePanel;
        confirm    = whileCommand.confirm;

        confirm.onClick.AddListener(TaskOnClick);
    }
Beispiel #19
0
    // Use this for initialization
    void Start()
    {
        floormask = LayerMask.GetMask("Floor");

        Player = GameObject.FindGameObjectWithTag("Player");
        // playerRigidbody = GetComponent<Rigidbody>();

        Button btn = playButton.GetComponent <Button>();

        btnInstance   = GetComponent <ButtonClick>();
        whileInstance = GetComponent <WhileCommand>();

        btn.onClick.AddListener(TaskOnClick);
    }
Beispiel #20
0
    public static void Main()
    {
        MachineComposite dublin = ExampleMachine.Dublin();
        //Machine starPress = (Machine) dublin.Find("StarPress:1401"); // uncomment to add bins
        //starPress.Load(new Bin(42));// and see it work
        //starPress.Load(new Bin(84));//
        Term         sp = new Constant((Machine)dublin.Find("StarPress:1401"));
        Term         ub = new Constant((Machine)dublin.Find("UnloadBuffer:1501"));
        WhileCommand wc = new WhileCommand(
            new HasMaterial(sp),
            new CarryCommand(sp, ub));

        wc.Execute();
    }
        public void CreateAndEvaluateSimpleWhileCommand()
        {
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("a", 1);
            IExpression condition = new CompareExpression(ComparisonOperator.Less, new NameExpression("a"), new ConstantExpression(10));
            ICommand    body      = new SetCommand("a", new BinaryOperatorExpression(new NameExpression("a"), new ConstantExpression(1), BinaryOperator.Add));

            WhileCommand command = new WhileCommand(condition, body);

            command.Execute(environment);

            Assert.AreEqual(condition, command.Condition);
            Assert.AreEqual(body, command.Command);
            Assert.AreEqual(10, environment.GetValue("a"));
        }
Beispiel #22
0
        public void ParseWhileCommandMultiline()
        {
            Parser   parser  = new Parser("while a \r\n a = b\r\nend");
            ICommand command = parser.ParseCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(WhileCommand));

            WhileCommand whilecommand = (WhileCommand)command;

            Assert.IsNotNull(whilecommand.Condition);
            Assert.IsNotNull(whilecommand.Command);

            Assert.IsInstanceOfType(whilecommand.Condition, typeof(VariableExpression));

            Assert.IsNull(parser.ParseCommand());
        }
Beispiel #23
0
        public void EvaluateWhileCommandUsingDecrement()
        {
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("a", 2);
            IExpression condition = new VariableExpression("a");

            ICommand command = new SetCommand("a", new BinaryArithmeticExpression(new VariableExpression("a"), new ConstantExpression(1), ArithmeticOperator.Subtract));

            WhileCommand wcommand = new WhileCommand(condition, command);

            Assert.AreEqual(command, wcommand.Command);
            Assert.AreEqual(condition, wcommand.Condition);

            wcommand.Execute(environment);

            Assert.AreEqual(0, environment.GetValue("a"));
        }
Beispiel #24
0
    // Use this for initialization
    void Start()
    {
        floormask = LayerMask.GetMask("Floor");

        Player = GameObject.FindGameObjectWithTag("Player");
        // playerRigidbody = GetComponent<Rigidbody>();

        //Button btn = playButton.GetComponent<Button>();
        btnInstance   = GetComponent <UIButtonClick>();
        whileInstance = GetComponent <WhileCommand>();
        spawn         = GetComponent <SpawnInstructions>();
        //btn.onClick.AddListener(TaskOnClick);

        //store original position of player
        //originalPos = player.transform.position;
        isRotating     = false;
        targetAngle    = Player.transform.eulerAngles.y;
        targetRotation = Quaternion.AngleAxis(targetAngle, Vector3.up);
    }
Beispiel #25
0
        public void WhileWithAddExpression()
        {
            var expression   = new CompareExpression(ComparisonOperator.Less, new VariableExpression("a"), new ConstantExpression(10));
            var inccommand   = new SetVariableCommand("k", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new ConstantExpression(1)));
            var addcommand   = new SetVariableCommand("a", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new VariableExpression("a")));
            var command      = new CompositeCommand(new ICommand[] { inccommand, addcommand });
            var whilecommand = new WhileCommand(expression, command);

            Context context = new Context();

            context.SetValue("a", 0);
            context.SetValue("k", 0);

            var result = whilecommand.Execute(context);

            Assert.IsNull(result);
            Assert.IsNotNull(whilecommand.Condition);
            Assert.IsNotNull(whilecommand.Command);
            Assert.AreEqual(4, context.GetValue("k"));
            Assert.AreEqual(10, context.GetValue("a"));
        }
    // Use this for initialization
    void Start()
    {
        /*Button btn = leftButton.GetComponent<Button>();
         * Button btn1 = rightButton.GetComponent<Button>();
         * Button btn2 = upButton.GetComponent<Button>();
         * Button btn3 = downButton.GetComponent<Button>();*/
        // Button btn4 = whileButton.GetComponent<Button>();
        //Button btn5 = methodButton.GetComponent<Button>();

        //Mairim
        //Button submitBtn = submit.GetComponent<Button>();
        //submitBtn.onClick.AddListener (SubmitCommands);

        //Record original position of the player
        //originalPos = player.transform.position;

        //Button clearBtn = clear.GetComponent<Button>();
        //clearBtn.onClick.AddListener (ClearCommands);
        // methodContainer = GameObject.Find("methodContainer");

        commandList     = new List <string>();
        whileList       = new List <string>();
        methodList      = new List <string>();
        jaggedWhileList = new List <List <string> >();

        //whileActive = false;
        whileInstance  = GetComponent <WhileCommand>();
        methodInstance = GetComponent <MethodCommand>();

        /*
         * btn.onClick.AddListener(TaskOnClick);
         * btn1.onClick.AddListener(TaskOnClick);
         * btn2.onClick.AddListener(TaskOnClick);
         * btn3.onClick.AddListener(TaskOnClick);*/

        // btn4.onClick.AddListener(TaskOnClick);
        //  btn5.onClick.AddListener(TaskOnClick);
    }
Beispiel #27
0
        public void ParseAndEvaluateSimpleWhileWithCompositeCommand()
        {
            Parser parser = new Parser("while (a) { a = a-1; b=b+1; }");

            ICommand command = parser.ParseCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(WhileCommand));

            WhileCommand whilecmd = (WhileCommand)command;

            Assert.IsInstanceOfType(whilecmd.Command, typeof(CompositeCommand));

            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("a", 2);
            environment.SetValue("b", 0);

            command.Execute(environment);

            Assert.AreEqual(0, environment.GetValue("a"));
            Assert.AreEqual(2, environment.GetValue("b"));
        }
    public bool TryGetSimpleCommand(string command, out BaseCommand returnCommand)
    {
        returnCommand = null;
        string[] arguments  = null;
        string   methodName = GetMethodSingature(command, ref arguments);

        CommandsMethods commandEnum = _availableCommands.FirstOrDefault(com => com.ToString() == methodName);

        if (commandEnum == 0)
        {
            return(false);
        }

        switch (commandEnum)
        {
        case (CommandsMethods.Forward):
            if (arguments == null || arguments.Length != 1 || arguments[0] != string.Empty)
            {
                return(false);
            }

            returnCommand = new MoveCommand(_implementator, MoveSides.FORWARD);
            return(true);

        case (CommandsMethods.Backward):
            if (arguments == null || arguments.Length != 1 || arguments[0] != string.Empty)
            {
                return(false);
            }

            returnCommand = new MoveCommand(_implementator, MoveSides.BACKWARD);
            return(true);

        case (CommandsMethods.Turn):
        {
            if (arguments == null || arguments.Length == 0)
            {
                return(false);
            }

            var turnSide = arguments[0].ToLower();

            if (turnSide == TurnArguments.Right.ToString().ToLower())
            {
                returnCommand = new RotateCommand(_implementator, TurnArguments.Right);
            }
            else if (turnSide == TurnArguments.Left.ToString().ToLower())
            {
                returnCommand = new RotateCommand(_implementator, TurnArguments.Left);
            }
            return(true);
        }

        case (CommandsMethods.Do):
        {
            int count = 0;
            if (arguments == null || arguments.Length == 0 || !int.TryParse(arguments[0], out count))
            {
                return(false);
            }

            returnCommand = new DoCommand(_implementator, count);
            return(true);
        }

        case (CommandsMethods.While):
        {
            if (arguments == null || arguments.Length == 0)
            {
                return(false);
            }

            WhileConditions argument = _allWhileConditions.FirstOrDefault(com => com.ToString() == arguments[0]);

            if (argument == 0)
            {
                return(false);
            }

            returnCommand = new WhileCommand(_implementator, argument);
            return(true);
        }
        }

        return(false);
    }
        public void GenerateCodeForInstruction(CodeLine line)
        {
            if (InstructionHelper.IsMoveInstruction(line.instruction))
            {
                var command = new MoveCommand(InstructionHelper.GetInstructionDirection(line.instruction), currentCodeLineNumber + 1);
                commandToCodeLineMapping.Add(command, line);
                allCommands.Add(command);
            }
            if (InstructionHelper.IsPutInstruction(line.instruction))
            {
                var command = new PutCommand(currentCodeLineNumber + 1);
                commandToCodeLineMapping.Add(command, line);
                allCommands.Add(command);
            }
            if (InstructionHelper.IsPickInstruction(line.instruction))
            {
                var command = new PickCommand(currentCodeLineNumber + 1);
                commandToCodeLineMapping.Add(command, line);
                allCommands.Add(command);
            }
            if (InstructionHelper.IsJumpInstruction(line.instruction))
            {
                ICommand command;

                if (InstructionHelper.IsJumpInstructionLabel(line.instruction))
                {
                    command = new JumpCommand(currentCodeLineNumber + 1);
                    allCommands.Add(command);
                }
                else
                {
                    //this is being set in code later - otherwise forward jumps will not work - see RepairJumps for reference.
                    command = new JumpCommand(currentCodeLineNumber + 1);
                    allCommands.Add(command);
                }
                commandToCodeLineMapping.Add(command, line);
            }

            if (InstructionHelper.IsIfInstruction(line.instruction))
            {
                int trueLineNumber = currentCodeLineNumber + 1;
                int elseLineNumber = currentCodeLineNumber + line.TotalChildrenCount + 1;
                var command        = new IfCommand(trueLineNumber, elseLineNumber, GetConditions(line), GetLogicalOperators(line));
                allCommands.Add(command);
                commandToCodeLineMapping.Add(command, line);
                currentCodeLineNumber++;
                foreach (var child in line.children)
                {
                    GenerateCodeForInstruction(child);
                }
                command.NextCommandId = currentCodeLineNumber;
                currentCodeLineNumber--; //this may seem wrong but actually it is not
            }

            if (InstructionHelper.IsWhileInstruction(line.instruction))
            {
                int trueLineNumber  = currentCodeLineNumber + 1;
                int falseLineNumber = currentCodeLineNumber + line.TotalChildrenCount + 1;
                var command         = new WhileCommand(trueLineNumber, falseLineNumber, GetConditions(line), GetLogicalOperators(line));
                allCommands.Add(command);
                commandToCodeLineMapping.Add(command, line);
                currentCodeLineNumber++;
                foreach (var child in line.children)
                {
                    GenerateCodeForInstruction(child);
                }
                var loopJumpCommand = new JumpCommand(trueLineNumber - 1);
                commandToCodeLineMapping.Add(loopJumpCommand, null);
                command.NextCommandId = currentCodeLineNumber + 1;
                allCommands.Add(loopJumpCommand);
            }

            if (InstructionHelper.IsRepeatInstruction(line.instruction))
            {
                int trueLineNumber  = currentCodeLineNumber + 1;
                int falseLineNumber = currentCodeLineNumber + line.TotalChildrenCount + 1;
                var command         = new RepeatCommand(trueLineNumber, falseLineNumber, InstructionHelper.GetRepeatTimes(line.instruction));
                allCommands.Add(command);
                commandToCodeLineMapping.Add(command, line);
                currentCodeLineNumber++;
                foreach (var child in line.children)
                {
                    GenerateCodeForInstruction(child);
                }
                var loopJumpCommand = new JumpCommand(trueLineNumber - 1);
                commandToCodeLineMapping.Add(loopJumpCommand, null);
                command.NextCommandId = currentCodeLineNumber;
                allCommands.Add(loopJumpCommand);
            }

            currentCodeLineNumber++;
        }