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) { if (!env.ContainsKey(varName)) { SValue tmp = (varName.First() == '@') ? (SValue) new SString(varName.Substring(1), true) : new SNull(true); if (env.ContainsKey(varName)) { return(env[varName]); } env[varName] = tmp; return(tmp); } var imd = env[varName].Evaluate(env); if (imd is SClosure && (imd as SClosure).Arguments.Count == 0) { // it is a closure that accepts 0 arguments, so just directly evaluate it return((imd as SClosure).Body.Evaluate(env)); } else { return(imd); } }
public override SValue Evaluate(ExecEnvironment env) { var text = this.text.Evaluate(env) as SString; if (text == null) { throw new VMException("must eval a string", headAtom); } if (this.env != null) { var e = this.env.Evaluate(env) as SDict; if (e == null) { throw new VMException("the second argument must be a dict if provided", headAtom); } env = new ExecEnvironment(); foreach (var kv in e.Get <Dictionary <string, SValue> >()) { env[kv.Key] = kv.Value; } } var p = new Parser(); var s = p.Parse(text.Get <String>(), "", "<eval>"); return(SExpression.Cast(s).Evaluate(env)); }
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); }
private string printSValue(SValue re, ExecEnvironment env, int padding) { if (re is SList) { return("(" + String.Join(" ", from v in re.Get <List <SValue> >() select printSValue(v.Evaluate(env), env, padding)) + ")"); } else if (re is SDict) { return("\n" + "(".PadLeft(padding - 1) + "\n" + String.Join("\n", re.Get <Dictionary <String, SValue> >().Select(kv => "".PadLeft(padding) + kv.Key + "=" + printSValue(kv.Value.Evaluate(env), env, padding + 2) )) + "\n" + ")".PadLeft(padding - 1)); } else if (re is SNull) { return("null"); } else { return(String.Format("{0}", re.Underlying)); } }
public override SValue Evaluate(ExecEnvironment env) { object subObj = this.obj.Evaluate(env).Underlying; var propName = this.propName.Evaluate(env) as SString; if (propName == null) { throw new VMException("property name must be a string", headAtom); } if (subObj == null) { throw new VMException("property operation on a null object", headAtom); } var info = subObj.GetType().GetMember(propName.Get <String>()).Select(m => { if (m is FieldInfo) { return(((FieldInfo)m).GetValue(subObj)); } else { return(((PropertyInfo)m).GetValue(subObj, null)); } }); return(InteropHelper.ObjectToSValue(info)); }
private bool compareNumber(int[] trueSigns, ExecEnvironment env) { if (arguments.Count < 2) { throw new VMException("it takes at least 2 arguments", headAtom); } for (var i = 0; i < arguments.Count() - 1; i++) { var v1 = arguments[i].Evaluate(env); var v2 = arguments[i + 1].Evaluate(env); if (v1.Is <Decimal>() && v2.Is <Decimal>()) { var sgn = Math.Sign(v1.Get <Decimal>() - v2.Get <Decimal>()); if (!trueSigns.Contains(sgn)) { return(false); } } else { throw new VMException("inconsistent comparison of types", headAtom); } } return(true); }
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 dict = this.dict.Evaluate(env); for (var i = 0; i < keys.Count; i++) { if (dict is SDict) { var key = this.keys[i].Evaluate(env) as SString; if (key == null) { throw new VMException("it only take a string as the key", headAtom); } var d = dict.Get <Dictionary <String, SValue> >(); var k = key.Get <String>(); if (!d.ContainsKey(k)) { d[k] = new SNull(); } d[k].RefDict = dict as SDict; d[k].RefDictKey = k; d[k].RefList = null; dict = d[k]; } else if (dict is SList) { var index = this.keys[i].Evaluate(env) as SNumber; if (index == null) { throw new VMException("it only take a number as the index", headAtom); } var l = dict.Get <List <SValue> >(); var idx = (int)index.Get <Decimal>(); if (idx >= l.Count) { throw new VMException("index out of range", headAtom); } l[idx].RefList = dict as SList; l[idx].RefListIndex = idx; l[idx].RefDict = null; dict = l[idx]; } else { throw new VMException("it only take a list or a dict as the first argument", headAtom); } } return(dict); }
public override SValue Evaluate(ExecEnvironment env) { SValue ret = new SBool(false); foreach (SExpression se in expressions) { ret = se.Evaluate(env); } 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) { var type = Type.GetType((this.typeName.Evaluate(env) as SString)?.Get <String>()); if (type == null) { throw new VMException("cannot get type", headAtom); } return(new SObject(type)); }
public override SValue Evaluate(ExecEnvironment env) { var arguments = SExpression.EvalSExpressions(this.arguments, env); var subObj = arguments[0]; try { if (subObj is SString) { SString subStr = subObj as SString; if (arguments.Count == 2) { return(new SString(subStr.Get <string>().Substring( (int)arguments[1].Get <Decimal>() ))); } else { return(new SString(subStr.Get <string>().Substring( (int)arguments[1].Get <Decimal>(), (int)arguments[2].Get <Decimal>() ))); } } else if (subObj is SList) { SList subList = subObj as SList; if (arguments.Count == 2) { return(new SList(subList.Get <List <SValue> >().Skip( (int)arguments[1].Get <Decimal>() ).ToList())); } else { return(new SList(subList.Get <List <SValue> >().Skip( (int)arguments[1].Get <Decimal>()).Take( (int)arguments[2].Get <Decimal>() ).ToList())); } } else { throw new VMException("the first argument must be a string or a list", headAtom); } } catch { throw new VMException("invalid arguments", headAtom); } }
public override SValue Evaluate(ExecEnvironment env) { var listObj = this.list.Evaluate(env); if (listObj is SList) { return(new SList(listObj.Get <List <SValue> >().Skip(1).ToList())); } else { throw new VMException("it can only get the tail of a list", headAtom); } }
public override SValue Evaluate(ExecEnvironment env) { SValue ret = new SBool(false); foreach (var e in SExpression.EvalSExpressions(arguments, env)) { ret = e; Console.Write(printSValue(e, env, 2)); } Console.Write(delim); return(ret); }
public override SValue Evaluate(ExecEnvironment env) { var listObj = this.list.Evaluate(env); if (listObj is SList) { var list = listObj.Get <List <SValue> >(); return(list.Count > 0 ? list[0] : new SNull()); } else { throw new VMException("it can only get the head of a list", headAtom); } }
BuildMethodPattern(List <SExpression> args, ExecEnvironment env, SExprAtomic headAtom) { List <Type> pattern = new List <Type>(); List <object> arguments = new List <object>(); args.ForEach(arg => { var a = arg.Evaluate(env); if (a is SList && (a as SList).Get <List <SValue> >().Count == 2) { var list = (a as SList).Get <List <SValue> >(); var typeName = list[0] as SString; if (typeName == null) { throw new VMException("type name must be a string", headAtom); } var t = Type.GetType(typeName.Get <String>()); pattern.Add(t); if (t == typeof(Object)) { arguments.Add(list[1].Underlying); } else { arguments.Add(Convert.ChangeType(list[1].Underlying, t)); } } else { if (a is SString) { pattern.Add(typeof(string)); arguments.Add((string)a.Underlying); } else if (a is SBool) { pattern.Add(typeof(bool)); arguments.Add((bool)a.Underlying); } else { throw new VMException("you must specify a type to avoid ambiguousness", headAtom); } } }); return(new Tuple <List <Type>, List <object> >(pattern, arguments)); }
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 object ExecuteFile(string filePath, ExecEnvironment ee) { string code = File.ReadAllText(filePath); var p = new Parser(); var path = EugineVM.GetDirectoryName(filePath); var source = Path.GetFileName(filePath); ee["~path"] = new SString(path); ee["~source"] = new SString(source); var s = p.Parse(code, path, source); return(SExpression.Cast(s).Evaluate(ee).Get <object>()); }
public override SValue Evaluate(ExecEnvironment env) { var list = this.list.Evaluate(env); if (list is SList) { return(new SExploded(list.Get <List <SValue> >())); } else { return(new SExploded(new List <SValue>() { list })); } }
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)); } }
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)); }
public override SValue Evaluate(ExecEnvironment env) { Dictionary <String, SValue> ret = new Dictionary <String, SValue>(); foreach (var v in values) { var c = v.Evaluate(env).Get <List <SValue> >(); var key = c[0] as SString; if (key == null) { throw new VMException("key must be string", headAtom); } ret[key.Get <String>()] = c[1]; } return(new SDict(ret)); }
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 static List <SValue> EvalSExpressions(List <SExpression> arguments, ExecEnvironment env) { List <SValue> ret = new List <SValue>(); foreach (var e in arguments) { var v = e.Evaluate(env); if (v is SExploded) { ret.AddRange((v as SExploded).Comps); } else { ret.Add(v); } } return(ret); }
public object ExecuteString(string code, ExecEnvironment ee) { var p = new Parser(); var path = ""; var source = ""; if (ee.ContainsKey("~path")) { path = ee["~path"].Evaluate(null).Get <String>(); } if (ee.ContainsKey("~source")) { source = ee["~source"].Evaluate(null).Get <String>(); } var s = p.Parse(code, path, source); return(SExpression.Cast(s).Evaluate(ee).Get <object>()); }
public override SValue Evaluate(ExecEnvironment env) { var text = this.text.Evaluate(env) as SString; if (text == null) { throw new VMException("the first argument must be a string", headAtom); } var _delim = this.delim.Evaluate(env); List <string> delims = new List <string>(); if (_delim is SString) { delims.Add(_delim.Get <String>()); } else if (_delim is SList) { _delim.Get <List <SValue> >().ForEach(v => { var _v = v.Evaluate(env) as SString; if (_v != null) { delims.Add(_v.Get <String>()); } }); } else { throw new VMException("the second argument must be a string or a list of strings", headAtom); } if (delims.Count > 0) { var ret = text.Get <String>().Split(delims.ToArray(), StringSplitOptions.RemoveEmptyEntries); return(new SList(ret.Select(r => (SValue)(new SString(r))).ToList())); } else { return(new SList(text.Get <String>().ToCharArray() .Select(c => (SValue)(new SString(c.ToString()))).ToList())); } }
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(); }
private SValue tryPlus(ExecEnvironment env) { object f = arguments[0].Evaluate(env).Underlying; for (var i = 1; i < arguments.Count(); i++) { var n = arguments[i].Evaluate(env); if (f is String && n.Underlying is String) { f = (String)f + (String)n.Underlying; } else if (f is Decimal && n.Underlying is Decimal) { f = (Decimal)f + (Decimal)n.Underlying; } else if (f is List <SValue> ) { f = ((List <SValue>)f).Select(v => v).ToList(); ((List <SValue>)f).Add(n); } else { throw new VMException("it only apply to numbers, strings or lists", headAtom); } } if (f is String) { return(new SString(f as String)); } else if (f is Decimal) { return(new SNumber((decimal)f)); } else if (f is List <SValue> ) { return(new SList(f as List <SValue>)); } else { throw new VMException("failed plus", headAtom); } }