|
| 1 | +// Copyright (c) 2016 K Team. All Rights Reserved. |
| 2 | +package org.kframework; |
| 3 | + |
| 4 | +import com.google.inject.Provider; |
| 5 | +import org.kframework.RewriterResult; |
| 6 | +import org.kframework.attributes.Source; |
| 7 | +import org.kframework.backend.java.symbolic.InitializeRewriter; |
| 8 | +import org.kframework.backend.java.symbolic.JavaExecutionOptions; |
| 9 | +import org.kframework.definition.Definition; |
| 10 | +import org.kframework.kompile.CompiledDefinition; |
| 11 | +import org.kframework.kompile.Kompile; |
| 12 | +import org.kframework.kompile.KompileOptions; |
| 13 | +import org.kframework.kore.K; |
| 14 | +import org.kframework.kore.compile.KTokenVariablesToTrueVariables; |
| 15 | +import org.kframework.krun.KRun; |
| 16 | +import org.kframework.krun.KRunOptions; |
| 17 | +import org.kframework.krun.api.io.FileSystem; |
| 18 | +import org.kframework.krun.ioserver.filesystem.portable.PortableFileSystem; |
| 19 | +import org.kframework.main.GlobalOptions; |
| 20 | +import org.kframework.rewriter.Rewriter; |
| 21 | +import org.kframework.utils.errorsystem.KExceptionManager; |
| 22 | +import org.kframework.utils.file.FileUtil; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.lang.invoke.MethodHandle; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Optional; |
| 28 | +import java.util.function.BiFunction; |
| 29 | + |
| 30 | +import static org.kframework.Collections.*; |
| 31 | +import static org.kframework.kore.KORE.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * KRunAPI |
| 35 | + */ |
| 36 | +public class KRunAPI { |
| 37 | + |
| 38 | + public static RewriterResult run(CompiledDefinition compiledDef, String programText, Integer depth) { |
| 39 | + |
| 40 | + GlobalOptions globalOptions = new GlobalOptions(); |
| 41 | + KompileOptions kompileOptions = new KompileOptions(); |
| 42 | + KRunOptions krunOptions = new KRunOptions(); |
| 43 | + JavaExecutionOptions javaExecutionOptions = new JavaExecutionOptions(); |
| 44 | + |
| 45 | + KExceptionManager kem = new KExceptionManager(globalOptions); |
| 46 | + FileUtil files = FileUtil.testFileUtil(); |
| 47 | + boolean ttyStdin = false; |
| 48 | + |
| 49 | + FileSystem fs = new PortableFileSystem(kem, files); |
| 50 | + Map<String, Provider<MethodHandle>> hookProvider = HookProvider.get(kem); // new HashMap<>(); |
| 51 | + InitializeRewriter.InitializeDefinition initializeDefinition = new InitializeRewriter.InitializeDefinition(); |
| 52 | + |
| 53 | + BiFunction<String, Source, K> programParser = compiledDef.getProgramParser(kem); |
| 54 | + K pgm = programParser.apply(programText, Source.apply("generated by api")); |
| 55 | + K program = KRun.parseConfigVars(krunOptions, compiledDef, kem, files, ttyStdin, pgm); |
| 56 | + |
| 57 | + /* TODO: figure out if it is needed |
| 58 | + program = new KTokenVariablesToTrueVariables() |
| 59 | + .apply(compiledDef.kompiledDefinition.getModule(compiledDef.mainSyntaxModuleName()).get(), program); |
| 60 | + */ |
| 61 | + |
| 62 | + Rewriter rewriter = (InitializeRewriter.SymbolicRewriterGlue) |
| 63 | + new InitializeRewriter( |
| 64 | + fs, |
| 65 | + javaExecutionOptions, |
| 66 | + globalOptions, |
| 67 | + kem, |
| 68 | + kompileOptions.experimental.smt, |
| 69 | + hookProvider, |
| 70 | + kompileOptions, |
| 71 | + krunOptions, |
| 72 | + files, |
| 73 | + initializeDefinition) |
| 74 | + .apply(compiledDef.executionModule()); |
| 75 | + |
| 76 | + RewriterResult result = ((InitializeRewriter.SymbolicRewriterGlue) rewriter).execute(program, Optional.ofNullable(depth), true); |
| 77 | + |
| 78 | + return result; |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + GlobalOptions globalOptions = new GlobalOptions(); |
| 83 | + KompileOptions kompileOptions = new KompileOptions(); |
| 84 | + KRunOptions krunOptions = new KRunOptions(); |
| 85 | + KExceptionManager kem = new KExceptionManager(globalOptions); |
| 86 | + FileUtil files = FileUtil.testFileUtil(); |
| 87 | + |
| 88 | + if (args.length < 2) { |
| 89 | + System.out.println("usage: <def> <main-module> <pgm>"); |
| 90 | + return; |
| 91 | + } |
| 92 | + String def = FileUtil.load(new File(args[0])); // "require \"domains.k\" module A syntax KItem ::= \"run\" endmodule" |
| 93 | + String pgm = FileUtil.load(new File(args[2])); // "run" |
| 94 | + |
| 95 | + String mainModuleName = args[1]; // "A" |
| 96 | + |
| 97 | + // kompile |
| 98 | + Definition d = DefinitionParser.from(def, mainModuleName); |
| 99 | + CompiledDefinition compiledDef = Kompile.run(d, kompileOptions, kem); |
| 100 | + |
| 101 | + // krun |
| 102 | + RewriterResult result = run(compiledDef, pgm, null); |
| 103 | + |
| 104 | + // print output |
| 105 | + // from org.kframework.krun.KRun.run() |
| 106 | + KRun.prettyPrint(compiledDef, krunOptions.output, s -> KRun.outputFile(s, krunOptions, files), result.k()); |
| 107 | + |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments