Refactor main function.
@@ -11,6 +11,8 @@ import (
"os"
"path"
"strings"
+ "log"
+ "path/filepath"
)
func main() {
@@ -19,28 +21,16 @@ func main() {
flag.Parse()
- filepath := flag.Arg(0)
- args := flag.Args()[1:]
-
- var fileExt string
- dir, filename := path.Split(filepath)
- splitedFN := strings.Split(filename, ".")
-
- if len(splitedFN) <= 1 {
- fmt.Printf("Only support eval/compile single file now.")
- return
- }
-
- filename = splitedFN[0]
- fileExt = splitedFN[1]
-
- file, err := ioutil.ReadFile(filepath)
- check(err)
-
if *profileOptionPtr {
defer profile.Start().Stop()
}
+ filepath := flag.Arg(0)
+ args := flag.Args()[1:]
+
+ dir, filename, fileExt := extractFileInfo(filepath)
+ file := readFile(filepath)
+
switch fileExt {
case "ro":
program := parser.BuildAST(file)
@@ -64,6 +54,30 @@ func main() {
}
}
+func sourcePath() string {
+ dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
+
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ return dir
+}
+
+func extractFileInfo(filepath string) (dir, filename, fileExt string) {
+ dir, filename = path.Split(filepath)
+ splitedFN := strings.Split(filename, ".")
+
+ if len(splitedFN) <= 1 {
+ fmt.Printf("Only support eval/compile single file now.")
+ return
+ }
+
+ filename = splitedFN[0]
+ fileExt = splitedFN[1]
+ return
+}
+
func writeByteCode(bytecodes, dir, filename string) {
f, err := os.Create(dir + filename + ".robc")
@@ -74,8 +88,12 @@ func writeByteCode(bytecodes, dir, filename string) {
f.WriteString(bytecodes)
}
-func check(e error) {
- if e != nil {
- panic(e)
+func readFile(filepath string) []byte {
+ file, err := ioutil.ReadFile(filepath)
+
+ if err != nil {
+ panic(err)
}
+
+ return file
}
