11package deep.decaf.main
22
33import deep.decaf.ir.*
4+ import deep.decaf.low.amd64.*
45import deep.decaf.parser.*
56import org.antlr.v4.runtime.*
7+ import org.apache.commons.io.*
68import picocli.*
7- import picocli.CommandLine.*;
9+ import picocli.CommandLine.*
810import java.io.*
911import java.util.concurrent.*
1012import kotlin.system.*
@@ -30,11 +32,17 @@ class Compiler : Callable<Int> {
3032
3133 @Option(names = [" -c" , " --semantic" ], description = [" Scan, parse and then perform semantic checks" ])
3234 var check = false
35+
36+ @Option(names = [" -a" , " --all" ], description = [" Perform all steps and create assembly code" ])
37+ var all = false
3338 }
3439
3540 @Option(names = [" -g" , " --debug" ], description = [" Enable debug mode" ])
3641 var debug = false
3742
43+ @Option(names = [" -o" , " --output" ], description = [" Name of output file" ])
44+ lateinit var output: File
45+
3846 override fun call (): Int {
3947 if (! file.exists()) {
4048 val msg = Help .Ansi .AUTO .string(" @|bold,red ${file.absolutePath} does not exist|@" )
@@ -64,7 +72,41 @@ class Compiler : Callable<Int> {
6472 } else newExitCode
6573 } else exitCode
6674 }
67- else -> 0
75+ stage.all -> {
76+ val lexer = makeLexer(file)
77+ val exitCode = scanFile(lexer, debug)
78+ if (exitCode == 0 ) {
79+ val parser = makeParser(lexer)
80+ val (newExitCode, tree) = parseFile(parser)
81+ if (newExitCode == 0 ) {
82+ val ir = makeIR(tree)
83+ val semanticCheckCode = semanticCheck(ir, debug)
84+ if (semanticCheckCode == 0 ) {
85+ try {
86+ val bufferedWriter: BufferedWriter = if (::output.isInitialized) {
87+ output.bufferedWriter()
88+ } else {
89+ val fileNameNoExtension = FilenameUtils .getBaseName(file.name)
90+ val basePath = FilenameUtils .getFullPath(file.absolutePath)
91+ val outFilePath = " $basePath${File .separator}$fileNameNoExtension .s"
92+ val outFile = File (outFilePath)
93+ outFile.bufferedWriter()
94+ }
95+ val program = irProgramToLow(ir)
96+ bufferedWriter.append(program.toString())
97+ bufferedWriter.flush()
98+ bufferedWriter.close()
99+ 0
100+ } catch (e: IOException ) {
101+ val msg = Help .Ansi .AUTO .string(" @|bold,red $e |@" )
102+ println (msg)
103+ 1
104+ }
105+ } else semanticCheckCode
106+ } else newExitCode
107+ } else exitCode
108+ }
109+ else -> 1
68110 }
69111 }
70112}
0 commit comments