Skip to content

Commit

Permalink
Alteração da sintaxe para permitir a declaração explicita de tipos
Browse files Browse the repository at this point in the history
  • Loading branch information
lrlucena committed May 14, 2018
1 parent 212035b commit eb2bce8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
29 changes: 14 additions & 15 deletions src/main/antlr4/br/edu/ifrn/potigol/parser/potigol.g4
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
*/

/**
* _____ _ _ _
* _____ _ _ _
* | __ \ | | (_) | |
* | |__) |__ | |_ _ __ _ ___ | |
* | ___/ _ \| __| |/ _` |/ _ \| |
* | | | (_) | |_| | (_| | (_) | |
* |_| \___/ \__|_|\__, |\___/|_|
* __/ |
* |___/
* __/ |
* |___/
*
* @author Leonardo Lucena ([email protected])
*/
Expand Down Expand Up @@ -56,10 +56,10 @@ decl
| decl_uso ;

decl_valor
: id1 '=' expr # valor_simples
| id2 '=' expr2 # valor_multiplo
| 'var' id1 (':='| '=') expr # decl_var_simples
| 'var' id2 (':='| '=') expr2 # decl_var_multipla ;
: id1 (':' tipo)? '=' expr # valor_simples
| id2 (':' tipo)? '=' expr2 # valor_multiplo
| 'var' id1 (':' tipo)? (':='| '=') expr # decl_var_simples
| 'var' id2 (':' tipo)? (':='| '=') expr2 # decl_var_multipla ;

decl_funcao
: ID '(' dcls ')' (':' tipo)? '=' expr # def_funcao
Expand All @@ -77,7 +77,7 @@ retorne

dcl
: id1 ':' tipo ;

dcl_var
: 'var' id1 ':' tipo ;

Expand All @@ -95,7 +95,7 @@ tipo
| '(' tipo2 ')' # tipo_tupla
| <assoc=right> tipo '=>' tipo # tipo_funcao ;

// Expressao
// Expressao
expr
: literal # lit
| expr '.' ID ('(' expr1 ')')? # chamada_metodo
Expand Down Expand Up @@ -129,7 +129,7 @@ literal
| FLOAT # real
| CHAR # char ;


// Decisao
decisao
: se
Expand Down Expand Up @@ -166,7 +166,7 @@ padrao
| padrao ('|' padrao)+ # padrao_ou
| padrao (',' padrao)+ # padrao_virgula ;

// Repeticao
// Repeticao
repeticao
: para_faca
| para_gere
Expand Down Expand Up @@ -204,7 +204,7 @@ id1
id2
: ID (',' ID)+ ;

qualid
qualid
: (ID '.')* ID;

qualid1
Expand All @@ -222,7 +222,7 @@ exprlist

// Lexer
//channels { WSCHANNEL, MYHIDDEN };

ID
: (ALPHA|ACENTO) (ALPHA|ACENTO|DIGIT)* ;

Expand All @@ -233,7 +233,7 @@ fragment ALPHA

fragment ACENTO
: '\u00a1' .. '\ufffc' ;

INT
: DIGIT+ ;

