Beispiel #1
0
        protected IEnumerable <ISExpression> EvalAll(string input)
        {
            var arr  = ParserHelpers.Parse(input);
            var carr = arr.Select(a => SchemeCompiler.Compile(VM, a));
            var s    = carr.Select(c => VM.Execute(c));

            return(s);
        }
Beispiel #2
0
        public override ISExpression Compile(ISchemeVM vm, string i)
        {
            var exprs = ParserHelpers.Parse(i);

            if (exprs.Length == 1)
            {
                return(exprs[0]);
            }

            return(AtomHelper.CreateCons(CompilerConstants.Begin, exprs.Unflatten()));
        }
Beispiel #3
0
        public static ISExpression Read(IEnumerable <ISExpression> args)
        {
            var input = "";

            while (true)
            {
                input += Console.ReadLine() + "\n";

                if (IsBalanced(input))
                {
                    var expr = ParserHelpers.Parse(input);
                    return(expr.First());
                }
            }
        }
Beispiel #4
0
        protected ISExpression Eval(string input)
        {
            var arr  = ParserHelpers.Parse(input);
            var carr = arr.Select(a => SchemeCompiler.Compile(VM, a));
            var s    = carr.Select(c => VM.Execute(c));

            if (s.Count() > 0)
            {
                return(s.First());
            }
            else
            {
                return(null);
            }
        }
Beispiel #5
0
        public void Parse_WhenString_ExpectParsedAsString()
        {
            // Arrange

            // Act
            var stopwatch = Stopwatch.StartNew();
            var obj       = ParserHelpers.Parse("Some text");

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var objAsString = obj as string;

            Assert.That(objAsString, Is.Not.Null);
            Assert.That(objAsString, Is.EqualTo("Some text"));
        }
Beispiel #6
0
        public void Parse_WhenInt_ExpectParsedAsInt()
        {
            // Arrange

            // Act
            var stopwatch = Stopwatch.StartNew();
            var obj       = ParserHelpers.Parse("123");

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var objAsInt = obj as int?;

            Assert.That(objAsInt, Is.Not.Null);
            Assert.That(objAsInt, Is.EqualTo(123));
        }
Beispiel #7
0
        public void Parse_WhenDecimal_ExpectParsedAsDecimal()
        {
            // Arrange

            // Act
            var stopwatch = Stopwatch.StartNew();
            var obj       = ParserHelpers.Parse("123.52");

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var objAsDecimal = obj as decimal?;

            Assert.That(objAsDecimal, Is.Not.Null);
            Assert.That(objAsDecimal, Is.EqualTo(123.52));
        }
Beispiel #8
0
        public void Parse_WhenDateTime_ExpectParsedAsDateTime()
        {
            // Arrange

            // Act
            var stopwatch = Stopwatch.StartNew();
            var obj       = ParserHelpers.Parse("2018-1-1");

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var objAsDateTime = obj as DateTime?;

            Assert.That(objAsDateTime, Is.Not.Null);
            Assert.That(objAsDateTime, Is.EqualTo(new DateTime(2018, 1, 1)));
        }
Beispiel #9
0
        /// <summary>
        /// Parses the statement.
        /// </summary>
        /// <param name="statement">The statement.</param>
        /// <param name="type">The type.</param>
        /// <returns>
        /// The <see cref="UnaryNode" />.
        /// </returns>
        public UnaryNode ParseStatement(string statement, string type = null)
        {
            var cleanedStatement = statement.Trim('"');

            var isNot = false;

            if (cleanedStatement.First() == '!')
            {
                isNot            = true;
                cleanedStatement = cleanedStatement.Substring(1);
            }

            var value = ParserHelpers.Parse(cleanedStatement, type);

            return(new EqualsOperator
            {
                Name = null,
                Value = value,
                IsNot = isNot,
                Statement = statement,
            });
        }
Beispiel #10
0
        /// <inheritdoc />
        public UnaryNode ParseStatement(string statement, string type = null)
        {
            var first = statement.First();
            var last  = statement.Last();

            bool lowerInclusive;

            switch (first)
            {
            case '[':
                lowerInclusive = false;
                break;

            case '{':
                lowerInclusive = true;
                break;

            default:
                throw new InvalidOperationException($"Character '{first}' is not a valid part of a range query");
            }

            bool upperInclusive;

            switch (last)
            {
            case ']':
                upperInclusive = false;
                break;

            case '}':
                upperInclusive = true;
                break;

            default:
                throw new InvalidOperationException($"Character '{last}' is not a valid part of a range query");
            }

            var parts = statement
                        .Substring(1, statement.Length - 2)
                        .Split(',')
                        .Select(s => s.Trim())
                        .ToList();

            // If the first part is a wildcard, determine the last part only
            if (parts[0] == "*")
            {
                var lowerVal = ParserHelpers.Parse(parts[1], type);

                return(new LessThanOperator
                {
                    Value = lowerVal,
                    Inclusive = upperInclusive,
                    Name = null,
                    Statement = statement,
                });
            }

            // If the last part is a wildcard, determine the first part only
            if (parts[1] == "*")
            {
                var upperVal = ParserHelpers.Parse(parts[0], type);

                return(new GreaterThanOperator
                {
                    Value = upperVal,
                    Inclusive = lowerInclusive,
                    Name = null,
                    Statement = statement,
                });
            }

            var lower = ParserHelpers.Parse(parts[0], type);
            var upper = ParserHelpers.Parse(parts[1], type);

            return(new RangeOperator
            {
                LowerInclusive = lowerInclusive,
                UpperInclusive = upperInclusive,
                Lower = lower,
                Upper = upper,
                Name = null,
                Statement = statement,
            });
        }