package build import ( "bufio" "os" "path/filepath" "git.akyoto.dev/cli/q/src/compiler" "git.akyoto.dev/cli/q/src/elf" ) // Build describes a compiler build. type Build struct { Directory string WriteExecutable bool } // New creates a new build. func New(directory string) *Build { return &Build{ Directory: directory, WriteExecutable: true, } } // Run parses the input files and generates an executable file. func (build *Build) Run() error { functions, err := compiler.Compile(build.Directory) if err != nil { return err } if !build.WriteExecutable { return nil } code, data := compiler.Finalize(functions) return writeToDisk(build.Executable(), code, data) } // Executable returns the path to the executable. func (build *Build) Executable() string { return filepath.Join(build.Directory, filepath.Base(build.Directory)) } // writeToDisk writes the executable file to disk. func writeToDisk(filePath string, code []byte, data []byte) error { file, err := os.Create(filePath) if err != nil { return err } buffer := bufio.NewWriter(file) executable := elf.New(code, data) executable.Write(buffer) buffer.Flush() err = file.Close() if err != nil { return err } return os.Chmod(filePath, 0755) }