From c2d108443412b93654684b040d2a4115d5b76928 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 7 Feb 2025 19:41:54 +0100 Subject: [PATCH] Simplified code --- src/cli/Build.go | 21 +++++++-------------- src/cli/Run.go | 22 +++------------------- src/cli/exit.go | 27 +++++++++++++++++++++++++++ src/cli/readme.md | 4 ++-- src/x86/Move.go | 4 ++-- 5 files changed, 41 insertions(+), 37 deletions(-) create mode 100644 src/cli/exit.go diff --git a/src/cli/Build.go b/src/cli/Build.go index 5f78ccd..28510cd 100644 --- a/src/cli/Build.go +++ b/src/cli/Build.go @@ -13,25 +13,18 @@ import ( // Build parses the arguments and creates a build. func Build(args []string) int { - _, err := buildWithArgs(args) + _, err := buildExecutable(args) if err != nil { fmt.Fprintln(os.Stderr, err) - - switch err.(type) { - case *errors.ExpectedCLIParameter, *errors.UnknownCLIParameter: - return 2 - - default: - return 1 - } + return exit(err) } return 0 } -// buildWithArgs creates a new build with the given arguments. -func buildWithArgs(args []string) (*build.Build, error) { +// buildExecutable creates a new build with the given arguments. +func buildExecutable(args []string) (*build.Build, error) { b := build.New() for i := 0; i < len(args); i++ { @@ -93,12 +86,12 @@ func buildWithArgs(args []string) (*build.Build, error) { b.Files = append(b.Files, ".") } - err := makeExecutable(b) + err := start(b) return b, err } -// makeExecutable starts the build by running the compiler and then writing the result to disk. -func makeExecutable(b *build.Build) error { +// start starts the build by running the compiler and then writing the result to disk. +func start(b *build.Build) error { result, err := b.Run() if err != nil { diff --git a/src/cli/Run.go b/src/cli/Run.go index 11a5969..ce22b06 100644 --- a/src/cli/Run.go +++ b/src/cli/Run.go @@ -4,24 +4,15 @@ import ( "fmt" "os" "os/exec" - - "git.akyoto.dev/cli/q/src/errors" ) // Run builds and runs the executable. func Run(args []string) int { - b, err := buildWithArgs(args) + b, err := buildExecutable(args) if err != nil { fmt.Fprintln(os.Stderr, err) - - switch err.(type) { - case *errors.ExpectedCLIParameter, *errors.UnknownCLIParameter: - return 2 - - default: - return 1 - } + return exit(err) } cmd := exec.Command(b.Executable()) @@ -32,14 +23,7 @@ func Run(args []string) int { if err != nil { fmt.Fprintln(os.Stderr, err) - - switch err := err.(type) { - case *exec.ExitError: - return err.ExitCode() - - default: - return 1 - } + return exit(err) } return 0 diff --git a/src/cli/exit.go b/src/cli/exit.go new file mode 100644 index 0000000..69f8460 --- /dev/null +++ b/src/cli/exit.go @@ -0,0 +1,27 @@ +package cli + +import ( + "errors" + "os/exec" + + xerrors "git.akyoto.dev/cli/q/src/errors" +) + +// exit returns the exit code depending on the error type. +func exit(err error) int { + var ( + exit *exec.ExitError + expectedParameter *xerrors.ExpectedCLIParameter + unknownParameter *xerrors.UnknownCLIParameter + ) + + if errors.As(err, &exit) { + return exit.ExitCode() + } + + if errors.As(err, &expectedParameter) || errors.As(err, &unknownParameter) { + return 2 + } + + return 1 +} diff --git a/src/cli/readme.md b/src/cli/readme.md index 262701d..3c23b53 100644 --- a/src/cli/readme.md +++ b/src/cli/readme.md @@ -21,13 +21,13 @@ q build examples/hello q build examples/hello --dry ``` -Adding the `-a` or `--assembler` flag shows the generated assembly instructions: +Adding `-a` or `--assembler` shows the generated assembly instructions: ```shell q build examples/hello -a ``` -Adding the `-v` or `--verbose` flag shows verbose compiler information: +Adding `-v` or `--verbose` activates all flags that show additional information: ```shell q build examples/hello -v diff --git a/src/x86/Move.go b/src/x86/Move.go index 2c38ca5..fee16e1 100644 --- a/src/x86/Move.go +++ b/src/x86/Move.go @@ -34,9 +34,9 @@ func MoveRegisterNumber(code []byte, destination cpu.Register, number int) []byt if w == 1 { return binary.LittleEndian.AppendUint64(code, uint64(number)) - } else { - return binary.LittleEndian.AppendUint32(code, uint32(number)) } + + return binary.LittleEndian.AppendUint32(code, uint32(number)) } // MoveRegisterNumber32 moves an integer into the given register and sign-extends the register.