Ejemplo n.º 1
0
        public T EvalExpecting <T>(SExpr sexpr, ExternEvaluate customEval)
        {
            object val = customEval(this, sexpr, customEval);

            if (val.GetType() != typeof(T))
            {
                throw new Exception("Excepting a value of type {0} but got type {1}".F(typeof(T), val.GetType()));
            }

            return((T)val);
        }
Ejemplo n.º 2
0
        public object[] Eval(SExpr[] sexprs, ExternEvaluate customEval)
        {
            if (sexprs == null)
            {
                return(null);
            }
            if (sexprs.Length == 0)
            {
                return(new object[0]);
            }
            if (customEval == null)
            {
                customEval = defaultEval;
            }

            var results = new object[sexprs.Length];

            for (int i = 0; i < sexprs.Length; ++i)
            {
                results[i] = customEval(this, sexprs[i], customEval);
            }
            return(results);
        }
Ejemplo n.º 3
0
        public object Eval(SExpr sexpr, ExternEvaluate customEval)
        {
            if (customEval == null)
            {
                customEval = defaultEval;
            }

            sexpr.ThrowIfError();

            if (sexpr.Kind == SExprKind.ScopedIdentifier)
            {
                // Try to resolve the identifier:
                var          identExpr = (ScopedIdentifierExpr)sexpr;
                NamedStorage variable  = TryResolve(identExpr);
                if (variable == null)
                {
                    throw new Exception("Cannot find variable named '{0}' in scope".F(identExpr.Name));
                }

                return(variable.Value);
            }
            else if (sexpr.Kind == SExprKind.Invocation)
            {
                return(Invoke((InvocationExpr)sexpr));
            }
            else if (sexpr.Kind == SExprKind.List)
            {
                var le    = (ListExpr)sexpr;
                var items = new object[le.Count];
                for (int i = 0; i < items.Length; ++i)
                {
                    items[i] = customEval(this, le[i], customEval);
                }
                return(items);
            }
            else if (sexpr.Kind == SExprKind.Quote)
            {
                return(sexpr);
            }
            else if (sexpr.Kind == SExprKind.Integer)
            {
                return(((IntegerExpr)sexpr).Value);
            }
            else if (sexpr.Kind == SExprKind.Boolean)
            {
                return(((BooleanExpr)sexpr).Value);
            }
            else if (sexpr.Kind == SExprKind.Null)
            {
                return(null);
            }
            else if (sexpr.Kind == SExprKind.String)
            {
                return(sexpr.StartToken.Text);
            }
            else if (sexpr.Kind == SExprKind.Decimal)
            {
                return(((DecimalExpr)sexpr).Value);
            }
            else if (sexpr.Kind == SExprKind.Double)
            {
                return(((DoubleExpr)sexpr).Value);
            }
            else if (sexpr.Kind == SExprKind.Float)
            {
                return(((FloatExpr)sexpr).Value);
            }

            throw new Exception("Unknown expression kind: '{0}'".F(sexpr.Kind));
        }
Ejemplo n.º 4
0
 object defaultEval(Evaluator a, SExpr sexpr, ExternEvaluate customEval)
 {
     return(Eval(sexpr, customEval));
 }