コード例 #1
0
        private ParseResult BinOp(Func <ParseResult> fn, params Token[] tokenTypes)
        {
            var leftParse = fn();

            if (leftParse.HasError())
            {
                return(leftParse);
            }
            var left = leftParse.Node;

            while (tokenTypes.Contains(current_token.Type))
            {
                var op_tok = current_token;
                Advance();
                var rightParse = fn();
                if (rightParse.HasError())
                {
                    return(rightParse);
                }
                var right = rightParse.Node;
                left = new BinOpNode(left, op_tok, right).SetPos(left.Pos_Start, right.Pos_End);
            }
            return(left);
        }
コード例 #2
0
ファイル: Interpreter.cs プロジェクト: ajayn1986/Basic-Lang
        public InterpreterResult Visit(BinOpNode node, Context context)
        {
            InterpreterResult result     = null;
            InterpreterResult leftResult = Visit(node.Left, context);

            if (leftResult.Error != null)
            {
                return(leftResult);
            }
            if (leftResult.Result is Null && new[] { TokenType.EE, TokenType.NE }.Contains(node.Op_tok.Type))
            {
                Null left = leftResult.Result;
                InterpreterResult rightResult = Visit(node.Right, context);
                if (rightResult.Error != null)
                {
                    return(rightResult);
                }

                dynamic right = rightResult.Result;
                if (node.Op_tok.Type == TokenType.EE)
                {
                    result = left.ComparisonEq(right);
                }
                if (node.Op_tok.Type == TokenType.NE)
                {
                    result = left.ComparisonNe(right);
                }
                if (result.Error == null)
                {
                    result.Result.SetPos(left.Start_pos, right.End_pos);
                }
                return(result);
            }
            else if (leftResult.Result is Binary && new[] { TokenType.EE, TokenType.NE }.Contains(node.Op_tok.Type))
            {
                Binary            left        = leftResult.Result;
                InterpreterResult rightResult = Visit(node.Right, context);
                if (rightResult.Error != null)
                {
                    return(rightResult);
                }
                if (rightResult.Result is Null)
                {
                    Null rightNull = rightResult.Result as Null;
                    if (node.Op_tok.Type == TokenType.EE)
                    {
                        result = rightNull.ComparisonEq(left);
                    }
                    if (node.Op_tok.Type == TokenType.NE)
                    {
                        result = rightNull.ComparisonNe(left);
                    }
                    if (result.Error == null)
                    {
                        result.Result.SetPos(left.Start_pos, rightNull.End_pos);
                    }
                    return(result);
                }
                if (!(rightResult.Result is Binary))
                {
                    return(new InterpreterResult(null, new RuntimeError($"Expected boolean expression", node.Right.Pos_Start, node.Right.Pos_End, context)));
                }
                Binary right = rightResult.Result;
                if (node.Op_tok.Type == TokenType.EE)
                {
                    result = left.ComparisonEq(right);
                }
                if (node.Op_tok.Type == TokenType.NE)
                {
                    result = left.ComparisonNe(right);
                }
                if (result.Error == null)
                {
                    result.Result.SetPos(left.Start_pos, right.End_pos);
                }
                return(result);
            }
            else
            {
                if (!(leftResult.Result is Number))
                {
                    return(new InterpreterResult(null, new RuntimeError($"Expected number", node.Left.Pos_Start, node.Left.Pos_End, context)));
                }
                Number            left        = leftResult.Result;
                InterpreterResult rightResult = Visit(node.Right, context);
                if (rightResult.Error != null)
                {
                    return(rightResult);
                }
                if (rightResult.Result is Null)
                {
                    Null rightNull = rightResult.Result as Null;
                    if (node.Op_tok.Type == TokenType.EE)
                    {
                        result = rightNull.ComparisonEq(left);
                    }
                    if (node.Op_tok.Type == TokenType.NE)
                    {
                        result = rightNull.ComparisonNe(left);
                    }
                    if (result.Error == null)
                    {
                        result.Result.SetPos(left.Start_pos, rightNull.End_pos);
                    }
                    return(result);
                }
                if (!(rightResult.Result is Number))
                {
                    return(new InterpreterResult(null, new RuntimeError($"Expected number", node.Right.Pos_Start, node.Right.Pos_End, context)));
                }
                Number right = rightResult.Result;
                if (node.Op_tok.Type == TokenType.Plus)
                {
                    result = left.Added_To(right);
                }
                if (node.Op_tok.Type == TokenType.Minus)
                {
                    result = left.Subbed_by(right);
                }
                if (node.Op_tok.Type == TokenType.Mul)
                {
                    result = left.Multed_by(right);
                }
                if (node.Op_tok.Type == TokenType.Div)
                {
                    result = left.Dived_by(right);
                }
                if (node.Op_tok.Type == TokenType.Pow)
                {
                    result = left.Powed_by(right);
                }
                if (node.Op_tok.Type == TokenType.LT)
                {
                    result = left.ComparisonLt(right);
                }
                if (node.Op_tok.Type == TokenType.GT)
                {
                    result = left.ComparisonGt(right);
                }
                if (node.Op_tok.Type == TokenType.LTE)
                {
                    result = left.ComparisonLte(right);
                }
                if (node.Op_tok.Type == TokenType.GTE)
                {
                    result = left.ComparisonGte(right);
                }
                if (node.Op_tok.Type == TokenType.EE)
                {
                    result = left.ComparisonEq(right);
                }
                if (node.Op_tok.Type == TokenType.NE)
                {
                    result = left.ComparisonNe(right);
                }
                if (result.Error == null)
                {
                    result.Result.SetPos(left.Start_pos, right.End_pos);
                }
                return(result);
            }
        }