Example #1
0
        private CodeStatementCollection GenerateImperativeStatement(SwitchExpression node)
        {
            var condition = GenerateImperativeExpression(node.Expression);

            CodeConditionStatement switchStatement     = null;
            CodeConditionStatement rootSwitchStatement = null;

            Action <CodeConditionStatement> setSwitchStmt = x =>
            {
                if (switchStatement != null)
                {
                    switchStatement.FalseStatements.Add(x);
                    switchStatement = x;
                }
                else
                {
                    rootSwitchStatement = switchStatement = x;
                }
            };

            foreach (var c in node.Cases)
            {
                var caseChecks = c.Cases
                                 .Cast <IdentifierExpression>()
                                 .Select(x => x.Identifier)
                                 .Select(x => IsTokenType(x) ? (CodeExpression)GetTokenTypeRef(x) : CodeHelper.VarRef(x))
                                 .Select(x => CodeHelper.BinOpExp(condition, CodeBinaryOperatorType.ValueEquality, x))
                                 .ToArray();

                var exp = CodeHelper.BinOpExpJoin(caseChecks, CodeBinaryOperatorType.BooleanOr);

                var body = GenerateImperativeStatements(c.Body);

                //var body = c.Body
                //    .SelectMany(x => GenerateImperativeStatement(x).OfType<CodeStatement>())
                //    .ToArray();

                setSwitchStmt(new CodeConditionStatement(exp, body.Cast <CodeStatement>().ToArray()));
            }

            if (node.DefaultCase?.Count > 0)
            {
                if (setSwitchStmt == null)
                {
                    throw new NotImplementedException();
                }

                //var defaultStmts = node.DefaultCase
                //    .SelectMany(x => GenerateImperativeStatement(x).OfType<CodeStatement>())
                //    .ToArray();

                var defaultStmts = GenerateImperativeStatements(node.DefaultCase)
                                   .OfType <CodeStatement>()
                                   .ToArray();

                switchStatement.FalseStatements.AddRange(defaultStmts);
            }

            return(new CodeStatementCollection(new[] { rootSwitchStatement }));
        }
Example #2
0
        private CodeStatementCollection GenerateImperativeStatement(DoWhileExpression node)
        {
            var body = GenerateImperativeStatements(node.Body);

            body.Add(new CodeConditionStatement(
                         CodeHelper.BinOpExp(
                             GenerateImperativeExpression(node.Condition, isCondition: true),
                             CodeBinaryOperatorType.ValueEquality,
                             CodeHelper.False()),
                         new CodeSnippetStatement("break;")));

            return(new CodeStatementCollection(new[] { CodeHelper.While(CodeHelper.True(), body.OfType <CodeStatement>().ToArray()) }));
        }
Example #3
0
        private CodeExpression GenerateImperativeExpression(BinaryOperatorExpression node, bool isCondition = false)
        {
            var opTable = new Dictionary <AphidTokenType, CodeBinaryOperatorType>
            {
                { AphidTokenType.AdditionOperator, CodeBinaryOperatorType.Add },
                { AphidTokenType.MinusOperator, CodeBinaryOperatorType.Subtract },
                { AphidTokenType.MultiplicationOperator, CodeBinaryOperatorType.Multiply },
                { AphidTokenType.DivisionOperator, CodeBinaryOperatorType.Divide },
                { AphidTokenType.OrOperator, CodeBinaryOperatorType.BooleanOr },
                { AphidTokenType.AndOperator, CodeBinaryOperatorType.BooleanAnd },
                { AphidTokenType.BinaryOrOperator, CodeBinaryOperatorType.BooleanOr },
                { AphidTokenType.BinaryAndOperator, CodeBinaryOperatorType.BooleanAnd },
                { AphidTokenType.EqualityOperator, CodeBinaryOperatorType.ValueEquality },
                { AphidTokenType.NotEqualOperator, CodeBinaryOperatorType.IdentityInequality },
                { AphidTokenType.LessThanOperator, CodeBinaryOperatorType.LessThan },
                { AphidTokenType.GreaterThanOperator, CodeBinaryOperatorType.GreaterThan },
                { AphidTokenType.LessThanOrEqualOperator, CodeBinaryOperatorType.LessThanOrEqual },
                { AphidTokenType.GreaterThanOrEqualOperator, CodeBinaryOperatorType.GreaterThanOrEqual }
            };

            switch (node.Operator)
            {
            case AphidTokenType.MemberOperator:
                if (node.RightOperand.Type != AphidExpressionType.IdentifierExpression)
                {
                    throw new NotImplementedException();
                }

                return(CodeHelper.PropRef(
                           GenerateImperativeExpression(node.LeftOperand),
                           ((IdentifierExpression)node.RightOperand).Identifier));

            default:
                return(CodeHelper.BinOpExp(
                           GenerateImperativeExpression(node.LeftOperand, isCondition),
                           opTable[node.Operator],
                           GenerateImperativeExpression(node.RightOperand, isCondition)));
            }
        }
Example #4
0
        private CodeExpression GenerateImperativeExpression(UnaryOperatorExpression node, bool isCondition = false)
        {
            if (!node.IsPostfix)
            {
                switch (node.Operator)
                {
                case AphidTokenType.NotOperator:
                    var exp = GenerateImperativeExpression(node.Operand, isCondition);

                    return(CodeHelper.BinOpExp(
                               exp,
                               CodeBinaryOperatorType.ValueEquality,
                               CodeHelper.False()));

                default:
                    throw new NotImplementedException();
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Example #5
0
        private CodeExpression GenerateImperativeExpression(IdentifierExpression node, bool isCondition = false)
        {
            switch (node.Identifier)
            {
            case ParserGeneratorDirective.CurrentTokenType:
                return(GetCurrentTokenType());

            case ParserGeneratorDirective.CurrentLexeme:
                return(GetCurrentLexeme());

            default:
                if (IsTokenType(node.Identifier))
                {
                    var exp = (CodeExpression)GetTokenTypeRef(node.Identifier);

                    return(!isCondition
                            ? exp
                            : CodeHelper.BinOpExp(
                               GetCurrentTokenType(),
                               CodeBinaryOperatorType.ValueEquality,
                               exp));
                }
                else if (node.Attributes.Count > 0)
                {
                    var attr = node.Attributes.Single().Identifier;

                    return(attr == "list"
                            ? new CodeObjectCreateExpression(
                               ParserCode.GetListTypeRef(CodeHelper.TypeRef(node.Identifier)))
                            : throw new NotImplementedException());
                }
                else
                {
                    return(CodeHelper.VarRef(node.Identifier));
                }
            }
        }