Пример #1
0
        public virtual TreeNode OnCompareGreaterThanOrEqual(TreeNode [] args)
        {
            BooleanLiteral a = (BooleanLiteral)OnCompareGreaterThan(args);
            BooleanLiteral b = (BooleanLiteral)OnCompareEqual(args);

            return(new BooleanLiteral(a.Value || b.Value));
        }
Пример #2
0
        public virtual TreeNode OnIf(TreeNode [] args)
        {
            if (args == null || args.Length < 2 || args.Length > 3)
            {
                throw new ArgumentException("if accepts 2 or 3 arguments");
            }

            TreeNode arg = Evaluate(args[0]);

            if (!(arg is BooleanLiteral))
            {
                throw new ArgumentException("first if argument must be boolean");
            }

            BooleanLiteral conditional = (BooleanLiteral)arg;

            if (conditional.Value)
            {
                return(Evaluate(args[1]));
            }
            else if (args.Length == 3)
            {
                return(Evaluate(args[2]));
            }

            return(new VoidLiteral());
        }
Пример #3
0
        private TreeNode OnAndOr(TreeNode [] args, bool and)
        {
            if (args.Length < 2)
            {
                throw new ArgumentException("must have two or more boolean arguments");
            }

            bool result = false;

            for (int i = 0; i < args.Length; i++)
            {
                TreeNode node = Evaluate(args[i]);
                if (!(node is BooleanLiteral))
                {
                    throw new ArgumentException("arguments must be boolean");
                }

                BooleanLiteral arg = (BooleanLiteral)node;

                if (i == 0)
                {
                    result = arg.Value;
                    continue;
                }

                if (and)
                {
                    result &= arg.Value;
                }
                else
                {
                    result |= arg.Value;
                }
            }

            return(new BooleanLiteral(result));
        }
Пример #4
0
        public virtual TreeNode OnCompareNotEqual(TreeNode [] args)
        {
            BooleanLiteral result = (BooleanLiteral)OnCompareEqual(args);

            return(new BooleanLiteral(!result.Value));
        }
Пример #5
0
        void TokenPush(bool as_string)
        {
            if (current_token.Length == 0 && !as_string)
            {
                return;
            }

            string token = current_token.ToString();

            if (Debug)
            {
                Console.Write("{3}[{0}] TOKEN({4},{5}): [{2}{1}{2}]", scope, token,
                              as_string ? "\"" : string.Empty, string.Empty.PadLeft(scope - 1, ' '),
                              line, column - current_token.Length);
            }


            TreeNode node;

            if (as_string)
            {
                node = new StringLiteral(token);
            }
            else if (token == "#t")
            {
                node = new BooleanLiteral(true);
            }
            else if (token == "#f")
            {
                node = new BooleanLiteral(false);
            }
            else if (token.Length > 0 && token != "." && token != "-" &&
                     token != "+" && number_regex.IsMatch(token))
            {
                try {
                    if (token.StartsWith("0x") || token.StartsWith("-0x"))
                    {
                        int offset = token[0] == '-' ? 3 : 2;
                        int value  = int.Parse(token.Substring(offset),
                                               NumberStyles.HexNumber, culture_info.NumberFormat);
                        node = new IntLiteral(value * (offset == 3 ? -1 : 1));
                    }
                    else if (token.Contains("."))
                    {
                        node = new DoubleLiteral(double.Parse(token,
                                                              NumberStyles.Float, culture_info.NumberFormat));
                    }
                    else
                    {
                        node = new IntLiteral(int.Parse(token,
                                                        NumberStyles.Integer, culture_info.NumberFormat));
                    }
                } catch {
                    throw new FormatException("Invalid number format: " + token);
                }
            }
            else
            {
                node = new FunctionNode(token);
            }

            if (Debug)
            {
                Console.WriteLine(" => [{0}]", node);
            }

            node.Line   = line;
            node.Column = column;
            current_parent.AddChild(node);

            current_token.Remove(0, current_token.Length);
        }