@@ -14,10 +14,12 @@ import org.polystat.odin.analysis.ASTAnalyzer
1414import org .polystat .odin .analysis .EOOdinAnalyzer
1515import org .polystat .odin .analysis .EOOdinAnalyzer .OdinAnalysisResult
1616import org .polystat .odin .parser .EoParser .sourceCodeEoParser
17+ import org .polystat .py2eo .transpiler .Transpile
1718
1819import PolystatConfig .*
1920import IncludeExclude .*
2021import InputUtils .*
22+ import org .polystat .py2eo .parser .PythonLexer
2123object Main extends IOApp :
2224 override def run (args : List [String ]): IO [ExitCode ] =
2325 for exitCode <- CommandIOApp .run(
@@ -49,66 +51,14 @@ object Main extends IOApp:
4951 }
5052 case None => analyzers.map(_._2)
5153
52- def analyze (
53- analyzers : List [ASTAnalyzer [IO ]]
54- )(code : String ): IO [List [EOOdinAnalyzer .OdinAnalysisResult ]] =
55- analyzers.traverse(a =>
56- EOOdinAnalyzer
57- .analyzeSourceCode(a)(code)(cats.Monad [IO ], sourceCodeEoParser[IO ]())
58- )
59-
60- def listAnalyzers : IO [Unit ] = analyzers.traverse_ { case (name, _) =>
61- IO .println(name)
62- }
63-
64- def readConfigFromFile (path : Path ): IO [PolystatUsage .Analyze ] =
65- HoconConfig (path).config.load
66-
67- def writeOutputTo (path : Path )(output : String ): IO [Unit ] =
68- for
69- _ <- path.parent
70- .map(Files [IO ].createDirectories)
71- .getOrElse(IO .unit)
72- _ <- Stream
73- .emits(output.getBytes)
74- .through(Files [IO ].writeAll(path))
75- .compile
76- .drain
77- yield ()
78- end for
79- end writeOutputTo
80-
81- def analyzeEO (
82- inputFiles : Stream [IO , (Path , String )],
83- outputFormats : List [OutputFormat ],
84- out : Output ,
85- filteredAnalyzers : List [ASTAnalyzer [IO ]],
86- ): IO [Unit ] =
87- inputFiles
88- .evalMap { case (codePath, code) =>
89- for
90- _ <- IO .println(s " Analyzing $codePath... " )
91- analyzed <- analyze(filteredAnalyzers)(code)
92- _ <- out match
93- case Output .ToConsole => IO .println(analyzed)
94- case Output .ToDirectory (out) =>
95- outputFormats.traverse_ { case OutputFormat .Sarif =>
96- val outPath =
97- out / " sarif" / codePath.replaceExt(" .sarif.json" )
98- val sarifJson = SarifOutput (analyzed).json.toString
99- IO .println(s " Writing results to $outPath" ) *>
100- writeOutputTo(outPath)(sarifJson)
101- }
102- yield ()
103- }
104- .compile
105- .drain
106-
10754 def execute (usage : PolystatUsage ): IO [Unit ] =
10855 usage match
10956 case PolystatUsage .List (cfg) =>
110- if (cfg) then IO .println(HoconConfig .keys.explanation)
111- else listAnalyzers
57+ if cfg then IO .println(HoconConfig .keys.explanation)
58+ else
59+ analyzers.traverse_ { case (name, _) =>
60+ IO .println(name)
61+ }
11262 case PolystatUsage .Misc (version, config) =>
11363 if (version) then IO .println(BuildInfo .version)
11464 else
@@ -118,55 +68,26 @@ object Main extends IOApp:
11868 lang,
11969 AnalyzerConfig (inex, input, tmp, fmts, out),
12070 ) =>
121- val filteredAnalyzers = filterAnalyzers(inex)
122- val tempDir : IO [Path ] = tmp match
123- case Some (path) => IO .pure(path)
124- case None => Files [IO ].createTempDirectory
125- val inputExt : String = lang match
126- case SupportedLanguage .EO => " .eo"
127- case SupportedLanguage .Java (_) => " .java"
128- case SupportedLanguage .Python => " .py"
129-
71+ val processedConfig = ProcessedConfig (
72+ filteredAnalyzers = filterAnalyzers(inex),
73+ tempDir = tmp match
74+ case Some (path) =>
75+ (IO .println(s " Cleaning ${path.absolute}... " ) *>
76+ Files [IO ].deleteRecursively(path) *>
77+ Files [IO ].createDirectory(path))
78+ .as(path)
79+ case None => Files [IO ].createTempDirectory
80+ ,
81+ output = out,
82+ input = input,
83+ fmts = fmts,
84+ )
13085 val analysisResults : IO [Unit ] =
13186 lang match
132- case SupportedLanguage .EO =>
133- val inputFiles = readCodeFromInput(ext = inputExt, input = input)
134- analyzeEO(
135- inputFiles = inputFiles,
136- outputFormats = fmts,
137- out = out,
138- filteredAnalyzers = filteredAnalyzers,
139- )
87+ case SupportedLanguage .EO => EO .analyze(processedConfig)
14088 case SupportedLanguage .Java (j2eo) =>
141- for
142- tmp <- tempDir
143- _ <- input match // writing EO files to tempDir
144- case Input .FromStdin =>
145- for
146- code <- readCodeFromStdin.compile.string
147- stdinTmp <- Files [IO ].createTempDirectory.map(path =>
148- path / " stdin.eo"
149- )
150- _ <- writeOutputTo(stdinTmp)(code)
151- _ <- J2EO .run(j2eo, inputDir = stdinTmp, outputDir = tmp)
152- yield ()
153- case Input .FromFile (path) =>
154- J2EO .run(j2eo, inputDir = path, outputDir = tmp)
155- case Input .FromDirectory (path) =>
156- J2EO .run(j2eo, inputDir = path, outputDir = tmp)
157- inputFiles = readCodeFromInput(
158- " .eo" ,
159- Input .FromDirectory (tmp),
160- )
161- _ <- analyzeEO(
162- inputFiles = inputFiles,
163- outputFormats = fmts,
164- out = out,
165- filteredAnalyzers = filteredAnalyzers,
166- )
167- yield ()
168- case SupportedLanguage .Python =>
169- IO .println(" Analyzing Python is not implemented yet!" )
89+ Java .analyze(j2eo, processedConfig)
90+ case SupportedLanguage .Python => Python .analyze(processedConfig)
17091 analysisResults
17192 end execute
17293
0 commit comments