private static Expression PredefinedAtom_Default(ParseTreeNode root, CompilerState state)
        {
            var funArgs = ExpressionTreeExtensions.UnwindTupleExprList(root.RequireChild("exprList", 1, 0));

            funArgs.RequireChildren(1);

            var arg1Node   = funArgs.ChildNodes[0];
            var targetType = RequireSystemType(arg1Node);

            return(ExpressionTreeExtensions.GetDefaultExpression(targetType));
        }
        private static Expression PredefinedAtom_Cast(ParseTreeNode root, CompilerState state)
        {
            root.RequireChildren(2);

            var arg1Node = root.RequireChild(null, 1, 0, 0);
            var value    = state.ParentRuntime.Analyze(arg1Node, state).RemoveNullability();

            var arg2Node   = root.RequireChild("string", 1, 0, 1);
            var targetType = RequireSystemType(arg2Node);

            if (value.IsVoid())
            {
                return(ExpressionTreeExtensions.GetDefaultExpression(targetType));
            }

            // bluntly attempt to convert the type; will throw if types are not compatible
            return(ConstantHelper.TryEvalConst(root, value, ExpressionType.Convert, targetType));
        }
Exemplo n.º 3
0
        private Expression BuildCaseStatementExpression(CompilerState state, ParseTreeNode whenThenListNode, Expression caseDefault)
        {
            // now start building on top of the tail, right to left,
            // also making sure that types are compatible
            var tail = caseDefault;

            for (var i = whenThenListNode.ChildNodes.Count - 1; i >= 0; i--)
            {
                var caseWhenThenNode = whenThenListNode.RequireChild("caseWhenThen", i);
                caseWhenThenNode.RequireChildren(4);
                var whenNode = caseWhenThenNode.RequireChild(null, 1);
                var thenNode = caseWhenThenNode.RequireChild(null, 3);

                if (whenNode.Term.Name == "tuple")
                {
                    throw new CompilationException("When variable for CASE is not specified, you can only have one expression in each WHEN clause", whenNode);
                }

                // in this flavor of CASE, we are building a sequence of IIFs
                // it requires that our "WHEN" clause is of non-nullable boolean type
                var when = Analyze(whenNode, state).RemoveNullability();
                when.RequireBoolean(whenNode);

                var then = Analyze(thenNode, state);

                // try to auto-adjust types of this "THEN" and current tail expression if needed
                if (tail != null)
                {
                    Expression adjusted;
                    if (ExpressionTreeExtensions.TryAdjustReturnType(thenNode, then, tail.Type, out adjusted))
                    {
                        then = adjusted;
                    }
                    else if (ExpressionTreeExtensions.TryAdjustReturnType(thenNode, tail, then.Type, out adjusted))
                    {
                        tail = adjusted;
                    }
                    else
                    {
                        throw new CompilationException(
                                  string.Format(
                                      "Incompatible types within CASE statement. Tail is of type {0}, and then is of type {1}",
                                      tail.Type.FullName, then.Type.FullName), thenNode);
                    }
                }

                if (when is ConstantExpression)
                {
                    if ((bool)((ConstantExpression)when).Value)
                    {
                        tail = then;
                    }
                }
                else
                {
                    tail = Expression.Condition(when, then, tail ?? ExpressionTreeExtensions.GetDefaultExpression(then.Type));
                }
            }

            return(tail);
        }
