Esempio n. 1
0
        Sentencia CompoundStatement()
        {
            try
            {
                switch (currentToken.Tipo)
                {
                    case TipoToken.TK_IF:
                        currentToken = lex.NextToken();
                        return parseIf();

                    case TipoToken.TK_FOR:
                        currentToken = lex.NextToken();
                        return parseFor();

                    case TipoToken.TK_WHILE:
                        currentToken = lex.NextToken();
                        return parseWhile();

                    case TipoToken.TK_CASE:
                        currentToken = lex.NextToken();
                        return parseSwitch();

                    case TipoToken.TK_REPEAT:
                        currentToken = lex.NextToken();
                        return parseDo();

                    case TipoToken.TK_ID:
                        return parseAssignOrCall();

                    #region read/print/break/continue/return
                    case TipoToken.TK_PRINT:
                        {
                            currentToken = lex.NextToken();
                            S_Print ret = new S_Print();
                            ret.Expr = Expr();
                            return ret;
                        }
                    case TipoToken.TK_READ:
                        {
                            currentToken = lex.NextToken();
                            if (currentToken.Tipo != TipoToken.TK_ID)
                                throw new Exception("Se esperaba identificador.");
                            else
                            {
                                S_Read ret = new S_Read();
                                ret.var = new Variable(currentToken.Lexema, AccessList(currentToken.Lexema));
                                return ret;
                            }
                        }

                    case TipoToken.TK_BREAK:
                        currentToken = lex.NextToken();
                        return new S_Break();

                    case TipoToken.TK_CONTINUE:
                        currentToken = lex.NextToken();
                        return new S_Continue();

                    case TipoToken.TK_RETURN:
                        {
                            currentToken = lex.NextToken();
                            S_Return ret = new S_Return();
                            ret.Expr = Expr();
                            return ret;
                        }
                    #endregion
                    default:
                        throw new Exception("Sentencia no reconocida.");
                }
            }
            catch (Exception ex) { throw ex; }
        }
