Пример #1
0
        private SyntaxTree ParseTerm(int precedenceLevel)
        {
            Token      token = CurrentToken;
            SyntaxTree tree  = null;
            Operator   op;

            if (TryResolveOperator(token.TokenType, true, out op) && GetOperatorAryness(op) == Aryness.Unary)
            {
                Match(CurrentToken.TokenType);
                UnaryOperatorTree unaryOperator = new UnaryOperatorTree(token);
                unaryOperator.Operator = op;
                unaryOperator.Operand  = ParseExpression(GetPrecedenceLevel(op));
                tree = unaryOperator;
            }
            else if (token.TokenType == TokenType.OpenParenthesis)
            {
                Match(TokenType.OpenParenthesis);
                tree = ParseMathExpression(0);
                Match(TokenType.CloseParenthesis);
            }
            else
            {
                tree = ParseFactorOrFunctionCall();
            }
            return(tree);
        }
Пример #2
0
        protected virtual string EmitUnaryOperator(UnaryOperatorTree op)
        {
            string code = string.Empty;

            switch (op.Operator)
            {
            case Operator.UnaryNot:
                code = string.Format(
                    "!{0}",
                    EmitExpression(op.Operand)
                    );
                break;

            case Operator.UnaryMinus:
                code = string.Format(
                    "-({0})",
                    EmitExpression(op.Operand)
                    );
                break;

            case Operator.UnaryPlus:
                code = string.Format(
                    "+({0})",
                    EmitExpression(op.Operand)
                    );
                break;

            default:
                throw new InternalCompilerException(
                          string.Format("Could not generate code for unary operator: {0}", op.Operator)
                          );
            }
            return(code);
        }
Пример #3
0
        protected virtual List <CodeErrorException> TypeCheckUnaryOperator(UnaryOperatorTree op)
        {
            List <CodeErrorException> errors = new List <CodeErrorException>();

            switch (op.Operator)
            {
            case Operator.UnaryMinus:
            case Operator.UnaryPlus:
                break;

            default:
                errors.Add(
                    new CodeErrorException(
                        op.Token.Line,
                        op.Token.StartPosition,
                        op.Token.EndPosition,
                        string.Format(
                            "Typechecker does not support unary operator '{0}'",
                            op.Token.Value
                            )
                        )
                    );
                break;
            }

            // typecheck expressions
            errors.AddRange(TypeCheckExpression(op.Operand));
            return(errors);
        }