Exemple #1
0
        public override SValue Evaluate(ExecEnvironment env)
        {
            SValue lenValue = dict.Evaluate(env);

            if (lenValue is SDict)
            {
                return(new SList((from k in lenValue.Get <Dictionary <string, SValue> >().Keys.ToList()
                                  select(SValue) new SString(k)).ToList()));
            }
            else
            {
                throw new VMException("it only take a dict as the argument", headAtom);
            }
        }
Exemple #2
0
        public override SValue Evaluate(ExecEnvironment env)
        {
            SValue lenValue = argument.Evaluate(env);

            if (lenValue is SString)
            {
                return(new SNumber(lenValue.Get <string>().Length));
            }
            else if (lenValue is SList)
            {
                return(new SNumber(lenValue.Get <List <SValue> >().Count));
            }
            else
            {
                return(new SNumber(0));
            }
        }
Exemple #3
0
        private SValue execLoop(SClosure body, SValue v, Decimal idx)
        {
            var newEnv = new ExecEnvironment();

            if (body.Arguments.Count >= 1)
            {
                newEnv[body.Arguments[0]] = v;
            }

            if (body.Arguments.Count == 2)
            {
                newEnv[body.Arguments[1]] = new SNumber(idx);
            }

            newEnv.ParentEnv = body.InnerEnv;
            return(body.Body.Evaluate(newEnv));
        }
Exemple #4
0
        public override SValue Evaluate(ExecEnvironment env)
        {
            var body = this.body.Evaluate(env) as SClosure;

            if (body == null)
            {
                throw new VMException("the second argument must be a lambda or a named function", headAtom);
            }

            SValue        _list  = this.list.Evaluate(env);
            List <SValue> values = new List <SValue>();
            SList         list   = new SList(values);

            bool whileLoop      = false;
            bool condAlwaysTrue = false;

            if (_list is SList)
            {
                list   = _list as SList;
                values = list.Get <List <SValue> >();
                if (values.Count == 0)
                {
                    whileLoop      = true;
                    condAlwaysTrue = true;
                }
            }
            else if (_list is SBool)
            {
                whileLoop      = true;
                condAlwaysTrue = false;
            }
            else
            {
                throw new VMException("the first argument must be a list or a bool", headAtom);
            }

            if (whileLoop)
            {
                while (condAlwaysTrue || this.list.Evaluate(env).Get <bool>())
                {
                    var ret = execLoop(body, new SNull(), 0);
                    if (ret.Is <bool>() && !ret.Get <bool>())
                    {
                        break;
                    }
                }
            }
            else
            {
                for (var i = 0; i < values.Count; i++)
                {
                    var ret = execLoop(body, values[i], i);
                    if (ret.Is <bool>() && !ret.Get <bool>())
                    {
                        break;
                    }
                }
            }


            return(new SBool(true));
        }
Exemple #5
0
        public override SValue Evaluate(ExecEnvironment env)
        {
            SValue ret = (nameExpr == null) ? ret = env[vname].Evaluate(env) : nameExpr.Evaluate(env);

            return(new SString(ret.GetType().Name.Substring(1).ToLower()));
        }