summaryrefslogtreecommitdiff
path: root/as/main.go
blob: 95aff460d7ae8bee299ab8a6ac150bb635c801d3 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main

import (
	"os"
	"fmt"
)

func usage(arg0 string) {
	fmt.Fprintf(os.Stderr, "usage: %s [-o file] file.easm\n", os.Args[0])
	os.Exit(1)
}

func main() {
	if (len(os.Args) <= 2) {
		usage(os.Args[0])
	}
	
	
	var aout int = 0
	var ain int = 0

	for i := 0; i < len(os.Args); i++ {
		fmt.Println(os.Args[i])
		switch os.Args[i] {
		case "-o": 
			i = i + 1
			aout = i
		default: 
			ain = i
		}
	}

	if (ain == 0) {
		usage(os.Args[0])
	}
	
	var outf string = "out.bin"
	if (aout != 0) {
		outf = os.Args[aout]
	}

	b, errin := os.ReadFile(os.Args[ain])
	out, errout := os.Create(outf)

	if errin != nil {
		fmt.Fprintf(os.Stderr, "failed to read file: %s\n", os.Args[ain])
		os.Exit(1)
	}

	if errout != nil {
		fmt.Fprintf(os.Stderr, "failed to create file for output: %s\n", outf)
		os.Exit(1)
	}

	s := string(b)
	c := Assemble(s)

	out.Write(c)
}