-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (37 loc) · 709 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/icholy/cc/compiler"
)
func main() {
flag.Parse()
if flag.NArg() < 1 {
log.Fatalf("no input files")
}
for _, file := range flag.Args() {
if err := compile(file); err != nil {
log.Fatalf("%s: %v", file, err)
}
}
}
func compile(file string) error {
src, err := ioutil.ReadFile(file)
if err != nil {
return err
}
asm, err := compiler.Compile(string(src))
if err != nil {
return err
}
name := outputName(file)
return ioutil.WriteFile(name, []byte(asm), os.ModePerm)
}
func outputName(file string) string {
ext := filepath.Ext(file)
return fmt.Sprintf("%s.s", file[:len(file)-len(ext)])
}