-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeSystem.java
More file actions
241 lines (206 loc) · 8.6 KB
/
Copy pathTypeSystem.java
File metadata and controls
241 lines (206 loc) · 8.6 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// TypeSystem.java
// Sistema de tipos e analisador semântico do TONACO SCRIPT v2.0
// Criado por: Guilherme Lucas Tonaco Carvalho
package com.tonaco.tns;
import java.util.*;
public enum TNSType {
INTEGER, FLOAT, STRING, BOOLEAN, ARRAY, FUNCTION, NULL, UNKNOWN;
public static TNSType fromValue(Object value) {
if (value == null) return NULL;
if (value instanceof Integer) return INTEGER;
if (value instanceof Double) return FLOAT;
if (value instanceof String) return STRING;
if (value instanceof Boolean) return BOOLEAN;
if (value instanceof List) return ARRAY;
return UNKNOWN;
}
public static boolean isCompatible(TNSType a, TNSType b) {
if (a == UNKNOWN || b == UNKNOWN) return true;
if (a == NULL || b == NULL) return true;
if (a == INTEGER && b == FLOAT) return true;
if (a == FLOAT && b == INTEGER) return true;
return a == b;
}
public static TNSType promote(TNSType a, TNSType b) {
if (a == INTEGER && b == INTEGER) return INTEGER;
if ((a == INTEGER || a == FLOAT) && (b == INTEGER || b == FLOAT)) return FLOAT;
if (a == STRING || b == STRING) return STRING;
if (a == UNKNOWN) return b;
if (b == UNKNOWN) return a;
return UNKNOWN;
}
public static TNSType fromHint(String hint) {
return switch (hint.toLowerCase()) {
case "int", "inteiro" -> INTEGER;
case "float", "decimal"-> FLOAT;
case "string", "texto" -> STRING;
case "bool", "booleano"-> BOOLEAN;
case "array", "lista" -> ARRAY;
default -> UNKNOWN;
};
}
}
// ============================================================
// Analisador Semântico
// ============================================================
class SemanticAnalyzer implements Visitor<Void> {
// Escopo em pilha: cada frame é um Map nome→tipo
private final Deque<Map<String, TNSType>> scopes = new ArrayDeque<>();
private final List<String> errors = new ArrayList<>();
private final List<String> warnings = new ArrayList<>();
public SemanticAnalyzer() {
// escopo global
scopes.push(new HashMap<>());
}
public List<String> getErrors() { return errors; }
public List<String> getWarnings() { return warnings; }
public boolean analyze(ProgramNode program) {
program.accept(this);
return errors.isEmpty();
}
// --- Utilitários de escopo ---
private void define(String name, TNSType type) {
scopes.peek().put(name, type);
}
private TNSType lookup(String name) {
for (Map<String, TNSType> scope : scopes) {
if (scope.containsKey(name)) return scope.get(name);
}
return null;
}
private void pushScope() { scopes.push(new HashMap<>()); }
private void popScope() { scopes.pop(); }
private void err(String msg) { errors.add(" ✖ " + msg); }
private void warn(String msg) { warnings.add(" ⚠ " + msg); }
// ============================================================
// VISITORS
// ============================================================
@Override public Void visitProgramNode(ProgramNode n) {
for (ASTNode s : n.statements) if (s != null) s.accept(this);
return null;
}
@Override public Void visitBlockNode(BlockNode n) {
pushScope();
for (ASTNode s : n.statements) if (s != null) s.accept(this);
popScope();
return null;
}
@Override public Void visitPrintNode(PrintNode n) {
n.expression.accept(this);
return null;
}
@Override public Void visitLetNode(LetNode n) {
TNSType valueType = inferType(n.value);
n.value.accept(this);
if (n.typeHint != null) {
TNSType declared = TNSType.fromHint(n.typeHint);
if (!TNSType.isCompatible(declared, valueType)) {
err("Variável '" + n.name + "' declarada como " + declared + " mas recebe " + valueType);
}
}
if (lookup(n.name) != null && scopes.peek().containsKey(n.name)) {
warn("Variável '" + n.name + "' redeclarada no mesmo escopo");
}
define(n.name, valueType);
return null;
}
@Override public Void visitAssignNode(AssignNode n) {
TNSType current = lookup(n.name);
if (current == null) {
err("Variável '" + n.name + "' não declarada antes de ser atribuída");
}
TNSType valueType = inferType(n.value);
n.value.accept(this);
if (current != null && !TNSType.isCompatible(current, valueType)) {
warn("Atribuição incompatível: '" + n.name + "' é " + current + ", recebe " + valueType);
}
return null;
}
@Override public Void visitVariableNode(VariableNode n) {
if (lookup(n.name) == null) {
err("Variável '" + n.name + "' não declarada");
}
return null;
}
@Override public Void visitBinaryNode(BinaryNode n) {
n.left.accept(this);
n.right.accept(this);
TNSType lt = inferType(n.left), rt = inferType(n.right);
if (n.operator == TokenType.PLUS) return null; // permite string + qualquer
if (!TNSType.isCompatible(lt, rt)) {
warn("Operação " + n.operator + " entre tipos incompatíveis: " + lt + " e " + rt);
}
return null;
}
@Override public Void visitUnaryNode(UnaryNode n) {
n.expression.accept(this);
return null;
}
@Override public Void visitIfNode(IfNode n) {
n.condition.accept(this);
n.thenBranch.accept(this);
if (n.elseBranch != null) n.elseBranch.accept(this);
return null;
}
@Override public Void visitWhileNode(WhileNode n) {
n.condition.accept(this);
n.body.accept(this);
return null;
}
@Override public Void visitForNode(ForNode n) {
pushScope();
if (n.init != null) n.init.accept(this);
if (n.condition != null) n.condition.accept(this);
if (n.increment != null) n.increment.accept(this);
n.body.accept(this);
popScope();
return null;
}
@Override public Void visitFunctionNode(FunctionNode n) {
define(n.name, TNSType.FUNCTION);
pushScope();
for (String p : n.params) define(p, TNSType.UNKNOWN);
n.body.accept(this);
popScope();
return null;
}
@Override public Void visitCallNode(CallNode n) {
if (lookup(n.callee) == null) {
err("Função '" + n.callee + "' não declarada");
}
for (ASTNode arg : n.arguments) arg.accept(this);
return null;
}
@Override public Void visitReturnNode(ReturnNode n) {
if (n.value != null) n.value.accept(this);
return null;
}
// Literais — sem verificação necessária
@Override public Void visitStringNode(StringNode n) { return null; }
@Override public Void visitNumberNode(NumberNode n) { return null; }
@Override public Void visitBooleanNode(BooleanNode n) { return null; }
@Override public Void visitNullNode(NullNode n) { return null; }
// Comandos TNS
@Override public Void visitScanNode(ScanNode n) { n.url.accept(this); return null; }
@Override public Void visitDeepScanNode(DeepScanNode n) { n.url.accept(this); if (n.depth != null) n.depth.accept(this); return null; }
@Override public Void visitAnalyzeNode(AnalyzeNode n) { n.url.accept(this); return null; }
@Override public Void visitDownloadNode(DownloadNode n) { n.url.accept(this); if (n.destination != null) n.destination.accept(this); return null; }
@Override public Void visitGithubNode(GithubNode n) { n.repo.accept(this); return null; }
@Override public Void visitReportNode(ReportNode n) { return null; }
@Override public Void visitSyncNode(SyncNode n) { return null; }
// --- Inferência de tipo ---
private TNSType inferType(ASTNode n) {
if (n instanceof StringNode) return TNSType.STRING;
if (n instanceof NumberNode num)
return (num.value instanceof Double) ? TNSType.FLOAT : TNSType.INTEGER;
if (n instanceof BooleanNode) return TNSType.BOOLEAN;
if (n instanceof NullNode) return TNSType.NULL;
if (n instanceof VariableNode var) {
TNSType t = lookup(var.name);
return t != null ? t : TNSType.UNKNOWN;
}
if (n instanceof BinaryNode bin)
return TNSType.promote(inferType(bin.left), inferType(bin.right));
return TNSType.UNKNOWN;
}
}