From aab33fe86d982046863086460b668b77bd165c53 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 19 Oct 2023 10:14:52 +0200 Subject: [PATCH] Minor changes --- build/Build.go | 55 +++++++++++++++++++++++++++++------------ build/elf/elf.go | 3 ++- cli/Build.go | 16 +----------- cli/Help.go | 2 +- cli/System.go | 2 +- {log => cli/log}/log.go | 0 cli_test.go | 2 +- main_test.go | 2 +- 8 files changed, 46 insertions(+), 36 deletions(-) rename {log => cli/log}/log.go (100%) diff --git a/build/Build.go b/build/Build.go index ac3198e..fbc2a56 100644 --- a/build/Build.go +++ b/build/Build.go @@ -6,6 +6,7 @@ import ( "path/filepath" "git.akyoto.dev/cli/q/build/elf" + "git.akyoto.dev/cli/q/cli/log" ) // Build describes a compiler build. @@ -23,6 +24,23 @@ func New(directory string) (*Build, error) { return nil, err } + file, err := os.Open(directory) + + if err != nil { + return nil, err + } + + defer file.Close() + files, err := file.Readdirnames(0) + + if err != nil { + return nil, err + } + + for _, name := range files { + log.Info.Println(name) + } + executableName := filepath.Base(directory) build := &Build{ @@ -36,29 +54,34 @@ func New(directory string) (*Build, error) { // Run parses the input files and generates an executable file. func (build *Build) Run() error { + code := build.Compile() + if build.WriteExecutable { - sampleCode := []byte{ - 0xb8, 0x01, 0x00, 0x00, 0x00, // mov eax, 1 - 0xbf, 0x01, 0x00, 0x00, 0x00, // mov edi, 1 - 0xbe, 0x80 + 0x22, 0x00, 0x40, 0x00, // mov esi, 0x4000A2 - 0xba, 0x06, 0x00, 0x00, 0x00, // mov edx, 6 - 0x0f, 0x05, // syscall - - 0xb8, 0x3c, 0x00, 0x00, 0x00, // mov eax, 60 - 0xbf, 0x00, 0x00, 0x00, 0x00, // mov edi, 0 - 0x0f, 0x05, // syscall - - 'H', 'e', 'l', 'l', 'o', '\n', - } - - return writeToDisk(build.ExecutablePath, sampleCode, nil) + return writeToDisk(build.ExecutablePath, code) } return nil } +// Compile compiles all the functions. +func (build *Build) Compile() []byte { + return []byte{ + 0xb8, 0x01, 0x00, 0x00, 0x00, // mov eax, 1 + 0xbf, 0x01, 0x00, 0x00, 0x00, // mov edi, 1 + 0xbe, 0xa2, 0x00, 0x40, 0x00, // mov esi, 0x4000a2 + 0xba, 0x06, 0x00, 0x00, 0x00, // mov edx, 6 + 0x0f, 0x05, // syscall + + 0xb8, 0x3c, 0x00, 0x00, 0x00, // mov eax, 60 + 0xbf, 0x00, 0x00, 0x00, 0x00, // mov edi, 0 + 0x0f, 0x05, // syscall + + 'H', 'e', 'l', 'l', 'o', '\n', + } +} + // writeToDisk writes the executable file to disk. -func writeToDisk(filePath string, code []byte, data []byte) error { +func writeToDisk(filePath string, code []byte) error { file, err := os.Create(filePath) if err != nil { diff --git a/build/elf/elf.go b/build/elf/elf.go index 1da251e..22e800e 100644 --- a/build/elf/elf.go +++ b/build/elf/elf.go @@ -6,7 +6,8 @@ import ( ) const ( - baseAddress = 0x400000 + minAddress = 0x10000 + baseAddress = 0x40 * minAddress ) // ELF64 represents an ELF 64-bit file. diff --git a/cli/Build.go b/cli/Build.go index 19f7544..e93d442 100644 --- a/cli/Build.go +++ b/cli/Build.go @@ -1,10 +1,8 @@ package cli import ( - "os" - "git.akyoto.dev/cli/q/build" - "git.akyoto.dev/cli/q/log" + "git.akyoto.dev/cli/q/cli/log" ) func Build(args []string) int { @@ -14,18 +12,6 @@ func Build(args []string) int { directory = args[0] } - stat, err := os.Stat(directory) - - if err != nil { - log.Error.Println(err) - return 1 - } - - if !stat.IsDir() { - log.Error.Println("Build path must be a directory") - return 2 - } - b, err := build.New(directory) if err != nil { diff --git a/cli/Help.go b/cli/Help.go index 87da5c3..73559c9 100644 --- a/cli/Help.go +++ b/cli/Help.go @@ -1,7 +1,7 @@ package cli import ( - "git.akyoto.dev/cli/q/log" + "git.akyoto.dev/cli/q/cli/log" ) func Help(args []string) int { diff --git a/cli/System.go b/cli/System.go index 4bde621..538a178 100644 --- a/cli/System.go +++ b/cli/System.go @@ -4,7 +4,7 @@ import ( "os" "runtime" - "git.akyoto.dev/cli/q/log" + "git.akyoto.dev/cli/q/cli/log" ) func System(args []string) int { diff --git a/log/log.go b/cli/log/log.go similarity index 100% rename from log/log.go rename to cli/log/log.go diff --git a/cli_test.go b/cli_test.go index c1aef37..376ddd1 100644 --- a/cli_test.go +++ b/cli_test.go @@ -18,7 +18,7 @@ func TestCLI(t *testing.T) { {[]string{"invalid"}, 2}, {[]string{"system"}, 0}, {[]string{"build", "non-existing-directory"}, 1}, - {[]string{"build", "examples/hello/hello.q"}, 2}, + {[]string{"build", "examples/hello/hello.q"}, 1}, {[]string{"build", "examples/hello", "--invalid"}, 2}, } diff --git a/main_test.go b/main_test.go index 9bf72ae..d05e9a6 100644 --- a/main_test.go +++ b/main_test.go @@ -5,7 +5,7 @@ import ( "os" "testing" - "git.akyoto.dev/cli/q/log" + "git.akyoto.dev/cli/q/cli/log" ) func TestMain(m *testing.M) {