aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorAstatin <[email protected]>2024-09-19 18:55:25 +0900
committerAstatin <astatin@redacted>2024-09-19 18:55:25 +0900
commit1680bc5146b25d3631e6b7bfee3946ef59d906fd (patch)
tree5aac450b147ab4c02b51f2968caefbc343a0a577 /main.go
parent7f41367ce01fbecaec6eb5f173152a59fe55e669 (diff)
Create 1st draft of new assembler in go
Diffstat (limited to 'main.go')
-rw-r--r--main.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..101e8d7
--- /dev/null
+++ b/main.go
@@ -0,0 +1,55 @@
+package main
+
+import (
+ "fmt"
+ "io"
+ "os"
+ "strings"
+)
+
+func main() {
+ if len(os.Args) != 3 {
+ fmt.Fprintf(os.Stderr, "Usage: gbasm [input_file] [output_file]")
+ os.Exit(1)
+ }
+
+ input_file_name := os.Args[1]
+ output_file_name := os.Args[2]
+
+ input_file, err := os.Open(input_file_name)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error while opening input file: %s", err.Error())
+ os.Exit(1)
+ }
+
+ input, err := io.ReadAll(input_file)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error while reading input file: %s", err.Error())
+ os.Exit(1)
+ }
+
+ lines := strings.Split(string(input), "\n")
+
+ result := []byte{}
+ for _, line := range lines {
+ next_instruction, err := Instructions.Parse(line)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s", err.Error())
+ os.Exit(1)
+ }
+
+ result = append(result, next_instruction...)
+ }
+
+ output_file, err := os.Create(output_file_name)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error while opening output file: %s", err.Error())
+ os.Exit(1)
+ }
+
+ _, err = output_file.Write(result)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error while writing to output file: %s", err.Error())
+ os.Exit(1)
+ }
+}