public T EvalExpecting <T>(SExpr sexpr) { object val = Eval(sexpr, null); 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); }
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); }
public QuoteExpr(Token start, Token end, SExpr sexpr) : base(SExprKind.Quote, start, end) { SExpr = sexpr; }
public object Eval(SExpr sexpr) { return(Eval(sexpr, defaultEval)); }
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)); }
object defaultEval(Evaluator a, SExpr sexpr, ExternEvaluate customEval) { return(Eval(sexpr, customEval)); }