Ejemplo n.º 1
0
            public RedILNode VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, State data)
            {
                var operand = CastUtilities.CastRedILNode <ExpressionNode>(unaryOperatorExpression.Expression.AcceptVisitor(this, data.NewState(unaryOperatorExpression, null)));

                if (OperatorUtilities.IsIncrement(unaryOperatorExpression.Operator))
                {
                    if (data.ParentNode.NodeType != NodeType.Statement)
                    {
                        throw new RedILException($"Incremental operators can only be used within statements");
                    }

                    BinaryExpressionOperator binaryOp = default;
                    switch (unaryOperatorExpression.Operator)
                    {
                    case UnaryOperatorType.Increment:
                    case UnaryOperatorType.PostIncrement:
                        binaryOp = BinaryExpressionOperator.Add;
                        break;

                    case UnaryOperatorType.Decrement:
                    case UnaryOperatorType.PostDecrement:
                        binaryOp = BinaryExpressionOperator.Subtract;
                        break;
                    }

                    var constantOne = new ConstantValueNode(DataValueType.Integer, 1);
                    return(new AssignNode(operand, VisitBinaryOperatorExpression(operand, constantOne, binaryOp, data.NewState(unaryOperatorExpression, null))));
                }

                var op = OperatorUtilities.UnaryOperator(unaryOperatorExpression.Operator);

                return(new UnaryExpressionNode(op, operand));
            }
Ejemplo n.º 2
0
            public RedILNode VisitIndexerExpression(IndexerExpression indexerExpression, State data)
            {
                //TODO: set parent node
                var target = CastUtilities.CastRedILNode <ExpressionNode>(indexerExpression.Target.AcceptVisitor(this, data.NewState(indexerExpression, null)));

                foreach (var arg in indexerExpression.Arguments)
                {
                    var argVisited = CastUtilities.CastRedILNode <ExpressionNode>(arg.AcceptVisitor(this, data.NewState(indexerExpression, null)));

                    // In LUA, array indices start at 1
                    if (target.DataType == DataValueType.Array && argVisited.DataType == DataValueType.Integer)
                    {
                        if (argVisited.Type == RedILNodeType.Constant)
                        {
                            argVisited = new ConstantValueNode(DataValueType.Integer,
                                                               int.Parse(((ConstantValueNode)argVisited).Value.ToString()) + 1);
                        }
                        else
                        {
                            argVisited = new BinaryExpressionNode(DataValueType.Integer, BinaryExpressionOperator.Add, argVisited, new ConstantValueNode(DataValueType.Integer, 1));
                        }
                    }

                    target = new TableKeyAccessNode(target, argVisited);
                }

                return(target);
            }