|
1 | 1 | package tools.samt.cli |
2 | 2 |
|
| 3 | +import tools.samt.codegen.Codegen |
3 | 4 | import tools.samt.common.DiagnosticController |
4 | 5 | import tools.samt.common.DiagnosticException |
| 6 | +import tools.samt.common.collectSamtFiles |
| 7 | +import tools.samt.common.readSamtSource |
5 | 8 | import tools.samt.lexer.Lexer |
6 | 9 | import tools.samt.parser.Parser |
7 | 10 | import tools.samt.semantic.SemanticModel |
| 11 | +import java.io.IOException |
| 12 | +import kotlin.io.path.isDirectory |
| 13 | +import kotlin.io.path.notExists |
8 | 14 |
|
9 | 15 | internal fun compile(command: CompileCommand, controller: DiagnosticController) { |
10 | | - val sourceFiles = command.files.readSamtSourceFiles(controller) |
| 16 | + val (configuration ,_) = CliConfigParser.readConfig(command.file, controller) ?: return |
| 17 | + |
| 18 | + if (configuration.source.notExists() || !configuration.source.isDirectory()) { |
| 19 | + controller.reportGlobalError("Source path '${configuration.source.toUri()}' does not point to valid directory") |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + val sourceFiles = collectSamtFiles(configuration.source.toUri()).readSamtSource(controller) |
11 | 24 |
|
12 | 25 | if (controller.hasErrors()) { |
13 | 26 | return |
@@ -40,7 +53,24 @@ internal fun compile(command: CompileCommand, controller: DiagnosticController) |
40 | 53 | } |
41 | 54 |
|
42 | 55 | // build up the semantic model from the AST |
43 | | - SemanticModel.build(fileNodes, controller) |
| 56 | + val model = SemanticModel.build(fileNodes, controller) |
44 | 57 |
|
45 | | - // Code Generators will be called here |
| 58 | + // if the semantic model failed to build, exit |
| 59 | + if (controller.hasErrors()) { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + if (configuration.generators.isEmpty()) { |
| 64 | + controller.reportGlobalInfo("No generators configured, did you forget to add a 'generators' section to the 'samt.yaml' configuration?") |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + for (generator in configuration.generators) { |
| 69 | + val files = Codegen.generate(model, generator, controller) |
| 70 | + try { |
| 71 | + OutputWriter.write(generator.output, files) |
| 72 | + } catch (e: IOException) { |
| 73 | + controller.reportGlobalError("Failed to write output for generator '${generator.name}': ${e.message}") |
| 74 | + } |
| 75 | + } |
46 | 76 | } |
0 commit comments