Esempio n. 2
0
        Sentencia Statement()
        {
            try
            {
                switch (currentToken.Tipo)
                {
                    case TipoToken.TK_IF:
                        currentToken = lex.NextToken();
                        return parseIf();

                    case TipoToken.TK_FOR:
                        currentToken = lex.NextToken();
                        return parseFor();

                    case TipoToken.TK_WHILE:
                        currentToken = lex.NextToken();
                        return parseWhile();

                    case TipoToken.TK_CASE:
                        currentToken = lex.NextToken();
                        return parseSwitch();

                    case TipoToken.TK_REPEAT:
                        currentToken = lex.NextToken();
                        return parseDo();

                    #region variable
                    case TipoToken.TK_VAR:
                        {
                            currentToken = lex.NextToken();
                            Declaracion ret = VariableDeclarationList();
                            if (currentToken.Tipo != TipoToken.TK_END)
                                throw new Exception("Se esperaba end.");
                            else
                            {
                                currentToken = lex.NextToken();
                                return ret;
                            }
                        }
                    #endregion

                    case TipoToken.TK_ID:
                        return parseAssignOrCall();

                    #region read/print
                    case TipoToken.TK_PRINT:
                        {
                            currentToken = lex.NextToken();
                            S_Print ret = new S_Print();
                            ret.Expr = Expr();
                            return ret;
                        }
                    case TipoToken.TK_READ:
                        {
                            currentToken = lex.NextToken();
                            if (currentToken.Tipo != TipoToken.TK_ID)
                                throw new Exception("Se esperaba identificador.");
                            else
                            {
                                S_Read ret = new S_Read();
                                ret.var = new Variable(currentToken.Lexema, AccessList(currentToken.Lexema));
                                return ret;
                            }
                        }
                    #endregion

                    #region Procedure
                    case TipoToken.TK_VOID:
                        {
                            currentToken = lex.NextToken();
                            if (currentToken.Tipo != TipoToken.TK_ID)
                                throw new Exception("Se esperaba ID");
                            else
                            {
                                S_Functions ret = new S_Functions();
                                ret.Retorno = new Voids();
                                ret.Var = currentToken.Lexema;
                                currentToken = lex.NextToken();
                                if (currentToken.Tipo != TipoToken.TK_OPENPAR)
                                    throw new Exception("Se esperaba \"(\"");
                                else
                                {
                                    currentToken = lex.NextToken();
                                    ret.Campo = VariableDeclarationList();
                                    if (currentToken.Tipo != TipoToken.TK_CLOSEPAR)
                                        throw new Exception("Se esperaba\")\"");
                                    else
                                    {
                                        currentToken = lex.NextToken();
                                        ret.S = CodeBlock();
                                        return ret;
                                    }
                                }
                            }
                        }
                    #endregion

                    #region Function
                    case TipoToken.TK_FUNCTION:
                        {
                            currentToken = lex.NextToken();
                            if (currentToken.Tipo != TipoToken.TK_ID)
                                throw new Exception("Se esperaba ID");
                            else
                            {
                                S_Functions ret = new S_Functions();
                                ret.Var = currentToken.Lexema;
                                currentToken = lex.NextToken();
                                if (currentToken.Tipo != TipoToken.TK_OPENPAR)
                                    throw new Exception("Se esperaba \"(\"");
                                else
                                {
                                    currentToken = lex.NextToken();
                                    ret.Campo = VariableDeclarationList();
                                    if (currentToken.Tipo != TipoToken.TK_CLOSEPAR)
                                        throw new Exception("Se esperaba\")\"");
                                    else
                                    {
                                        currentToken = lex.NextToken();
                                        if (currentToken.Tipo != TipoToken.TK_DOSPUNTOS)
                                            throw new Exception("Se esperaba\":\"");
                                        else
                                        {
                                            currentToken = lex.NextToken();
                                            ret.Retorno = ParseType();
                                            ret.S = CodeBlock();
                                            return ret;
                                        }
                                    }
                                }
                            }
                        }
                    #endregion

                    #region type
                    case TipoToken.TK_TYPE:
                        {
                            currentToken = lex.NextToken();
                            TypeDef ret = TypeDeclarationList();
                            if (currentToken.Tipo != TipoToken.TK_END)
                                throw new Exception("Se esperaba end.");
                            else
                            {
                                currentToken = lex.NextToken();
                                return ret;
                            }
                        }
                    #endregion

                    #region break/continue/return
                    case TipoToken.TK_BREAK:
                        currentToken = lex.NextToken();
                        return new S_Break();

                    case TipoToken.TK_CONTINUE:
                        currentToken = lex.NextToken();
                        return new S_Continue();

                    case TipoToken.TK_RETURN:
                        {
                            currentToken = lex.NextToken();
                            S_Return ret = new S_Return();
                            ret.Expr = Expr();
                            return ret;
                        }
                    #endregion
                    default:
                        throw new Exception("Sentencia no reconocida.");
                }
            }
            catch (Exception ex) { throw ex; }
        }
