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 void SetAndGetValueIgnoringCase() { ValueEnvironment environment = new ValueEnvironment(); environment.SetValue("Foo", "bar"); Assert.AreEqual("bar", environment.GetValue("foo")); Assert.AreEqual("bar", environment.GetValue("FOO")); Assert.AreEqual("bar", environment.GetValue("fOO")); }
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 void ParseAndEvaluateSimpleLocalVariable() { Parser parser = new Parser(File.OpenText("SimpleLocalVariable.prg")); ICommand command = parser.ParseCommandList(); ValueEnvironment environment = new ValueEnvironment(ValueEnvironmentType.Public); command.Execute(null, environment); Assert.AreEqual("publicbar", environment.GetValue("bar")); Assert.AreEqual("localbar", environment.GetValue("foo")); }
public void SetAndGetPublicValue() { ValueEnvironment parent = new ValueEnvironment(ValueEnvironmentType.Public); ValueEnvironment environment = new ValueEnvironment(parent); environment.SetPublicValue("foo", "bar"); Assert.AreEqual("bar", environment.GetValue("foo")); Assert.AreEqual("bar", parent.GetValue("foo")); Assert.AreEqual("bar", environment.GetValue("FOO")); Assert.AreEqual("bar", parent.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 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 void DefineSetAndGetPublicValue() { ValueEnvironment parent = new ValueEnvironment(ValueEnvironmentType.Public); ValueEnvironment environment = new ValueEnvironment(parent); environment.SetPublicValue("foo", null); environment.SetValue("foo", "bar"); environment.SetValue("one", 1); Assert.AreEqual("bar", environment.GetValue("foo")); Assert.AreEqual("bar", parent.GetValue("foo")); Assert.AreEqual(1, environment.GetValue("one")); Assert.IsNull(parent.GetValue("one")); }
public void ParseAndEvaluateDataUseWorkArea() { Parser parser = new Parser(File.OpenText("DataUseWorkArea.prg")); ICommand command = parser.ParseCommandList(); ValueEnvironment environment = new ValueEnvironment(ValueEnvironmentType.Public); command.Execute(null, environment); object result = environment.GetValue("test"); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(WorkArea)); object result2 = environment.GetValue(ValueEnvironment.CurrentWorkArea); Assert.IsTrue(result == result2); }
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 ParseAndEvaluateSimpleFunction() { Parser parser = new Parser(File.OpenText("SimpleFunction.prg")); ICommand command = parser.ParseCommandList(); ValueEnvironment environment = new ValueEnvironment(ValueEnvironmentType.Public); command.Execute(null, environment); object func = environment.GetValue("Increment"); Assert.IsNotNull(func); Assert.IsInstanceOfType(func, typeof(Procedure)); object result = environment.GetValue("foo"); Assert.IsNotNull(result); Assert.AreEqual(2, result); }
public void SkipIfCommandsIfFalse() { Parser parser = new Parser("if 0\r\n a:=1\r\n b:=2\r\nendif"); ICommand command = parser.ParseCommand(); ValueEnvironment environment = new ValueEnvironment(); command.Execute(null, environment); object value = environment.GetValue("a"); Assert.IsNull(value); value = environment.GetValue("b"); Assert.IsNull(value); }
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 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 void ParseAndEvaluateSimpleIf() { Parser parser = new Parser(File.OpenText("SimpleIf.prg")); ICommand command = parser.ParseCommandList(); ValueEnvironment environment = new ValueEnvironment(); command.Execute(null, environment); Assert.AreEqual("positive", environment.GetValue("bar")); }
public static Type GetType(ValueEnvironment environment, string name) { object obj = environment.GetValue(name); if (obj != null && obj is Type) { return((Type)obj); } return(GetType(name)); }
public override void Execute(Machine machine, ValueEnvironment environment) { ValueEnvironment localenv = environment.GetLocalEnvironment(); foreach (string name in this.names) { if (localenv.GetValue(name) == null) { localenv.SetEnvironmentValue(name, null); } } }
public override void Execute(Machine machine, ValueEnvironment environment) { ValueEnvironment privateenv = environment.GetNormalEnvironment(); foreach (string name in this.names) { if (privateenv.GetValue(name) == null) { privateenv.SetEnvironmentValue(name, null); } } }
public void ParseAndExecuteSetVariableCommand() { Parser parser = new Parser("foo := \"bar\""); ICommand command = parser.ParseCommand(); ValueEnvironment environment = new ValueEnvironment(); command.Execute(null, environment); Assert.AreEqual("bar", environment.GetValue("foo")); }
public override void Execute(Machine machine, ValueEnvironment environment) { IList <object> values = new List <object>(); foreach (IExpression argument in this.arguments) { values.Add(argument.Evaluate(environment)); } Procedure procedure = (Procedure)environment.GetValue(this.name); procedure.Apply(values, environment); }
public void ParseAndEvaluateSimpleNewObject() { Parser parser = new Parser(File.OpenText("SimpleNewObject.prg")); ICommand command = parser.ParseCommandList(); ValueEnvironment environment = new ValueEnvironment(ValueEnvironmentType.Public); command.Execute(null, environment); object result = environment.GetValue("foo"); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(System.IO.FileInfo)); }
public void ExecuteIfCommandWithMultipleCommands() { Parser parser = new Parser("if 1\r\n a:=1\r\n b:=2\r\nendif"); ICommand command = parser.ParseCommand(); ValueEnvironment environment = new ValueEnvironment(); command.Execute(null, environment); object value = environment.GetValue("a"); Assert.IsNotNull(value); Assert.IsInstanceOfType(value, typeof(int)); Assert.AreEqual(1, (int)value); value = environment.GetValue("b"); Assert.IsNotNull(value); Assert.IsInstanceOfType(value, typeof(int)); Assert.AreEqual(2, (int)value); }
public void ParseAndEvaluateSimpleProcedure() { Parser parser = new Parser(File.OpenText("SimpleProcedure.prg")); ICommand command = parser.ParseCommandList(); ValueEnvironment environment = new ValueEnvironment(ValueEnvironmentType.Public); command.Execute(null, environment); object result = environment.GetValue("setbar"); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(Procedure)); }
public static Type GetType(ValueEnvironment environment, Identifier identifier) { string name = identifier.Name; object obj = environment.GetValue(name); if (obj != null && obj is Type) { return((Type)obj); } return(GetType(name)); }
public void SetAndGetValueInParentEnvironment() { ValueEnvironment parent = new ValueEnvironment(); ValueEnvironment environment = new ValueEnvironment(parent); parent.SetValue("foo", "bar"); environment.SetValue("foo", "newbar"); Assert.AreEqual("newbar", environment.GetValue("foo")); Assert.AreEqual("newbar", parent.GetValue("foo")); Assert.IsTrue(parent.ContainsValue("foo")); Assert.IsFalse(environment.ContainsValue("foo")); Assert.IsTrue(parent.ContainsValue("Foo")); Assert.IsFalse(environment.ContainsValue("Foo")); }
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 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 void ExecuteSimpleWhile() { Parser parser = new Parser("while a\r\n a:=0\r\nenddo"); ICommand command = parser.ParseCommand(); ValueEnvironment environment = new ValueEnvironment(); environment.SetValue("a", 1); command.Execute(null, environment); object value = environment.GetValue("a"); Assert.IsNotNull(value); Assert.IsInstanceOfType(value, typeof(int)); Assert.AreEqual(0, (int)value); }
public object Evaluate(ValueEnvironment environment) { Procedure procedure = (Procedure)environment.GetValue(this.name); object[] parameters = null; if (this.arguments != null) { List <object> values = new List <object>(); foreach (IExpression argument in this.arguments) { values.Add(argument.Evaluate(environment)); } parameters = values.ToArray(); } return(procedure.Apply(parameters, environment)); }
public override object Evaluate(ValueEnvironment environment) { return(environment.GetValue(this.name)); }
public object Evaluate(ValueEnvironment env) { return(env.GetValue(this.value)); }
public void GetNullIfNoValue() { ValueEnvironment environment = new ValueEnvironment(); Assert.IsNull(environment.GetValue("foo")); }