Exemple #1
0
        /// <summary>
        /// Parses the an expression into a node that implements IExpressionContainer interface.
        /// </summary>
        /// <returns>The next token.</returns>
        /// <param name="token">Token.</param>
        /// <param name="node">An IExpressionContainer.</param>
        private Token ParseExpression(Token token, IExpressionContainer node)
        {
            Token next;

            switch (token.Type)
            {
            case TokenType.INT_VAL:
            case TokenType.STR_VAL:
            case TokenType.BOOL_VAL:
            case TokenType.PARENTHESIS_LEFT:
            case TokenType.ID:
                // parse a binary operation
                BinOpNode binOp = nodeBuilder.CreateBinOpNode(node, token);
                // parse the first operand
                next = ParseOperand(token, binOp);
                // parse the rest of the operation
                return(ParseBinaryOp(next, binOp));

            case TokenType.UNARY_OP_LOG_NEG:
                // parse a unary operation
                UnOpNode unOp = nodeBuilder.CreateUnOpNode(node, token);
                // parse the operation, then the operand
                next = ParseUnaryOp(token, unOp);
                return(ParseOperand(next, unOp));

            default:
                throw new UnexpectedTokenException(token, TokenType.UNDEFINED, ParserConstants.EXPECTATION_SET_EXPRESSION);
            }
        }
Exemple #2
0
        public BinOpNode CreateBinOpNode(IExpressionContainer parent, Token t)
        {
            BinOpNode binOp = new BinOpNode(t);

            parent.AddExpression(binOp);

            return(binOp);
        }
Exemple #3
0
        public BinOpNode CreateBinOpNode(IExpressionContainer parent, IExpressionNode leftHandSide, Token operation)
        {
            BinOpNode binOp = new BinOpNode(parent.Token);

            binOp.AddExpression(leftHandSide);
            binOp.Operation = operation.Type;
            parent.AddExpression(binOp);

            return(binOp);
        }
Exemple #4
0
        private AssignNode createIndexAccumulator(VariableIdNode idNode, Dictionary <string, IProperty> symbolTable, Token token)
        {
            AssignNode accumulator = new AssignNode(idNode, symbolTable, token);

            BinOpNode accumulationOperation = new BinOpNode(token);

            accumulationOperation.Operation = TokenType.BINARY_OP_ADD;
            accumulationOperation.AddExpression(idNode);
            accumulationOperation.AddExpression(new IntValueNode(1, token));

            accumulator.ExprNode = accumulationOperation;

            return(accumulator);
        }
Exemple #5
0
        /// <summary>
        /// Visits the bin op node.
        /// </summary>
        /// <returns>An ISemanticCheckValue.</returns>
        /// <param name="node">Node.</param>
        public ISemanticCheckValue VisitBinOpNode(BinOpNode node)
        {
            // we start by evaluating the left hand side
            IProperty leftHandEval = getEvaluation(node.LeftOperand);

            // if it's a "no operation", return the left hand side evaluation
            if (node.Operation == TokenType.BINARY_OP_NO_OP)
            {
                return(leftHandEval);
            }

            // otherwise, evaluate the right hand side
            IProperty rightHandEval = getEvaluation(node.RightOperand);
            // and then evaluate the operation
            IProperty evaluation = binaryOperation(node.Operation, leftHandEval, rightHandEval, node.Token);

            return(evaluation);
        }
Exemple #6
0
        /// <summary>
        /// Parses a binary operation's operation into a BinOpNode,
        /// </summary>
        /// <returns>The next token.</returns>
        /// <param name="token">Token.</param>
        /// <param name="binOp">A BinOpNode.</param>
        private Token ParseOperation(Token token, BinOpNode binOp)
        {
            switch (token.Type)
            {
            case TokenType.BINARY_OP_ADD:
            case TokenType.BINARY_OP_DIV:
            case TokenType.BINARY_OP_LOG_AND:
            case TokenType.BINARY_OP_LOG_EQ:
            case TokenType.BINARY_OP_LOG_LT:
            case TokenType.BINARY_OP_MUL:
            case TokenType.BINARY_OP_SUB:
                binOp.Operation = token.Type;
                return(scanner.getNextToken(token));

            default:
                throw new UnexpectedTokenException(token, TokenType.UNDEFINED, null);
            }
        }
Exemple #7
0
        /// <summary>
        /// Parses a binary operation's operation and righthand side into a BinOpNode
        /// </summary>
        /// <returns>The next token.</returns>
        /// <param name="token">Token.</param>
        /// <param name="binOp">A BinOpNode.</param>
        private Token ParseBinaryOp(Token token, BinOpNode binOp)
        {
            switch (token.Type)
            {
            case TokenType.BINARY_OP_ADD:
            case TokenType.BINARY_OP_SUB:
            case TokenType.BINARY_OP_MUL:
            case TokenType.BINARY_OP_DIV:
            case TokenType.BINARY_OP_LOG_LT:
            case TokenType.BINARY_OP_LOG_EQ:
            case TokenType.BINARY_OP_LOG_AND:
                // In case it is a binary operation, the operation and the
                // righthand side is parsed
                Token next = ParseOperation(token, binOp);
                return(ParseOperand(next, binOp));

            default:
                // in case it wasn't a binary operation, the BinOpNode's
                // operation remains "no operation" and the righthand side
                // is not parsed
                return(token);
            }
        }
Exemple #8
0
 /// <summary>
 /// Checks the static semantic constraints of a BinOpNode.
 /// </summary>
 /// <returns>An ISemanticCheckValue.</returns>
 /// <param name="node">Node.</param>
 public ISemanticCheckValue VisitBinOpNode(BinOpNode node)
 {
     // This is not a statement so it needs not to be actually checked here.
     // So, we pass it to the TypeCheckerVisitor instead.
     return(node.Accept(this.typeChecker));
 }
Exemple #9
0
 /// <summary>
 /// Visits the bin op node.
 /// </summary>
 /// <returns>An ISemanticCheckValue.</returns>
 /// <param name="node">Node.</param>
 public ISemanticCheckValue VisitBinOpNode(BinOpNode node)
 {
     // return the evaluation of the node using a helper method
     return(VisitOperationNode(node));
 }
Exemple #10
0
 /// <summary>
 /// Visits the bin op node.
 /// </summary>
 /// <returns>An ISemanticCheckValue.</returns>
 /// <param name="node">Node.</param>
 public ISemanticCheckValue VisitBinOpNode(BinOpNode node)
 {
     // let the evaluator evaluate this node
     return(node.Accept(evaluator));
 }