TypeDef TypeDeclaration() { if (currentToken.Tipo != TipoToken.TK_ID) throw new Exception("Se esperaba identificador."); else {//type id string tmp = currentToken.Lexema; currentToken = lex.NextToken(); if (currentToken.Tipo != TipoToken.TK_ASSIGN) throw new Exception("Se esperaba asignacion."); else {//type id := currentToken = lex.NextToken(); if (currentToken.Tipo == TipoToken.TK_RECORD) {//type id := record currentToken = lex.NextToken(); Structs ret = new Structs(); ret.nombre = tmp; try { ret.campos = VariableDeclarationList(); } catch (Exception ex) { throw ex; } if (currentToken.Tipo != TipoToken.TK_END) throw new Exception("Se esperaba end."); else { currentToken = lex.NextToken(); Struct type = new Struct(); type.Campos = new T_Campos(); type.nombre = ret.nombre; Declaracion temp = ret.campos; while (temp != null) { type.Campos.Add(temp.Var.id, temp.Tip); temp = temp.Sig; } try { InfSemantica.getInstance().tblTipos.Add(type.nombre, type); } catch (Exception ex) { throw new Exception("Ya se ha definido un tipo con ese nombre."); } return ret; } } else {//type id := something Alias ret = new Alias(); ret.type = new UserType(); ret.type.Nombre = tmp; try { ret.type.Tip = ParseType(); } catch (Exception ex) { throw ex; } InfSemantica.getInstance().tblTipos.Add(ret.type.Nombre, ret.type); return ret; } } } }
public Sentencia DeclaracionesCPrima(Declaracion decl) { try { /*Cuando se declaran varias variables seguidas int x,t,y */ #region if (currentToken.Tipo == Lexico.TipoToken.TK_COMA) { currentToken = lex.NextToken(); if (currentToken.Tipo != Lexico.TipoToken.TK_ID) throw new Exception("Se esperaba un token ID"); Variable nombre = new Variable(currentToken.Lexema, null); decl.Sig.Tip = decl.Tip; decl.Sig.Var = nombre; currentToken = lex.NextToken(); DeclCPrima(decl.Sig); return decl; } #endregion /*Declaracion de arreglos */ #region else if (currentToken.Tipo == Lexico.TipoToken.TK_OPENCOR) { currentToken = lex.NextToken(); Expresiones E1 = Expression(); if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSECOR) throw new Exception("Se esperaba el token ] "); currentToken = lex.NextToken(); Arreglo tipoArreglo = new Arreglo(); tipoArreglo.Contenido = decl.Tip; tipoArreglo.Rangos.Add(E1); DeclArreglo(tipoArreglo); tipoArreglo.Dimensiones = tipoArreglo.Rangos.Count; decl.Tip = tipoArreglo; return decl; } #endregion /*Declaracion de funciones */ #region else if (currentToken.Tipo == Lexico.TipoToken.TK_OPENPAR) { currentToken = lex.NextToken(); Declaracion listaParams = FuncionesParams(); if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR) throw new Exception("Se esperaba el token )"); currentToken = lex.NextToken(); S_Functions declFuncion = new S_Functions(); declFuncion.Retorno = decl.Tip; declFuncion.Var = decl.Var.id; declFuncion.S = CompoundStatement(); if (listaParams != null) declFuncion.Campo = listaParams; return declFuncion; } #endregion /*Declaracion de Structs */ #region else if (currentToken.Tipo == Lexico.TipoToken.TK_OPENLLAVE) { currentToken = lex.NextToken(); Declaracion strDec = StructDeclaration(); Declaracion tmp = StructDeclaration(); while (tmp != null) { strDec.Sig = tmp; tmp = StructDeclaration(); } Structs s = new Structs(); s.nombre = decl.Var.id; s.campos = strDec; if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSELLAVE) throw new Exception("Error sintactico se esperaba }"); currentToken = lex.NextToken(); if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA) throw new Exception("Error sintactico se esperaba ;"); currentToken = lex.NextToken(); return s; } #endregion return decl; } catch (Exception ex) { throw ex; } }