fspl/cmd/fsplc/main.go

86 lines
2.0 KiB
Go
Raw Normal View History

2023-11-17 03:40:57 +00:00
package main
2023-11-23 06:26:28 +00:00
import "os"
2024-02-21 18:21:58 +00:00
import "path/filepath"
2024-02-23 00:22:53 +00:00
import "git.tebibyte.media/fspl/fspl/cli"
import "git.tebibyte.media/fspl/fspl/entity"
import "git.tebibyte.media/fspl/fspl/compiler"
import ferrors "git.tebibyte.media/fspl/fspl/errors"
2023-11-23 06:26:28 +00:00
2023-11-17 03:40:57 +00:00
func main () {
2024-02-21 18:21:58 +00:00
// instantiate the compiler
// FIXME: perhaps we want different paths on Windows?
2024-02-21 18:21:58 +00:00
comp := new(compiler.Compiler)
comp.Stderr = os.Stderr
comp.Resolver = compiler.NewResolver (
"/usr/local/src/fspl",
"/usr/src/fspl",
2024-02-21 18:21:58 +00:00
"/usr/local/incude/fspl",
"/usr/include/fspl")
2024-02-21 18:21:58 +00:00
homeDir, err := os.UserHomeDir()
if err != nil {
comp.Errorln(err)
os.Exit(2)
}
comp.Resolver.AddPathFront (
filepath.Join(homeDir, ".local/src/fspl"),
filepath.Join(homeDir, ".local/include/fspl"))
2024-02-20 06:23:00 +00:00
2024-02-21 18:21:58 +00:00
// take in CLI flags
2024-02-11 17:13:49 +00:00
help := cli.NewFlag (
2024-02-11 08:33:06 +00:00
'h', "help",
"Display usage information and exit")
2024-02-20 06:23:00 +00:00
debug := cli.NewFlag (
0, "debug",
"Print extra debug information while compiling")
quiet := cli.NewFlag (
'q', "quiet",
"Don't print warnings, errors, etc.")
format := cli.NewInputFlag (
'm', "format",
"Output format (.s, .o, .ll)", "",
cli.NewValSet(".s", ".o", ".ll"))
2024-02-11 17:13:49 +00:00
output := cli.NewInputFlag (
2024-02-11 08:33:06 +00:00
'o', "output",
"Output filename", "",
2024-02-11 17:13:49 +00:00
cli.ValString)
optimization := cli.NewInputFlag (
2024-02-11 08:33:06 +00:00
'O', "optimization",
"Optimization level (0-3)", "0",
cli.NewValSet("0", "1", "2", "3"))
2023-11-23 06:26:28 +00:00
application := cli.New (
"Compile FSPL source files",
help,
2024-02-20 06:23:00 +00:00
debug,
quiet,
format,
output,
optimization)
2024-02-14 00:15:40 +00:00
application.Syntax = "[OPTION]... FILE"
2024-02-11 17:13:49 +00:00
application.ParseOrExit(os.Args)
2024-02-11 08:33:06 +00:00
if help.Value != "" {
2024-02-11 17:13:49 +00:00
application.Usage()
os.Exit(2)
}
2024-02-14 00:15:40 +00:00
if len(application.Args) != 1 {
2024-02-21 18:21:58 +00:00
comp.Errorln("please specify one unit address")
2024-02-14 00:15:40 +00:00
application.Usage()
2024-02-20 05:52:23 +00:00
os.Exit(2)
2024-02-14 00:15:40 +00:00
}
2024-02-11 08:33:06 +00:00
2024-02-21 18:21:58 +00:00
// configure the compiler based on user input
comp.Output = output.Value
comp.Optimization = optimization.Value
comp.Format = format.Value
comp.Debug = debug.Value != ""
comp.Quiet = quiet.Value != ""
2024-02-21 18:21:58 +00:00
err = comp.CompileUnit(entity.Address(application.Args[0]))
2023-11-23 06:26:28 +00:00
if err != nil {
2024-02-21 18:21:58 +00:00
comp.Errorln(ferrors.Format(err))
2023-11-23 06:26:28 +00:00
os.Exit(1)
}
}