示例#1
0
        public void TestAssignAndGetVariable()
        {
            var variable = new VariableExpression("test");
            var value    = new IntegerConstantExpression(99);
            var scope    = new InterpreterScope();

            scope.AssignVariable(variable, value);
            Assert.That(scope.GetVariable("test"), Is.SameAs(value));
        }
示例#2
0
        public void TestDefineAndGetVariableNested()
        {
            var variable = new VariableDefinitionExpression("test");
            var value    = new IntegerConstantExpression(99);
            var scope    = new InterpreterScope();

            scope.DefineVariable(variable, value);
            var scope2 = new InterpreterScope(scope);

            Assert.That(scope2.GetVariable("test"), Is.SameAs(value));
            Assert.That(scope.GetVariable("test"), Is.SameAs(value));
        }
示例#3
0
        protected override void OnFormatLine(LineFormatEventArgs e)
        {
            int line        = e.Line.Line;
            var expressions = new List <ExpressionBase>();

            _parsedContent.GetExpressionsForLine(expressions, line);

            foreach (var expression in expressions)
            {
                string tooltip = null;

                if (_scope != null)
                {
                    var variable = expression as VariableExpression;
                    if (variable != null)
                    {
                        var value = _scope.GetVariable(variable.Name);
                        if (value != null)
                        {
                            tooltip = BuildTooltip(value);
                        }
                    }

                    var functionCall = expression as FunctionCallExpression;
                    if (functionCall != null)
                    {
                        var function = _scope.GetFunction(functionCall.FunctionName.Name);
                        if (function != null && function.Expressions.Count == 1)
                        {
                            tooltip = BuildTooltip(function.Expressions.First());
                        }
                    }
                }

                var expressionStart = (expression.Line == line) ? expression.Column : 1;
                var expressionEnd   = (expression.EndLine == line) ? expression.EndColumn : e.Line.Text.Length + 1;

                var parseError = expression as ParseErrorExpression;
                if (parseError != null)
                {
                    e.SetError(expressionStart, expressionEnd - expressionStart + 1, parseError.Message);
                }
                else
                {
                    e.SetColor(expressionStart, expressionEnd - expressionStart + 1, (int)expression.Type, tooltip);
                }
            }

            base.OnFormatLine(e);
        }
示例#4
0
        public void TestMerge()
        {
            var scope1 = new InterpreterScope();

            scope1.DefineVariable(new VariableDefinitionExpression("a"), new IntegerConstantExpression(1));

            var scope2 = new InterpreterScope();

            scope2.DefineVariable(new VariableDefinitionExpression("a"), new IntegerConstantExpression(4));
            scope2.DefineVariable(new VariableDefinitionExpression("d"), new IntegerConstantExpression(5));
            scope2.AddFunction(new FunctionDefinitionExpression("f1"));

            var scope3 = new InterpreterScope();

            scope3.DefineVariable(new VariableDefinitionExpression("b"), new IntegerConstantExpression(2));
            scope3.DefineVariable(new VariableDefinitionExpression("c"), new IntegerConstantExpression(3));

            var scope4 = new InterpreterScope();

            scope4.AddFunction(new FunctionDefinitionExpression("f1"));
            scope4.AddFunction(new FunctionDefinitionExpression("f2"));

            var union = new InterpreterScope();

            Assert.That(union.VariableCount, Is.EqualTo(0));
            Assert.That(union.FunctionCount, Is.EqualTo(0));

            union.Merge(scope1);
            Assert.That(union.VariableCount, Is.EqualTo(1));
            Assert.That(union.FunctionCount, Is.EqualTo(0));

            union.Merge(scope2);
            Assert.That(union.VariableCount, Is.EqualTo(2));
            Assert.That(union.FunctionCount, Is.EqualTo(1));
            Assert.That(union.GetVariable("a"), Is.EqualTo(new IntegerConstantExpression(4)));

            union.Merge(scope3);
            Assert.That(union.VariableCount, Is.EqualTo(4));
            Assert.That(union.FunctionCount, Is.EqualTo(1));

            union.Merge(scope4);
            Assert.That(union.VariableCount, Is.EqualTo(4));
            Assert.That(union.FunctionCount, Is.EqualTo(2));
        }
示例#5
0
        public void TestGetVariableUndefined()
        {
            var scope = new InterpreterScope();

            Assert.That(scope.GetVariable("undefined"), Is.Null);
        }