Example #1
0
        public void Handler(CmsCodeGenerator generator)
        {
            switch (generator.Token)
            {
            case ReadToken token:
                generator.GeneratorState = TinyCodeGeneratorState.Read;
                break;

            case WriteToken token:
                generator.GeneratorState = TinyCodeGeneratorState.Write;
                break;

            case IfToken token:
                generator.GeneratorState = TinyCodeGeneratorState.If;
                generator.TokenStack.Push(generator.Token);
                break;

            case WhileToken token:
                generator.GeneratorState = TinyCodeGeneratorState.While;
                generator.TokenStack.Push(generator.Token);
                break;

            case AttributionToken token:
                generator.GeneratorState = TinyCodeGeneratorState.Attribution;
                break;

            case EndToken token:
                generator.GeneratorState = TinyCodeGeneratorState.End;
                break;

            case IdentifierToken token:
                generator.MoveNextToken();
                if (generator.Token is AttributionToken)
                {
                    generator.AttributionTokenStack.Push(token);
                    generator.GeneratorState = TinyCodeGeneratorState.Attribution;
                }
                //else
                //    generator.GeneratorState = TinyCodeGeneratorState.Identifier;
                break;

            case RepeatToken token:
                generator.GeneratorState = TinyCodeGeneratorState.Repeat;
                //generator.TokenStack.Push(generator.Token);
                break;

            case UntilToken token:
                generator.GeneratorState = TinyCodeGeneratorState.Until;
                break;

            default:
                generator.MoveNextToken();
                break;
            }
        }
 private void HandleInitial(CmsCodeGenerator generator)
 {
     if (generator.Token.IsIdentifier())
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.Adress;
     }
     else if (generator.Token.IsNumber())
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.Number;
     }
     else if (generator.Token is EqualToken)
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.EqualsThen;
     }
     else if (generator.Token is NotEqualToken)
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.NotEqualsThen;
     }
     else if (generator.Token is GreatToken)
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.GreatThen;
     }
     else if (generator.Token is LessToken)
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.LessThen;
     }
     else if (generator.Token is GreatOrEqualToken)
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.GreatEqualThen;
     }
     else if (generator.Token is LessOrEqualToken)
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.LessEqualThen;
     }
     else if (generator.Token is PlusToken)
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.Plus;
     }
     else if (generator.Token is SubToken)
     {
         ExpressionStatmentState = CmsCodeExpressionStatmentState.Sub;
     }
     //else if (generator.Token is Token)
     //    ExpressionStatmentState = CmsCodeExpressionStatmentState.Plus;
     else
     {
         generator.MoveNextToken();
     }
 }
Example #3
0
        public void Handler(CmsCodeGenerator generator)
        {
            if (generator.TokenStack.Any())
            {
                var pop = generator.TokenStack.Pop();
                switch (pop)
                {
                case IfToken token:
                    generator.JFCodeReferenceStack.Pop().Reference.ValueDecimal = generator.CodesLengh;
                    break;

                case WhileToken token:
                    generator.AddCode(generator.StartWhileCodeReference.Pop());
                    generator.JFCodeReferenceStack.Pop().Reference.ValueDecimal = generator.CodesLengh;
                    break;
                }
            }

            generator.MoveNextToken();
            generator.GeneratorState = TinyCodeGeneratorState.Initial;
        }
        public void Handler(CmsCodeGenerator generator)
        {
            try
            {
                CmsCode compReference = null;
                ExpressionStatmentState = CmsCodeExpressionStatmentState.Initial;
                generator.MoveNextToken();

                while (!(generator.Token is ThenToken || generator.Token is DoToken || generator.Token is SemiColonToken || generator.Token is EndToken))
                {
                    switch (ExpressionStatmentState)
                    {
                    case CmsCodeExpressionStatmentState.Initial:
                        HandleInitial(generator);
                        break;

                    case CmsCodeExpressionStatmentState.Adress:
                        generator.AddCode(CmsCodeFactory.LOD(generator.VariableArea[generator.Token.Value]));
                        if (compReference != null)
                        {
                            generator.AddCode(compReference);
                        }
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.Number:
                        generator.AddCode(CmsCodeFactory.LDI(new CmsCode(int.Parse(generator.Token.Value))));
                        if (compReference != null)
                        {
                            generator.AddCode(compReference);
                        }
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.GreatThen:
                        compReference = CmsCodeFactory.GT;
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.LessThen:
                        compReference = CmsCodeFactory.LT;
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.EqualsThen:
                        compReference = CmsCodeFactory.EQ;
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.GreatEqualThen:
                        compReference = CmsCodeFactory.GE;
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.LessEqualThen:
                        compReference = CmsCodeFactory.LE;
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.NotEqualsThen:
                        compReference = CmsCodeFactory.NE;
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.Plus:
                        generator.MoveNextToken();
                        generator.AddCode(CmsCodeFactory.ADI(new CmsCode(int.Parse(generator.Token.Value))));
                        GoToInitialIfState(generator);
                        break;

                    case CmsCodeExpressionStatmentState.Sub:
                        compReference = CmsCodeFactory.SUI;
                        GoToInitialIfState(generator);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception($"Token: {generator.Token}\nTokenStack: {generator.Stack}\nError: {e.Message}", e);
            }
        }
 private void GoToInitialIfState(CmsCodeGenerator generator)
 {
     generator.MoveNextToken();
     ExpressionStatmentState = CmsCodeExpressionStatmentState.Initial;
 }
 public void Handler(CmsCodeGenerator generator)
 {
     generator.RepeatReferenceStack.Push(new CmsCode(generator.CodesLengh));
     generator.MoveNextToken();
     generator.GeneratorState = TinyCodeGeneratorState.Initial;
 }