コード例 #1
0
        public ForLoopNode(VariableIdNode idNode, Dictionary <string, IProperty> symbolTable, Token token)
        {
            this.idNode = idNode;
            this.token  = token;

            this.indexAccumulator = createIndexAccumulator(idNode, symbolTable, token);
        }
コード例 #2
0
        /// <summary>
        /// Checks the static semantic constraints of an AssignNode.
        /// </summary>
        /// <returns>An ISemanticCheckValue.</returns>
        /// <param name="node">Node.</param>
        public ISemanticCheckValue VisitAssignNode(AssignNode node)
        {
            // Check that the id property in this AssignNode is ok.
            IProperty property = getVariableProperty(node);

            // if the property was voidProperty, return at this point,
            // an error has been reported already
            if (property == voidProperty)
            {
                return(voidProperty);
            }

            VariableIdNode idNode = node.IDNode;

            // check that the id variable is declared
            checkPropertyDeclared(idNode, property, true);

            // check that the id property we should be assigning to is not constant
            // at this point of its lifecycle (i.e. not a current block's control variable)
            if (property.Constant)
            {
                analyzer.notifyError(new IllegalAssignmentError(node));
            }

            // evaluate the type of this property
            IProperty evaluated = node.Accept(this.typeChecker).asProperty();

            // check the type of the expression in the assign node matches the one in the symbol table
            if (!checkPropertyType(property, evaluated.GetTokenType()))
            {
                analyzer.notifyError(new IllegalTypeError(idNode));
            }

            return(voidProperty);
        }
コード例 #3
0
        public AssignNode CreateAssignNode(VariableIdNode idNode, StatementsNode statementsNode, Token t)
        {
            AssignNode assignNode = new AssignNode(idNode, symbolTable, t);

            statementsNode.Statement = assignNode;

            return(assignNode);
        }
コード例 #4
0
        /// <summary>
        /// Visits the assign node.
        /// </summary>
        /// <returns>An ISemanticCheckValue.</returns>
        /// <param name="node">Node.</param>
        public ISemanticCheckValue VisitAssignNode(AssignNode node)
        {
            // assign the idNode's value to the one in the expression node
            VariableIdNode idNode      = node.IDNode;
            IProperty      assignValue = node.ExprNode.Accept(this).asProperty();

            symbolTable [idNode.ID] = assignValue;

            return(voidProperty);
        }
コード例 #5
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);
        }
コード例 #6
0
        /// <summary>
        /// Parses an assign statement.
        /// </summary>
        /// <returns>The next token.</returns>
        /// <param name="token">Token.</param>
        /// <param name="statementsNode">Statements node.</param>
        private Token ParseVariableAssign(Token token, StatementsNode statementsNode)
        {
            try {
                VariableIdNode idNode = nodeBuilder.CreateIdNode();
                // parse the target id
                Token next = ParseVarId(token, idNode);

                match(next, TokenType.ASSIGN);
                AssignNode assignNode = nodeBuilder.CreateAssignNode(idNode, statementsNode, token);

                // parses the expression of the assignment
                return(ParseExpression(scanner.getNextToken(next), assignNode));
            } catch (UnexpectedTokenException ex) {
                return(FastForwardToStatementEnd(ex));
            }
        }
コード例 #7
0
        /// <summary>
        /// Visits the IO read node.
        /// </summary>
        /// <returns>An ISemanticCheckValue.</returns>
        /// <param name="node">Node.</param>
        public ISemanticCheckValue VisitIOReadNode(IOReadNode node)
        {
            // read an input
            string input = reader.readLine();

            // select the first word of the input
            input = input.Split(new[] { ' ', '\t', '\n' })[0];

            // set the input to the ioreadnode's assignment
            AssignNode assignNode = node.AssignNode;

            setAssignValue(input, assignNode);
            // execute the assign node
            assignNode.Accept(this);

            return(voidProperty);
        }
