Exemple #1
0
        public ArrV(List <long> dims, Val init)
        {
            dim = dims[0];
            arr = new Val[dim];

            List <long> dimsCopy = new List <long>();

            for (int i = 1; i < dims.Count; i++) // Skip first dim on copy
            {
                dimsCopy.Add(dims[i]);
            }

            for (long i = 0; i < dim; i++)
            {
                if (dimsCopy.Count > 0) // Still more subdimensions to fill
                {
                    arr[i] = new ArrV(dimsCopy, init);
                }
                else
                {
                    arr[i] = (Val)init.Clone();
                }
            }
        }
Exemple #2
0
 public ArrV(ArrV other)
 {
     arr = (Val[])other.arr.Clone();
     dim = other.dim;
 }
Exemple #3
0
        public override Val interp(Environment env)
        {
            Val result;

            if (lexpr is IdC) // binding a variable
            {
                result = rexpr.interp(env);
                env[((IdC)lexpr).sym] = result;
            }
            else if (lexpr is FuncC) // defining a function
            {
                FuncC lexprFunc = (FuncC)lexpr;

                if (lexprFunc.func is IdC)
                {
                    List <string> parms = new List <string>();
                    foreach (ExprC parm in lexprFunc.args)
                    {
                        if (parm is IdC)
                        {
                            parms.Add(((IdC)parm).sym);
                        }
                        else
                        {
                            throw new InterpException("Malformed function definition params");
                        }
                    }
                    result = new CloV((Environment)env.Clone(), rexpr, parms);
                    ((CloV)result).env.SetLocal(((IdC)lexprFunc.func).sym, result); // allow recursion
                    env[((IdC)lexprFunc.func).sym] = result;
                }
                else
                {
                    throw new InterpException("Malformed function definition name");
                }
            }
            else if (lexpr is IndexC) // defining an array or setting an element
            {
                IndexC lexprArray = (IndexC)lexpr;

                if (lexprArray.arr is IdC)
                {
                    List <long> indices = new List <long>();
                    foreach (ExprC idx in lexprArray.indices)
                    {
                        Val idxVal = idx.interp(env);

                        if (idxVal is NumV)
                        {
                            indices.Add(((NumV)idxVal).num);
                        }
                        else
                        {
                            throw new InterpException("Non-numeric array index");
                        }
                    }

                    if (env.ContainsKey(((IdC)lexprArray.arr).sym)) // setting element
                    {
                        ArrV arr = lexprArray.arr.interp(env) as ArrV;
                        if (arr == null)
                        {
                            throw new InterpException("Indexing non-array");
                        }
                        result = rexpr.interp(env);
                        arr.SetValue(indices, result);
                    }
                    else // defining new array
                    {
                        result = new ArrV(indices, rexpr.interp(env));
                        env[((IdC)lexprArray.arr).sym] = result;
                    }
                }
                else
                {
                    throw new InterpException("Malformed array definition name");
                }
            }
            else
            {
                throw new InterpException("Binding with invalid lvalue");
            }

            return(result);
        }