public object Evaluate(Machine machine, ValueEnvironment environment) { // TODO get a predefined empty dictionary object IPersistentMap map = new DictionaryObject(new Hashtable()); foreach (DictionaryEntry entry in this.map) { object key = machine.Evaluate(entry.Key, environment); object value = machine.Evaluate(entry.Value, environment); map = map.Associate(key, value); } return map; }
public void AssociateToDictionaryObject() { IDictionary dict = new Hashtable(); dict["one"] = 0; dict["two"] = 2; DictionaryObject dictionary = new DictionaryObject(dict); IAssociative result = Operations.Associate(dictionary, "one", 1); Assert.IsNotNull(result); Assert.IsTrue(result.ContainsKey("one")); Assert.AreEqual(2, result.Count); Assert.AreEqual(1, result.ValueAt("one")); Assert.AreEqual(2, result.ValueAt("two")); }
public void SetUpDictionary() { Hashtable values = new Hashtable(); values["one"] = 1; values["two"] = 2; values["three"] = 3; this.dictionary = new DictionaryObject(values); }
public object ParseForm() { Token token = this.lexer.NextToken(); if (token == null) return null; if (token.TokenType == TokenType.Macro) { while (token != null && token.Value == ";") { this.lexer.SkipUpToEndOfLine(); token = this.lexer.NextToken(); } if (token == null) return null; if (token.TokenType == TokenType.Macro && token.Value == "'") { IList list = new ArrayList(); list.Add(quoteSymbol); list.Add(this.ParseForm()); return list; } if (token.TokenType == TokenType.Macro && token.Value == "^") { IList list = new ArrayList(); list.Add(metaSymbol); list.Add(this.ParseForm()); return list; } if (token.TokenType == TokenType.Macro && token.Value == "@") { IList list = new ArrayList(); list.Add(derefSymbol); list.Add(this.ParseForm()); return list; } if (token.TokenType == TokenType.Macro && token.Value == "#'") { IList list = new ArrayList(); list.Add(varSymbol); list.Add(this.ParseForm()); return list; } if (token.TokenType == TokenType.Macro && token.Value == "#^") { IDictionary metadata = null; token = this.lexer.NextToken(); if (token != null && token.Value == "{") metadata = this.ParseFormMap(); else { this.lexer.PushToken(token); object tag = this.ParseForm(); IDictionary dict = new Hashtable(); dict[tagKeyword] = tag; metadata = new DictionaryObject(dict); } object form = this.ParseForm(); return Utilities.ToObject(form).WithMetadata((IPersistentMap)metadata); } if (token.TokenType == TokenType.Macro && token.Value == "`") { IList list = new ArrayList(); list.Add(backquoteSymbol); list.Add(this.ParseForm()); return list; } if (token.TokenType == TokenType.Macro && token.Value == "~") { IList list = new ArrayList(); list.Add(unquoteSymbol); list.Add(this.ParseForm()); return list; } if (token.TokenType == TokenType.Macro && token.Value == "~@") { IList list = new ArrayList(); list.Add(unquoteSplicingSymbol); list.Add(this.ParseForm()); return list; } if (token.TokenType == TokenType.Macro) throw new ParserException(string.Format("Unknown macro {0}", token.Value)); } if (token.TokenType == TokenType.Symbol) { if (token.Value == "true") return true; if (token.Value == "false") return false; return Symbol.Create(token.Value); } if (token.TokenType == TokenType.Keyword) return Keyword.Create(token.Value); if (token.TokenType == TokenType.String) return token.Value; if (token.TokenType == TokenType.Integer) return Int32.Parse(token.Value); if (token.TokenType == TokenType.Character) return token.Value[0]; if (token.TokenType == TokenType.Separator && token.Value == "(") return this.ParseFormList(); if (token.TokenType == TokenType.Separator && token.Value == "[") return this.ParseFormArray(); if (token.TokenType == TokenType.Separator && token.Value == "{") return this.ParseFormMap(); throw new ParserException(string.Format("Unexpected token: {0}", token.Value)); }
public void EvaluateDictionaryObjectWithVariables() { Machine machine = new Machine(); IDictionary dict = new Hashtable(); dict["one"] = Utilities.ToVariable(machine, machine.Environment, Symbol.Create("symone")); dict["two"] = Utilities.ToVariable(machine, machine.Environment, Symbol.Create("symtwo")); DictionaryObject dictionary = new DictionaryObject(dict); machine.SetVariableValue(Utilities.ToVariable(machine, machine.Environment, Symbol.Create("symone")), 1); machine.SetVariableValue(Utilities.ToVariable(machine, machine.Environment, Symbol.Create("symtwo")), 2); object result = machine.Evaluate(dictionary); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IAssociative)); IAssociative associative = (IAssociative)result; Assert.AreEqual(2, associative.Count); Assert.IsTrue(associative.ContainsKey("one")); Assert.IsTrue(associative.ContainsKey("two")); Assert.AreEqual(1, associative.ValueAt("one")); Assert.AreEqual(2, associative.ValueAt("two")); }
public void EvaluateDictionaryObjectAsIAssociative() { IDictionary dict = new Hashtable(); dict["one"] = 1; dict["two"] = 2; DictionaryObject dictionary = new DictionaryObject(dict); Machine machine = new Machine(); object result = machine.Evaluate(dictionary); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IAssociative)); IAssociative associative = (IAssociative)result; Assert.AreEqual(2, associative.Count); Assert.IsTrue(associative.ContainsKey("one")); Assert.IsTrue(associative.ContainsKey("two")); Assert.AreEqual(1, associative.ValueAt("one")); Assert.AreEqual(2, associative.ValueAt("two")); }
public void RecognizeMapAsEvaluable() { IDictionary dict = new Hashtable(); DictionaryObject dictionary = new DictionaryObject(dict); Assert.IsTrue(Utilities.IsEvaluable(dictionary)); }
public void GetMapExpressionEvaluatingMap() { IDictionary dictionary = new Hashtable(); IPersistentMap map = new DictionaryObject(dictionary); object result = Utilities.ToExpression(map); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(MapExpression)); }