Skip to content

Commit

Permalink
Acrescentada a vertificação e validação de tipos das atribuições de v…
Browse files Browse the repository at this point in the history
…ariáveis
  • Loading branch information
mtsgeneroso committed Jun 18, 2018
1 parent 8beb700 commit 425149e
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 45 deletions.
4 changes: 0 additions & 4 deletions nbproject/private/private.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group name="analisador-lexico"/>
<group>
<file>file:/D:/Projetos/hal/src/net/unesc/hal/analysis/Semantic.java</file>
<file>file:/D:/Projetos/hal/src/net/unesc/hal/analysis/semantic/Node.java</file>
<file>file:/D:/Projetos/hal/src/net/unesc/hal/views/Editor.java</file>
<file>file:/D:/Projetos/hal/src/net/unesc/hal/analysis/semantic/Identifier.java</file>
<file>file:/D:/Projetos/hal/src/net/unesc/hal/languages/HAL.java</file>
</group>
</open-files>
Expand Down
5 changes: 3 additions & 2 deletions samples/Exemplo1.lms
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var
var
a :integer;
begin
a:=200000;
a:=20000;
readln(a);
if a=x then
z:=z+x
Expand All @@ -26,4 +26,5 @@ begin
end.





2 changes: 0 additions & 2 deletions src/net/unesc/hal/analysis/Lexicon.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ private void run() {
is_comment_loop = false;
cur_char = chars.get(++car);
buffer.clear();
} else {
System.out.println(parseBuffer(buffer) + cur_char);
}
}

