Simplified code
This commit is contained in:
parent
63a2752c56
commit
c2d1084434
@ -13,25 +13,18 @@ import (
|
|||||||
|
|
||||||
// Build parses the arguments and creates a build.
|
// Build parses the arguments and creates a build.
|
||||||
func Build(args []string) int {
|
func Build(args []string) int {
|
||||||
_, err := buildWithArgs(args)
|
_, err := buildExecutable(args)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return exit(err)
|
||||||
switch err.(type) {
|
|
||||||
case *errors.ExpectedCLIParameter, *errors.UnknownCLIParameter:
|
|
||||||
return 2
|
|
||||||
|
|
||||||
default:
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildWithArgs creates a new build with the given arguments.
|
// buildExecutable creates a new build with the given arguments.
|
||||||
func buildWithArgs(args []string) (*build.Build, error) {
|
func buildExecutable(args []string) (*build.Build, error) {
|
||||||
b := build.New()
|
b := build.New()
|
||||||
|
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
@ -93,12 +86,12 @@ func buildWithArgs(args []string) (*build.Build, error) {
|
|||||||
b.Files = append(b.Files, ".")
|
b.Files = append(b.Files, ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := makeExecutable(b)
|
err := start(b)
|
||||||
return b, err
|
return b, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// makeExecutable starts the build by running the compiler and then writing the result to disk.
|
// start starts the build by running the compiler and then writing the result to disk.
|
||||||
func makeExecutable(b *build.Build) error {
|
func start(b *build.Build) error {
|
||||||
result, err := b.Run()
|
result, err := b.Run()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -4,24 +4,15 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"git.akyoto.dev/cli/q/src/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run builds and runs the executable.
|
// Run builds and runs the executable.
|
||||||
func Run(args []string) int {
|
func Run(args []string) int {
|
||||||
b, err := buildWithArgs(args)
|
b, err := buildExecutable(args)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return exit(err)
|
||||||
switch err.(type) {
|
|
||||||
case *errors.ExpectedCLIParameter, *errors.UnknownCLIParameter:
|
|
||||||
return 2
|
|
||||||
|
|
||||||
default:
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(b.Executable())
|
cmd := exec.Command(b.Executable())
|
||||||
@ -32,14 +23,7 @@ func Run(args []string) int {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return exit(err)
|
||||||
switch err := err.(type) {
|
|
||||||
case *exec.ExitError:
|
|
||||||
return err.ExitCode()
|
|
||||||
|
|
||||||
default:
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
27
src/cli/exit.go
Normal file
27
src/cli/exit.go
Normal file
@ -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
|
||||||
|
}
|
@ -21,13 +21,13 @@ q build examples/hello
|
|||||||
q build examples/hello --dry
|
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
|
```shell
|
||||||
q build examples/hello -a
|
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
|
```shell
|
||||||
q build examples/hello -v
|
q build examples/hello -v
|
||||||
|
@ -34,9 +34,9 @@ func MoveRegisterNumber(code []byte, destination cpu.Register, number int) []byt
|
|||||||
|
|
||||||
if w == 1 {
|
if w == 1 {
|
||||||
return binary.LittleEndian.AppendUint64(code, uint64(number))
|
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.
|
// MoveRegisterNumber32 moves an integer into the given register and sign-extends the register.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user