Ejemplo n.º 1
0
        private object AssignTuple(Environment env, TupleNode left, string op, object rvalue)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left), "null left tuple");
            }

            Tuple right = rvalue as Tuple;

            if (right != null)
            {
                object result = 0;

                int count = 0;
                foreach (IAstNode node in left)
                {
                    if (count >= right.Count)
                    {
                        break;
                    }

                    TupleNode tuple = node as TupleNode;

                    if (node != null)
                    {
                        result = AssignTuple(env, tuple, op, right[count++]);
                    }
                    else
                    {
                        FactorNode factor = node as FactorNode;

                        if (factor == null || !factor.IsAssignable)
                        {
                            throw new LepException("bad assignment", this);
                        }

                        PrimaryNode primary = factor.Operand as PrimaryNode;
                        if (primary == null)
                        {
                            throw new LepException("bad assignment", this);
                        }

                        if (((NameNode)primary.Operand).IsAnonymous)
                        {
                            ++count;
                        }
                        else
                        {
                            result = Assign(env, primary, op, right[count++], factor.AssignType);
                        }
                    }
                }

                return(result);
            }
            else
            {
                throw new LepException("bad assignment", this);
            }
        }
Ejemplo n.º 2
0
        private object Assign(Environment env, PrimaryNode left, string op, object rvalue, int type)
        {
            if (env == null) throw new ArgumentNullException(nameof(env), "null environment");
            if (left == null) throw new ArgumentNullException(nameof(left), "null left name");
            if (op == null) throw new ArgumentNullException(nameof(op), "null operator");

            if (left.IsName)
            {
                NameNode var = (NameNode)left.Operand;

                if (op == "=")
                {
                    env.Set(var.Name, rvalue, type);
                    return rvalue;
                }
                else
                {
                    object name = env.Get(var.Name, type);

                    if (name == null) throw new LepException("undefined name: " + var.Name, this);
                    else
                    {
                        object result = ComputeOperator(name, op.Substring(0, op.Length - 1), rvalue);
                        env.Set(var.Name, result, type);

                        return result;
                    }
                }
            }
            else
            {
                ArrayReferenceNode arrRef = left.Suffix(0) as ArrayReferenceNode;

                if (arrRef != null)
                {
                    object lvalue = left.EvaluateSub(env, 1, type);

                    object[] arr = lvalue as object[];

                    if (arr != null)
                    {
                        object index = arrRef.Index.Evaluate(env);
                        if (index is int) return arr[(int)index] = rvalue;
                    }

                    Dictionary<object, object> table = lvalue as Dictionary<object, object>;

                    if (table != null)
                    {
                        object index = arrRef.Index.Evaluate(env);
                        return table[index] = rvalue;
                    }
                }
            }

            throw new LepException("bad assignment", this);
        }
Ejemplo n.º 3
0
        public void Evaluate(Environment env, int index, object value)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env), "null environment");
            }

            IAstNode current = Parameter(index);

            FactorNode factor = current as FactorNode;

            if (factor == null || !factor.IsNoPrefixPrimary)
            {
                throw new LepException("bad parameter");
            }

            PrimaryNode primary = factor.Operand as PrimaryNode;

            if (primary == null || !primary.IsName)
            {
                throw new LepException("bad parameter");
            }


            NameNode name = primary.Operand as NameNode;

            if (name == null)
            {
                throw new LepException("bad parameter");
            }

            if (name.IsAnonymous)
            {
                return;
            }
            env.Set(name.Token.Text, value, Environment.LocalVariable);
        }
Ejemplo n.º 4
0
        private object Assign(Environment env, PrimaryNode left, string op, object rvalue, int type)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env), "null environment");
            }
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left), "null left name");
            }
            if (op == null)
            {
                throw new ArgumentNullException(nameof(op), "null operator");
            }

            if (left.IsName)
            {
                NameNode var = (NameNode)left.Operand;

                if (op == "=")
                {
                    env.Set(var.Name, rvalue, type);
                    return(rvalue);
                }
                else
                {
                    object name = env.Get(var.Name, type);

                    if (name == null)
                    {
                        throw new LepException("undefined name: " + var.Name, this);
                    }
                    else
                    {
                        object result = ComputeOperator(name, op.Substring(0, op.Length - 1), rvalue);
                        env.Set(var.Name, result, type);

                        return(result);
                    }
                }
            }
            else
            {
                ArrayReferenceNode arrRef = left.Suffix(0) as ArrayReferenceNode;

                if (arrRef != null)
                {
                    object lvalue = left.EvaluateSub(env, 1, type);

                    object[] arr = lvalue as object[];

                    if (arr != null)
                    {
                        object index = arrRef.Index.Evaluate(env);
                        if (index is int)
                        {
                            return(arr[(int)index] = rvalue);
                        }
                    }

                    Dictionary <object, object> table = lvalue as Dictionary <object, object>;

                    if (table != null)
                    {
                        object index = arrRef.Index.Evaluate(env);
                        return(table[index] = rvalue);
                    }
                }
            }

            throw new LepException("bad assignment", this);
        }