blob: 37d4d7943389fc4f4f330981b762addec8c98290 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package ast.nodes;
import ast.types.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import semanticanalysis.SemanticError;
import semanticanalysis.SymbolTable;
/**
* Node for the `atom` statement of the grammar.
*/
public class AtomNode implements Node {
protected String val;
protected TestlistCompNode exprlist;
public AtomNode(String val, Node exprlist) {
this.val = val;
this.exprlist = (TestlistCompNode) exprlist;
}
/**
* Returns the identifier of the `AtomNode` if it's not `null`, otherwise
* returns `null`.
*/
public String getId() {
return this.val;
}
@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<>();
if (val != null) {
if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) {
errors.add(new SemanticError("name '" + this.getId() + "' is not defined."));
} else {
// System.out.println("exist " + this.typeCheck());
}
}
if (exprlist != null) {
errors.addAll(exprlist.checkSemantics(ST, _nesting));
}
return errors;
}
// ENHANCE: return more specific types
@Override
public Type typeCheck() {
if (this.val == null) {
return new VoidType();
}
Pattern noneVariable = Pattern.compile("^(None)$");
Pattern booleanVariable = Pattern.compile("^(True|False)$");
Pattern reservedWords = Pattern.compile("^(continue|break|int|float)$");
// this regex should match every possible atom name written in this format: CHAR
// (CHAR | DIGIT)*
Pattern simpleVariable = Pattern.compile("^[a-zA-Z][a-zA-Z0-9]*$", Pattern.CASE_INSENSITIVE);
Matcher noneVariableMatcher = noneVariable.matcher(this.val);
Matcher booleanVariableMatcher = booleanVariable.matcher(this.val);
Matcher reservedWordsMatcher = reservedWords.matcher(this.val);
Matcher simpleVariableMatcher = simpleVariable.matcher(this.val);
boolean matchFoundNone = noneVariableMatcher.find();
boolean matchFoundBoolean = booleanVariableMatcher.find();
boolean matchFoundContinueBreak = reservedWordsMatcher.find();
boolean matchFoundSimpleVariable = simpleVariableMatcher.find();
if (matchFoundBoolean) {
return new BoolType();
} else if (matchFoundContinueBreak) {
return new ReservedWordsType();
} else if (matchFoundNone) {
return new NoneType();
} else if (matchFoundSimpleVariable) {
return new AtomType(); // could be a variable or a fuction
} else {
return new VoidType(); // could be any type of data
}
}
// TODO: add code generation for atom node
@Override
public String codeGeneration() {
return "";
}
@Override
public String toPrint(String prefix) {
// FIXME: can be a testlist_comp with two expr and two atoms
if (val != null) {
return prefix + "Atom(" + val + ")\n";
}
return prefix + "Atom(null)\n";
}
}
|