Skip to content
Snippets Groups Projects
Verified Commit 5e01fa6d authored by Recolic Keghart's avatar Recolic Keghart
Browse files

remove Exception on duplicate id

parent a21ab384
No related branches found
No related tags found
No related merge requests found
Pipeline #54 passed with stages
in 3 minutes and 22 seconds
package chocopy.common.analysis;
import org.apache.tools.ant.taskdefs.PathConvert;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
......
......@@ -70,9 +70,15 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
continue;
}
// TODO: DO NOT throw on duplicate id. generate a compiler error.
if(isBuildingSym)
sym.put(name, type);
if(isBuildingSym) {
try {
sym.put(name, type);
}
catch(RuntimeException e) {
// duplicate id
err(decl, e.getMessage());
}
}
}
if(isBuildingSym)
......@@ -160,18 +166,30 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
for(TypedVar param : funcDef.params) {
String name = param.identifier.name;
ValueType type = resolveTypeAnnotation(param.type);
//if(typeIsUserDefinedClass(type))
// type = (ValueType) sym.get(((ClassValueType) type).className());
if(isBuildingSym)
sym.put(name, type);
if(isBuildingSym) {
try {
sym.put(name, type);
}
catch(RuntimeException e) {
// duplicate id
err(param, e.getMessage());
}
}
args.add(type);
}
for(Declaration decl : funcDef.declarations) {
String name = decl.getIdentifier().name;
SymbolType type = decl.dispatch(this);
if(isBuildingSym)
sym.put(name, type);
if(isBuildingSym) {
try {
sym.put(name, type);
}
catch(RuntimeException e) {
// duplicate id
err(decl, e.getMessage());
}
}
}
if(!isBuildingSym) {
......@@ -182,8 +200,17 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
}
FuncType funcT = new FuncType(args, resolveTypeAnnotation(funcDef.returnType));
if(isBuildingSym)
sym.put(funcDef.name.name, funcT); // this funcdef should be add to both parentSym and localSym to support recursive func.
if(isBuildingSym) {
try {
// this funcdef should be add to both parentSym and localSym to support recursive func.
// stmt will fill the parent sym, and I will fill the local sym.
sym.put(funcDef.name.name, funcT);
}
catch(RuntimeException e) {
// duplicate id
err(funcDef, e.getMessage());
}
}
// TA don't like it. OK I won't dispatch this id...
//funcDef.name.dispatch(this); // dispatch it.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment