Ejemplo n.º 1
0
        private AstItem ReadStatement(TokenIterator tokens)
        {
            AstItem result = null;

            var token = tokens.Current();

            tokens.StartCapture();

            switch (token.Kind)
            {
            case TokenKind.Let:
                result = ReadVarDecl(tokens);
                break;

            case TokenKind.Ret:
                //Pop "ret"
                tokens.Pop();

                var      current         = tokens.Current();
                DataType valueType       = DataType.VOID;
                AstItem  valueExpression = null;
                if (current.Kind != TokenKind.SemiColon)
                {
                    valueExpression = ReadExpression(tokens, TokenKind.SemiColon);
                    valueType       = valueExpression.DataType;
                }
                result = AstItem.Return(valueType, valueExpression);
                tokens.PopExpected(TokenKind.SemiColon);
                break;

            case TokenKind.Word:
                var next = tokens.Next();
                if (next.Kind == TokenKind.RoundBracketOpen)
                {
                    result = ReadFuncCall(tokens);
                    tokens.PopExpected(TokenKind.SemiColon);
                }
                else if (next.Kind == TokenKind.AssigmnentOperator)
                {
                    result = ReadVariableAssignment(tokens);
                }
                else if (next.Kind == TokenKind.SquareBracketOpen)
                {
                    result = ReadIndexerAssignment(tokens);
                }
                else
                {
                    throw new Exception("Unexpected token after identifier '" + token.Value + "'. Expected '=' or '(', found: " + next);
                }
                break;

            case TokenKind.If:
                result = ReadIfStatement(tokens);
                break;

            case TokenKind.Del:
                result = ReadDelStatement(tokens);
                tokens.PopExpected(TokenKind.SemiColon);
                break;

            case TokenKind.ExpressionOperator:
                if (token.Value == "*")
                {
                    result = ReadPointerAssignment(tokens);
                }
                else
                {
                    throw new Exception("Unexpected token. Expected 'let', 'ret', '}' or identifier, found: " + token);
                }

                break;

            case TokenKind.For:
                result = ReadForLoop(tokens);
                break;

            case TokenKind.While:
                result = ReadWhileLoop(tokens);
                break;

            case TokenKind.Break:
                result = ReadBreak(tokens);
                break;

            case TokenKind.CurlyBracketClose:
                //End of function
                return(null);

            default:
                throw new Exception("Unexpected token. Expected 'let', 'ret', '}' or identifier, found: " + token);
            }

            var lineTokens = tokens.GetCapture();

            if (lineTokens.Count > 0)
            {
                result.SourceLine = String.Join(" ", lineTokens.ConvertAll <string>((a) => a.Value));
            }

            return(result);
        }