use of org.abs_models.frontend.ast.ParamDecl in project abstools by abstools.
the class ClassGenerator method generateExports.
private void generateExports() {
ecs.println("-export([get_val_internal/2,set_val_internal/3,init_internal/0,get_state_for_modelapi/1,implemented_interfaces/0,exported/0]).");
ecs.println("-compile(export_all).");
ecs.println();
HashSet<MethodSig> callable_sigs = new HashSet<>();
HashSet<InterfaceDecl> visited = new HashSet<>();
for (InterfaceTypeUse i : classDecl.getImplementedInterfaceUseList()) {
visited.add((InterfaceDecl) i.getDecl());
}
while (!visited.isEmpty()) {
InterfaceDecl id = visited.iterator().next();
visited.remove(id);
for (MethodSig ms : id.getBodyList()) {
if (ms.isHTTPCallable()) {
callable_sigs.add(ms);
}
}
for (InterfaceTypeUse i : id.getExtendedInterfaceUseList()) {
visited.add((InterfaceDecl) i.getDecl());
}
}
ecs.print("implemented_interfaces() -> [ ");
String separator = "";
for (InterfaceDecl i : classDecl.getSuperTypes()) {
ecs.format("%s<<\"%s\">>", separator, i.getName());
separator = ", ";
}
ecs.println(" ].");
ecs.println();
ecs.print("exported() -> #{ ");
boolean first = true;
for (MethodSig ms : callable_sigs) {
if (ms.isHTTPCallable()) {
if (!first)
ecs.print(", ");
first = false;
ecs.print("<<\"" + ms.getName() + "\">> => { ");
ecs.print("'m_" + ms.getName() + "'");
ecs.print(", ");
ecs.print("<<\"" + ms.getReturnType().getType().toString() + "\">>");
ecs.print(", ");
ecs.print("[ ");
boolean innerfirst = true;
for (ParamDecl p : ms.getParamList()) {
// ABS type, and ABS type arguments (if present)
if (!innerfirst)
ecs.print(", ");
innerfirst = false;
ecs.print("{ ");
ecs.print("<<\"" + p.getName() + "\">>, ");
ecs.print("<<\"" + p.getType().toString() + "\">>, ");
generateParameterDescription(p.getType());
ecs.print(" }");
}
ecs.print("] ");
ecs.print("}");
}
}
ecs.println(" }.");
ecs.println();
}
use of org.abs_models.frontend.ast.ParamDecl in project abstools by abstools.
the class JavaGeneratorHelper method generateParamArgs.
public static void generateParamArgs(PrintStream stream, String firstArg, List<ParamDecl> params) {
stream.print("(");
boolean first = true;
if (firstArg != null) {
stream.print(firstArg);
first = false;
}
for (ParamDecl d : params) {
if (!first)
stream.print(", ");
stream.print(JavaBackend.getVariableName(d.getName()));
first = false;
}
stream.print(")");
}
use of org.abs_models.frontend.ast.ParamDecl in project abstools by abstools.
the class ClassDeclGenerator method generateFields.
private void generateFields() {
for (ParamDecl p : decl.getParams()) {
stream.print("private ");
p.generateJava(stream);
stream.println(";");
}
for (FieldDecl f : decl.getFields()) {
f.generateJava(stream);
}
}
use of org.abs_models.frontend.ast.ParamDecl in project abstools by abstools.
the class ClassDeclGenerator method generateConstructor.
private void generateConstructor() {
// constructor
stream.print("public " + className);
JavaGeneratorHelper.generateParams(stream, decl.getParams());
stream.println(" {");
for (ParamDecl p : decl.getParams()) {
stream.println("this." + JavaBackend.getVariableName(p.getName()) + " = " + JavaBackend.getVariableName(p.getName()) + ";");
}
stream.println("getCOG().objectCreated(this);");
stream.println("}");
stream.println("protected final void __ABS_init() {");
for (FieldDecl f : decl.getFields()) {
if (f.hasInitExp()) {
stream.print("this.");
stream.print(JavaBackend.getVariableName(f.getName()));
stream.print(" = ");
f.getInitExp().generateJava(stream);
stream.print(";");
}
}
if (decl.hasInitBlock()) {
decl.getInitBlock().generateJava(stream);
}
stream.println("getCOG().objectInitialized(this);");
stream.println("}");
}
use of org.abs_models.frontend.ast.ParamDecl in project abstools by abstools.
the class ClassDeclGenerator method generateClassBody.
private void generateClassBody() {
// singleton() method instantiates strictly one dynamic class object and does initialisation,
// i.e. adds fields, constructor and methods.
stream.println("{");
stream.println("private static " + ABSDynamicClass.class.getName() + " instance;");
stream.println("public static void setInstance(" + ABSDynamicClass.class.getName() + " cls) { instance = cls; }");
stream.println("public static " + ABSDynamicClass.class.getName() + " singleton() {");
stream.println("if (instance == null) {");
stream.println("instance = new " + ABSDynamicClass.class.getName() + "();");
stream.println("instance.setName(\"" + decl.getName() + "\");");
// add class parameters
for (ParamDecl p : decl.getParams()) {
String name = JavaBackend.getVariableName(p.getName());
stream.println("instance.addField(\"" + name + "\", new " + ABSField.class.getName() + "());");
}
// add fields
for (FieldDecl f : decl.getFields()) {
String name = JavaBackend.getVariableName(f.getName());
stream.println("instance.addField(\"" + name + "\", " + className + ".field$" + name + ".singleton());");
}
// add constructor
stream.println("instance.setConstructor(" + className + ".CON$TRUCT.singleton());");
stream.print("instance.setParams(");
for (int i = 0; i < decl.getNumParam(); i++) {
if (i != 0)
stream.print(", ");
stream.print("\"" + JavaBackend.getVariableName(decl.getParam(i).getName()) + "\"");
}
stream.println(");");
// add methods
for (MethodImpl m : decl.getMethods()) {
String methodName = m.getMethodSig().getName();
stream.println("instance.addMethod(\"" + methodName + "\", " + className + "." + methodName + ".singleton());");
}
stream.println("}");
stream.println("return instance;");
stream.println("}");
// generate field inner classes
for (FieldDecl field : decl.getFields()) {
field.generateJavaDynamic(stream);
}
// generate CON$TRUCT inner class
generateConstructor();
// generate method inner classes
for (MethodImpl method : decl.getMethods()) {
method.generateJavaDynamic(stream);
// FIXME not sure how to handle FLI methods
if (method.isForeign()) {
stream.println("/* FLI method: not implemented yet */");
DynamicJavaGeneratorHelper.generateFLIMethod(stream, method);
}
}
generateCreateNewCOGMethod();
generateNewObjectMethods();
stream.println("}");
}
Aggregations