-
Notifications
You must be signed in to change notification settings - Fork 4
/
dump_list_defs_cmd.go
99 lines (88 loc) · 2.81 KB
/
dump_list_defs_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"flag"
"fmt"
"os"
"github.com/DDP-Projekt/Kompilierer/src/compiler"
"github.com/DDP-Projekt/Kompilierer/src/ddperror"
"github.com/DDP-Projekt/Kompilierer/src/ddppath"
)
// $kddp dumplistdefs dumps the list definitions to the specified file
type DumpListDefsCommand struct {
fs *flag.FlagSet // FlagSet for the arguments
// arguments
filePath string // output file path
asm bool // wether to output assembly
llvm_ir bool // wether to output llvm ir
llvm_bc bool // wether to output llvm bitcode
object bool // wether to output an object file
}
func NewDumpListDefsCommand() *DumpListDefsCommand {
return &DumpListDefsCommand{
fs: flag.NewFlagSet("dump-list-defs", flag.ExitOnError),
filePath: ddppath.LIST_DEFS_NAME,
asm: false,
llvm_ir: false,
llvm_bc: false,
object: false,
}
}
func (cmd *DumpListDefsCommand) Init(args []string) error {
// a input .ddp file is necessary
if len(args) < 1 {
return fmt.Errorf("Der starte Befehl braucht eine Eingabedatei")
}
// set all the flags
cmd.fs.StringVar(&cmd.filePath, "o", cmd.filePath, "Ausgabe Datei")
cmd.fs.BoolVar(&cmd.asm, "asm", cmd.asm, "Wether to output assembly")
cmd.fs.BoolVar(&cmd.llvm_ir, "llvm_ir", cmd.llvm_ir, "Wether to output llvm ir")
cmd.fs.BoolVar(&cmd.llvm_bc, "llvm_bc", cmd.llvm_bc, "Wether to output llvm bitcode")
cmd.fs.BoolVar(&cmd.object, "object", cmd.object, "Wether to output object file")
return parseFlagSet(cmd.fs, args)
}
func (cmd *DumpListDefsCommand) Run() error {
outputTypes := []compiler.OutputType{}
if cmd.asm {
outputTypes = append(outputTypes, compiler.OutputAsm)
}
if cmd.llvm_ir {
outputTypes = append(outputTypes, compiler.OutputIR)
}
if cmd.llvm_bc {
outputTypes = append(outputTypes, compiler.OutputBC)
}
if cmd.object {
outputTypes = append(outputTypes, compiler.OutputObj)
}
for _, outType := range outputTypes {
ext := ".ll"
switch outType {
case compiler.OutputAsm:
ext = ".asm"
case compiler.OutputObj:
ext = ".o"
case compiler.OutputBC:
ext = ".bc"
}
file, err := os.OpenFile(changeExtension(cmd.filePath, ext), os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm)
if err != nil {
return fmt.Errorf("Fehler beim Öffnen der Ausgabedatei: %w", err)
}
defer file.Close()
if err := compiler.DumpListDefinitions(file, outType, ddperror.MakeBasicHandler(os.Stderr)); err != nil {
return err
}
}
return nil
}
func (cmd *DumpListDefsCommand) Name() string {
return cmd.fs.Name()
}
func (cmd *DumpListDefsCommand) Usage() string {
return `dump-list-defs <Optionen>: Schreibt die Definitionen der eingebauten Listen Typen in die gegebene Ausgbe Datei
Optionen:
-o: Ausgabe Datei
--asm: ob assembly ausgegeben werden soll
--llvm_ir: ob llvm ir ausgegeben werden soll
--object: ob eine Objekt Datei ausgageben werden soll`
}