public object Evaluate(Machine machine, ValueEnvironment environment) { string nsname = null; if (string.IsNullOrEmpty(this.symbol.Namespace)) { // TODO this lookup should be for special forms if (machine.Environment.IsDefined(this.symbol.Name)) return machine.Environment.GetValue(this.symbol.Name); // Test if it is a Type // TODO import treatment if (this.symbol.Name.IndexOf('.') > 0) { Type type = Utilities.GetType(this.symbol.Name); if (type != null) return type; } if (environment.IsDefined(this.symbol.Name)) return environment.GetValue(this.symbol.Name); nsname = (string)environment.GetValue(Machine.CurrentNamespaceKey); } else nsname = this.symbol.Namespace; return machine.GetVariableValue(nsname, this.symbol.Name); }
public object Evaluate(Machine machine, ValueEnvironment environment) { return machine.GetVariableValue(this.variable); }
public void SetMacro(Machine machine) { object obj = machine.GetVariableValue(this); if (obj is DefinedFunction) { obj = ((DefinedFunction)obj).ToMacro(); machine.SetVariableValue(this, obj); } else if (obj is DefinedMultiFunction) { obj = ((DefinedMultiFunction)obj).ToMacro(); machine.SetVariableValue(this, obj); } else if (!(obj is DefinedMacro)) throw new InvalidOperationException(); if (this.metadata != null) this.metadata = this.metadata.Associate(Keyword.Create("macro"), true); else { IDictionary dict = new Hashtable(); dict[Keyword.Create("macro")] = true; this.metadata = new DictionaryObject(dict); } }
public void SetMacroOnDefinedMultiFunction() { Machine machine = new Machine(); machine.CreateNamespace("ns"); FnStarPrimitive fnprim = new FnStarPrimitive(); Parser parser = new Parser("([x] (+ x 1)) ([x y] (+ x y 1))"); object[] parameters = new object[2]; parameters[0] = parser.ParseForm(); parameters[1] = parser.ParseForm(); object result = fnprim.Apply(machine, machine.Environment, parameters); DefinedMultiFunction multifn = (DefinedMultiFunction)result; Variable variable = Variable.Intern(machine, "ns/func"); machine.SetVariableValue(variable, multifn); variable.SetMacro(machine); object mresult = machine.GetVariableValue(variable); Assert.IsNotNull(mresult); Assert.IsInstanceOfType(mresult, typeof(DefinedMultiMacro)); }
public void SetMacroOnDefinedFunction() { Machine machine = new Machine(); machine.CreateNamespace("ns"); Variable variable = Variable.Intern(machine, "ns/func"); Parser parser = new Parser("[x y] (list x y) 1 2"); object argumentNames = parser.ParseForm(); object body = parser.ParseForm(); DefinedFunction func = new DefinedFunction("simple-list", (ICollection)argumentNames, Utilities.ToExpression(body)); machine.SetVariableValue(variable, func); variable.SetMacro(machine); object result = machine.GetVariableValue(variable); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefinedMacro)); }