@@ -13,6 +13,7 @@ import (
13
13
"io"
14
14
"io/ioutil"
15
15
"os"
16
+ "path/filepath"
16
17
"strconv"
17
18
"strings"
18
19
@@ -340,6 +341,56 @@ const (
340
341
Verbose Option = 1 << iota // verbose output during assembly
341
342
)
342
343
344
+ // AssembleFile reads a file containing 6502 assembly code, assembles it,
345
+ // and produces a binary output file and a source map file.
346
+ func AssembleFile (path string , options Option , out io.Writer ) error {
347
+ inFile , err := os .Open (path )
348
+ if err != nil {
349
+ return err
350
+ }
351
+ defer inFile .Close ()
352
+
353
+ assembly , sourceMap , err := Assemble (inFile , path , out , options )
354
+ if err != nil {
355
+ for _ , e := range assembly .Errors {
356
+ fmt .Fprintln (out , e )
357
+ }
358
+ return err
359
+ }
360
+
361
+ ext := filepath .Ext (path )
362
+ prefix := path [:len (path )- len (ext )]
363
+ binPath := prefix + ".bin"
364
+ binFile , err := os .OpenFile (binPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
365
+ if err != nil {
366
+ return err
367
+ }
368
+ defer binFile .Close ()
369
+
370
+ _ , err = assembly .WriteTo (binFile )
371
+ if err != nil {
372
+ return err
373
+ }
374
+
375
+ mapPath := prefix + ".map"
376
+ mapFile , err := os .OpenFile (mapPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
377
+ if err != nil {
378
+ return err
379
+ }
380
+ defer mapFile .Close ()
381
+
382
+ _ , err = sourceMap .WriteTo (mapFile )
383
+ if err != nil {
384
+ return err
385
+ }
386
+
387
+ fmt .Fprintf (out , "Assembled '%s' to produce '%s' and '%s'.\n " ,
388
+ filepath .Base (path ),
389
+ filepath .Base (binPath ),
390
+ filepath .Base (mapPath ))
391
+ return nil
392
+ }
393
+
343
394
// Assemble reads data from the provided stream and attempts to assemble it
344
395
// into 6502 byte code.
345
396
func Assemble (r io.Reader , filename string , out io.Writer , options Option ) (* Assembly , * SourceMap , error ) {
0 commit comments