Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix codegen + improve command line usage
  • Loading branch information
jonathanlking committed Jan 12, 2019
commit fa5305d1bec9ad7940441cb37a95651be58a14b3
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ protected int parseFlag(int i) throws CommandLineException
case CLArgParser.VALIDATION_EFSM_FLAG:
case CLArgParser.UNFAIR_EFSM_FLAG:
case CLArgParser.API_GEN_FLAG:
case CLArgParser.API_GEN_PS_FLAG:
case CLArgParser.STATECHAN_API_GEN_FLAG:
{
return parseProtoAndRoleArgs(flag, i);
Expand All @@ -241,6 +240,7 @@ protected int parseFlag(int i) throws CommandLineException
case CLArgParser.SGRAPH_FLAG:
case CLArgParser.UNFAIR_SGRAPH_FLAG:
case CLArgParser.SESSION_API_GEN_FLAG:
case CLArgParser.API_GEN_PS_FLAG:
{
return parseProtoArg(flag, i);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.scribble.codegen.purescript;

import org.scribble.main.RuntimeScribbleException;

import java.util.*;

public class ForeignType {
Expand Down Expand Up @@ -31,6 +33,9 @@ public static String generateImports(Set<ForeignType> types) {
for (String source : imports.keySet()) {
// No need to explicitly import
if (source.equals(PRIM_MODULE)) continue;
if (source.trim().length() == 0) {
throw new RuntimeScribbleException("Foreign type import source for " + imports.get(source) + " cannot be empty");
}
is.append("import " + source + " (");
List<String> ts = new ArrayList<>(imports.get(source));
is.append(ts.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.scribble.ast.global.GProtocolDecl;
import org.scribble.main.Job;
import org.scribble.main.JobContext;
import org.scribble.main.RuntimeScribbleException;
import org.scribble.main.ScribbleException;
import org.scribble.model.endpoint.EGraph;
import org.scribble.model.endpoint.EState;
Expand Down Expand Up @@ -48,20 +49,24 @@ public Map<String, String> generateApi(GProtocolName fullname) throws ScribbleEx
GProtocolName simpname = fullname.getSimpleName();
GProtocolDecl gpd = (GProtocolDecl) mod.getProtocolDecl(simpname);

if (!gpd.isExplicitModifier()) {
throw new RuntimeScribbleException("Only protocols with explicit connections are currently supported in this version");
}

String moduleName = fullname.getPrefix().toString();
String protocolName = fullname.getSimpleName().toString();

// Generate protocol type-level information
DataType protocolType = new DataType(protocolName, null, "Protocol", true);
TypeClassInstance protocolNameInst = new TypeClassInstance("protocolName" + protocolType.name, "ProtocolName", new String[] {protocolType.name, ("\"" + protocolType.name + "\"")});
// TypeClassInstance protocolNameInst = new TypeClassInstance("protocolName" + protocolType.name, "ProtocolName", new String[] {protocolType.name, ("\"" + protocolType.name + "\"")});

StringBuilder roleNames = new StringBuilder("(");
// For each role make a projection, then traverse the graph getting the states + transitions
for (Role r : gpd.header.roledecls.getRoles()) {
roleNames.append("\"" + r.toString() + "\" ::: ");
}
roleNames.append("SNil)");
TypeClassInstance protocolRoleNames = new TypeClassInstance("protocolRoleNames" + protocolType.name, "ProtocolRoleNames", new String[] {protocolType.name, roleNames.toString()});
// StringBuilder roleNames = new StringBuilder("(");
// // For each role make a projection, then traverse the graph getting the states + transitions
// for (Role r : gpd.header.roledecls.getRoles()) {
// roleNames.append("\"" + r.toString() + "\" ::: ");
// }
// roleNames.append("SNil)");
// TypeClassInstance protocolRoleNames = new TypeClassInstance("protocolRoleNames" + protocolType.name, "ProtocolRoleNames", new String[] {protocolType.name, roleNames.toString()});

// Message actions and their corresponding datatype
Map<String, Payload> datatypes = new HashMap<>();
Expand Down Expand Up @@ -131,7 +136,13 @@ public Map<String, String> generateApi(GProtocolName fullname) throws ScribbleEx
instances.add(new TypeClassInstance(("disconnect" + curr), "Disconnect", new String[]{r.toString(), to, curr, next}));

} else if (action.isRequest()) {
instances.add(new TypeClassInstance(("request" + curr), "Request", new String[]{r.toString(), to, curr, next}));
String connectedState = curr + "Connected";
instances.add(new TypeClassInstance(("connect" + curr), "Connect", new String[]{r.toString(), to, curr, connectedState}));
String type = action.mid.toString();
// Add the instance and message data type
states.add(new DataType(curr + "Connected", null, DataType.KIND_TYPE, true));
instances.add(new TypeClassInstance(("send" + curr), "Send", new String[]{to, connectedState, next, type}));
addDatatype(datatypes, action, foreignImports);
} else {
// TODO: What is wrap-client + do we need to handle it?
throw new ScribbleException(null, "Unsupported action " + s.getStateKind());
Expand Down Expand Up @@ -219,10 +230,10 @@ public Map<String, String> generateApi(GProtocolName fullname) throws ScribbleEx
EAction action = s.getAllActions().get(0);
String next = getStateTypeName(s.getAllSuccessors().get(0));
String to = action.obj.toString();
instances.add(new TypeClassInstance(("connect" + curr), "Connect", new String[]{r.toString(), to, curr, next}));
instances.add(new TypeClassInstance(("accept" + curr), "Accept", new String[]{r.toString(), to, curr, next}));
break;
case WRAP_SERVER:
throw new ScribbleException(null, "Unsupported action " + s.getStateKind());
throw new RuntimeScribbleException("Unsupported action " + s.getStateKind());
}

}
Expand Down Expand Up @@ -258,8 +269,8 @@ public Map<String, String> generateApi(GProtocolName fullname) throws ScribbleEx

// Protocol
sections.add(protocolType.generateDataType());
sections.add(protocolNameInst.generateInstance());
sections.add(protocolRoleNames.generateInstance());
// sections.add(protocolNameInst.generateInstance());
// sections.add(protocolRoleNames.generateInstance());

// ProtocolRoleNames

Expand Down Expand Up @@ -302,7 +313,7 @@ private static String moduleDeclaration(String moduleName, String protocolName)
private static String staticImports() {
StringBuilder sb = new StringBuilder();
sb.append("import Scribble.FSM\n");
sb.append("import Scribble.Type.SList (type (:::), SLProxy(..), SNil, symbols)\n");
// sb.append("import Scribble.Type.SList (type (:::), SLProxy(..), SNil, symbols)\n");
sb.append("import Type.Row (Cons, Nil)\n");
sb.append("import Data.Void (Void)\n");
sb.append("import Data.Tuple (Tuple)\n");
Expand Down