public override SValue Evaluate(ExecEnvironment env) { SValue v = varValue.Evaluate(env); SValue ret = v; if (!makeImmutable && v.Immutable) { ret = v.Clone(); ret.Immutable = false; } if (makeImmutable) { ret.Immutable = true; } ret.RefDict = null; ret.RefList = null; if (env.ContainsKey(varName) && env[varName].Immutable) { throw new VMException(varName + ": variable is immutable", headAtom); } env.NewVar(varName, ret); return(ret); }
public override SValue Evaluate(ExecEnvironment env) { var subObj = host.Evaluate(env); var idx = this.index.Evaluate(env); if (idx is SNumber && subObj is SList) { if (subObj.Immutable) { throw new VMException("list is immutable", headAtom); } List <SValue> subList = subObj.Get <List <SValue> >(); subList.RemoveAt((int)idx.Get <Decimal>()); return(subObj); } else if (idx is SString && subObj is SDict) { if (subObj.Immutable) { throw new VMException("dict is immutable", headAtom); } Dictionary <string, SValue> dict = subObj.Get <Dictionary <string, SValue> >(); dict.Remove(idx.Get <String>()); return(subObj); } else { throw new VMException("deleting from a dict or a list requires a string key or a number index", headAtom); } }
public override SValue Evaluate(ExecEnvironment env) { SValue v = varValue.Evaluate(env); SValue ret = v; if (!makeImmutable && v.Immutable) { ret = v.Clone(); ret.Immutable = false; } if (makeImmutable) { ret.Immutable = true; } ret.RefDict = null; ret.RefList = null; if (nameExpr == null) { if (env.ContainsKey(varName) && env[varName].Immutable) { throw new VMException(varName + ": variable is immutable", headAtom); } if (makeImmutable) { env.NewVar(varName, ret); } else { env[varName] = ret; } } else { var n = nameExpr.Evaluate(env); if (n.RefDict?.Immutable == true || n.RefList?.Immutable == true) { throw new VMException(varName + ": variable is immutable", headAtom); } if (n.RefDict != null) { n.RefDict.Get <Dictionary <string, SValue> >()[n.RefDictKey] = ret; } else if (n.RefList != null) { n.RefList.Get <List <SValue> >()[n.RefListIndex] = ret; } else { throw new VMException("invalid variable setting", headAtom); } } return(ret); }
public override SValue Evaluate(ExecEnvironment env) { var msg = argument.Evaluate(env) as SString; if (msg == null) { throw new VMException("message must be a string", headAtom); } throw new VMException(msg.Get <String>(), headAtom); }
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); } }
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)); } }
public override SValue Evaluate(ExecEnvironment env) { if (condition.Evaluate(env).Get <bool>()) { return(trueBranch.Evaluate(env)); } else { if (falseBranch != null) { return(falseBranch.Evaluate(env)); } else { return(new SNull()); } } }
public override SValue Evaluate(ExecEnvironment env) { var cond = condition.Evaluate(env).Get <object>(); foreach (var b in branches) { if (b.Item1.Evaluate(env).Get <object>().Equals(cond)) { return(b.Item2.Evaluate(env)); } } if (defaultBranch != null) { return(defaultBranch.Evaluate(env)); } return(new SBool(false)); }
public override SValue Evaluate(ExecEnvironment env) { object subObj = this.type.Evaluate(env).Underlying; var type = subObj as Type; var mip = InteropHelper.BuildMethodPattern(this.arguments, env, headAtom); List <Type> pattern = mip.Item1; List <object> arguments = mip.Item2; var method = methodName.Evaluate(env) as SString; if (method == null) { throw new VMException("method name must be a string", headAtom); } if (type != null) { var m = type.GetMethod(method.Get <String>(), pattern.ToArray()); if (m == null) { throw new VMException("cannot get the method", headAtom); } return(InteropHelper.ObjectToSValue(m.Invoke(null, arguments.ToArray()))); } else if (subObj != null) { var m = subObj.GetType().GetMethod(method.Get <String>(), pattern.ToArray()); if (m == null) { throw new VMException("cannot get the method", headAtom); } return(InteropHelper.ObjectToSValue(m.Invoke(subObj, arguments.ToArray()))); } else { throw new VMException("calling on a null object", headAtom); } //DateTime.Today.GetDateTimeFormats(); }
public override SValue Evaluate(ExecEnvironment env) { return(new SObject(obj.Evaluate(env).Underlying)); }
public override SValue Evaluate(ExecEnvironment env) { return(convert(argument.Evaluate(env))); }
public override SValue Evaluate(ExecEnvironment env) { if (argument == null) { return(new SClosure(env, new List <string>() { "x" }, SExpression.Cast(SExpr.MakeLambda(func, "x")))); } var arg = argument.Evaluate(env); Decimal n = 0; if (arg is SNumber) { n = arg.Get <Decimal>(); } else if (arg is SBool) { n = arg.Get <bool>() ? 1 : 0; } switch (func) { case "sin": return(new SNumber((Decimal)Math.Sin((double)n))); case "cos": return(new SNumber((Decimal)Math.Cos((double)n))); case "tan": return(new SNumber((Decimal)Math.Tan((double)n))); case "asin": return(new SNumber((Decimal)Math.Asin((double)n))); case "acos": return(new SNumber((Decimal)Math.Acos((double)n))); case "atan": return(new SNumber((Decimal)Math.Atan((double)n))); case "round": return(new SNumber((Decimal)Math.Round((double)n))); case "floor": return(new SNumber((Decimal)Math.Floor((double)n))); case "abs": return(new SNumber((Decimal)Math.Abs((double)n))); case "sqrt": return(new SNumber((Decimal)Math.Sqrt((double)n))); case "random": if (n != 0) { var rand = new Random((int)(n % Int32.MaxValue)); return(new SNumber((Decimal)rand.NextDouble())); } else { return(new SNumber((Decimal)SExpression.DefaultRandom.NextDouble())); } case "time": var t = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))); return(new SNumber((Decimal)t.TotalMilliseconds / n)); case "not": return(new SBool(Math.Abs(n - 1) == 1)); default: throw new NotImplementedException(); } }
public override SValue Evaluate(ExecEnvironment env) { var newEnv = new ExecEnvironment(); SValue _closure; SClosure closure = null; if (lambdaObject == null) { if (env.ContainsKey(closureName)) { _closure = env[closureName].Evaluate(env); closure = _closure as SClosure; } else { _closure = new SString(closureName); env[closureName] = _closure; } } else { _closure = lambdaObject.Evaluate(env); closure = _closure as SClosure; } List <SValue> arguments = SExpression.EvalSExpressions(this.arguments, env); if (closure == null) { List <SValue> ret = new List <SValue>(); ret.Add(_closure); foreach (var a in arguments) { ret.Add(a); } return(new SList(ret)); } if (closure.Arguments.Count() > arguments.Count()) { var argNames = closure.Arguments.Skip(arguments.Count); return(new SClosure( env, argNames.ToList(), new SECall( closure, arguments.ConvertAll(a => (SExpression)a) .Concat(argNames.Select(a => new SEVariable(a, headAtom, tailCompound))).ToList(), headAtom, tailCompound ) )); } // prepare the executing environment for (int i = 0; i < closure.Arguments.Count(); i++) { string argName = closure.Arguments[i]; if (argName.Length > 3 && argName.Substring(argName.Length - 3) == "...") { argName = argName.Substring(0, argName.Length - 3); newEnv[argName] = new SList(arguments.Skip(i).ToList()); break; } else { newEnv[argName] = arguments[i]; } } newEnv["~parent"] = new SDict(closure.InnerEnv); newEnv["~atom"] = new SObject(headAtom); newEnv.ParentEnv = closure.InnerEnv; return(closure.Body.Evaluate(newEnv)); }
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())); }