示例#1
0
        private void AddToConTable(ref string token, ref int i)
        {
            if (OutputTokenTable.Table.Count > 1)
            {
                string penultimateToken = OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name;

                if (OutputTokenTable.Table.Last().Name.Equals("=") && IdnTable.Contains(penultimateToken))
                {
                    if (Checker.GetType(token) == IdnTable.GetType(penultimateToken))
                    {
                        ConTable.Add(token, IdnTable.GetType(penultimateToken));
                    }
                    else
                    {
                        throw new Exception($"Error on {i + 1} line!\tInvalid type of constant. Unable to cast " +
                                            $"'{Checker.GetType(token)}' to '{IdnTable.GetType(penultimateToken)}'");
                    }
                }
                else if (!ConTable.Contains(token))
                {
                    ConTable.Add(token);
                }
            }
            else if (!ConTable.Contains(token))
            {
                ConTable.Add(token);
            }
        }
示例#2
0
        public static bool IsAbleToCast(string token)
        {
            //output tokens is exist and last token in table is "="
            return(OutputTokenTable.Table.Count != 0 && OutputTokenTable.Table.Last().Name == "="

                   //penultimate token is identifier
                   && IdnTable.Contains(OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name)

                   //current token is identifier
                   && IdnTable.Contains(token)

                   //types of tokens on both sides of "=" are equal
                   && (IdnTable.GetType(token) == IdnTable.GetType(OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name)));
        }
示例#3
0
        public void Execute()
        {
            double secondOperand, firstOperand;
            string @operator, lastLabel = string.Empty;
            bool   jumpFlag   = false;
            Regex  labelRegex = new Regex("l\\d+");

            for (int i = 0; i < Generator.Polish.Count; i++)
            {
                string token = Generator.Polish[i];

                if (jumpFlag)
                {
                    if (token == lastLabel)
                    {
                        jumpFlag = false;
                    }
                    continue;
                }

                if ((token != "true" && token != "false") && (Checker.IsUnaryOperator(token) || Checker.IsBinaryOperator(token)))
                {
                    if (Checker.IsBinaryOperator(token))
                    {
                        @operator = token;

                        if (Checker.IsOperator(@operator))
                        {
                            secondOperand = GetOperand();
                            firstOperand  = GetOperand();

                            ExecuteExpression(firstOperand, secondOperand, @operator);
                            AddToViewTable(token, $"{firstOperand} {@operator} {secondOperand}");
                        }
                        else if (Checker.IsRelation(@operator))
                        {
                            secondOperand = GetOperand();
                            firstOperand  = GetOperand();

                            ExecuteRelation(firstOperand, secondOperand, @operator);
                            AddToViewTable(token, $"{firstOperand} {@operator} {secondOperand}");
                        }
                        else if (Checker.IsIO(@operator))
                        {
                            ExecuteIO(stack.Pop(), @operator);
                        }
                        else if (@operator == "=")
                        {
                            secondOperand = GetOperand();
                            string identifierName = stack.Pop();

                            if (new Regex("r\\d+").IsMatch(identifierName))
                            {
                                if (cycleDesignations.FirstOrDefault(r => r.Key == identifierName).Key is null)
                                {
                                    cycleDesignations.Add(identifierName, secondOperand);
                                }
                                else
                                {
                                    cycleDesignations[identifierName] = secondOperand;
                                }
                            }
                            else
                            {
                                IdnTable.SetValue(identifierName, secondOperand);
                            }
                            AddToViewTable(token, $"{identifierName} {@operator} {secondOperand}");
                        }
                    }
                    else if (Checker.IsUnaryOperator(token))
                    {
                        if (token == "@")
                        {
                            string operand = stack.Pop();

                            if (IdnTable.Contains(operand) && IdnTable.GetValue(operand).HasValue)
                            {
                                IdnTable.SetValue(operand, -IdnTable.GetValue(operand).Value);
                            }

                            stack.Push($"-{operand}");
                            AddToViewTable(token, $"Change sign of {operand}");
                        }
                        else
                        {
                            string identifier;
                            while (stack.Count > 0)
                            {
                                identifier = stack.Pop();

                                if (IdnTable.GetType(identifier) != token)
                                {
                                    IdnTable.SetType(identifier, token);
                                }
                            }
                        }
                    }
                }
                else if (token.Contains("JNE"))
                {
                    lastLabel = $"{labelRegex.Match(token).Groups[0].Value}:";
                    bool condition = Boolean.Parse(stack.Pop());

                    jumpFlag = condition ? false : true;
                    AddToViewTable(token, $"Jump to label {lastLabel} if last condition is false.");
                }
                else if (token.Contains("JMP"))
                {
                    string label = labelRegex.Match(token).Groups[0].Value;

                    i = Generator.LabelsTable[label];
                    AddToViewTable(token, $"Jump to label {label}:");
                }
                else if (!token.Contains(":"))
                {
                    stack.Push(token);
                    AddToViewTable(token, "Add input token to stack");
                }
            }
        }