use of xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition in project atlas by lorenzleutgeb.
the class FunctionDefinitionVisitor method visitFunc.
@Override
public FunctionDefinition visitFunc(SplayParser.FuncContext ctx) {
List<String> args = new ArrayList<>(ctx.args.size());
for (Token t : ctx.args) {
String arg = t.getText();
args.add(arg);
}
FunctionSignature functionSignature = null;
if (ctx.signature() != null) {
if (!ctx.signature().name.getText().equals(ctx.name.getText())) {
throw new RuntimeException("mismatching name in type annotation and definition (\"" + ctx.signature().name.getText() + "\" vs. \"" + ctx.name.getText() + "\"");
}
functionSignature = functionSignatureVisitor.visitSignature(ctx.signature());
}
return new FunctionDefinition(getModuleName(), ctx.name.getText(), args, expressionVisitor.visit(ctx.body), functionSignature);
}
use of xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition in project atlas by lorenzleutgeb.
the class Java method run.
@Override
public void run() {
final var loader = Loader.atDefaultHome();
try {
loader.autoload();
} catch (IOException e) {
throw new RuntimeException(e);
}
Program program;
try {
program = loader.loadMatching(pattern);
} catch (IOException e) {
throw new RuntimeException(e);
}
program.normalize();
if (!program.infer()) {
return;
}
Multimap<String, FunctionDefinition> output = ArrayListMultimap.create();
Multimap<String, String> imports = HashMultimap.create();
for (var entry : program.getFunctionDefinitions().entrySet()) {
var fd = entry.getValue();
if (pattern.asMatchPredicate().test(fd.getFullyQualifiedName())) {
output.put(fd.getModuleName(), fd);
imports.putAll(fd.getModuleName(), fd.importedFunctions().stream().map(Loader::moduleName).collect(Collectors.toSet()));
}
}
for (var e : output.keySet()) {
var path = Loader.path(e, this.path.resolve(Path.of("xyz", "leutgeb", "lorenz", "atlas")), ".java");
var lastModulePart = e.substring(Math.max(0, e.lastIndexOf(".")));
try {
java.nio.file.Files.createDirectories(path.getParent());
} catch (IOException ioException) {
throw new RuntimeException(ioException);
}
final var sink = Files.asCharSink(path.toFile(), StandardCharsets.UTF_8);
try (var stream = new PrintStream(output(path))) {
stream.println("// This file was generated automatically.");
stream.println();
stream.println("package xyz.leutgeb.lorenz.atlas;");
stream.println();
stream.println("import java.util.Objects;");
stream.println("import xyz.leutgeb.lorenz.atlas.Tree;");
stream.println("import static xyz.leutgeb.lorenz.atlas.Tree.node;");
stream.println("import static xyz.leutgeb.lorenz.atlas.Tree.leaf;");
stream.println();
for (String imp : imports.get(e)) {
stream.println("import xyz.leutgeb.lorenz.atlas." + imp + ";");
}
stream.println("public final class " + lastModulePart + " {");
stream.println();
for (var fd : output.get(e)) {
fd.printJavaTo(stream, true);
System.out.println(fd.getFullyQualifiedName());
}
stream.println("}");
} catch (IOException ex) {
throw new RuntimeException(ex);
}
log.info("{}", path);
}
}
use of xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition in project atlas by lorenzleutgeb.
the class Run method run.
@Override
public void run() {
final var start = Instant.now();
Loader loader = Loader.atDefaultHome();
/*
try {
loader.autoload();
} catch (IOException e) {
throw new RuntimeException(e);
}*/
Program program;
try {
program = loader.load(fqns);
// program = loader.loadMatching(pattern);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (name != null && !name.isBlank()) {
program.setName(name);
}
if (program.isEmpty()) {
System.out.println("Program to analyze is empty, nothing to do!");
System.exit(3);
}
program.normalize();
if (!program.infer()) {
return;
}
program.unshare(true);
program.analyzeSizes();
// log.info("Loaded definitions:");
// program.printAllSimpleSignaturesInOrder(System.out);
System.out.println("Output Directory: " + program.getBasePath().toAbsolutePath());
System.out.println();
Map<String, Path> tacticsMap = new HashMap<>();
// log.info(infer ? "Given for comparison:" : "Will check following types:");
log.info("Manual tactics disabled. All tactics will be automatically generated.");
System.out.println("Function Definitions:");
for (int i = 0; i < program.getOrder().size(); i++) {
final var stratum = program.getOrder().get(i);
for (var fqn : stratum) {
FunctionDefinition fd = program.getFunctionDefinitions().get(fqn);
if (tactics != null) {
var path = tactics.resolve(fd.getModuleName().replace(".", "/") + "/" + fd.getName() + ".txt");
if (Files.exists(path) && Files.isReadable(path)) {
tacticsMap.put(fd.getFullyQualifiedName(), path);
}
}
System.out.println(fd.getAnnotatedSignatureString());
// log.info("\tDependencies: " + fd.getOcurringFunctionsNonRecursive());
System.out.println("\tSource: " + fd.getBody().getSource().getRoot());
System.out.println("\tBound: " + fd.getAnnotatedSignature().getAnnotation().map(a -> a.getBounds(fd.treeLikeArguments().stream().map(IdentifierExpression::getName).toList())).orElse("?"));
if (tactics != null) {
final var p = tacticsMap.get(fd.getFullyQualifiedName());
if (p == null) {
System.out.println("\tTactic: n/a (will be automatically generated)");
} else {
System.out.println("\tTactic: " + p.toAbsolutePath());
}
}
}
}
System.out.println();
System.setProperty(Util.getPropertyName(Prover.class, "tickAst"), useTickAst.toString());
final var result = program.solve(new HashMap<>(), tacticsMap, infer, consistentModules, equalRanks, simpleAnnotations, !infer, Collections.emptySet());
final var stop = Instant.now();
System.out.println("Elapsed Walltime: " + Duration.between(start, stop));
System.out.println();
if (Status.UNSATISFIABLE.equals(result.getStatus())) {
System.out.println("UNSAT");
System.exit(1);
} else if (Status.UNKNOWN.equals(result.getStatus())) {
System.out.println("UNKNOWN");
System.exit(2);
} else if (Status.SATISFIABLE.equals(result.getStatus())) {
if (!infer) {
System.out.println("SAT");
System.out.println();
} else {
System.out.println("Results:");
program.printAllInferredAnnotationsAndBoundsInOrder(System.out);
System.out.println();
}
} else {
throw new RuntimeException("encountered unknown status");
}
/*
if (json != null) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("result", program.inferredSignaturesToJson());
builder.add("duration", Json.createValue(Duration.between(start, stop).toString()));
JsonObjectBuilder z3ObjectBuilder = Json.createObjectBuilder();
z3ObjectBuilder.add("status", Json.createValue(result.getStatus().toString()));
JsonObjectBuilder z3StatisticsBuilder = Json.createObjectBuilder();
result.getStatistics().forEach(z3StatisticsBuilder::add);
z3ObjectBuilder.add("statistics", z3StatisticsBuilder.build());
if (result.getSmtFile().isPresent()) {
z3ObjectBuilder.add("file", Json.createValue(result.getSmtFile().get().toString()));
}
builder.add("z3", z3ObjectBuilder.build());
log.info("Writing JSON output to {}", json);
try (final var out = output(json)) {
Json.createWriter(out).writeObject(builder.build());
} catch (IOException ioException) {
log.error("Failed to write JSON output.", ioException);
}
}
*/
// System.exit(result.toExitCode());
System.exit(0);
}
use of xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition in project atlas by lorenzleutgeb.
the class Haskell method run.
@Override
public void run() {
Loader loader = Loader.atDefaultHome();
try {
loader.autoload();
} catch (IOException e) {
throw new RuntimeException(e);
}
Program program;
try {
program = loader.loadMatching(pattern);
} catch (IOException e) {
throw new RuntimeException(e);
}
program.normalize();
if (!program.infer()) {
return;
}
Multimap<String, FunctionDefinition> output = ArrayListMultimap.create();
Multimap<String, String> imports = HashMultimap.create();
for (var entry : program.getFunctionDefinitions().entrySet()) {
var fd = entry.getValue();
if (pattern.asMatchPredicate().test(fd.getFullyQualifiedName())) {
output.put(fd.getModuleName(), fd);
imports.putAll(fd.getModuleName(), fd.importedFunctions().stream().map(Loader::moduleName).collect(Collectors.toSet()));
}
}
for (var e : output.keySet()) {
var path = Loader.path(e, this.path, ".hs");
var lastModulePart = e.substring(Math.max(0, e.lastIndexOf(".")));
try (var stream = new PrintStream(new FileOutputStream(path.toFile()))) {
stream.println("-- This file was generated automatically.");
stream.println();
stream.println("module " + lastModulePart + " where");
stream.println("import InterpreterPrelude (Tree(Node, Leaf))");
stream.println();
for (String imp : imports.get(e)) {
stream.println("import qualified " + imp);
}
for (var fd : output.get(e)) {
fd.printHaskellTo(stream);
}
} catch (IOException ex) {
ex.printStackTrace();
}
log.info("{}", path);
}
}
Aggregations