Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="tokens"></param>
        public VarNode(string[] tokens) : base(tokens)
        {
            var parser = new Parse.Var(tokens);

            parser.ParseLhs();
            parser.ParseRhs();

            Lhs        = parser.Lhs;
            Rhs        = parser.Rhs;
            RhsType    = parser.RhsType;
            IsConstant = parser.IsConstant;
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="tokens"></param>
        public IfNode(string[] tokens) : base(tokens)
        {
            var parser = new Parse.If(tokens);

            Lhs                = parser.Lhs;
            Rhs                = parser.Rhs;
            IfType             = parser.Type;
            LhsType            = parser.LhsType;
            RhsType            = parser.RhsType;
            InverseEval        = parser.InverseEval;
            ComparisonOperator = parser.ComparisonOperator;
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="tokens"></param>
        public UseNode(string[] tokens) : base(tokens)
        {
            var parser = new Parse.Use(tokens);

            Arg = parser.Arg;
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="tokens"></param>
        /// <exception cref="ArgumentException"></exception>
        public If(IEnumerable <string> tokens) : base(tokens)
        {
            InverseEval = false;
            Type        = IfType.Unknown;

            Consume();
            switch (Peek())
            {
            case Tokens.TokenType.BooleanFalse:
            case Tokens.TokenType.BooleanTrue:
            {
                var(_, s) = Consume();
                Type      = s == Tokens.TokenType.BooleanFalse ?
                            IfType.EvaluatedToFalse : IfType.EvaluatedToTrue;
                Ensure(0);
                return;
            }
            }

            if (Peek() == Tokens.TokenType.NotOperator)
            {
                InverseEval = true;
                Consume();
            }

            (Lhs, LhsType) = Consume();
            var _ = LhsType switch
            {
                Tokens.TokenType.Variable => true,
                Tokens.TokenType.Numeric => true,
                Tokens.TokenType.StringLiteral => true,
                _ => throw new ArgumentException("parse: invalid LHS for if")
            };

            if (Empty())
            {
                Type = IfType.VariableLookup;
                return;
            }

            // ==, !=, <, <=, >, >=
            // is, is not
            // consume the first part of the operator
            var(c, compType) = Consume();
            var comp = (string)c;

            if (compType == Tokens.TokenType.Is)
            {
                // is -> ==
                comp = "==";

                // is not -> !=
                if (Peek() == Tokens.TokenType.Not)
                {
                    comp = "!=";
                    Consume(); // not
                }
            }
            else
            {
                // <=, >=
                if (Peek() == Tokens.TokenType.EqualTo)
                {
                    var(t, _) = Consume();
                    comp     += (string)t;
                }
            }

            ComparisonOperator = Tokens.GetTokenType(comp);
            var isValid = ComparisonOperator switch
            {
                Tokens.TokenType.LessThan => true,
                Tokens.TokenType.LessThanEq => true,
                Tokens.TokenType.GreaterThan => true,
                Tokens.TokenType.GreaterThanEq => true,
                Tokens.TokenType.CompEq => true,
                Tokens.TokenType.NotEqual => true,
                _ => false
            };

            if (!isValid)
            {
                throw new ArgumentException($"fatal: invalid comparison operator {comp}");
            }

            (Rhs, RhsType) = Consume();
            _ = RhsType switch
            {
                Tokens.TokenType.Variable => true,
                Tokens.TokenType.Numeric => true,
                Tokens.TokenType.StringLiteral => true,
                _ => throw new ArgumentException("parse: invalid RHS for if")
            };

            Ensure(0);
            if (Type == IfType.Unknown)
            {
                Type = IfType.SimpleComparison;
            }
        }
    }
}