Minor changes

This commit is contained in:
Eduard Urbach 2023-10-19 10:14:52 +02:00
parent 506d1e30bf
commit aab33fe86d
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
8 changed files with 46 additions and 36 deletions

View File

@ -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 {

View File

@ -6,7 +6,8 @@ import (
)
const (
baseAddress = 0x400000
minAddress = 0x10000
baseAddress = 0x40 * minAddress
)
// ELF64 represents an ELF 64-bit file.

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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},
}

View File

@ -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) {