Expand Down
100 changes: 70 additions & 30 deletions src/net/unesc/hal/analysis/Semantic.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ public class Semantic {

private Node tree;
private Node currNode;
private String attrId;
private String lastId;

private boolean isAttribution;
private boolean isArgument;

public Semantic() {
this.errors = new ArrayList<>();
this.tree = new Node();
this.currNode = tree;
this.currType = 0;
this.currCategory = -1;
this.isAttribution = false;
this.isArgument = false;
this.attrId = null;
this.lastId = null;
}

public ArrayList<String[]> getErrors() {
Expand All @@ -51,60 +60,54 @@ public void check(String[] item) {

String line = item[0];
Integer cod = new Integer(item[1]);
String token = item[2];
String source = item[3];

//1, "program"
//2, "label"
//3, "const"
//4, "var"
//5, "procedure"
//6, "begin"
//7, "end"
//8, "integer"
//9, "array"
//10, "of"
//11, "call"
//12, "goto"
//13, "if"
//14, "then"
//15, "else"
//16, "while"
//17, "do"
//18, "repeat"
//19, "until"
//20, "readln"
//21, "writeln"
//22, "or"
//23, "and"
//24, "not"
//27, "for"
//28, "to"
//29, "case"
switch (cod) {
case 2:
// Label
currCategory = CATEGORY_LABEL;
break;
case 3:
// Constant
currCategory = CATEGORY_CONSTANT;
break;
case 4:
// Var
currCategory = CATEGORY_VARIABLE;
break;
case 5:
// Procedure
currCategory = CATEGORY_PROCEDURE;
break;
case 6:
// Begin
currCategory = 0;
break;
case 7:
// End
if (currNode.getParent() != null) {
currNode = currNode.getParent();
}
break;
case 25:
// Identifier
lastId = source;
handleIdentifier(line, source);
break;
case 38:
// :=
if(currCategory == 0){
attrId = lastId;
isAttribution = true;
}
break;
case 47:
// Ponto e vírgula
if(currCategory == 0){
isAttribution = false;
isArgument = false;
}
break;
}

}
Expand All @@ -113,7 +116,7 @@ private void handleIdentifier(String line, String source) {
switch (currCategory) {
case -1:
// Nome do programa
return;
break;
case CATEGORY_PROCEDURE:
if (addProcedure(source) == -1) {
addError("Linha ->" + line + ": A procedure " + source + " já foi declarada");
Expand Down Expand Up @@ -144,10 +147,47 @@ private void handleIdentifier(String line, String source) {
default:
if (!anyMatchDeclaration(source, currNode)) {
addError("Linha ->" + line + ": O identificador " + source + " não foi declarado");
break;
}
if(isAttribution && attrId != null && getIdentifierType(attrId, currNode) != getIdentifierType(source, currNode)){
addError("Linha ->" + line + ": A atribuição de valor (" + source + ") para o identificador " + attrId + " é incompatível com o seu tipo");
break;
}
if(anyMatchProcedure(source)){
attrId = source;
isArgument = true;
break;
}
if(isArgument && getIdentifierType(source, currNode) != TYPE_INTEGER){
addError("Linha ->" + line + ": O argumento (" + source + ") é incompatível com a procedure " + attrId);
break;
}


}
}

private int getIdentifierType(String name, Node n){

for (Node p : n.getChildren()) {
if (p.isProcedure() && p.getName().toUpperCase().equals(name.toUpperCase())) {
return -1;
}
}

for (Identifier i : n.getIdentifiers()) {
if (i.getName().toUpperCase().equals(name.toUpperCase())) {
return i.getType();
}
}

if (n.getParent() != null) {
return getIdentifierType(name, n.getParent());
}

return -1;
}

private int addIdentifier(String name, int category, int type) {
if (anyMatchIdentifier(name)) {
return -1;
Expand Down Expand Up @@ -272,7 +312,7 @@ private String parseCategory(int category) {
case CATEGORY_CONSTANT:
return "Constante";
case CATEGORY_LABEL:
return "Label";
return "Rótulo";
case CATEGORY_PARAMETER:
return "Parâmetro";
case CATEGORY_PROCEDURE:
Expand Down
4 changes: 2 additions & 2 deletions src/net/unesc/hal/analysis/semantic/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public void addIdentifier(Identifier id){
this.identifiers.add(id);
}

public void getIdentifier(int value){
this.identifiers.get(value);
public Identifier getIdentifier(int value){
return this.identifiers.get(value);
}

public ArrayList<Node> getChildren() {
Expand Down
5 changes: 2 additions & 3 deletions src/net/unesc/hal/listeners/EditorListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import net.unesc.hal.analysis.lexicon.Source;
import net.unesc.hal.exceptions.EditorException;
Expand Down Expand Up @@ -48,7 +47,7 @@ private void handle(String cmd) throws EditorException, IOException {
+ "\n\nDisciplina:"
+ "\n- Compiladores 2018/1"
+ "\n\nProfessor:"
+ "\n- Gilberto Vieira", "Sobre", JOptionPane.INFORMATION_MESSAGE);
+ "\n- Gilberto Vieira", "Sobre", JOptionPane.PLAIN_MESSAGE);
break;
case Editor.SAVE:
ed.setPath(File.write(src.getCode(), ed, ed.getPath()));
Expand Down
Binary file removed src/net/unesc/hal/resources/favicon.png
Binary file not shown.
Binary file added src/net/unesc/hal/resources/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/net/unesc/hal/views/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void initEditor() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (unsaved) {
if (JOptionPane.showConfirmDialog(null, "Deseja salvar as alteraçãos antes de sair?", "Salvar alterações", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
if (JOptionPane.showConfirmDialog(null, "Deseja salvar as alteraçãos antes de sair?", "Salvar alterações", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
btnSave.doClick();
};
}
Expand All @@ -197,7 +197,7 @@ public void windowClosing(WindowEvent e) {
);
fa = new FiniteAutomaton(new HAL());

setIconImage(new ImageIcon(getClass().getResource("../resources/favicon.png")).getImage());
setIconImage(new ImageIcon(getClass().getResource("../resources/icon.png")).getImage());

fieldEditor = new TextPanelHighLight();
fieldEditor.setCaretColor(Color.WHITE);
Expand Down

0 comments on commit 425149e

Please sign in to comment.