func main() {
	if len(os.Args) == 2 {
		path := os.Args[1]
		newFName := path[:strings.Index(path, ".")] + ".asm"
		fo, _ := os.Create(newFName)
		writer := bufio.NewWriter(fo)

		translator := CommandTranslator{
			labelCount: 0,
		}

		cmdtoks := nand2tetris_lib.ReadAndTokenize(path, tokenizeLine)
		for i := range cmdtoks {
			fmt.Println(cmdtoks[i], typeofCommand(cmdtoks[i]))
			asm, success := translator.translate(cmdtoks[i])

			if success {
				if _, err := writer.WriteString(asm + "\n"); err != nil {
					panic(err)
				}
			}
		}
		writer.Flush()
	}
}
func main() {
	if len(os.Args) == 2 {
		fname := os.Args[1]
		symtab := initSymbolTable()
		commands := createHCs(nand2tetris_lib.ReadAndTokenize(fname, tokenizeLine))

		var icount uint = 0 // the index of the *next* command

		// pass 1: add labels to symbol table
		for i := range commands {
			cmd := commands[i]

			if commands[i].cmdType == L_COMMAND {
				L_CMDToBin(cmd, &symtab, icount)
			} else {
				icount += 1
			}
		}

		counter := 16
		varCounter := func() int { counter += 1; return counter - 1 }
		newFName := fname[:strings.Index(fname, ".")] + ".hack"
		fo, _ := os.Create(newFName)

		write := bufio.NewWriter(fo)

		for i := range commands {
			cmd := commands[i]

			bin := ""
			switch cmd.cmdType {
			case A_COMMAND:
				bin = A_CMDToBin(cmd, &symtab, varCounter)
			case C_COMMAND:
				bin = C_CMDToBin(cmd)
			}

			if bin != "" {
				if _, err := write.WriteString(bin + "\n"); err != nil {
					panic(err)
				}

			}
		}

		write.Flush()
	}
}