예제 #1
0
        public TypeKeywordNode ParseTypeKeyword(Token token)
        {
            if (token.Type == TokenType.TypeKeyword)
            {
                TypeKeywordNode type       = null;
                CobaltType      cobaltType = token.GetData <CobaltType>(TokenDataKeys.COBALT_TYPE);
                switch (cobaltType)
                {
                case CobaltType.Boolean:
                    type = new BooleanTypeKeywordNode(token.SourceLine);
                    break;

                case CobaltType.Float:
                    type = new FloatTypeKeywordNode(token.SourceLine);
                    break;

                case CobaltType.Integer:
                    type = new IntegerTypeKeywordNode(token.SourceLine);
                    break;

                default:
                    throw new CompilerException($"`{MethodBase.GetCurrentMethod().Name}` called with unknown Cobalt type `{cobaltType}`.");
                }
                return(type);
            }
            else
            {
                throw new CompilerException($"`{MethodBase.GetCurrentMethod().Name}` called with a bad token. Expected a token of type `{TokenType.TypeKeyword}`, got token of type `{token.Type}` instead.");
            }
        }
예제 #2
0
        public void ShouldParseTypeKeywords(CobaltType type)
        {
            // Arrange
            Token token = new Token(TokenType.TypeKeyword, 1, 0);

            token.SetData(TokenDataKeys.COBALT_TYPE, type);

            // Act
            TypeKeywordNode node = Parser.ParseTypeKeyword(token);

            // Assert
            switch (type)
            {
            case CobaltType.Boolean:
                Assert.True(node is BooleanTypeKeywordNode);
                break;

            case CobaltType.Float:
                Assert.True(node is FloatTypeKeywordNode);
                break;

            case CobaltType.Integer:
                Assert.True(node is IntegerTypeKeywordNode);
                break;

            default:
                throw new XunitException("No test implemented for this type.");
            }
            Assert.Equal(type, node.Type);
        }
예제 #3
0
        public StatementNode ParseVariableDeclarationStatement(List <Token> tokens)
        {
            if (tokens.Count <= 3 ||
                tokens.ElementAt(0).Type != TokenType.Declaration ||
                tokens.ElementAt(1).Type != TokenType.Identifier ||
                tokens.ElementAt(2).Type != TokenType.Colon ||
                (tokens.ElementAt(3).Type != TokenType.Equal && tokens.ElementAt(3).Type != TokenType.TypeKeyword))
            {
                throw new CobaltSyntaxError($"Ivalid variable declaration statement.", tokens.First().SourceLine, tokens.First().PositionOnLine);
            }

            // Parse identifier
            IdentifierNode identifier = ParseIdentifier(tokens.ElementAt(1));

            // Parse type, if applicable
            TypeKeywordNode type = null;

            if (tokens.ElementAt(3).Type == TokenType.TypeKeyword)
            {
                type = ParseTypeKeyword(tokens.ElementAt(3));
            }

            // Parse expression
            ExpressionNode expression = null;

            if (tokens.Count > 4 && tokens.ElementAt(3).Type == TokenType.Equal)
            {
                expression = ParseExpression(tokens.GetRange(4, tokens.Count - 4));
            }
            else if (tokens.Count > 5 && tokens.ElementAt(4).Type == TokenType.Equal)
            {
                expression = ParseExpression(tokens.GetRange(5, tokens.Count - 5));
            }

            // Validate syntax
            if (type == null && expression == null)
            {
                throw new CobaltSyntaxError("Variable declaration has no explicit type and no expression to infer a type from.", tokens.First().SourceLine, tokens.First().PositionOnLine);
            }

            // Create and return output statement node
            VariableDeclarationStatementNode statement = new VariableDeclarationStatementNode(tokens.First().SourceLine);

            statement.Identifier = identifier;
            if (type != null)
            {
                statement.TypeKeyword = type;
            }
            if (expression != null)
            {
                statement.Expression = expression;
            }
            return(statement);
        }