Пример #1
0
        public static void Main()
        {
            while (true)
            {
                Console.Write("> ");

                string input = Console.ReadLine();

                if (input == "")
                {
                    continue;
                }

                if (input == "exit")
                {
                    return;
                }

                SyntaxLexer  lexer       = new SyntaxLexer();
                SyntaxParser parser      = new SyntaxParser();
                Interpreter  interpreter = new Interpreter();

                List <SyntaxToken> tokens = lexer.Lex(input);

                SyntaxParseResult result = parser.Parse(tokens);

                foreach (Diagnostic diagnostic in result.Diagnostics)
                {
                    Console.WriteLine($"{diagnostic.Severity}: {diagnostic.Message} at {diagnostic.Range.Start}");
                }

                DustObject value = interpreter.Interpret(result.Node);

                Console.WriteLine($"{value.ToString().Value} ({value.TypeName})");
            }
        }
Пример #2
0
        private DustObject EvaluateBinaryExpression(BoundBinaryExpression binaryExpression)
        {
            DustObject left  = EvaluateExpression(binaryExpression.Left);
            DustObject right = EvaluateExpression(binaryExpression.Right);

            BoundBinaryOperator @operator = binaryExpression.Operator;

            if (@operator.IsInterchangeable && !left.IsAssignableFrom(@operator.LeftType))
            {
                (left, right) = (right, left);
            }

            switch (binaryExpression.Operator.Kind)
            {
            case BinaryOperatorKind.Add:
                return(left.Add(right));

            case BinaryOperatorKind.Subtract:
                return(left.Subtract(right));

            case BinaryOperatorKind.Multiply:
                return(left.Multiply(right));

            case BinaryOperatorKind.Divide:
                return(left.Divide(right));

            /*case BinaryOperatorKind.Modulo:
             * if (left is double || right is double)
             * {
             *  return (double) left % (double) right;
             * }
             *
             * if (left is float || right is float)
             * {
             *  return (float) left % (float) right;
             * }
             *
             * return (int) left % (int) right;
             * case BinaryOperatorKind.Exponentiate:
             * return Convert.ChangeType(Math.Pow(Convert.ToDouble(left), Convert.ToDouble(right)), binaryExpression.Type.ToNativeType());
             * case BinaryOperatorKind.Equal:
             * {
             * if (left is bool leftBoolValue && right is bool rightBoolValue)
             * {
             *  return leftBoolValue == rightBoolValue;
             * }
             *
             * if (left is double || right is double)
             * {
             *  return (double) left == (double) right;
             * }
             *
             * if (left is float || right is float)
             * {
             *  return (float) left == (float) right;
             * }
             *
             * if (left is int || right is int)
             * {
             *  return (int) left == (int) right;
             * }
             *
             * throw new Exception("Equal only implemented for booleans.");
             * }
             * case BinaryOperatorKind.NotEqual:
             * {
             * if (left is bool leftBoolValue && right is bool rightBoolValue)
             * {
             *  return leftBoolValue != rightBoolValue;
             * }
             *
             * if (left is double || right is double)
             * {
             *  return (double) left != (double) right;
             * }
             *
             * if (left is float || right is float)
             * {
             *  return (float) left != (float) right;
             * }
             *
             * if (left is int || right is int)
             * {
             *  return (int) left != (int) right;
             * }
             *
             * throw new Exception("NotEqual only implemented for booleans.");
             * }
             * case BinaryOperatorKind.And:
             * return (bool) left && (bool) right;
             * case BinaryOperatorKind.Or:
             * return (bool) left || (bool) right;
             * case BinaryOperatorKind.GreaterThan:
             * return Convert.ToDouble(left) > Convert.ToDouble(right);
             * case BinaryOperatorKind.GreaterThanEqual:
             * return Convert.ToDouble(left) >= Convert.ToDouble(right);
             * case BinaryOperatorKind.LessThan:
             * return Convert.ToDouble(left) < Convert.ToDouble(right);
             * case BinaryOperatorKind.LessThanEqual:
             * return Convert.ToDouble(left) <= Convert.ToDouble(right);*/
            default:
                return(null);
            }
        }