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 override void Execute(Machine machine, ValueEnvironment environment) { string name = EvaluateUtilities.EvaluateAsName(this.nameExpression, environment); string commandText = null; if (this.commandExpression != null) { commandText = (string)this.commandExpression.Evaluate(environment); } Database database = (Database)environment.GetValue(ValueEnvironment.CurrentDatabase); WorkArea workarea; if (commandText != null) { IDbCommand command = database.ProviderFactory.CreateCommand(); command.CommandText = commandText; command.Connection = database.GetConnection(); workarea = new WorkArea(name, command, database.ProviderFactory); } else { workarea = new WorkArea(name, database.GetConnection(), database.ProviderFactory); } environment.SetPublicValue(name, workarea); environment.SetPublicValue(ValueEnvironment.CurrentWorkArea, workarea); }
public override void Execute(Machine machine, ValueEnvironment environment) { while (ExpressionUtilities.IsTrue(this.condition.Evaluate(environment))) { this.command.Execute(machine, environment); } }
internal ValueEnvironment MakeEnvironment(List arguments, ValueEnvironment parent) { ValueEnvironment nenv = new ValueEnvironment(parent); if (Predicates.IsNil(this.arguments)) { return(nenv); } if (Predicates.IsIdentifier(this.arguments)) { nenv.SetValue(((Identifier)this.arguments).Name, arguments); return(nenv); } List argnames = (List)this.arguments; List argvalues = arguments; Identifier argname; object argvalue; while (!Predicates.IsNil(argnames) && !Predicates.IsNil(argvalues)) { argname = (Identifier)argnames.First; argvalue = argvalues.First; nenv.SetValue(argname.Name, argvalue); argnames = argnames.Next; argvalues = argvalues.Next; } return(nenv); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { object name = arguments[0]; object result = machine.Evaluate(name, environment); Type type = null; if (!(result is Type)) { if (name is INamed || name is string) type = Utilities.GetType(name); if (type == null) throw new ArgumentException("New should receive a type name"); } else type = (Type)result; object[] parameters = new object[arguments.Length - 1]; for (int k = 1; k < arguments.Length; k++) parameters[k - 1] = machine.Evaluate(arguments[k], environment); return Activator.CreateInstance(type, parameters); }
public override void Execute(Machine machine, ValueEnvironment environment) { if (this.expression != null) { this.expression.Evaluate(environment); } }
internal ValueEnvironment MakeEnvironment(List arguments, ValueEnvironment parent) { ValueEnvironment nenv = new ValueEnvironment(parent); nenv.SetValue(this.argumentName.Name, arguments); return(nenv); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { object result = null; ValueEnvironment newenv = null; foreach (object argument in arguments) { if (newenv == null) { newenv = new ValueEnvironment(environment); if (argument != null) { if (!(argument is ICollection)) throw new InvalidOperationException("Let must receive a list as first argument"); Utilities.EvaluateBindings(machine, newenv, (ICollection)argument); } } else result = machine.Evaluate(argument, newenv); } return result; }
public override void Execute(Machine machine, ValueEnvironment environment) { foreach (IExpression expression in this.expressions) { System.Console.Write(expression.Evaluate(environment)); } }
public void CreateEnvironmentAsLocal() { ValueEnvironment environment = new ValueEnvironment(ValueEnvironmentType.Local); Assert.AreEqual(ValueEnvironmentType.Local, environment.Type); Assert.IsNull(environment.GetNonLocalEnvironment()); }
public void SetAndGetValue() { ValueEnvironment environment = new ValueEnvironment(); environment.SetValue("foo", "bar"); Assert.AreEqual("bar", environment.GetValue("foo")); }
public object Evaluate(ValueEnvironment environment) { string name = EvaluateUtilities.EvaluateAsName(this.nameExpression, environment); object value = environment.GetValue(name); Type type = null; type = TypeUtilities.GetType(environment, name); object[] parameters = null; if (this.arguments != null && this.arguments.Count > 0) { List <object> values = new List <object>(); foreach (IExpression argument in this.arguments) { values.Add(argument.Evaluate(environment)); } parameters = values.ToArray(); } return(Activator.CreateInstance(type, parameters)); }
public object Apply(IList <object> parameters, ValueEnvironment environment) { ValueEnvironment normalenv = new ValueEnvironment(environment); if (this.parameterNames != null) { for (int k = 0; k < this.parameterNames.Count; k++) { normalenv.SetValue(this.parameterNames[k], parameters[k]); } } ValueEnvironment localenv = new ValueEnvironment(normalenv, ValueEnvironmentType.Local); try { this.command.Execute(this.machine, localenv); } catch (ReturnException ex) { return(ex.Value); } return(null); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { ICollection argumentNames; IExpression body; if (arguments[0] is Symbol) { if (arguments.Length > 1 && arguments[1] != null && !(arguments[1] is IPersistentVector)) return this.ApplyMultiFunction(machine, environment, arguments); Symbol symbol = (Symbol)arguments[0]; argumentNames = (ICollection)arguments[1]; this.CheckArgumentNames(argumentNames); body = Utilities.ToExpression(arguments[2]); return new DefinedFunction(symbol.Name, argumentNames, body); } if (arguments.Length > 1 && arguments[0] != null && !(arguments[0] is IPersistentVector)) return this.ApplyMultiFunction(machine, environment, arguments); argumentNames = (ICollection)arguments[0]; this.CheckArgumentNames(argumentNames); body = Utilities.ToExpression(arguments[1]); return new DefinedFunction(null, argumentNames, body); }
public void RaiseIfNoPublicEnvironmentInSetPublicValue() { ValueEnvironment parent = new ValueEnvironment(); ValueEnvironment environment = new ValueEnvironment(parent); environment.SetPublicValue("foo", "bar"); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { if (arguments == null || arguments.Length == 0) return null; return arguments[0]; }
public object Evaluate(Machine machine, ValueEnvironment environment) { if (this.elements == null || this.elements.Count == 0) return null; IExpression formhead = (IExpression)Utilities.ToExpression(this.elements[0]); IFunction function = (IFunction)formhead.Evaluate(machine, environment); if (function == null) { if (this.elements[0] is INamed) throw new InvalidOperationException(string.Format("Unknown form {0}", ((INamed)this.elements[0]).FullName)); else throw new InvalidOperationException(string.Format("Unknown form {0}", this.elements[0].ToString())); } object[] arguments = null; if (this.elements.Count > 1) arguments = new object[this.elements.Count - 1]; if (function.IsSpecialForm) for (int k = 1; k < this.elements.Count; k++) arguments[k - 1] = this.elements[k]; else for (int k = 1; k < this.elements.Count; k++) arguments[k - 1] = machine.Evaluate(this.elements[k], environment); return function.Apply(machine, environment, arguments); }
public override void Execute(Machine machine, ValueEnvironment environment) { foreach (ICommand command in this.commands) { command.Execute(machine, environment); } }
public override object Evaluate(ValueEnvironment environment) { object leftValue = this.leftExpression.Evaluate(environment); object rightValue = this.rightExpression.Evaluate(environment); return(this.EvaluateValues(leftValue, rightValue)); }
public object ApplyToObject(object obj, Machine machine, ValueEnvironment environment, object[] arguments) { Type type = obj.GetType(); INamed named = null; object[] pars = null; if (arguments[1] is IList) { IList parameters = (IList)arguments[1]; named = (INamed)parameters[0]; pars = new object[parameters.Count - 1]; for (int k = 1; k < parameters.Count; k++) pars[k - 1] = machine.Evaluate(parameters[k], environment); } else { named = (INamed)arguments[1]; pars = new object[arguments.Length - 2]; for (int k = 2; k < arguments.Length; k++) pars[k - 2] = machine.Evaluate(arguments[k], environment); } return type.InvokeMember(named.Name, System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null, obj, pars); }
public override object Execute(List arguments, ValueEnvironment environment) { ValueEnvironment nenv; nenv = this.MakeEnvironment(arguments, environment); return(this.ExecuteBody(nenv)); }
public void RaiseIfNoPublicEnvironmentInGetPublicEnvironment() { ValueEnvironment parent = new ValueEnvironment(); ValueEnvironment environment = new ValueEnvironment(parent); environment.GetPublicEnvironment(); }
public override object Execute(List arguments, ValueEnvironment env) { ValueEnvironment newenv; newenv = MakeEnvironment(arguments, this.environment); return(ExecuteBody(newenv)); }
public override object Execute(object argument1, object argument2, ValueEnvironment environment) { Identifier atom = (Identifier)argument1; environment.SetValue(atom.Name, argument2); return(argument2); }
public override object Execute(List args, ValueEnvironment env) { ValueEnvironment nenv; nenv = this.MakeEnvironment(args, env); return(Machine.Evaluate(this.ExecuteBody(nenv), env)); }
public void SetGlobalValueInTopEnvironmentUsingChild() { ValueEnvironment child = new ValueEnvironment(this.environment); child.SetGlobalValue("foo", "bar"); Assert.AreEqual("bar", this.environment.GetValue("foo")); Assert.AreEqual("bar", child.GetValue("foo")); }
public void GetPublicEnvironment() { ValueEnvironment parent = new ValueEnvironment(ValueEnvironmentType.Public); ValueEnvironment environment = new ValueEnvironment(parent); Assert.AreEqual(parent, environment.GetPublicEnvironment()); Assert.AreEqual(parent, parent.GetPublicEnvironment()); }
public void SetGlobalAndLocalValue() { ValueEnvironment child = new ValueEnvironment(this.environment); child.SetGlobalValue("foo", "bar"); child.SetValue("foo", "bar2"); Assert.AreEqual("bar", this.environment.GetValue("foo")); Assert.AreEqual("bar2", child.GetValue("foo")); }
public object Evaluate(ValueEnvironment environment) { if (this.expression == null) { return(null); } return(this.expression.Evaluate(environment)); }
public override object Execute(List arguments, ValueEnvironment environment) { if (arguments == null) { return(Activator.CreateInstance(this.type)); } return(Activator.CreateInstance(this.type, arguments.ToObjectArray())); }
public override object Execute(List arguments, ValueEnvironment environment) { ValueEnvironment newenv; newenv = MakeEnvironment((List)arguments.First, environment); FSubrProgN progn = new FSubrProgN(); return(progn.Execute(arguments.Next, newenv)); }
private static List EvaluateList(List arguments, ValueEnvironment environment) { if (arguments == null) { return(null); } return(new List(Machine.Evaluate(arguments.First, environment), EvaluateList(arguments.Next, environment))); }
public void ShouldEvaluateName() { IExpression expression = new NameExpression("foo"); ValueEnvironment environment = new ValueEnvironment(); environment.SetValue("foo", "bar"); Assert.AreEqual("bar", expression.Evaluate(environment)); }
public void SetVariable() { ICommand command = new SetVariableCommand("foo", new ConstantExpression("bar")); ValueEnvironment environment = new ValueEnvironment(); command.Execute(null, environment); Assert.AreEqual("bar", environment.GetValue("foo")); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { if (arguments == null || arguments.Length != 1) throw new InvalidOperationException("Closure should have an argument"); IFunction function = (IFunction)arguments[0]; return new FnFunction(function, machine, environment); }
public object Evaluate(Machine machine, ValueEnvironment environment) { object[] result = new object[this.vector.Count]; for (int k = 0; k < this.vector.Length; k++) result[k] = machine.Evaluate(this.vector[k], environment); return PersistentVector.Create(result); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { object result = null; foreach (object argument in arguments) result = machine.Evaluate(argument, environment); return result; }
public void ParseAndExecuteDotExpressionWithArguments() { ValueEnvironment environment = new ValueEnvironment(); environment.SetValue("dinfo", new System.IO.DirectoryInfo(".")); IExpression expression = ParseExpression("dinfo.GetFiles(\"*.exe\")"); object result = expression.Evaluate(environment); Assert.IsNotNull(result); }
public void ParseAndEvaluateSimplePublicVariable() { Parser parser = new Parser(File.OpenText("SimplePublicVariable.prg")); ICommand command = parser.ParseCommandList(); ValueEnvironment environment = new ValueEnvironment(ValueEnvironmentType.Public); command.Execute(null, environment); Assert.AreEqual("foo", environment.GetValue("bar")); }
public void GetValueFromParent() { ValueEnvironment parent = new ValueEnvironment(); ValueEnvironment environment = new ValueEnvironment(parent); parent.SetValue("foo", "bar"); Assert.AreEqual("bar", environment.GetValue("foo")); Assert.AreEqual("bar", environment.GetValue("FOO")); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { object result = 1; if (arguments == null) return result; foreach (object argument in arguments) result = Numbers.Multiply(result, argument); return result; }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { if (arguments == null || arguments.Length == 0) throw new InvalidOperationException("Divide should have an argument at least"); if (arguments.Length == 1) return Numbers.Divide(1, arguments[0]); object result = arguments[0]; for (int k = 1; k < arguments.Length; k++) result = Numbers.Divide(result, arguments[k]); return result; }
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 object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { Symbol symbol = (Symbol)arguments[0]; string ns = symbol.Namespace; if (string.IsNullOrEmpty(ns)) ns = (string)environment.GetValue(Machine.CurrentNamespaceKey); Variable variable = machine.GetVariable(ns, symbol.Name); if (variable == null) throw new InvalidOperationException(string.Format("Unable to resolve Variable from Symbol {0}", symbol.FullName)); return variable; }
public object ApplyMultiFunction(Machine machine, ValueEnvironment environment, object[] arguments) { Symbol symbol = null; List<DefinedFunction> functions = new List<DefinedFunction>(); if (arguments[0] is Symbol) symbol = (Symbol)arguments[0]; for (int k = (symbol == null) ? 0 : 1; k < arguments.Length; k++) { object[] funarguments = (new ArrayList((IList)arguments[k])).ToArray(); functions.Add((DefinedFunction)this.Apply(machine, environment, funarguments)); } return new DefinedMultiFunction((symbol == null) ? null : symbol.Name, functions); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { object name = arguments[0]; object result = machine.Evaluate(name, environment); Type type = null; if (name is INamed || name is string) type = Utilities.GetType(name); if (type != null) return this.ApplyToType(type, machine, environment, arguments); return this.ApplyToObject(result, machine, environment, arguments); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { Symbol symbol = (Symbol)arguments[0]; object value = arguments[1]; if (Utilities.IsEvaluable(value)) { IExpression expression = Utilities.ToExpression(arguments[1]); value = expression.Evaluate(machine, environment); } environment.SetValue(symbol.FullName, value); return value; }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { if (arguments == null || arguments.Length == 0) return null; if (arguments[0] is Symbol) { Symbol symbol = (Symbol)arguments[0]; if (string.IsNullOrEmpty(symbol.Namespace)) return Symbol.Create((string)environment.GetValue(Machine.CurrentNamespaceKey), symbol.Name); return symbol; } return MacroUtilities.Expand(arguments[0], machine, environment); }
public object Evaluate(Machine machine, ValueEnvironment environment) { IDictionary result = new Hashtable(); foreach (object key in this.dictionary.Keys) { object value = this.dictionary[key]; object newkey = machine.Evaluate(key, environment); object newvalue = machine.Evaluate(value, environment); result[newkey] = newvalue; } return result; }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { if (arguments == null || arguments.Length < 2) throw new InvalidOperationException("If should receive a test and a body"); if (arguments.Length > 3) throw new InvalidOperationException("Too many arguments to If"); object result = machine.Evaluate(arguments[0], environment); if (Utilities.IsFalse(result)) { if (arguments.Length == 3) return machine.Evaluate(arguments[2], environment); return null; } else return machine.Evaluate(arguments[1], environment); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { object[] argumentNames; IList body; if (arguments[0] is Symbol) { Symbol symbol = (Symbol)arguments[0]; argumentNames = (object[])arguments[1]; body = (IList)arguments[2]; return new DefinedMacro(symbol.Name, argumentNames, body); } argumentNames = (object[])arguments[0]; body = (IList)arguments[1]; return new DefinedMacro(null, argumentNames, body); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { ICollection argumentNames; IExpression body; if (arguments[0] is Symbol) { Symbol symbol = (Symbol)arguments[0]; argumentNames = (ICollection)arguments[1]; body = Utilities.ToExpression(arguments[2]); return new DefinedSpecialForm(symbol.Name, argumentNames, body); } argumentNames = (ICollection)arguments[0]; body = Utilities.ToExpression(arguments[1]); return new DefinedFunction(null, argumentNames, body); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { if (arguments == null || arguments.Length == 0) return null; object result = null; ValueEnvironment newenv = null; string[] names = null; newenv = new ValueEnvironment(environment); object argument = arguments[0]; if (argument != null) { if (!(argument is ICollection)) throw new InvalidOperationException("Let must receive a list as first argument"); names = Utilities.EvaluateBindings(machine, newenv, (ICollection)argument); } for (int k = 1; k < arguments.Length; k++) result = machine.Evaluate(arguments[k], newenv); while (result != null && result is RecursionData) { RecursionData data = (RecursionData)result; if (Utilities.GetArity(data.Arguments) != Utilities.GetArity(names)) throw new InvalidOperationException("Invalid recursion data"); newenv = new ValueEnvironment(environment); result = null; for (int k = 0; k < names.Length; k++) newenv.SetValue(names[k], data.Arguments[k]); for (int k = 1; k < arguments.Length; k++) result = machine.Evaluate(arguments[k], newenv); } return result; }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { Symbol symbol = (Symbol)arguments[0]; if (!string.IsNullOrEmpty(symbol.Namespace)) throw new InvalidOperationException("Defined name should not have namespace"); Variable variable = Utilities.ToVariable(machine, environment, symbol); object value = machine.Evaluate(arguments[1], environment); if (symbol.Metadata != null) { IDictionary dictionary = (IDictionary)symbol.Metadata; IDictionary evaluated = new Hashtable(); foreach (object key in dictionary.Keys) { object val = machine.Evaluate(dictionary[key], environment); evaluated[key] = val; } variable.ResetMetadata(new DictionaryObject(evaluated)); } else variable.ResetMetadata(null); IPersistentMap metadata = variable.Metadata; if (metadata != null && metadata.ContainsKey(macroKeyword)) if ((bool)metadata.ValueAt(macroKeyword) && value is DefinedFunction) value = ((DefinedFunction)value).ToMacro(); machine.SetVariableValue(variable, value); if (value is IFunction && ((IFunction)value).IsSpecialForm) machine.Environment.SetValue(variable.Name, value, true); return value; }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { if (arguments == null || arguments.Length == 0) throw new InvalidOperationException("Less or Equal should have an argument at least"); if (arguments.Length == 1) return true; bool result = true; object argument = null; for (int k = 0; k < arguments.Length; k++) { if (k > 0) result = result && (Utilities.Compare(argument, arguments[k]) <= 0); if (!result) return false; argument = arguments[k]; } return true; }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { throw (Exception)arguments[0]; }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { return new RecursionData(arguments); }
public object Evaluate(Machine machine, ValueEnvironment environment) { return machine.GetVariableValue(this.variable); }
public object Apply(Machine machine, ValueEnvironment environment, object[] arguments) { return environment; }
public object Evaluate(Machine machine, ValueEnvironment environment) { return this.value; }