Expand Down Expand Up @@ -275,4 +275,3 @@ COMMENT
NL
: '\n' ->channel(2) ;
2 changes: 1 addition & 1 deletion src/main/java/br/edu/ifrn/potigol/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static String guarda(final String cond) {

public static String tipo(final String valor) {
final String resp;
if (valor.isEmpty()) {
if (valor == null || valor.isEmpty()) {
resp = "";
} else {
resp = DOISPONTOS + valor;
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/br/edu/ifrn/potigol/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
*/

/**
* _____ _ _ _
* _____ _ _ _
* | __ \ | | (_) | |
* | |__) |__ | |_ _ __ _ ___ | |
* | ___/ _ \| __| |/ _` |/ _ \| |
* | | | (_) | |_| | (_| | (_) | |
* |_| \___/ \__|_|\__, |\___/|_|
* __/ |
* |___/
* __/ |
* |___/
*
* @author Leonardo Lucena ([email protected])
*/
Expand Down Expand Up @@ -316,20 +316,22 @@ public void exitDecl(final DeclContext ctx) {
public void exitDecl_var_multipla(final Decl_var_multiplaContext ctx) {
final String id = data.getValue(ctx.id2());
final String[] ids = M.split(id);
final String tipo = data.getOrElse(ctx.tipo());
final String exp = data.getValue(ctx.expr2());
final String[] exps = M.split(exp);
final String resposta = M.declVariavelMult(ids, exps);
final String resposta = M.declVariavelMult(ids, exps, tipo);
data.setValue(ctx, resposta.toString());
data.verificarDuplicados(M.string2List(id), ctx);
data.verificarDuplicados(M.string2List(id), ctx, tipo.isEmpty());
}

@Override
public void exitDecl_var_simples(final Decl_var_simplesContext ctx) {
final String id = data.getValue(ctx.id1());
final String tipo = data.getOrElse(ctx.tipo());
final String exp = data.getValue(ctx.expr());
final String resposta = M.declVariavel(id, exp);
final String resposta = M.declVariavel(id, exp, tipo);
data.setValue(ctx, resposta);
data.verificarDuplicados(M.string2List(id), ctx);
data.verificarDuplicados(M.string2List(id), ctx, tipo.isEmpty());
}

@Override
Expand Down Expand Up @@ -658,20 +660,22 @@ public void exitTupla(final TuplaContext ctx) {
@Override
public void exitValor_multiplo(final Valor_multiploContext ctx) {
final List<String> ids = M.string2List(data.getValue(ctx.id2()));
final String tipo = data.getOrElse(ctx.tipo());
final List<String> exps = M.string2List(data.getValue(ctx.expr2()));
final String resposta = M.valorMultiplo(ids, exps);
final String resposta = M.valorMultiplo(ids, exps, tipo);
data.setValue(ctx, resposta);
data.verificarDuplicados(ids, ctx);
data.verificarDuplicados(ids, ctx, tipo.isEmpty());
}

@Override
public void exitValor_simples(final Valor_simplesContext ctx) {
final String id = data.getValue(ctx.id1());
final String tipo = data.getOrElse(ctx.tipo());
final String exp = data.getValue(ctx.expr());
final List<String> ids = M.string2List(id);
final String resposta = M.declValor(id, exp);
final String resposta = M.declValor(id, exp, tipo);
data.setValue(ctx, resposta);
data.verificarDuplicados(ids, ctx);
data.verificarDuplicados(ids, ctx, tipo.isEmpty());
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/br/edu/ifrn/potigol/ListenerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public String getOrElse(final ParseTree node) {

// TODO: Refatorar
public void verificarDuplicados(final List<String> ids,
final ParserRuleContext ctx) {
final ParserRuleContext ctx, final boolean show) {
if (show){
for (final String i : ids) {
if (this.valores().contains(i)) {
final int linha = ctx.getStart().getLine();
Expand All @@ -69,6 +70,7 @@ public void verificarDuplicados(final List<String> ids,
+ linha + ".");
}
}
}
this.declaracoes.peek().addAll(ids);
}

Expand Down
27 changes: 23 additions & 4 deletions src/main/java/br/edu/ifrn/potigol/M.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,29 @@ public static String defFuncao(final String id, final String param,
+ K.bloco(corpo) + K.NEWLINE;
}

public static String declVariavel(final String id, final String exp, final String tipo) {
return K.VAR + id + K.tipo(tipo) + K.IGUAL + exp + K.NEWLINE;
}

public static String declVariavel(final String id, final String exp) {
return K.VAR + id + K.IGUAL + exp + K.NEWLINE;
return declVariavel(id, exp, "");
}

public static String declVariavelMult(final String[] ids,
final String[] exps) {
final String[] exps, final String tipo) {
final StringBuilder resposta = new StringBuilder();
for (int i = 0; i < ids.length; i++) {
resposta.append(M.declVariavel(ids[i], exps[i]) + K.NEWLINE);
resposta.append(M.declVariavel(ids[i], exps[i], tipo) + K.NEWLINE);
}
return resposta.toString();
}

public static String declVariavelMult(final String[] ids,
final String[] exps) {
return declVariavelMult(ids, exps, "");
}


public static String tupla(final int tamanho, final String exp) {
return K.param(exp);
}
Expand All @@ -247,6 +257,10 @@ public static String declValor(final String ident, final String exp) {
return K.VAL + ident + K.IGUAL + exp + K.SEMI;
}

public static String declValor(final String ident, final String exp, final String tipo) {
return K.VAL + ident + K.tipo(tipo) + K.IGUAL + exp + K.SEMI;
}

public static String atribMultipla(final List<String> ids,
final List<String> exps) {
final String[] aux = new String[ids.size()];
Expand Down Expand Up @@ -337,11 +351,16 @@ public static String faixas(final List<String> lista) {

public static String valorMultiplo(final List<String> ids,
final List<String> exps) {
return valorMultiplo(ids, exps, "");
}

public static String valorMultiplo(final List<String> ids,
final List<String> exps, final String tipo) {
final StringBuilder resposta = new StringBuilder();
for (int i = 0; i < ids.size(); i++) {
final String id = ids.get(i);
final String exp = exps.get(i);
resposta.append(M.declValor(id, exp));
resposta.append(M.declValor(id, exp, tipo));
}
return resposta.toString();
}
Expand Down

0 comments on commit eb2bce8

Please sign in to comment.