Skip to content

Commit

Permalink
Add generation of .lf files
Browse files Browse the repository at this point in the history
  • Loading branch information
Soroosh129 authored and petervdonovan committed Jun 13, 2022
1 parent c5e411e commit 2fbd047
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
10 changes: 9 additions & 1 deletion org.lflang/src/org/lflang/federated/FedFileConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.lflang.federated;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

Expand All @@ -47,12 +48,19 @@ public FedFileConfig(Resource resource, Path srcGenBasePath, boolean useHierarch

}

public FedFileConfig(FileConfig fileConfig) throws IOException {
super(fileConfig.resource, fileConfig.getSrcGenBasePath(), fileConfig.useHierarchicalBin);
}

/**
* FIXME
* @return
*/
public Path getFedGenPath() {
return srcPkgPath.resolve("fed-gen");
return srcPkgPath.resolve("fed-gen").resolve(this.name);
}

Path getFedSrcPath() {
return getFedGenPath().resolve("src");
}
}
41 changes: 41 additions & 0 deletions org.lflang/src/org/lflang/federated/FedGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.lflang.federated;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;

import org.lflang.ASTUtils;
import org.lflang.ErrorReporter;
import org.lflang.generator.LFGeneratorContext;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Reactor;

public class FedGenerator {

private FedFileConfig fileConfig;

public FedGenerator(FedFileConfig fileConfig, ErrorReporter errorReporter) {

this.fileConfig = fileConfig;
}
public boolean doGenerate(Resource resource, LFGeneratorContext context) throws IOException {
Reactor fedReactor = FedASTUtils.findFederatedReactor(resource);
for (Instantiation fedInstantiation : fedReactor.getInstantiations()) {
System.out.println("Generating code for federate " + fedInstantiation.getName() + " in directory "
+ fileConfig.getFedSrcPath());
Files.createDirectories(fileConfig.getFedSrcPath());

Path lfFilePath = fileConfig.getFedSrcPath().resolve(fedInstantiation.getName() + ".lf");
try (var srcWriter = Files.newBufferedWriter(lfFilePath)) {
srcWriter.write(NodeModelUtils.getNode(fedReactor.eContainer()).getText());
}

}
return false;
}

// private void generateFederate();
}
17 changes: 0 additions & 17 deletions org.lflang/src/org/lflang/federated/FooBarGenerator.java

This file was deleted.

16 changes: 9 additions & 7 deletions org.lflang/src/org/lflang/generator/LFGenerator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.lflang.generator;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -19,10 +18,9 @@
import org.lflang.Target;
import org.lflang.federated.FedASTUtils;
import org.lflang.federated.FedFileConfig;
import org.lflang.federated.FooBarGenerator;
import org.lflang.federated.FedGenerator;
import org.lflang.generator.c.CGenerator;
import org.lflang.generator.python.PythonGenerator;
import org.lflang.lf.Reactor;
import org.lflang.scoping.LFGlobalScopeProvider;

import com.google.inject.Inject;
Expand Down Expand Up @@ -61,9 +59,6 @@ private FileConfig createFileConfig(final Target target,
IFileSystemAccess2 fsa,
LFGeneratorContext context) throws IOException {
Path srcGenBasePath = FileConfig.getSrcGenRoot(fsa);
if (FedASTUtils.findFederatedReactor(resource) != null) {
return new FedFileConfig(resource, srcGenBasePath, context.useHierarchicalBin());
}
// Since our Eclipse Plugin uses code injection via guice, we need to
// play a few tricks here so that FileConfig does not appear as an
// import. Instead we look the class up at runtime and instantiate it if
Expand Down Expand Up @@ -170,7 +165,14 @@ public void doGenerate(Resource resource, IFileSystemAccess2 fsa,
final ErrorReporter errorReporter = lfContext.constructErrorReporter(fileConfig);

if (FedASTUtils.findFederatedReactor(resource) != null) {
generatorErrorsOccurred = (new FooBarGenerator(fileConfig, errorReporter)).doGenerate(resource, lfContext);
try {
generatorErrorsOccurred = (new FedGenerator(
new FedFileConfig(fileConfig),
errorReporter)).doGenerate(resource, lfContext);
} catch (IOException e) {
throw new RuntimeIOException("Error during federated code generation", e);
}

} else {

final GeneratorBase generator = createGenerator(target, fileConfig, errorReporter);
Expand Down

0 comments on commit 2fbd047

Please sign in to comment.