Пример #1
0
        public override object Evaluate(Environment env)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env), "null environment");
            }

            if (Operand is FactorNode && ((FactorNode)Operand).IsAssignable)
            {
                string prefix = ((AstLeaf)Prefix).Token.Text, name = ((NameNode)(((PrimaryNode)((FactorNode)Operand).Operand).Operand)).Name;

                object value = env.Get(name, ((FactorNode)Operand).AssignType);

                if (value == null)
                {
                    throw new LepException("undefined name: " + name, this);
                }
                else if (!(value is int))
                {
                    throw new LepException("bad type for " + prefix, this);
                }
                else
                {
                    int nvalue = (int)value + (prefix == "++" ? 1 : -1);
                    env.Set(name, nvalue, ((FactorNode)Operand).AssignType);

                    return(nvalue);
                }
            }
            else
            {
                throw new LepException("bad self change", this);
            }
        }
Пример #2
0
        public object Evaluate(Environment env, int type)
        {
            if (env == null) throw new ArgumentNullException(nameof(env), "null environment");

            object value = env.Get(Name, type);

            if (value == null) throw new LepException("undefined name: " + Name, this);
            else return value;
        }
Пример #3
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);
        }
Пример #4
0
        public object Evaluate(Environment env, int type)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env), "null environment");
            }

            object value = env.Get(Name, type);

            if (value == null)
            {
                throw new LepException("undefined name: " + Name, this);
            }
            else
            {
                return(value);
            }
        }
Пример #5
0
        public override object Evaluate(Environment env)
        {
            if (env == null) throw new ArgumentNullException(nameof(env), "null environment");

            if (Operand is FactorNode && ((FactorNode)Operand).IsAssignable)
            {
                string prefix = ((AstLeaf)Prefix).Token.Text, name = ((NameNode)(((PrimaryNode)((FactorNode)Operand).Operand).Operand)).Name;

                object value = env.Get(name, ((FactorNode)Operand).AssignType);

                if (value == null) throw new LepException("undefined name: " + name, this);
                else if (!(value is int)) throw new LepException("bad type for " + prefix, this);
                else
                {
                    int nvalue = (int)value + (prefix == "++" ? 1 : -1);
                    env.Set(name, nvalue, ((FactorNode)Operand).AssignType);

                    return nvalue;
                }
            }
            else throw new LepException("bad self change", this);
        }
Пример #6
0
        protected object Get(string name)
        {
            object value;

            return(_values.TryGetValue(name, out value) ? value : _father == null ? null : _father.Get(name));
        }
Пример #7
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);
        }