Skip to content

Commit

Permalink
Add abstract type declaration
Browse files Browse the repository at this point in the history
The implementation is not a generic abstract data type implementation.
Java does not support ADT as Haskell does.

Refs #30
  • Loading branch information
nobeh committed Aug 7, 2015
1 parent fbe18c0 commit 66f828e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
23 changes: 20 additions & 3 deletions src/main/java/jabsc/JavaTypeTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,32 @@
class JavaTypeTranslator implements Function<String, String> {

private final Map<String, String> abs2java = new HashMap<>();
private final Map<String, String> abstractTypes = new HashMap<>();

public JavaTypeTranslator() {
fill(abs2java);
}

@Override
public String apply(String absType) {
String javaType = abs2java.get(absType);
String javaType = translateJava(absType);
if (javaType != null) {
return javaType;
}
// TODO To be completed.
return absType;
String type = translateAbstract(absType);
if (type == null) {
return absType;
}
javaType = translateJava(type);
return javaType == null ? type : javaType;
}

private String translateJava(String absType) {
return abs2java.get(absType);
}

private String translateAbstract(String absType) {
return abstractTypes.get(absType);
}

protected void fill(Map<String, String> types) {
Expand All @@ -36,4 +49,8 @@ protected void fill(Map<String, String> types) {
types.put("Fut", Response.class.getSimpleName());
}

protected void registerAbstractType(String absType, String defType) {
this.abstractTypes.put(absType, defType);
}

}
58 changes: 44 additions & 14 deletions src/main/java/jabsc/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ static enum AbsElementType {
*/
FUNCTION,

/**
* An abstract data type declaration.
*/
TYPE,

;
}

Expand Down Expand Up @@ -102,14 +107,14 @@ static enum AbsElementType {
private final JavaTypeTranslator javaTypeTranslator;

// Internal state
private static final Multimap<String, MethodDefinition> METHODS =
private final Multimap<String, MethodDefinition> methods =
Multimaps.newSetMultimap(new HashMap<>(), new Supplier<Set<MethodDefinition>>() {
@Override
public Set<MethodDefinition> get() {
return new HashSet<>();
}
});
private static final Multimap<String, VarDefinition> VARIABLES =
private final Multimap<String, VarDefinition> variables =
Multimaps.newSetMultimap(new HashMap<>(), new Supplier<Set<VarDefinition>>() {
@Override
public Set<VarDefinition> get() {
Expand Down Expand Up @@ -158,6 +163,13 @@ public Prog visit(Prog p, JavaWriter w) {
public Prog visit(Modul m, JavaWriter w) {
try {

// Types
// Should be first to ensure correct type translation.
for (Decl decl : elements.get(AbsElementType.TYPE)) {
// Does NOT use writer.
decl.accept(this, w);
}

// Interfaces
for (Decl decl : elements.get(AbsElementType.INTERFACE)) {
String name = getTopLevelDeclIdentifier(decl);
Expand Down Expand Up @@ -767,7 +779,7 @@ public Prog visit(SExp p, JavaWriter w) {
if (exp instanceof ExpE) {
ExpE expE = (ExpE) exp;
EffExp effExp = expE.effexp_;
if (effExp instanceof Get) {
if (effExp instanceof Get || effExp instanceof New) {
// XXX Ideally fix the indentation
w.emitStatementEnd();
}
Expand Down Expand Up @@ -1335,14 +1347,19 @@ public Prog visit(TTypeSegmen p, JavaWriter arg) {
}

@Override
public Prog visit(TypeDecl p, JavaWriter arg) {
logNotImplemented("#visit(%s)", p);
public Prog visit(TypeDecl adt, JavaWriter arg) {
String adtName = adt.uident_;
String typeName = getTypeName(adt.type_);
this.javaTypeTranslator.registerAbstractType(adtName, typeName);
return prog;
}

@Override
public Prog visit(TypeParDecl p, JavaWriter arg) {
logNotImplemented("#visit(%s)", p);
public Prog visit(TypeParDecl adt, JavaWriter arg) {
String adtName = adt.uident_;
String typeName = getTypeName(adt.type_);
this.javaTypeTranslator.registerAbstractType(adtName, typeName);
logNotSupported("Parametric Type Declaration not supported: %s", adt.listuident_);
return prog;
}

Expand Down Expand Up @@ -2151,12 +2168,12 @@ private String getTypeName(Type type) {
}

private String findVariableType(String varName) {
for (String fqClassName : VARIABLES.keySet()) {
for (String fqClassName : variables.keySet()) {
if (!fqClassName.startsWith(this.packageName)) {
continue;
}
Collection<VarDefinition> variables = VARIABLES.get(fqClassName);
for (VarDefinition vd : variables) {
Collection<VarDefinition> vars = variables.get(fqClassName);
for (VarDefinition vd : vars) {
if (vd.getName().equals(varName)) {
return vd.getType();
}
Expand All @@ -2167,11 +2184,11 @@ private String findVariableType(String varName) {

private String findMethodReturnType(String methodName, String calleeClassType,
List<String> actualParamNames) {
for (String fqClassName : METHODS.keySet()) {
for (String fqClassName : methods.keySet()) {
if (calleeClassType != null && !fqClassName.endsWith(calleeClassType)) {
continue;
}
Collection<MethodDefinition> methods = METHODS.get(fqClassName);
Collection<MethodDefinition> methods = this.methods.get(fqClassName);
for (MethodDefinition md : methods) {
if (md.matches(methodName, actualParamNames)) {
return md.type();
Expand All @@ -2189,7 +2206,7 @@ private void createVarDefinition(String varName, String varType) {
String clazz = getQTypeName(((Modul) current).qtype_);
String fqClassName = this.packageName + "." + clazz;
VarDefinition vd = new VarDefinition(varName, varType);
VARIABLES.put(fqClassName, vd);
variables.put(fqClassName, vd);
}

private void createMethodDefinition(String returnType, String name, List<String> parameters) {
Expand All @@ -2200,7 +2217,7 @@ private void createMethodDefinition(String returnType, String name, List<String>
String clazz = getQTypeName(((Modul) current).qtype_);
String fqClassName = this.packageName + "." + clazz;
MethodDefinition md = new MethodDefinition(fqClassName, returnType, name, parameters);
METHODS.put(fqClassName, md);
methods.put(fqClassName, md);
}

private Module currentModule() {
Expand Down Expand Up @@ -2268,6 +2285,15 @@ private void buildProgramDeclarationTypes(Prog program) {
}
}
}
// 5. Type
for (Module module : program.listmodule_) {
Modul m = (Modul) module;
for (Decl decl : m.listdecl_) {
if (isAbsAbstractTypeDecl(decl)) {
elements.get(AbsElementType.TYPE).add(decl);
}
}
}
}

private boolean isAbsInterfaceDecl(Decl decl) {
Expand All @@ -2287,6 +2313,10 @@ private boolean isAbsDataTypeDecl(Decl decl) {
return decl instanceof DataDecl || decl instanceof DataParDecl;
}

private boolean isAbsAbstractTypeDecl(Decl decl) {
return decl instanceof TypeDecl || decl instanceof TypeParDecl;
}

private String getRefinedClassName(String name) {
return classNames.get(name);
}
Expand Down

0 comments on commit 66f828e

Please sign in to comment.