Exemplo n.º 4
0
        private Expression BuildSwitchStatementExpression(CompilerState state, ParseTreeNode caseVariableNode, ParseTreeNode whenThenListNode, Expression caseDefault)
        {
            var switchVariable = Analyze(caseVariableNode.ChildNodes[0], state);

            switchVariable.RequireNonVoid(caseVariableNode.ChildNodes[0]);

            if (switchVariable is ConstantExpression)
            {
                throw new CompilationException("CASE variable should not be a constant value", caseVariableNode);
            }

            var        cases              = new List <Tuple <Expression[], Expression, ParseTreeNode> >(whenThenListNode.ChildNodes.Count);
            Expression firstNonVoidThen   = null;
            var        mustReturnNullable = false;

            foreach (var caseWhenThenNode in whenThenListNode.ChildNodes)
            {
                caseWhenThenNode.RequireChildren(4);
                var whenNodesRoot = ExpressionTreeExtensions.UnwindTupleExprList(caseWhenThenNode.RequireChild(null, 1));
                var thenNode      = caseWhenThenNode.RequireChild(null, 3);

                IList <ParseTreeNode> whenNodes;
                if (whenNodesRoot.Term.Name == "exprList")
                {
                    whenNodes = whenNodesRoot.ChildNodes;
                }
                else
                {
                    whenNodes = new[] { whenNodesRoot };
                }

                var when = new Expression[whenNodes.Count];
                for (var i = 0; i < whenNodes.Count; i++)
                {
                    var whenNode = whenNodes[i];
                    when[i] = Analyze(whenNode, state);

                    if (!when[i].IsVoid() && !(when[i] is ConstantExpression))
                    {
                        throw new CompilationException("CASE statement with a test variable requires WHEN clauses to be constant values", whenNode);
                    }

                    Expression adjusted;
                    if (ExpressionTreeExtensions.TryAdjustReturnType(whenNode, when[i], switchVariable.Type, out adjusted))
                    {
                        when[i] = adjusted;
                    }
                    else
                    {
                        throw new CompilationException(
                                  string.Format(
                                      "Could not adjust WHEN value type {0} to CASE argument type {1}",
                                      when[i].Type.FullName, switchVariable.Type.FullName), whenNode);
                    }
                }

                var then = Analyze(thenNode, state);
                cases.Add(new Tuple <Expression[], Expression, ParseTreeNode>(when, then, thenNode));

                if (then.IsVoid())
                {
                    // if there is at least one "void" return value, resulting value must be nullable
                    mustReturnNullable = true;
                }
                else if (firstNonVoidThen == null)
                {
                    firstNonVoidThen = then;
                }
            }

            if (firstNonVoidThen == null && !caseDefault.IsVoid())
            {
                firstNonVoidThen = caseDefault;
            }

            var adjustedCaseDefault = caseDefault;

            // now try to adjust whatever remaining VOID "then-s" to the first-met non-void then
            // if all THENs are void, then just leave it as-is - type will be adjusted by caller
            if (firstNonVoidThen != null)
            {
                if (mustReturnNullable && firstNonVoidThen.Type.IsValueType && !firstNonVoidThen.IsNullableType())
                {
                    firstNonVoidThen = ExpressionTreeExtensions.MakeNewNullable(
                        typeof(UnboxableNullable <>).MakeGenericType(firstNonVoidThen.Type),
                        firstNonVoidThen);
                }

                for (var i = 0; i < cases.Count; i++)
                {
                    var thenNode = cases[i].Item3;
                    var then     = cases[i].Item2;

                    if (!ReferenceEquals(then, firstNonVoidThen) && then.IsVoid())
                    {
                        Expression adjusted;
                        if (ExpressionTreeExtensions.TryAdjustReturnType(thenNode, then, firstNonVoidThen.Type, out adjusted))
                        {
                            cases[i] = new Tuple <Expression[], Expression, ParseTreeNode>(cases[i].Item1, adjusted, cases[i].Item3);
                        }
                        else
                        {
                            throw new CompilationException(
                                      string.Format(
                                          "Could not adjust THEN value type {0} to first-met THEN value type {1}",
                                          then.Type.FullName, firstNonVoidThen.Type.FullName), thenNode);
                        }
                    }
                }

                if (caseDefault != null &&
                    !ExpressionTreeExtensions.TryAdjustReturnType(caseVariableNode, caseDefault, firstNonVoidThen.Type, out adjustedCaseDefault))
                {
                    throw new CompilationException(
                              string.Format(
                                  "Could not adjust CASE default value's type {0} to first-met THEN value type {1}",
                                  caseDefault.Type.FullName, firstNonVoidThen.Type.FullName), caseVariableNode);
                }
            }

            if (adjustedCaseDefault == null)
            {
                adjustedCaseDefault = ExpressionTreeExtensions.GetDefaultExpression(
                    firstNonVoidThen == null
                        ? typeof(UnboxableNullable <ExpressionTreeExtensions.VoidTypeMarker>)
                        : firstNonVoidThen.Type);
            }

            return(Expression.Switch(
                       switchVariable, adjustedCaseDefault, null,
                       cases.Select(x => Expression.SwitchCase(x.Item2, x.Item1))));
        }