コード例 #8
0
        /// <summary>
        /// Parses an assignment during a declaration statement into an AssignNode.
        /// </summary>
        /// <returns>The next token.</returns>
        /// <param name="token">Token.</param>
        /// <param name="assignNode">An AssignNode.</param>
        private Token ParseAssign(Token token, AssignNode assignNode)
        {
            switch (token.Type)
            {
            case TokenType.ASSIGN:
                // if assignment during a declaration was made, parse the expression to the AssignNode
                assignNode.Token = token;
                Token next = scanner.getNextToken(token);
                return(ParseExpression(next, assignNode));

            case TokenType.END_STATEMENT:
                // otherwise, set a default assignment
                setDefaultAssignment(assignNode);
                return(token);

            default:
                throw new UnexpectedTokenException(token, TokenType.UNDEFINED, ParserConstants.EXPECTATION_SET_ASSIGN);
            }
        }
コード例 #9
0
        /// <summary>
        /// Sets a default assignment to an AssignNode.
        /// </summary>
        /// <param name="assignNode">An AssignNode.</param>
        private void setDefaultAssignment(AssignNode assignNode)
        {
            TokenType idType = assignNode.IDNode.EvaluationType;

            switch (idType)
            {
            case TokenType.STR_VAL:
                nodeBuilder.CreateDefaultStringValueNode(assignNode.Token, assignNode);
                break;

            case TokenType.INT_VAL:
                nodeBuilder.CreateDefaultIntValueNode(assignNode.Token, assignNode);
                break;

            case TokenType.BOOL_VAL:
                nodeBuilder.CreateDefaultBoolValueNode(assignNode.Token, assignNode);
                break;

            default:
                throw new UnexpectedTokenException(assignNode.IDNode.Token, TokenType.UNDEFINED, ParserConstants.EXPECTATION_SET_ID_VAL);
            }
        }
コード例 #10
0
        /// <summary>
        /// Sets the value of a variable read from user.
        /// </summary>
        /// <param name="input">Input.</param>
        /// <param name="assignNode">Assign node.</param>
        private void setAssignValue(string input, AssignNode assignNode)
        {
            TokenType expectedType = assignNode.IDNode.EvaluationType;

            // based on the variable's type, we try to parse the user's input
            switch (expectedType)
            {
            case TokenType.INT_VAL:
                // if its supposed to be an integer but its not, throw a runtime exception
                if (!StringUtils.isInteger(input))
                {
                    throw new RuntimeException(ErrorConstants.NOT_AN_INTEGER_MESSAGE, assignNode.Token);
                }
                assignNode.AddExpression(new IntValueNode(StringUtils.parseToInt(input)));
                break;

            case TokenType.STR_VAL:
                assignNode.AddExpression(new StringValueNode(input));
                break;

            default:
                throw new RuntimeException(ErrorConstants.RUNTIME_ERROR_MESSAGE, assignNode.Token);
            }
        }
コード例 #11
0
 /// <summary>
 /// Visits the assign node.
 /// </summary>
 /// <returns>a VoidProperty.</returns>
 /// <param name="node">Node.</param>
 public ISemanticCheckValue VisitAssignNode(AssignNode node)
 {
     // nothing to evaluate here, it's done from the ExecutionVisitor
     return(voidProperty);
 }
コード例 #12
0
 public IOReadNode(VariableIdNode idNode, Dictionary <string, IProperty> symbolTable, Token t)
 {
     this.idNode     = idNode;
     this.assignNode = new AssignNode(this.idNode, symbolTable, t);
     this.token      = t;
 }
コード例 #13
0
 /// <summary>
 /// Visits the assign node.
 /// </summary>
 /// <returns>An ISemanticCheckValue.</returns>
 /// <param name="node">Node.</param>
 public ISemanticCheckValue VisitAssignNode(AssignNode node)
 {
     // return the evaluation of the node's expression
     return(getEvaluation(node.ExprNode));
 }