Esempio n. 3
0
        public Sentencia Statement()
        {
            try
            {
                //Para Declaraciones
                if (currentToken.Tipo == Lexico.TipoToken.TK_INT || currentToken.Tipo == Lexico.TipoToken.TK_FLOAT || currentToken.Tipo == Lexico.TipoToken.TK_STRUCT ||
                   currentToken.Tipo == Lexico.TipoToken.TK_CHAR || currentToken.Tipo == Lexico.TipoToken.TK_VOID || currentToken.Tipo == Lexico.TipoToken.TK_STRING ||
                    currentToken.Tipo == Lexico.TipoToken.TK_BOOL)
                {
                    return DeclaracionesC();
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_PRINT)
                {
                    #region
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");
                    this.currentToken = lex.NextToken();
                    S_Print sprint = new S_Print();
                    sprint.Expr = Expression();

                    //this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");
                    this.currentToken = lex.NextToken();
                    return sprint;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_READ)
                {
                    #region
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");
                    this.currentToken = lex.NextToken();

                    if (currentToken.Tipo != Lexico.TipoToken.TK_ID)
                        throw new Exception("Se esperaba un ID");

                    S_Read sread = new S_Read();
                    sread.var.id = currentToken.Lexema;
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");
                    this.currentToken = lex.NextToken();
                    return sread;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_IF)
                {
                    #region
                    S_If sif = new S_If();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");

                    this.currentToken = lex.NextToken();
                    sif.Condicion = Expression();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");

                    this.currentToken = lex.NextToken();
                    sif.Cierto = CompoundStatement();
                    sif.Falso = Else();
                    return sif;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_WHILE)
                {
                    #region
                    S_While swhile = new S_While();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");

                    this.currentToken = lex.NextToken();
                    swhile.Condicion = Expression();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");

                    this.currentToken = lex.NextToken();
                    swhile.S = CompoundStatement();
                    return swhile;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_DO)
                {
                    #region
                    S_Do sdo = new S_Do();
                    this.currentToken = lex.NextToken();
                    sdo.S = CompoundStatement();

                    if (this.currentToken.Tipo == Lexico.TipoToken.TK_WHILE)
                    {
                        this.currentToken = lex.NextToken();
                        if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                            throw new Exception("Se esperaba un (");

                        this.currentToken = lex.NextToken();
                        sdo.Condicion = Expression();

                        if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                            throw new Exception("Se esperaba un )");

                        this.currentToken = lex.NextToken();
                        if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                            throw new Exception("Se esperaba un ;");
                        this.currentToken = lex.NextToken();
                        return sdo;
                    }
                    else
                        throw new Exception("Se esperaba la palabra reservada WHILE");
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_FOR)
                {
                    #region
                    S_For sfor = new S_For();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");

                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_ID)
                        throw new Exception("Se esperaba un ID");

                    sfor.Var.id = this.currentToken.Lexema;
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_ASSIGN)
                        throw new Exception("Se esperaba el simbolo =");

                    this.currentToken = lex.NextToken();
                    sfor.Inicio = Expression();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");

                    this.currentToken = lex.NextToken();
                    sfor.Condicion = Expression();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");

                    this.currentToken = lex.NextToken();
                    sfor.Iteracion = Expression();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");

                    this.currentToken = lex.NextToken();
                    sfor.S = CompoundStatement();
                    return sfor;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_RETURN)
                {
                    #region
                    S_Return sreturn = new S_Return();
                    this.currentToken = lex.NextToken();
                    sreturn.Expr = Expression();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");

                    this.currentToken = lex.NextToken();
                    return sreturn;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_BREAK)
                {
                    #region
                    S_Break sbreak = new S_Break();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");

                    this.currentToken = lex.NextToken();
                    return sbreak;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_SWITCH)
                {
                    #region
                    S_Switch sSwitch = new S_Switch();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");
                    this.currentToken = lex.NextToken();
                    sSwitch.Var = Expression();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba una )");

                    this.currentToken = lex.NextToken();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENLLAVE)
                        throw new Exception("Se esperaba una {");

                    this.currentToken = lex.NextToken();
                    sSwitch.Casos = Cases();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_DEFAULT)
                        throw new Exception("Se esperaba la palabra reservada DEFAULT");

                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_DOSPUNTOS)
                        throw new Exception("Se esperaba el simbolo :");
                    this.currentToken = lex.NextToken();
                    sSwitch.sdefault = StatementList();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSELLAVE)
                        throw new Exception("Se esperaba una }");

                    this.currentToken = lex.NextToken();
                    return sSwitch;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_ID)
                {
                    #region
                    Sentencia s = SentenciaAssign_LlamaFun();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un token ;");
                    this.currentToken = lex.NextToken();
                    return s;
                    #endregion
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }